automatically show links for members only

Looking for a MOD? Have a MOD request? Post here for help. (Note: This forum is community supported; phpBB does not have official MOD authors)
Anti-Spam Guide
Locked
TheDean
Registered User
Posts: 53
Joined: Sat Oct 13, 2007 12:45 pm

automatically show links for members only

Post by TheDean » Tue Jun 29, 2010 9:24 pm

Hi everyone
am looking for a mod to hide links from visitors and show them only to forum members, I found a mod that hides it via BBcode but I want it done automatically.. there was a mod like that for phpBB2 but I didn't find any for phpBB3.. can any1 help?
TY..

Skinny Vinny
Registered User
Posts: 230
Joined: Tue Dec 01, 2009 7:10 pm

Re: automatically show links for members only

Post by Skinny Vinny » Wed Jun 30, 2010 12:04 am

How about a hook?
This is not the most efficient way of doing it, but I hate modifying original source unless I have to. This is one of those cases: we can chew up phpbb source or just do it this way. I like using hooks in combination with cache mods I've been working with: the gain in them overcompensates for the extra execution in something like this.

I'll update post later with a link to a file for download. For now, you'll need to copy and paste the code below into a new php file in the includes/hooks directory. The name of the file must begin with 'hook_', for example I have it as:

hook_svmods_strip_links_from_guests.php

Code: Select all

<?php
// Hook by Skinny Vinny of svmods
// Mod request to strip links from posts for guests.

function svmods_strip_links_from_guests(&$hook){
    global $template, $user;
    if ($user->data['user_id']==ANONYMOUS){
        if ($n=sizeof($template->_tpldata['postrow'])){
            for ($x=0;$x<$n;$x++){
                $template->_tpldata['postrow'][$x]['MESSAGE']=' '.$template->_tpldata['postrow'][$x]['MESSAGE'];
                while ($link_start=stripos($template->_tpldata['postrow'][$x]['MESSAGE'], '<a ')){
                    $link_end=stripos($template->_tpldata['postrow'][$x]['MESSAGE'], '</a>')+4;
                    $template->_tpldata['postrow'][$x]['MESSAGE']=trim(substr($template->_tpldata['postrow'][$x]['MESSAGE'],0,$link_start).' (link available to registered members only) '.substr($template->_tpldata['postrow'][$x]['MESSAGE'],$link_end));
                }
            }
        }
    }
}
    
$phpbb_hook->register(array('template','display'), 'svmods_strip_links_from_guests');
 
Stick the file in the includes/hooks directory, then go to administration index and 'purge the cache'. Done.

You can edit the returned text on line 13 of that file. You could use something added to $user->lang, or whatever you like. That's where it's at. You do not need to repurge after updating the file, just after the initial upload (or if you remove the file later).

Tested on localhost, worked perfect as posted.

Hits guests only as originally requested: bots still see and crawl liks. Beware that this is considered cloaking: you should hide the links from bots as well (or enable what google calls 'first click free'). To do so, I would change line 10 to:

Code: Select all

    if (!$user->data['is_registered']){ 
Usually don't publish hooks here, but what the heck.
Simple mod, few minutes to write. Enjoy :)

TheDean
Registered User
Posts: 53
Joined: Sat Oct 13, 2007 12:45 pm

Re: automatically show links for members only

Post by TheDean » Wed Jun 30, 2010 6:13 am

worked like a charm TY :)

TheDean
Registered User
Posts: 53
Joined: Sat Oct 13, 2007 12:45 pm

Re: automatically show links for members only

Post by TheDean » Wed Jun 30, 2010 6:32 am

sorry to disturb but is there also a way to modify that code so it shortens long urls for registered members (replace some text with ...) cause am using WYSIWYG editor and the shorten url MOD didn't work with it...

Thank You..

Skinny Vinny
Registered User
Posts: 230
Joined: Tue Dec 01, 2009 7:10 pm

Re: automatically show links for members only

Post by Skinny Vinny » Wed Jun 30, 2010 8:35 pm

I'll repeat that this is not the most efficient way of doing this, and if used on a high traffic forum should be combined with caching additions.

That said, again, here's the same file modified to shorten the link text for other users. You can set the length on line 8 ($length_of_link_text=15;).

Copy paste with same instructions as before. (Dean, you want to overwrite the code with the code below.)

Code: Select all

<?php
// Hook by Skinny Vinny of svmods
// Mod request to strip links from posts for guests.
// Further modified to shorten text of link for users other than guests.

function svmods_strip_links_from_guests(&$hook){
    global $template, $user;
    $length_of_link_text=15;
    if ($n=sizeof($template->_tpldata['postrow'])){
        for ($x=0;$x<$n;$x++){
            $template->_tpldata['postrow'][$x]['MESSAGE']=' '.$template->_tpldata['postrow'][$x]['MESSAGE'];
            if ($user->data['user_id']==ANONYMOUS){
                while ($link_start=stripos($template->_tpldata['postrow'][$x]['MESSAGE'], '<a ')){
                    $link_end=stripos($template->_tpldata['postrow'][$x]['MESSAGE'], '</a>')+4;
                    $template->_tpldata['postrow'][$x]['MESSAGE']=trim(substr($template->_tpldata['postrow'][$x]['MESSAGE'],0,$link_start).' (link available to registered members only) '.substr($template->_tpldata['postrow'][$x]['MESSAGE'],$link_end));
                }
            }
            else {
                $marker=0;
                while ($link_start=stripos($template->_tpldata['postrow'][$x]['MESSAGE'], '<a ', $marker)){
                    $marker=$link_start+1;
                    $end_a=strpos($template->_tpldata['postrow'][$x]['MESSAGE'], '>', $marker)+1;
                    $close_a=strpos($template->_tpldata['postrow'][$x]['MESSAGE'], '</a>', $end_a);
                    $link_text=substr($template->_tpldata['postrow'][$x]['MESSAGE'], $end_a, ($close_a-$end_a));
                    if (strlen($link_text)>$length_of_link_text){
                        $link_text=str_replace('http://', '', $link_text);
                        $link_text=str_replace('www.', '', $link_text);
                        if (strlen($link_text)>$length_of_link_text){
                            $link_text=substr($link_text, 0, $length_of_link_text).'...';
                        }
                        $template->_tpldata['postrow'][$x]['MESSAGE']=trim(substr($template->_tpldata['postrow'][$x]['MESSAGE'],0,$end_a).$link_text.substr($template->_tpldata['postrow'][$x]['MESSAGE'], $close_a));
                    }
                }
            }
        }
    }
}
    
$phpbb_hook->register(array('template','display'), 'svmods_strip_links_from_guests');

 
I set it up that if length of text is longer than value, it first removes any http:// and www. from any 'magic' urls, then rechecks length before cropping it.

I would use a value higher than 15, but it's up to those who use it where to set it.

TheDean
Registered User
Posts: 53
Joined: Sat Oct 13, 2007 12:45 pm

Re: automatically show links for members only

Post by TheDean » Thu Jul 01, 2010 11:06 am

works fine ty :)
combined with caching additions
can you explain that a little bit or maybe point me to a topic to read, cause my forums really has high traffic..

TY..

Skinny Vinny
Registered User
Posts: 230
Joined: Tue Dec 01, 2009 7:10 pm

Re: automatically show links for members only

Post by Skinny Vinny » Thu Jul 01, 2010 6:23 pm

What is or is not 'high traffic' depends on what your server/host is capable of delivering. If the addition of that hook causes the boards to excessively slow down, or the server load jumps, then you're considered too high traffic for this approach.

What we're doing in this method is letting phpbb do all of its work, then just before display we're looping back through all of the work again and modifying the content. It's adding execution time to your scripts. It should only be milliseconds, but if you're server is under pressure already... those milliseconds can stack up. Think of it as if we increase execution time by 5%, then it's 5% less max pages/requests per second we can generate.

Of course, if your sites peak is coming so close to your max resources... an upgrade is long overdue already. What we're looking at is at what point our site/server goes to sh?t. This approach adds execution time that lowers the limits already determined by our resources.

The caching I have been working on is not plug and play: it requires extensive modifications to templates, a stack of custom scripts, and I'm using memcached a lot. Most forum admins using virtual hosting simply won't be able to use them. I'll probably never release them due to the sheer volume of changes that have to be made for it. It's just not for the average forum.

There's not a simple caching option for this alone here, as most options will add enough execution times themselves that will essentially make them pointless. Effective caching will encompass far more than this hooks results.


Anyways, if you think this hook is impacting your sites performance:
during peak usage time, monitor your server load and page loading times. Then, comment out the final line ($phpbb_hook->register(array('template','display'), 'svmods_strip_links_from_guests');). If your server load drops and you find a notable improvement in page loading times, then this hook needs to be replaced.

If you added the hook and have not seen any impact to site/server performance, then it's safe to not be concerned with it.

TheDean
Registered User
Posts: 53
Joined: Sat Oct 13, 2007 12:45 pm

Re: automatically show links for members only

Post by TheDean » Fri Jul 02, 2010 10:09 pm

That was very informative Thank you :)

Dawn birth
Registered User
Posts: 8
Joined: Tue Jan 12, 2010 6:13 pm
Location: Egypt

Re: automatically show links for members only

Post by Dawn birth » Tue Jul 20, 2010 2:05 am

Thank you for this hook it works very well .
Is there a method to do the same for images?? i want images to be shown only to members

stylerro
Registered User
Posts: 68
Joined: Mon Dec 07, 2009 12:34 pm
Contact:

Re: automatically show links for members only

Post by stylerro » Sun Sep 05, 2010 12:06 am

Is there a method to do the same for images?? i want images to be shown only to members
me 2. :shock:

Anyasha
Registered User
Posts: 704
Joined: Mon Aug 07, 2006 4:02 am
Name: Anyasha

Re: automatically show links for members only

Post by Anyasha » Mon Sep 06, 2010 5:34 pm

This mod is abandoned but it still works great on 3.0.7PL-1: http://www.phpbb.com/community/viewtopi ... 0&t=669275 make sure you read all the pages for hints/tips. It works for URLs AND images, but again - read all the pages!
Kiss me, I'm Polish!

stylerro
Registered User
Posts: 68
Joined: Mon Dec 07, 2009 12:34 pm
Contact:

Re: automatically show links for members only

Post by stylerro » Tue Sep 07, 2010 7:18 pm

tks for link.
LE: Mod is so old and last version uploaded in latest posts : hide my links from signature...

Daronna
Registered User
Posts: 1
Joined: Thu Apr 14, 2011 1:45 pm

Re: automatically show links for members only

Post by Daronna » Sat Apr 30, 2011 11:44 am

First Post, please excuse the 'newbee-ness'

This mod works great for me, but, I would like to link to 'register' or 'login'

here is my line:

$template->_tpldata['postrow'][$x]['MESSAGE']=trim(substr($template->_tpldata['postrow'][$x]['MESSAGE'],0,$link_start).' (This link is hidden to guests, please login or register to view the link. ) '.substr($template->_tpldata['postrow'][$x]['MESSAGE'],$link_end));

Is there a way to link to login and register so that the link is right in front of the guest?

e.g.
This link is hidden to guests, please (start link)login(end link) or (start link)register(end link) to view the link.

Thanking you in advance for any help.
Daronna

Locked

Return to “[3.0.x] MOD Requests”