Hiding file attachments from guests (using style template)

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
kebev
Registered User
Posts: 7
Joined: Mon Sep 02, 2024 9:01 am

Hiding file attachments from guests (using style template)

Post by kebev »

Hello,

I'm trying to find a way to prevent guest/non-registered users from downloading file attachments. This is to prevent users leeching large files from the forum without participating. I know there is a general admin option to block all attachments from being viewed but this is not what I want because it stops important images from being shown.

So far in my search I have realised that the "Hide" extensions that are available only manage to hide text but not attachments. My next train of thought is to modify the "attachment.html" in the style template, however I am stuck. I am using the default prosilver style and editing the attacment.html file so the file attachment code looks like this:

Code: Select all

<!-- IF _file.S_FILE -->
        <!-- IF S_REGISTERED_USER -->
		<dl class="file">
			<dt><!-- IF _file.UPLOAD_ICON -->{_file.UPLOAD_ICON} <!-- ENDIF --><a class="postlink" href="{_file.U_DOWNLOAD_LINK}">{_file.DOWNLOAD_NAME}</a></dt>
			<!-- IF _file.COMMENT --><dd><em>{_file.COMMENT}</em></dd><!-- ENDIF -->
			<dd>({_file.FILESIZE} {_file.SIZE_LANG}) {_file.L_DOWNLOAD_COUNT}</dd>
		</dl>
		<!-- ENDIF -->
<!-- ENDIF -->
That is, I added a "if registered user" clause to the template. No matter what I do, however, I cannot stop the attachment from appearing - even if I delete the entire block of code.

Can someone tell me what I'm doing wrong and/or if I am on the right track here?

Thank you!
Last edited by thecoalman on Tue Sep 03, 2024 4:20 pm, edited 1 time in total.
Reason: Moved to custom coding.
User avatar
Anișor
Translator
Posts: 335
Joined: Tue Jan 08, 2013 9:36 pm
Location: Arbroath, Angus, Scotland

Re: Hiding file attachments from guests (using style template)

Post by Anișor »

You can do that by using builtin forum permissions for visitors and bot groups. No need for that.
User avatar
Mannix_
Registered User
Posts: 2010
Joined: Sun Oct 25, 2015 2:56 pm
Name: Matt

Re: Hiding file attachments from guests (using style template)

Post by Mannix_ »

When you edit the template files don't forget to purge the cache in ACP after making changes
Did I helped You? Consider a donation.
New version of phpBB has been released? My styles aren't validated for it yet? Check my page for the latest downloads!
User avatar
cabot
Registered User
Posts: 783
Joined: Sat Jan 07, 2012 4:16 pm

Re: Hiding file attachments from guests (using style template)

Post by cabot »

Hello,
kebev wrote: Tue Sep 03, 2024 7:57 am That is, I added a "if registered user" clause to the template. No matter what I do, however, I cannot stop the attachment from appearing - even if I delete the entire block of code
If it's not a cache problem as Mannix_ indicated, it looks like the behaviour of an attachment that you didn't insert to the post by clicking on the ‘Place inline’ button, in which case it's the viewtopic_body.html file that's concerned.

Code: Select all

			<!-- IF postrow.S_HAS_ATTACHMENTS -->
				<dl class="attachbox">
					<dt>
						{L_ATTACHMENTS}
					</dt>
					<!-- BEGIN attachment -->
						<dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd>
					<!-- END attachment -->
				</dl>
			<!-- ENDIF -->
You could possibly add the condition to this code but this would have an impact on all attachments, including images, that have not been inserted using ‘Place inline’:

Code: Select all

			<!-- IF postrow.S_HAS_ATTACHMENTS -->
				<dl class="attachbox">
					<dt>
						{L_ATTACHMENTS}
					</dt>
				<!-- IF S_REGISTERED_USER -->
					<!-- BEGIN attachment -->
						<dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd>
					<!-- END attachment -->
				<!-- ELSE -->
					<dd>{L_NO_ACCESS_ATTACHMENT}</dd>
				<!-- ENDIF -->
				</dl>
			<!-- ENDIF -->
kebev
Registered User
Posts: 7
Joined: Mon Sep 02, 2024 9:01 am

Re: Hiding file attachments from guests (using style template)

Post by kebev »

Thanks so much for your replies! @Mannix_, it was indeed the cache that needed clearing.
Unfortunately though, my code doesn't seem to work correctly. Regardless of the user being logged in/registered, the condition always seems to return false. My code in attachment.html looks like:

Code: Select all

		<!-- IF S_REGISTERED_USER -->
		<!-- IF _file.S_FILE -->
		<dl class="file">
			<dt><!-- IF _file.UPLOAD_ICON -->{_file.UPLOAD_ICON} <!-- ENDIF --><a class="postlink" href="{_file.U_DOWNLOAD_LINK}">{_file.DOWNLOAD_NAME}</a></dt>
			<!-- IF _file.COMMENT --><dd><em>{_file.COMMENT}</em></dd><!-- ENDIF -->
			<dd>({_file.FILESIZE} {_file.SIZE_LANG}) {_file.L_DOWNLOAD_COUNT}</dd>
		</dl>
		<!-- ENDIF -->
		<!-- ELSE -->
		<b>[Please login to view.]</b>
		<!-- ENDIF -->
I am guessing I am editing the correct file because I am getting the "Please login to view." message instead of the seeing attachment... but I never see the attachment when I'm logged in as a registered user.

Any extra help would be great, thank you!
User avatar
Mannix_
Registered User
Posts: 2010
Joined: Sun Oct 25, 2015 2:56 pm
Name: Matt

Re: Hiding file attachments from guests (using style template)

Post by Mannix_ »

try S_USER_LOGGED_IN instead of S_REGISTERED_USER
Did I helped You? Consider a donation.
New version of phpBB has been released? My styles aren't validated for it yet? Check my page for the latest downloads!
kebev
Registered User
Posts: 7
Joined: Mon Sep 02, 2024 9:01 am

Re: Hiding file attachments from guests (using style template)

Post by kebev »

Thanks for the suggestion, @Mannix_. I have tried both and they give the same result. I thought it might have something to do with being admin but I also tried to create a new registered user and get the same result.
User avatar
AbuHossam
Registered User
Posts: 361
Joined: Sun Sep 02, 2018 7:24 am

Re: Hiding file attachments from guests (using style template)

Post by AbuHossam »

Great Work! ♥
Recent Topics in DB-UK Community – Where Every Passion Finds Its Place.
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6334
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Hiding file attachments from guests (using style template)

Post by thecoalman »

Template edits only hide the link and would not prevent someone from downloading the file if for example the link was posted shared somewhere. As another example they can simply go through the file ID's if they wanted to go on fishing expedition.

As Anișor already mentioned there is permissions for this. Go into ACP >permissions tab >> Under "Global permissions" click the Groups permission link. On next page select guests and determine which role is assigned, repeat for bots*. By default it should be the guest role and bot role.

Once you know what roles go back to Permissions tab and under "Permissions roles" heading click User roles link, edit the appropriate role on the next page. Set "can download files" to never.

---------------------
I know there is a general admin option to block all attachments from being viewed but this is not what I want because it stops important images from being shown.
Optionally you can do it with forum permissions/roles but in that case only if you wanted to restrict downloads to certain forums.

*Whenever you restrict access to guests the bots should be restricted too. A)Someone can spoof user agent for bot and B)Search engines don't like being served content not available to the public. It's an easy way to get a site penalized, research cloaking. You can give access to guests and restrict bots but not the other way around.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
kebev
Registered User
Posts: 7
Joined: Mon Sep 02, 2024 9:01 am

Re: Hiding file attachments from guests (using style template)

Post by kebev »

Hi @Thecoalman,

Thanks for the suggestion, however as I mentioned, the idea is to only prevent specific attachments from being downloaded. That is, I am happy for images to be shown but I want the user to be logged to access larger files such as zips. The administration options seems to be an all-or-nothing configuration and this is not what I would like.

As for the users finding a workaround, that's not a big issue for me if there are users advanced enough to figure it out. The attachments aren't secret but I do want to encourage participation in the forum rather than letting users freely use my bandwidth for these large attachments. Images of course are not in the same category as a zip file and they should be able to be viewed openly, otherwise the thread will not make any sense.

So far, I feel that my code is close to what I need, it's just the "if" condition that isn't working for some reason.

Thanks for any help to solve the problem.
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6334
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Hiding file attachments from guests (using style template)

Post by thecoalman »

The reason it's not working is because it's in a loop, this is one of those rare occurrences where the global template variables are not available. The convoluted solution is to copy the entire code for the loop from <!-- BEGIN _file --> to <!-- END _file --> and paste it after <!-- END _file --> and then add the if/else.

<removed non functioning code>

Haven't tested it but that should work. Ideally you'd add template variable but that might require a little more work. It's on line 1300 of /includes/functions_content.php if someone else wants to take a look.
Last edited by thecoalman on Wed Sep 04, 2024 6:27 pm, edited 1 time in total.
Reason: See below.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
kebev
Registered User
Posts: 7
Joined: Mon Sep 02, 2024 9:01 am

Re: Hiding file attachments from guests (using style template)

Post by kebev »

Hi @thecoalman,

Thanks for helping me understand more about how this thing works. I did try what you suggested but it looks like that even above the <!-- BEGIN _file> directive, the S_USER_LOGGED_IN variable is still unavailable. It always appears to be returning false.

I think your second suggestion of using temporary variables might be even further out of my depth of knowledge. I will investigate but if anyone has any ideas they would obviously be more than welcome!

Thanks.
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6334
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Hiding file attachments from guests (using style template)

Post by thecoalman »

It doesn't work because I was sleepy and being lazy. Doesn't appear to be any easy way if at all to do this within the template system .

Open includes/functions_content.php around line 1299 find:

Code: Select all

						'S_FILE'		=> true,
After add:

Code: Select all

						'S_DENIED'		=> (empty($user->data['is_registered']) || !empty($user->data['is_bot'])) ? true : false,
						'DENIED_MESSAGE'	=> 'You must be logged in to view this attachment.',
No other edits required, revert any edits you made to the template. Make sure to purge the cache after making template edit, This does not block access to the file if it's not blocked through permissions, it only prevents the link from being presented to guests or bots.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
User avatar
warmweer
Jr. Extension Validator
Posts: 11720
Joined: Fri Jul 04, 2003 6:34 am
Location: Van Allen Bel ... gium

Re: Hiding file attachments from guests (using style template)

Post by warmweer »

Don't shoot me if I've overlooked/missed something, but I just had a quick look at viewbody_topic.html.
Wouldn't it possible to use the Group template vars extension to prevent sections in viewbody_topic.html being visible to guest (or other groups)

lines 345 to 354 (prosilver 3.3.13)

Code: Select all

<!-- IF postrow.S_HAS_ATTACHMENTS -->
	<dl class="attachbox">
		<dt>
			{L_ATTACHMENTS}
		</dt>
		<!-- BEGIN attachment -->
			<dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd>
		<!-- END attachment -->
	</dl>
<!-- ENDIF -->
It looks as though that whole section could be made to not show up for Guests (or even show the {L_ATTACHMENTS}, but NOT the DISPLAY_ATTACHMENT).
Spelling is freeware, which means you can use it for free.
On the other hand, it is not open source, which means you cannot change it or publish it in a modified form.


Time flies like an arrow, but fruit flies like a banana.
kebev
Registered User
Posts: 7
Joined: Mon Sep 02, 2024 9:01 am

Re: Hiding file attachments from guests (using style template)

Post by kebev »

Hi @warmweer, Thanks for your suggestion. I'm not sure what you mean by "Group template vars extension" but I tried to wrap the section you mentioned in an "IF S_USER_LOGGED_IN" condition but it had no effect.

As for @thecoalman's solution - thank you very much!! The code you suggested seems to have done the trick! Thanks for the awesome help.

I'm wondering if I can convert this solution into some sort of extension or is this beyond the current capability of phpBB? Presumably I will otherwise lose these changes every time I update phpBB?

Thanks!

Return to “phpBB Custom Coding”