Skip to content

Commit

Permalink
Move strip protocol up.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Mar 18, 2017
1 parent 00f9829 commit 6b2afc7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
20 changes: 20 additions & 0 deletions src/Utility/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ public static function cleanUrl($url, $headerRedirect = false) {
return $url;
}

/**
* Remove http:// or other protocols from the link
*
* @param string $url
* @param array $protocols Defaults to http and https. Pass empty array for all.
* @return string strippedUrl
*/
public static function stripProtocol($url, $protocols = ['http', 'https']) {
$pieces = parse_url($url);
// Already stripped?
if (empty($pieces['scheme'])) {
return $url;
}
if ($protocols && !in_array($pieces['scheme'], $protocols)) {
return $url;
}

return mb_substr($url, mb_strlen($pieces['scheme']) + 3);
}

/**
* A more robust wrapper around for file_exists() which easily
* fails to return true for existent remote files.
Expand Down
18 changes: 2 additions & 16 deletions src/View/Helper/TextHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\View\Helper\TextHelper as CakeTextHelper;
use Cake\View\View;
use Tools\Utility\Number;
use Tools\Utility\Utility;

if (!defined('CHAR_HELLIP')) {
define('CHAR_HELLIP', '…'); # � (horizontal ellipsis = three dot leader)
Expand Down Expand Up @@ -60,7 +61,7 @@ public function minimizeUrl($url, $max = null, array $options = []) {
return $url;
}
// http:// etc has not to be displayed, so
$url = $this->stripProtocol($url);
$url = Utility::stripProtocol($url);
// cut the parameters
if (mb_strpos($url, '/') !== false) {
$url = strtok($url, '/');
Expand All @@ -81,21 +82,6 @@ public function minimizeUrl($url, $max = null, array $options = []) {
return $front . $placeholder . $end;
}

/**
* Remove http:// or other protocols from the link
*
* @param string $url
* @return string strippedUrl
*/
public function stripProtocol($url) {
$pieces = parse_url($url);
// Already stripped?
if (empty($pieces['scheme'])) {
return $url;
}
return mb_substr($url, mb_strlen($pieces['scheme']) + 3); # +3 <=> :// # can only be 4 with "file" (file:///)...
}

/**
* Transforming int values into ordinal numbers (1st, 3rd, ...).
* When using HTML, you can use <sup>, as well.
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Utility/UtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,23 @@ public function testCleanUrl() {
$this->assertSame('http://www.spiegel.de', $res);
}

/**
* @return void
*/
public function testStripUrl() {
$urls = [
'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
'www.cakephp.org' => 'www.cakephp.org',
'https://spiegel.de' => 'spiegel.de',
'ftp://xyz' => 'ftp://xyz',
];

foreach ($urls as $url => $expected) {
$is = Utility::stripProtocol($url);
$this->assertEquals($expected, $is, $url);
}
}

/**
* @covers ::trimDeep
* @return void
Expand Down
17 changes: 0 additions & 17 deletions tests/TestCase/View/Helper/TextHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,6 @@ public function testAutoLinkEmailsWithHtmlOrDangerousStrings() {
$this->assertEquals($expected, $result);
}

/**
* TextExtHelperTest::testStripProtocol()
*
* @return void
*/
public function testStripProtocol() {
$urls = [
'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
'www.cakephp.org' => 'www.cakephp.org'
];

foreach ($urls as $url => $expected) {
$is = $this->Text->stripProtocol($url);
$this->assertEquals($expected, $is);
}
}

/**
* TextExtHelperTest::testAutoLinkUrls()
*
Expand Down

0 comments on commit 6b2afc7

Please sign in to comment.