From 791fe2feb169b8d45839c44f66fd45c38a782bd9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 Sep 2024 18:15:51 +0900 Subject: [PATCH 1/2] fix: auto_link() converts invalid string like "://codeigniter.com" The scheme is missing, so it is not a valid URL. --- system/Helpers/url_helper.php | 2 +- tests/system/Helpers/URLHelper/MiscUrlTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/system/Helpers/url_helper.php b/system/Helpers/url_helper.php index d68fea2aad7d..6c11233ff77b 100644 --- a/system/Helpers/url_helper.php +++ b/system/Helpers/url_helper.php @@ -351,7 +351,7 @@ 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"' : ''; diff --git a/tests/system/Helpers/URLHelper/MiscUrlTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php index 219b5322dbb5..a006ec80c6ae 100644 --- a/tests/system/Helpers/URLHelper/MiscUrlTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -523,7 +523,7 @@ public static function provideAutoLinkUrl(): iterable ], 'test06' => [ 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', - 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', + 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', ], 'test07' => [ 'Visit example.com or email foo@bar.com', @@ -623,7 +623,7 @@ public static function provideAutolinkBoth(): iterable ], 'test06' => [ 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', - 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', + 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', ], 'test07' => [ 'Visit example.com or email foo@bar.com', @@ -675,7 +675,7 @@ public static function provideAutoLinkPopup(): iterable ], 'test06' => [ 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', - 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', + 'This one: ://codeigniter.com must not break this one: http://codeigniter.com', ], 'test07' => [ 'Visit example.com or email foo@bar.com', From 94fe31fc64c885a5e658cc5e7f9275356796f86f Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 Sep 2024 18:18:50 +0900 Subject: [PATCH 2/2] refactor: break long lines --- system/Helpers/url_helper.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/system/Helpers/url_helper.php b/system/Helpers/url_helper.php index 6c11233ff77b..fcfce09f5697 100644 --- a/system/Helpers/url_helper.php +++ b/system/Helpers/url_helper.php @@ -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('#([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)) { + 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"' : ''; @@ -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]));