]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Make sure that the tag exists before altering/removing it
authorArthurHoaro <arthur@hoa.ro>
Wed, 31 May 2017 16:24:21 +0000 (18:24 +0200)
committerArthurHoaro <arthur@hoa.ro>
Wed, 31 May 2017 16:24:21 +0000 (18:24 +0200)
Fixes #886

index.php

index 2ff2505a5e7b27355d71b64d6b88478d2f87e7e2..39230c80f0a0b3862d7a0856fc33bdeeadc54f19 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1176,6 +1176,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
             die('Wrong token.');
         }
 
+        $count = 0;
         // Delete a tag:
         if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
             $needle = trim($_POST['fromtag']);
@@ -1184,13 +1185,16 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
             foreach($linksToAlter as $key=>$value)
             {
                 $tags = explode(' ',trim($value['tags']));
-                unset($tags[array_search($needle,$tags)]); // Remove tag.
-                $value['tags']=trim(implode(' ',$tags));
-                $LINKSDB[$key]=$value;
-                $history->updateLink($LINKSDB[$key]);
+                if (($pos = array_search($needle,$tags)) !== false) {
+                    unset($tags[$pos]); // Remove tag.
+                    $value['tags']=trim(implode(' ',$tags));
+                    $LINKSDB[$key]=$value;
+                    $history->updateLink($LINKSDB[$key]);
+                    ++$count;
+                }
             }
             $LINKSDB->save($conf->get('resource.page_cache'));
-            echo '<script>alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?do=changetag\';</script>';
+            echo '<script>alert("Tag was removed from '.$count.' links.");document.location=\'?do=changetag\';</script>';
             exit;
         }
 
@@ -1202,13 +1206,16 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
             foreach($linksToAlter as $key=>$value) {
                 $tags = preg_split('/\s+/', trim($value['tags']));
                 // Replace tags value.
-                $tags[array_search($needle, $tags)] = trim($_POST['totag']);
-                $value['tags'] = implode(' ', array_unique($tags));
-                $LINKSDB[$key] = $value;
-                $history->updateLink($LINKSDB[$key]);
+                if (($pos = array_search($needle,$tags)) !== false) {
+                    $tags[$pos] = trim($_POST['totag']);
+                    $value['tags'] = implode(' ', array_unique($tags));
+                    $LINKSDB[$key] = $value;
+                    $history->updateLink($LINKSDB[$key]);
+                    ++$count;
+                }
             }
             $LINKSDB->save($conf->get('resource.page_cache')); // Save to disk.
-            echo '<script>alert("Tag was renamed in '.count($linksToAlter).' links.");document.location=\'?searchtags='.urlencode(escape($_POST['totag'])).'\';</script>';
+            echo '<script>alert("Tag was renamed in '.$count.' links.");document.location=\'?searchtags='.urlencode(escape($_POST['totag'])).'\';</script>';
             exit;
         }
     }