Skip to content

Commit 6b2afc7

Browse files
author
dereuromark
committed
Move strip protocol up.
1 parent 00f9829 commit 6b2afc7

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed

src/Utility/Utility.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ public static function cleanUrl($url, $headerRedirect = false) {
215215
return $url;
216216
}
217217

218+
/**
219+
* Remove http:// or other protocols from the link
220+
*
221+
* @param string $url
222+
* @param array $protocols Defaults to http and https. Pass empty array for all.
223+
* @return string strippedUrl
224+
*/
225+
public static function stripProtocol($url, $protocols = ['http', 'https']) {
226+
$pieces = parse_url($url);
227+
// Already stripped?
228+
if (empty($pieces['scheme'])) {
229+
return $url;
230+
}
231+
if ($protocols && !in_array($pieces['scheme'], $protocols)) {
232+
return $url;
233+
}
234+
235+
return mb_substr($url, mb_strlen($pieces['scheme']) + 3);
236+
}
237+
218238
/**
219239
* A more robust wrapper around for file_exists() which easily
220240
* fails to return true for existent remote files.

src/View/Helper/TextHelper.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Cake\View\Helper\TextHelper as CakeTextHelper;
77
use Cake\View\View;
88
use Tools\Utility\Number;
9+
use Tools\Utility\Utility;
910

1011
if (!defined('CHAR_HELLIP')) {
1112
define('CHAR_HELLIP', '…'); # � (horizontal ellipsis = three dot leader)
@@ -60,7 +61,7 @@ public function minimizeUrl($url, $max = null, array $options = []) {
6061
return $url;
6162
}
6263
// http:// etc has not to be displayed, so
63-
$url = $this->stripProtocol($url);
64+
$url = Utility::stripProtocol($url);
6465
// cut the parameters
6566
if (mb_strpos($url, '/') !== false) {
6667
$url = strtok($url, '/');
@@ -81,21 +82,6 @@ public function minimizeUrl($url, $max = null, array $options = []) {
8182
return $front . $placeholder . $end;
8283
}
8384

84-
/**
85-
* Remove http:// or other protocols from the link
86-
*
87-
* @param string $url
88-
* @return string strippedUrl
89-
*/
90-
public function stripProtocol($url) {
91-
$pieces = parse_url($url);
92-
// Already stripped?
93-
if (empty($pieces['scheme'])) {
94-
return $url;
95-
}
96-
return mb_substr($url, mb_strlen($pieces['scheme']) + 3); # +3 <=> :// # can only be 4 with "file" (file:///)...
97-
}
98-
9985
/**
10086
* Transforming int values into ordinal numbers (1st, 3rd, ...).
10187
* When using HTML, you can use <sup>, as well.

tests/TestCase/Utility/UtilityTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,23 @@ public function testCleanUrl() {
283283
$this->assertSame('http://www.spiegel.de', $res);
284284
}
285285

286+
/**
287+
* @return void
288+
*/
289+
public function testStripUrl() {
290+
$urls = [
291+
'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
292+
'www.cakephp.org' => 'www.cakephp.org',
293+
'https://spiegel.de' => 'spiegel.de',
294+
'ftp://xyz' => 'ftp://xyz',
295+
];
296+
297+
foreach ($urls as $url => $expected) {
298+
$is = Utility::stripProtocol($url);
299+
$this->assertEquals($expected, $is, $url);
300+
}
301+
}
302+
286303
/**
287304
* @covers ::trimDeep
288305
* @return void

tests/TestCase/View/Helper/TextHelperTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,6 @@ public function testAutoLinkEmailsWithHtmlOrDangerousStrings() {
7373
$this->assertEquals($expected, $result);
7474
}
7575

76-
/**
77-
* TextExtHelperTest::testStripProtocol()
78-
*
79-
* @return void
80-
*/
81-
public function testStripProtocol() {
82-
$urls = [
83-
'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
84-
'www.cakephp.org' => 'www.cakephp.org'
85-
];
86-
87-
foreach ($urls as $url => $expected) {
88-
$is = $this->Text->stripProtocol($url);
89-
$this->assertEquals($expected, $is);
90-
}
91-
}
92-
9376
/**
9477
* TextExtHelperTest::testAutoLinkUrls()
9578
*

0 commit comments

Comments
 (0)