Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: MrMorph on February 20, 2022, 09:49:39 AM

Title: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 09:49:39 AM
I've looked through the function list, but don't see where these are made.

I mean; when you submit a new post with a plain URL in it, the URL is turned into a link once the post is submitted.

I would like to find where the code to do that is located please.

Thanks!
Title: Re: Where are the auto created hyperlinks made?
Post by: Arantor on February 20, 2022, 10:29:44 AM
What *exactly* do you want to do with it?

This particular piece of code is one of the most complex parts of the bbcode parser and changing it can actually make your site insecure, so it is not recommended.

But if you can explain what you're trying to do, we'll help explain what exactly is needed to be done to do it safely.
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 10:58:45 AM
Thanks for the reply. I am wanting to make internal links open in the same window rather than open them in new windows.
Title: Re: Where are the auto created hyperlinks made?
Post by: Aleksi "Lex" Kilpinen on February 20, 2022, 11:00:50 AM
Unless I'm terribly mistaken, that is default behaviour in SMF.

EDIT: Testing this, I may actually be mistaken.  And thinking a bit, it actually makes sense it isn't like that always.

Default (URL) link https://www.simplemachines.org/community/index.php
Internal (IURL) link https://www.simplemachines.org/community/index.php
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 11:07:30 AM
Did you use the IURL tag?

I know that I can use the BBC code, but it's annoying having to use it all the time when I want to link internally.
Title: Re: Where are the auto created hyperlinks made?
Post by: Aleksi "Lex" Kilpinen on February 20, 2022, 11:10:47 AM
Yeah, turns out I was mistaken. Some sort of brain fart moment there.
It actually makes perfect sense to me that it isn't defaulting to iurl behaviour.
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 11:11:42 AM
It doesn't to me  ;D

To me it's bad practice to force new windows on a user.
Title: Re: Where are the auto created hyperlinks made?
Post by: Kindred on February 20, 2022, 11:19:24 AM
There was a mod which made all local links use IURL
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 11:21:13 AM
Yep this one: https://custom.simplemachines.org/index.php?mod=980

But it's not been updated for years. I will take a look at the code to see if I can find any pointers.
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 11:23:20 AM
I had a look and it's not a good solution - it works by using preg_replace on the entire message.

I'd like to nip it in the bud at the point the link gets made.
Title: Re: Where are the auto created hyperlinks made?
Post by: Arantor on February 20, 2022, 12:31:59 PM
Subs.php line 3133 is where it's actually built after the 300 or so lines of figuring out where the links are (and if you fret about the preg-regex in that mod, do NOT look at the code before line 3133 in an unmodified Subs.php because it's many MANY lines of regex)

return '[url="' . str_replace(array('[', ']'), array('[', ']'), iri_to_url($fullUrl)) . '"]' . $url . '[/url]';
As long as you return the correct bbcode format it'll be fine. Do not try to unwrap the iri_to_url call (this is required for proper link handling in SMF)
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 01:12:50 PM
I don't see that at that line.

Whereabouts did you think it has to go please?

Thanks!

Regex is fine when necessary.
Title: Re: Where are the auto created hyperlinks made?
Post by: Arantor on February 20, 2022, 01:38:17 PM
I gave you the very line you need to change, and that's where it is in an unmodified SMF 2.1.1, the current SMF version.
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 20, 2022, 02:16:01 PM
Okay, which function is it in please?

My file is edited.
Title: Re: Where are the auto created hyperlinks made?
Post by: Arantor on February 20, 2022, 02:24:37 PM
It's in parse_bbc. The function that in 2.0 is 1500 lines long, and even longer in 2.1. There's a reason I went and found you the code from 2.1.1 that you'd specifically need to edit because finding it otherwise is a complete nightmare.

Wait, you *are* using 2.1, right? Because if not, the line I linked doesn't exist (as that whole piece of code was rewritten in 2.1)... in which case for 2.0 you need to find this chunk instead:

// Parse any URLs.... have to get rid of the @ problems some things cause... stupid email addresses.
if (!isset($disabled['url']) && (strpos($data, '://') !== false || strpos($data, 'www.') !== false) && strpos($data, '[url') === false)
{
// Switch out quotes really quick because they can cause problems.
$data = strtr($data, array('&#039;' => '\'', '&nbsp;' => $context['utf8'] ? "\xC2\xA0" : "\xA0", '&quot;' => '>">', '"' => '<"<', '&lt;' => '<lt<'));

// Only do this if the preg survives.
if (is_string($result = preg_replace(array(
'~(?<=[\s>\.(;\'"]|^)((?:http|https)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@!,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i',
'~(?<=[\s>\.(;\'"]|^)((?:ftp|ftps)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i',
'~(?<=[\s>(\'<]|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\w\-_\~%\.@!,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i'
), array(
'[url]$1[/url]',
'[ftp]$1[/ftp]',
'[url=http://$1]$1[/url]'
), $data)))
$data = $result;

$data = strtr($data, array('\'' => '&#039;', $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;', '>">' => '&quot;', '<"<' => '"', '<lt<' => '&lt;'));
}

and modify $data just before the closing brace because that's the part where the url bbcode will have been inserted around the link. 2.0 doesn't support IRI addresses so matching against your site URL will be a lot simpler here.
Title: Re: Where are the auto created hyperlinks made?
Post by: Wellwisher on February 20, 2022, 11:26:44 PM
Quote from: MrMorph on February 20, 2022, 09:49:39 AMI've looked through the function list, but don't see where these are made.

I mean; when you submit a new post with a plain URL in it, the URL is turned into a link once the post is submitted.

I would like to find where the code to do that is located please.

Thanks!

There's a github mod I use for this purpose, works for 2.0 not sure about 2.1:

https://github.com/LinuxPanda/SMF2.0Mod--Auto_-iurl-_For_Internal_Links_And_-nofollow-_For_External_Links
Title: Re: Where are the auto created hyperlinks made?
Post by: Sesquipedalian on February 21, 2022, 02:03:21 AM
Quote from: Wellwisher on February 20, 2022, 11:26:44 PMThere's a github mod I use for this purpose, works for 2.0 not sure about 2.1:

That will not work for SMF 2.1.
Title: Re: Where are the auto created hyperlinks made?
Post by: MrMorph on February 21, 2022, 11:52:52 AM
Hi guys, sorry for the late reply.

@Arantor I'm using 2.0.19 so that explains why I couldn't find it!

So $data will be holding the raw href it looks like? I can just do a test against my $scripturl if that's the case.

@Wellwisher Thanks, I will take a look if I get nowhere on my own  8)
Title: Re: Where are the auto created hyperlinks made?
Post by: Sesquipedalian on February 21, 2022, 12:50:20 PM
I would suggest $boardurl rather than $scripturl, in case you want to catch links to files stored within your SMF directory as well.
Title: Re: Where are the auto created hyperlinks made?
Post by: Arantor on February 21, 2022, 02:30:23 PM
If you implement where I suggested, it'll contain the full bbcode form including the tag - this is because depending on the form of the link it may or may not have the http: part in front of it so this fixes it either way.

Note that if you're using HTTPS you'll want to not match boardurl directory (definitely better than scripturl) precisely because of the interplay between HTTP and HTTPS links.