Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto_link() converts invalid strings like ://codeigniter.com #9180

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,15 @@ function safe_mailto(string $email, string $title = '', $attributes = ''): strin
function auto_link(string $str, string $type = 'both', bool $popup = false): string
{
// Find and replace any URLs.
if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[a-z0-9]+(-+[a-z0-9]+)*(\.[a-z0-9]+(-+[a-z0-9]+)*)+(/([^\s()<>;]+\w)?/?)?#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
if (
$type !== 'email'
&& preg_match_all(
'#([a-z][a-z0-9+\-.]*://|www\.)[a-z0-9]+(-+[a-z0-9]+)*(\.[a-z0-9]+(-+[a-z0-9]+)*)+(/([^\s()<>;]+\w)?/?)?#i',
$str,
$matches,
PREG_OFFSET_CAPTURE | PREG_SET_ORDER
)
) {
// Set our target HTML if using popup links.
$target = ($popup) ? ' target="_blank"' : '';

Expand All @@ -370,7 +378,15 @@ function auto_link(string $str, string $type = 'both', bool $popup = false): str
}

// Find and replace any emails.
if ($type !== 'url' && preg_match_all('#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i', $str, $matches, PREG_OFFSET_CAPTURE)) {
if (
$type !== 'url'
&& preg_match_all(
'#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i',
$str,
$matches,
PREG_OFFSET_CAPTURE
)
) {
foreach (array_reverse($matches[0]) as $match) {
if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== false) {
$str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
Expand Down
6 changes: 3 additions & 3 deletions tests/system/Helpers/URLHelper/MiscUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public static function provideAutoLinkUrl(): iterable
],
'test06' => [
'This one: ://codeigniter.com must not break this one: http://codeigniter.com',
'This one: <a href="://codeigniter.com">://codeigniter.com</a> must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
'This one: ://codeigniter.com must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
],
'test07' => [
'Visit example.com or email [email protected]',
Expand Down Expand Up @@ -623,7 +623,7 @@ public static function provideAutolinkBoth(): iterable
],
'test06' => [
'This one: ://codeigniter.com must not break this one: http://codeigniter.com',
'This one: <a href="://codeigniter.com">://codeigniter.com</a> must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
'This one: ://codeigniter.com must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
],
'test07' => [
'Visit example.com or email [email protected]',
Expand Down Expand Up @@ -675,7 +675,7 @@ public static function provideAutoLinkPopup(): iterable
],
'test06' => [
'This one: ://codeigniter.com must not break this one: http://codeigniter.com',
'This one: <a href="://codeigniter.com" target="_blank">://codeigniter.com</a> must not break this one: <a href="http://codeigniter.com" target="_blank">http://codeigniter.com</a>',
'This one: ://codeigniter.com must not break this one: <a href="http://codeigniter.com" target="_blank">http://codeigniter.com</a>',
],
'test07' => [
'Visit example.com or email [email protected]',
Expand Down
Loading