• Welcome to Simple Machines Community Forum. Please login or sign up.
January 26, 2022, 04:01:10 AM

News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord


Internal Links Use Same Window v1.2

Started by karlbenson, October 14, 2007, 04:45:07 PM

Previous topic - Next topic

karlbenson

October 14, 2007, 04:45:07 PM Last Edit: February 12, 2008, 08:37:20 PM by karlbenson

INTERNAL LINKS USE SAME WINDOW v1.2
By Karl Benson

Link to Mod | Support Topic | Demo | 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
Manual Installation Of Mods
How Do I Modify Files?

Donate
Has this modification helped you? Support the developer by Donating

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

Kela

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);
[nofollow]

karlbenson

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"

Kela

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 [nofollow]))
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 [nofollow]
[nofollow]

karlbenson

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.

Kela

I'll provide you an example of post which become empty...
[nofollow]

karlbenson

thanks.

Will look at this as soon as i get back from job interview.

karlbenson

Ok I've uploaded v1.1 [Mod]

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

ApplianceJunk

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

ApplianceJunk

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.

karlbenson

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)

ApplianceJunk

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

karlbenson

Thanks.

Indeed, this is one of those mods that if its working, you will forget that its there.

crazedlemming

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

karlbenson

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';


crazedlemming

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.

karlbenson

December 04, 2007, 10:27:15 PM #16 Last Edit: December 04, 2007, 10:30:15 PM by karlbenson
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\.|)

karlbenson

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.

karlbenson

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

karlbenson

FYI, I've just tested and it works for SMF 2.0 Beta 3 Public

Advertisement: