Browse Source

Move strip protocol up.

dereuromark 9 years ago
parent
commit
6b2afc7711

+ 20 - 0
src/Utility/Utility.php

@@ -216,6 +216,26 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
+	 * 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
 	 * A more robust wrapper around for file_exists() which easily
 	 * fails to return true for existent remote files.
 	 * fails to return true for existent remote files.
 	 * Per default it allows http/https images to be looked up via urlExists()
 	 * Per default it allows http/https images to be looked up via urlExists()

+ 2 - 16
src/View/Helper/TextHelper.php

@@ -6,6 +6,7 @@ use Cake\Utility\Hash;
 use Cake\View\Helper\TextHelper as CakeTextHelper;
 use Cake\View\Helper\TextHelper as CakeTextHelper;
 use Cake\View\View;
 use Cake\View\View;
 use Tools\Utility\Number;
 use Tools\Utility\Number;
+use Tools\Utility\Utility;
 
 
 if (!defined('CHAR_HELLIP')) {
 if (!defined('CHAR_HELLIP')) {
 	define('CHAR_HELLIP', '…'); # � (horizontal ellipsis = three dot leader)
 	define('CHAR_HELLIP', '…'); # � (horizontal ellipsis = three dot leader)
@@ -60,7 +61,7 @@ class TextHelper extends CakeTextHelper {
 			return $url;
 			return $url;
 		}
 		}
 		// http:// etc has not to be displayed, so
 		// http:// etc has not to be displayed, so
-		$url = $this->stripProtocol($url);
+		$url = Utility::stripProtocol($url);
 		// cut the parameters
 		// cut the parameters
 		if (mb_strpos($url, '/') !== false) {
 		if (mb_strpos($url, '/') !== false) {
 			$url = strtok($url, '/');
 			$url = strtok($url, '/');
@@ -82,21 +83,6 @@ class TextHelper extends CakeTextHelper {
 	}
 	}
 
 
 	/**
 	/**
-	 * 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, ...).
 	 * Transforming int values into ordinal numbers (1st, 3rd, ...).
 	 * When using HTML, you can use <sup>, as well.
 	 * When using HTML, you can use <sup>, as well.
 	 *
 	 *

+ 17 - 0
tests/TestCase/Utility/UtilityTest.php

@@ -284,6 +284,23 @@ class UtilityTest extends TestCase {
 	}
 	}
 
 
 	/**
 	/**
+	 * @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
 	 * @covers ::trimDeep
 	 * @return void
 	 * @return void
 	 */
 	 */

+ 0 - 17
tests/TestCase/View/Helper/TextHelperTest.php

@@ -74,23 +74,6 @@ class TextHelperTest extends TestCase {
 	}
 	}
 
 
 	/**
 	/**
-	 * 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()
 	 * TextExtHelperTest::testAutoLinkUrls()
 	 *
 	 *
 	 * @return void
 	 * @return void