[BUGFIX] t3lib_div: adjust substUrlsInPlainText to also work on URLs at end of sentence
authorRobert Heel <typo3.org@bobosch.de>
Mon, 6 Feb 2012 02:47:48 +0000 (03:47 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 29 Oct 2012 08:42:22 +0000 (09:42 +0100)
Reworked substUrlsInPlainText by using regex.
Adding unittest.

Change-Id: I76630dac633b4fb1875f585a2bca888bfd4a16cf
Resolves: #28248
Releases: 4.5, 4.6, 4.7, 6.0
Reviewed-on: http://review.typo3.org/10539
Reviewed-by: Mario Rimann
Reviewed-by: Stefan Neufeind
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
t3lib/class.t3lib_div.php
tests/t3lib/t3lib_divTest.php

index 203519a..425803c 100644 (file)
@@ -5780,29 +5780,32 @@ final class t3lib_div {
         * @see makeRedirectUrl()
         */
        public static function substUrlsInPlainText($message, $urlmode = '76', $index_script_url = '') {
-                       // Substitute URLs with shorter links:
-               foreach (array('http', 'https') as $protocol) {
-                       $urlSplit = explode($protocol . '://', $message);
-                       foreach ($urlSplit as $c => &$v) {
-                               if ($c) {
-                                       $newParts = preg_split('/\s|[<>"{}|\\\^`()\']/', $v, 2);
-                                       $newURL = $protocol . '://' . $newParts[0];
-
-                                       switch ((string) $urlmode) {
-                                               case 'all':
-                                                       $newURL = self::makeRedirectUrl($newURL, 0, $index_script_url);
-                                                       break;
-                                               case '76':
-                                                       $newURL = self::makeRedirectUrl($newURL, 76, $index_script_url);
-                                                       break;
-                                       }
-                                       $v = $newURL . substr($v, strlen($newParts[0]));
-                               }
-                       }
-                       $message = implode('', $urlSplit);
+               $lengthLimit = FALSE;
+
+               switch ((string) $urlmode) {
+                       case '':
+                               $lengthLimit = FALSE;
+                               break;
+                       case 'all':
+                               $lengthLimit = 0;
+                               break;
+                       case '76':
+                       default:
+                               $lengthLimit = (int) $urlmode;
+               }
+
+               if ($lengthLimit === FALSE) {
+                               // no processing
+                       $messageSubstituted = $message;
+               } else {
+                       $messageSubstituted = preg_replace(
+                               '/(http|https):\/\/.+(?=[\]\.\?]*([\! \'"()<>]+|$))/eiU',
+                               'self::makeRedirectUrl("\\0",' . $lengthLimit . ',"' . $index_script_url . '")',
+                               $message
+                       );
                }
 
-               return $message;
+               return $messageSubstituted;
        }
 
        /**
index c2b5e1d..30e11a9 100644 (file)
@@ -3192,5 +3192,45 @@ class t3lib_divTest extends tx_phpunit_testcase {
                        t3lib_div::hasValidClassPrefix('customPrefix_foo', array('customPrefix_'))
                );
        }
+
+       ///////////////////////////////////////////////////
+       // Tests concerning substUrlsInPlainText
+       ///////////////////////////////////////////////////
+
+       /**
+        * @return array
+        */
+       public function substUrlsInPlainTextDataProvider() {
+               $urlMatch = 'http://example.com/index.php\?RDCT=[0-9a-z]{20}';
+               return array(
+                       array('http://only-url.com', '|^' . $urlMatch . '$|'),
+                       array('https://only-secure-url.com', '|^' . $urlMatch . '$|'),
+                       array('A http://url in the sentence.', '|^A ' . $urlMatch . ' in the sentence\.$|'),
+                       array('URL in round brackets (http://www.example.com) in the sentence.', '|^URL in round brackets \(' . $urlMatch . '\) in the sentence.$|'),
+                       array('URL in square brackets [http://www.example.com/a/b.php?c[d]=e] in the sentence.', '|^URL in square brackets \[' . $urlMatch . '\] in the sentence.$|'),
+                       array('URL in square brackets at the end of the sentence [http://www.example.com/a/b.php?c[d]=e].', '|^URL in square brackets at the end of the sentence \[' . $urlMatch . '].$|'),
+                       array('Square brackets in the http://www.url.com?tt_news[uid]=1', '|^Square brackets in the ' . $urlMatch . '$|'),
+                       array('URL with http://dot.com.', '|^URL with ' . $urlMatch . '.$|'),
+                       array('URL in <a href="http://www.example.com/">a tag</a>', '|^URL in <a href="' . $urlMatch . '">a tag</a\>$|'),
+                       array('URL in HTML <b>http://www.example.com</b><br />', '|^URL in HTML <b>' . $urlMatch . '</b><br />$|'),
+                       array('URL with http://username@example.com/', '|^URL with ' . $urlMatch . '$|'),
+                       array('Secret in URL http://username:secret@example.com', '|^Secret in URL ' . $urlMatch . '$|'),
+                       array('URL in quotation marks "http://example.com"', '|^URL in quotation marks "' . $urlMatch . '"$|'),
+                       array('URL with umlauts http://müller.de', '|^URL with umlauts ' . $urlMatch . '$|'),
+                       array("Multiline\ntext with a http://url.com", "|^Multiline\ntext with a " . $urlMatch . '$|s'),
+                       array('http://www.shout.com!', '|^' . $urlMatch . '!$|'),
+                       array('And with two URLs http://www.two.com/abc http://urls.com/abc?x=1&y=2', '|^And with two URLs ' . $urlMatch . ' ' . $urlMatch . '$|'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider substUrlsInPlainTextDataProvider
+        * @param string $input Text to recognise URLs from
+        * @param string $expected Text with correctly detected URLs
+        */
+       public function substUrlsInPlainText($input, $expectedPreg) {
+               $this->assertTrue(preg_match($expectedPreg, t3lib_div::substUrlsInPlainText($input, 1, 'http://example.com/index.php')) == 1);
+       }
 }
 ?>