]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fixes presence of empty tags for private tags and in search results 725/head
authorArthurHoaro <arthur@hoa.ro>
Tue, 20 Dec 2016 10:06:22 +0000 (11:06 +0100)
committerArthurHoaro <arthur@hoa.ro>
Tue, 3 Jan 2017 08:47:15 +0000 (09:47 +0100)
  * Private tags: make sure empty tags are properly filtered
  * Search results:
    * Use preg_split instead of function combination
    * Add normalize_spaces to remove extra whitespaces displaying empty tags search

application/LinkFilter.php
application/Utils.php
index.php
tests/UtilsTest.php

index daa6d9cc26a8ed139f34581ff619d98969415073..57ebfd5cb8070e55bd87545f9ad716434a8c488b 100644 (file)
@@ -348,7 +348,7 @@ class LinkFilter
         $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8');
         $tagsOut = str_replace(',', ' ', $tagsOut);
 
-        return array_values(array_filter(explode(' ', trim($tagsOut)), 'strlen'));
+        return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY);
     }
 }
 
index 62902341aee1a3d2bea676c8bbe3a53a2a721984..35d652241bb6a5a4c42c7ded7b7381be48dc7f15 100644 (file)
@@ -257,3 +257,16 @@ function generate_api_secret($username, $salt)
 
     return str_shuffle(substr(hash_hmac('sha512', uniqid($salt), $username), 10, 12));
 }
+
+/**
+ * Trim string, replace sequences of whitespaces by a single space.
+ * PHP equivalent to `normalize-space` XSLT function.
+ *
+ * @param string $string Input string.
+ *
+ * @return mixed Normalized string.
+ */
+function normalize_spaces($string)
+{
+    return preg_replace('/\s{2,}/', ' ', trim($string));
+}
index dd9b48bd2f84ed3cedea29ce0f8b7552a3b52cdf..427eb23941cc319840afc43d676795edaba345b7 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1601,8 +1601,8 @@ function renderPage($conf, $pluginManager, $LINKSDB)
 function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
 {
     // Used in templates
-    $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
-    $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
+    $searchtags = !empty($_GET['searchtags']) ? escape(normalize_spaces($_GET['searchtags'])) : '';
+    $searchterm = !empty($_GET['searchterm']) ? escape(normalize_spaces($_GET['searchterm'])) : '';
 
     // Smallhash filter
     if (! empty($_SERVER['QUERY_STRING'])
@@ -1649,7 +1649,7 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
         } else {
             $link['updated_timestamp'] = '';
         }
-        $taglist = explode(' ', $link['tags']);
+        $taglist = preg_split('/\s+/', $link['tags'], -1, PREG_SPLIT_NO_EMPTY);
         uasort($taglist, 'strcasecmp');
         $link['taglist'] = $taglist;
         // Check for both signs of a note: starting with ? and 7 chars long.
index 0cf9a921ed0ee979da42d0ec3d5846e822c919f7..c885f552350b62cf003921c5b36673b3224eea14 100644 (file)
@@ -253,7 +253,7 @@ class UtilsTest extends PHPUnit_Framework_TestCase
             is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
         );
     }
-
+    
     /**
      * Test generateSecretApi.
      */
@@ -270,4 +270,16 @@ class UtilsTest extends PHPUnit_Framework_TestCase
         $this->assertFalse(generate_api_secret('', ''));
         $this->assertFalse(generate_api_secret(false, false));
     }
+
+    /**
+     * Test normalize_spaces.
+     */
+    public function testNormalizeSpace()
+    {
+        $str = ' foo   bar is   important ';
+        $this->assertEquals('foo bar is important', normalize_spaces($str));
+        $this->assertEquals('foo', normalize_spaces('foo'));
+        $this->assertEquals('', normalize_spaces(''));
+        $this->assertEquals(null, normalize_spaces(null));
+    }
 }