Simple Machines Community Forum

Customizing SMF => Modifications and Packages => Topic started by: karlbenson on October 14, 2007, 04:45:07 PM

Title: Internal Links Use Same Window v1.2
Post by: karlbenson on October 14, 2007, 04:45:07 PM

INTERNAL LINKS USE SAME WINDOW v1.2
By Karl Benson (http://custom.simplemachines.org/mods/index.php?action=profile;u=63186)

Link to Mod (http://custom.simplemachines.org/mods/index.php?mod=980) | Support Topic (http://www.simplemachines.org/community/index.php?topic=200406.0) | Demo (http://www.adrevenueshare.com/index.php?topic=25.0) | Donate (http://www.adrevenueshare.com/donate)

Compatibility
For SMF 1.1.x and SMF 2.0 Beta x

Introduction
This simple mod fixes all internal SMF urls in posts/pms etc to open in the same window rather than a new window.

Alot of people either just post straight links (which get autoconverted to bbcode), don't know about [iurl][/iurl], can't be bothered or forget to use it when posting links to other smf pages on your site.
So that sometimes results in having many new tabs/windows opening for internal links when really opening in the same window is preferrable.

Features
o Makes local links (to local smf pages) open in same window
(It detects $scripturl at the beginning of the link)
o Makes adjustment if using queryless urls.
o Works for both www. and non www. links

Installation
Any previous versions of this mod need to be uninstalled prior to installing this version.

Install the mod via the SMF Package Manager, and your done. It installs on ALL themes automatically as it only affects Source files.

There are NO theme edits required and there are NO language strings to translate.

Useful Links
SMF Package Parser (http://www.adrevenueshare.com/parser)
Manual Installation Of Mods (http://docs.simplemachines.org/index.php?topic=402)
How Do I Modify Files? (http://www.simplemachines.org/community/index.php?topic=24110.0)

Donate
Has this modification helped you? Support the developer by Donating (http://www.adrevenueshare.com/donate)

Support
Please use the modification thread for support with this modification.
(Please don't ask me to do the edits for you)

Changelog
1.0 - 9th October 2007
o Initial release per request
1.1 - 16th October 2007
o Fixed bug with dots (.) not escaped
o Added /u modifier for when using UTF-8
1.2 - 13th February 2008
o Checked 2.0 compatible (no changes necessary)
o Improved/optimized regex pattern
o Added adjustment to use boardurl instead of scripturl if using queryless urls
o Fixed to work for www. and non www. versions
o Updated readme
Title: Re: Internal Links Use Same Window
Post by: Kela on October 16, 2007, 05:20:15 AM
It is unsafe mod! Don't use it!

You cannot just use preg_replace:

$message = preg_replace('#<a href="'.$scripturl.'(.*?)"(.*?)(target="_blank")(.*?)</a>#i','<a href="'.$scripturl.'$1"$2$4</a>',$message);
Title: Re: Internal Links Use Same Window
Post by: karlbenson on October 16, 2007, 06:12:09 AM
Kela, as far as I can see it IS safe.

I am eager to know why you believe it is not safe.

a) SMF has already validated/sanitized the url and converted it to bbcode
b) The ONLY change that is made is that it when it parses back out everything, it removes target="blank"
Title: Re: Internal Links Use Same Window
Post by: Kela on October 16, 2007, 06:29:26 AM
karlbenson
I have installed your mod and have deleted after 1 day of usage. Some user post became empty (BUT in edit mode all are ok)! I have deleted your mod and posts appeared again.

Second, your preg_replace doesn't take into consideration text encoding (e.g. UTF-8: u (PCRE_UTF8 (http://ua.php.net/manual/en/reference.pcre.pattern.modifiers.php)))
Third, $scripturl can be somethink like http://www.simplemachines.org/community/index.php. Dot (.) match any character except newline (by default). See http://ua.php.net/manual/en/reference.pcre.pattern.syntax.php
Title: Re: Internal Links Use Same Window
Post by: karlbenson on October 16, 2007, 07:07:06 AM
I'll have another test of this later.

Re:Utf-8
I tested in utf-8 without a problem.  But that is easy enough to add the u support if using utf8. (and with a bunch of arabic sample text)

Re: Blank posts, I'll test again.

Re: scripturl, indeed, you are correct, the dot can match any character, however the idea was to loosely match urls, NOT to validate them.  At the time I discarded the idea of escaping dots because it would add unneeded overhead and since it wasnt important for them to match. However it maybe worth looking at again.

Overall though this mod shouldnt be regarded as unsafe.  Room for improvement maybe.
Title: Re: Internal Links Use Same Window
Post by: Kela on October 16, 2007, 07:10:53 AM
I'll provide you an example of post which become empty...
Title: Re: Internal Links Use Same Window
Post by: karlbenson on October 16, 2007, 07:14:41 AM
thanks.

Will look at this as soon as i get back from job interview.
Title: Re: Internal Links Use Same Window
Post by: karlbenson on October 16, 2007, 04:17:00 PM
Ok I've uploaded v1.1 [Mod] (http://custom.simplemachines.org/mods/index.php?mod=980)

During my test the /u modifier didnt seem to make any difference.  But I've left it in since SMF does use it in some of its own preg_replaces.

1.1 - 16th October 2007
o Fixed bug with dots (.) not escaped
o Added /u modifier for when using UTF-8
Title: Re: Internal Links Use Same Window
Post by: ApplianceJunk on October 20, 2007, 10:48:59 AM
Hi,

Thank you for this mod.
I have downloaded and installed it without any problems.
I am not sure if it works like I was thinking it would.

For example the tinyurl links in this post do not open in the same window.
http://appliancejunk.com/forums/index.php?topic=201.msg859#msg859

Should they?

Thank you,
AJ
Title: Re: Internal Links Use Same Window
Post by: ApplianceJunk on October 20, 2007, 10:49:51 AM
Did you get the job? :)

Quote from: karlbenson on October 16, 2007, 07:14:41 AM
thanks.

Will look at this as soon as i get back from job interview.
Title: Re: Internal Links Use Same Window
Post by: karlbenson on October 20, 2007, 11:49:57 AM
Nope.  But had another interview yesterday.

The ONLY links which this mod affects are ones which START http://appliancejunk.com/forums/index.php
Those ones will open in the same window rather than a new window.

Tinyurl links are external links and so would open in new window (unless you used [iurl] for the same window)
Title: Re: Internal Links Use Same Window
Post by: ApplianceJunk on October 20, 2007, 11:55:53 AM
ok, then it looks like its working the way it should.

QuoteNope.  But had another interview yesterday.

Good luck and thanks again for the mod.

AJ
Title: Re: Internal Links Use Same Window
Post by: karlbenson on November 21, 2007, 09:35:05 PM
Thanks.

Indeed, this is one of those mods that if its working, you will forget that its there.
Title: Re: Internal Links Use Same Window
Post by: crazedlemming on December 04, 2007, 03:03:25 PM
This is exactly what I needed!...almost anyway. Could you possibly help push me in the right direction on modifying the mod?

I've put together a site for some people using smf and mkportal. As it is now, the mod only appears to fix links to pages within the forum. I'd like to fix is so it takes care of anything on the same domain.

Currently Opens in Same Window:
http://domain.com/forum/index?board=1.0

Currently Opens in New Window:
http://www.domain.com/forum/index?board=1.0 (users do silly things like add www to things they copy&paste)
http://domain.com/index?whatever


Any help is greatly appreciated,
Ron
Title: Re: Internal Links Use Same Window
Post by: karlbenson on December 04, 2007, 03:55:54 PM
This mod simply preg_matches the smf variable $scripturl

You would need to replace the $scripturl with your domain

Replace the line
$internal = str_replace('.','\.',$scripturl);
with
$internal = 'http://(?:www\.|)domain\.com';

Title: Re: Internal Links Use Same Window
Post by: crazedlemming on December 04, 2007, 07:05:26 PM
The '. $scripturl . ' part also needed to be removed from the preg_replace line to get that fix to work.

Would there be an easy way to get it to treat all subdomains as internal pages instead of letting them open a new window or stripping them out as it's currently doing with the www?

Also while plugging in different urls testing the output, I found that if someone happens to enter the domain name without a slash at the end the link gets directed to the current page.

Sorry to be a pest. I'm trying to understand what the code is doing and regular expressions have always tended to go over my head. Thank you very much for the help.
Title: Re: Internal Links Use Same Window
Post by: karlbenson on December 04, 2007, 10:27:15 PM
There isnt a quick or easy way to securely do it for subdomain links.
Thats why I settled for the scripturl.

You could use
(.*?) instead of (?:www\.|)
but that would also make anonymized links open in a new window eg

http://www.externalsite.com/?http://www.internallink.com

If you have a specific subdomain thats easy to do
For a subdomain called 'sub'
(?:www\.|sub\.|) instead of (?:www\.|)
Title: Re: Internal Links Use Same Window
Post by: karlbenson on January 18, 2008, 11:52:03 PM
I have reuploaded this mod and screenshots for it.

I am in the process of doing it for ALL my other mods aswell.  Please bear with me.
Title: Re: Internal Links Use Same Window v1.2
Post by: karlbenson on February 12, 2008, 08:37:33 PM
1.2 - 13th February 2008
o Checked 2.0 compatible (no changes necessary)
o Improved/optimized regex pattern
o Added adjustment to use boardurl instead of scripturl if using queryless urls
o Fixed to work for www. and non www. versions
o Updated readme
Title: Re: Internal Links Use Same Window v1.2
Post by: karlbenson on March 18, 2008, 10:29:25 AM
FYI, I've just tested and it works for SMF 2.0 Beta 3 Public
Title: Re: Internal Links Use Same Window v1.2
Post by: forummaker on May 05, 2008, 08:27:23 PM
Here's one problem I noticed. I'm also using the "referral mod". When someone is registering, when it comes time to fill in the referral part, if they click on the "find referrer" link button, it opens in the same window, instead of a popup window, and there is now way to get back to the previous window. Well, you can get back to the previous window, but it will not fill in the referred box. I'll have to uninstall this mod until I can get a fix for this. I like the mod idea. Any fix for this? Thanks

Modified:
Well I uninstalled the mod and I'm still having the same issue...hmmmm. I know it's not the referral mod, I've been using it with no problem. There's something else I noticed, I'm using the default theme, visitors can click on the "plus/minus" at the top of the forum and it works to min/max the news. But, members can't. I've made sure that I've checked the box in admin to allow this feature, but it doesn't work. Wonder if there's a connection...?
Title: Re: Internal Links Use Same Window v1.2
Post by: karlbenson on May 05, 2008, 09:14:57 PM
The opening in the same window is not caused by the mod.

Although it does the same. The Opening in the same window ONLY affects links which we bbcoded.

The find/popup is not bbcoded. I believe it might be a bug with the referrals mod.
Title: Re: Internal Links Use Same Window v1.2
Post by: b33z on July 06, 2008, 05:04:41 AM
i dont care what anybody says about this mod. i installed it and it works perfect! its god sent! thanks for all the hard work!
Title: Re: Internal Links Use Same Window v1.2
Post by: Apllicmz on July 06, 2008, 06:20:06 AM
This is an outdated mod that is no longer supported by its author. Use at your own risk

can check that..
Title: Re: Internal Links Use Same Window v1.2
Post by: spiros on December 28, 2012, 07:34:34 AM
Very good mod idea, I wish someone could update it.
Title: Re: Internal Links Use Same Window v1.2
Post by: Tomcraft on December 28, 2012, 07:38:26 AM
I would also appreciate an update. :-)

Regards, Torsten
Title: Re: Internal Links Use Same Window v1.2
Post by: Mr. Jinx on November 14, 2014, 12:14:18 PM
I have been using this mod from the day it was created :)
Recently I changed my domain name from http://www.domain.com to https://domain.com
Internal links are opened in a new window.

Any idea's? Something must be wrong with this preg_match:

if(preg_match('~(?:http|ftp)(?:s)?://(?:www\.)?(.*?)$~i'.($context['utf8'] ? 'u' : ''), $internal, $match))
                $internal = '(?:http|ftp)(?:s)?://(?:www\.)?(?:'.  preg_quote($match[1], '~') .')';
        else
                $internal = preg_quote($internal, '~');
Title: Re: Internal Links Use Same Window v1.2
Post by: Mr. Jinx on December 28, 2014, 05:20:45 AM
Anyone?