Skip to content

Commit d49d700

Browse files
authored
Merge pull request #9180 from kenjis/fix-auto_link-bug
fix: auto_link() converts invalid strings like `://codeigniter.com`
2 parents 36f9c2f + 94fe31f commit d49d700

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

system/Helpers/url_helper.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,15 @@ function safe_mailto(string $email, string $title = '', $attributes = ''): strin
351351
function auto_link(string $str, string $type = 'both', bool $popup = false): string
352352
{
353353
// Find and replace any URLs.
354-
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)) {
354+
if (
355+
$type !== 'email'
356+
&& preg_match_all(
357+
'#([a-z][a-z0-9+\-.]*://|www\.)[a-z0-9]+(-+[a-z0-9]+)*(\.[a-z0-9]+(-+[a-z0-9]+)*)+(/([^\s()<>;]+\w)?/?)?#i',
358+
$str,
359+
$matches,
360+
PREG_OFFSET_CAPTURE | PREG_SET_ORDER
361+
)
362+
) {
355363
// Set our target HTML if using popup links.
356364
$target = ($popup) ? ' target="_blank"' : '';
357365

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

372380
// Find and replace any emails.
373-
if ($type !== 'url' && preg_match_all('#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i', $str, $matches, PREG_OFFSET_CAPTURE)) {
381+
if (
382+
$type !== 'url'
383+
&& preg_match_all(
384+
'#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i',
385+
$str,
386+
$matches,
387+
PREG_OFFSET_CAPTURE
388+
)
389+
) {
374390
foreach (array_reverse($matches[0]) as $match) {
375391
if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== false) {
376392
$str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public static function provideAutoLinkUrl(): iterable
523523
],
524524
'test06' => [
525525
'This one: ://codeigniter.com must not break this one: http://codeigniter.com',
526-
'This one: <a href="://codeigniter.com">://codeigniter.com</a> must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
526+
'This one: ://codeigniter.com must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
527527
],
528528
'test07' => [
529529
'Visit example.com or email [email protected]',
@@ -623,7 +623,7 @@ public static function provideAutolinkBoth(): iterable
623623
],
624624
'test06' => [
625625
'This one: ://codeigniter.com must not break this one: http://codeigniter.com',
626-
'This one: <a href="://codeigniter.com">://codeigniter.com</a> must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
626+
'This one: ://codeigniter.com must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>',
627627
],
628628
'test07' => [
629629
'Visit example.com or email [email protected]',
@@ -675,7 +675,7 @@ public static function provideAutoLinkPopup(): iterable
675675
],
676676
'test06' => [
677677
'This one: ://codeigniter.com must not break this one: http://codeigniter.com',
678-
'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>',
678+
'This one: ://codeigniter.com must not break this one: <a href="http://codeigniter.com" target="_blank">http://codeigniter.com</a>',
679679
],
680680
'test07' => [
681681
'Visit example.com or email [email protected]',

0 commit comments

Comments
 (0)