Skip to content

Commit

Permalink
Merge pull request nextcloud#264 from owncloud/fix-overwrite-on-import
Browse files Browse the repository at this point in the history
Fix overwrite of bookmarks when importing links without URL
  • Loading branch information
blizzz authored Jul 31, 2016
2 parents c419da8 + c28a41a commit 9b214e5
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions controller/lib/bookmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = '';
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 9b214e5

Please sign in to comment.