Make sure that the tag exists before altering/removing it

Fixes #886
This commit is contained in:
ArthurHoaro 2017-05-31 18:24:21 +02:00
parent 5c6fac0bfc
commit 4c970f099f

View file

@ -1176,6 +1176,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
die('Wrong token.'); die('Wrong token.');
} }
$count = 0;
// Delete a tag: // Delete a tag:
if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
$needle = trim($_POST['fromtag']); $needle = trim($_POST['fromtag']);
@ -1184,13 +1185,16 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
foreach($linksToAlter as $key=>$value) foreach($linksToAlter as $key=>$value)
{ {
$tags = explode(' ',trim($value['tags'])); $tags = explode(' ',trim($value['tags']));
unset($tags[array_search($needle,$tags)]); // Remove tag. if (($pos = array_search($needle,$tags)) !== false) {
$value['tags']=trim(implode(' ',$tags)); unset($tags[$pos]); // Remove tag.
$LINKSDB[$key]=$value; $value['tags']=trim(implode(' ',$tags));
$history->updateLink($LINKSDB[$key]); $LINKSDB[$key]=$value;
$history->updateLink($LINKSDB[$key]);
++$count;
}
} }
$LINKSDB->save($conf->get('resource.page_cache')); $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; exit;
} }
@ -1202,13 +1206,16 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
foreach($linksToAlter as $key=>$value) { foreach($linksToAlter as $key=>$value) {
$tags = preg_split('/\s+/', trim($value['tags'])); $tags = preg_split('/\s+/', trim($value['tags']));
// Replace tags value. // Replace tags value.
$tags[array_search($needle, $tags)] = trim($_POST['totag']); if (($pos = array_search($needle,$tags)) !== false) {
$value['tags'] = implode(' ', array_unique($tags)); $tags[$pos] = trim($_POST['totag']);
$LINKSDB[$key] = $value; $value['tags'] = implode(' ', array_unique($tags));
$history->updateLink($LINKSDB[$key]); $LINKSDB[$key] = $value;
$history->updateLink($LINKSDB[$key]);
++$count;
}
} }
$LINKSDB->save($conf->get('resource.page_cache')); // Save to disk. $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; exit;
} }
} }