From c28a41a2394a8efa15061a46ade5bdcd9ef54b99 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Sat, 30 Jul 2016 01:46:30 +0200 Subject: [PATCH] Fix overwrite of bookmarks when importing links without URL --- controller/lib/bookmarks.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/controller/lib/bookmarks.php b/controller/lib/bookmarks.php index f328393997..2bb391b522 100644 --- a/controller/lib/bookmarks.php +++ b/controller/lib/bookmarks.php @@ -428,7 +428,10 @@ public static function editBookmark($userid, IDb $db, $id, $url, $title, $tags = */ public static function addBookmark($userid, IDb $db, $url, $title, $tags = array(), $description = '', $is_public = false) { $public = $is_public ? 1 : 0; - $url_without_prefix = substr($url, strpos($url, "://") + 3); // Removes everything from the url before the "://" pattern (included) + $url_without_prefix = trim(substr($url, strpos($url, "://") + 3)); // Removes everything from the url before the "://" pattern (included) + if($url_without_prefix === '') { + throw new \InvalidArgumentException('Bookmark URL is missing'); + } $enc_url_noprefix = htmlspecialchars_decode($url_without_prefix); $enc_url = htmlspecialchars_decode($url); // Change lastmodified date if the record if already exists @@ -522,8 +525,12 @@ public static function importFile($user, IDb $db, $file) { $dom->loadHTMLFile($file); $links = $dom->getElementsByTagName('a'); + $l = \OC::$server->getL10NFactory()->get('bookmarks'); + $errors = []; + // Reintroduce transaction here!? foreach ($links as $link) { + /* @var \DOMElement $link */ $title = $link->nodeValue; $ref = $link->getAttribute("href"); $tag_str = ''; @@ -534,11 +541,15 @@ public static function importFile($user, IDb $db, $file) { $desc_str = ''; if ($link->hasAttribute("description")) $desc_str = $link->getAttribute("description"); - - self::addBookmark($user, $db, $ref, $title, $tags, $desc_str); + try { + self::addBookmark($user, $db, $ref, $title, $tags, $desc_str); + } catch (\InvalidArgumentException $e) { + \OC::$server->getLogger()->logException($e, ['app' => 'bookmarks']); + $errors[] = $l->t('Failed to import one bookmark, because: ') . $e->getMessage(); + } } - return array(); + return $errors; } /**