From d592daea8343bb4dfecff5d97e93699581ccc58c Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 28 Nov 2016 18:24:15 +0100 Subject: Add a persistent 'shorturl' key to all links All existing link will keep their permalinks. New links will have smallhash generated with date+id. The purpose of this is to avoid collision between links due to their creation date. --- application/FeedBuilder.php | 2 +- application/LinkDB.php | 12 ++++++++---- application/LinkFilter.php | 2 +- application/LinkUtils.php | 13 +++++++++++++ application/NetscapeBookmarkUtils.php | 1 + application/Updater.php | 3 +++ application/Utils.php | 6 +++++- 7 files changed, 32 insertions(+), 7 deletions(-) (limited to 'application') diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php index bfdf2fd3..fedd90e6 100644 --- a/application/FeedBuilder.php +++ b/application/FeedBuilder.php @@ -143,7 +143,7 @@ class FeedBuilder */ protected function buildItem($link, $pageaddr) { - $link['guid'] = $pageaddr .'?'. smallHash($link['created']->format('Ymd_His')); + $link['guid'] = $pageaddr .'?'. $link['shorturl']; // Check for both signs of a note: starting with ? and 7 chars long. if ($link['url'][0] === '?' && strlen($link['url']) === 7) { $link['url'] = $pageaddr . $link['url']; diff --git a/application/LinkDB.php b/application/LinkDB.php index e429ab4f..1e13286a 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -22,6 +22,7 @@ * Can be absolute or relative. * Relative URLs are permalinks (e.g.'?m-ukcw') * - real_url Absolute processed URL. + * - shorturl Permalink smallhash * * Implements 3 interfaces: * - ArrayAccess: behaves like an associative array; @@ -264,6 +265,7 @@ You use the community supported version of the original Shaarli project, by Seba 'created'=> new DateTime(), 'tags'=>'opensource software' ); + $link['shorturl'] = link_small_hash($link['created'], $link['id']); $this->links[1] = $link; $link = array( @@ -273,8 +275,9 @@ You use the community supported version of the original Shaarli project, by Seba 'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.', 'private'=>1, 'created'=> new DateTime('1 minute ago'), - 'tags'=>'secretstuff' + 'tags'=>'secretstuff', ); + $link['shorturl'] = link_small_hash($link['created'], $link['id']); $this->links[0] = $link; // Write database to disk @@ -335,10 +338,11 @@ You use the community supported version of the original Shaarli project, by Seba // To be able to load links before running the update, and prepare the update if (! isset($link['created'])) { $link['id'] = $link['linkdate']; - $link['created'] = DateTime::createFromFormat('Ymd_His', $link['linkdate']); + $link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']); if (! empty($link['updated'])) { - $link['updated'] = DateTime::createFromFormat('Ymd_His', $link['updated']); + $link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']); } + $link['shorturl'] = smallHash($link['linkdate']); } } @@ -558,7 +562,7 @@ You use the community supported version of the original Shaarli project, by Seba * * @param int $id Persistent ID of a link. * - * @return int Real offset in local array, or null if doesn't exists. + * @return int Real offset in local array, or null if doesn't exist. */ protected function getLinkOffset($id) { diff --git a/application/LinkFilter.php b/application/LinkFilter.php index 7bab46ba..daa6d9cc 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php @@ -120,7 +120,7 @@ class LinkFilter { $filtered = array(); foreach ($this->links as $key => $l) { - if ($smallHash == smallHash($l['created']->format('Ymd_His'))) { + if ($smallHash == $l['shorturl']) { // Yes, this is ugly and slow $filtered[$key] = $l; return $filtered; diff --git a/application/LinkUtils.php b/application/LinkUtils.php index 9d9ae3cb..cf58f808 100644 --- a/application/LinkUtils.php +++ b/application/LinkUtils.php @@ -169,3 +169,16 @@ function space2nbsp($text) function format_description($description, $redirector = '', $indexUrl = '') { return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl))); } + +/** + * Generate a small hash for a link. + * + * @param DateTime $date Link creation date. + * @param int $id Link ID. + * + * @return string the small hash generated from link data. + */ +function link_small_hash($date, $id) +{ + return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id); +} diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php index 8a939adb..e7148d00 100644 --- a/application/NetscapeBookmarkUtils.php +++ b/application/NetscapeBookmarkUtils.php @@ -174,6 +174,7 @@ class NetscapeBookmarkUtils $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get())); $newLink['created'] = $newLinkDate; $newLink['id'] = $linkDb->getNextId(); + $newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']); $linkDb[$newLink['id']] = $newLink; $importCount++; } diff --git a/application/Updater.php b/application/Updater.php index 16c8c376..f0d02814 100644 --- a/application/Updater.php +++ b/application/Updater.php @@ -223,6 +223,9 @@ class Updater * Since this update is very sensitve (changing the whole database), the datastore will be * automatically backed up into the file datastore..php. * + * LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash), + * which will be saved by this method. + * * @return bool true if the update is successful, false otherwise. */ public function updateMethodDatastoreIds() diff --git a/application/Utils.php b/application/Utils.php index 0166ee2a..0a5b476e 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -31,7 +31,11 @@ function logm($logFile, $clientIp, $message) * - are NOT cryptographically secure (they CAN be forged) * * In Shaarli, they are used as a tinyurl-like link to individual entries, - * e.g. smallHash('20111006_131924') --> yZH23w + * built once with the combination of the date and item ID. + * e.g. smallHash('20111006_131924' . 142) --> eaWxtQ + * + * @warning before v0.8.1, smallhashes were built only with the date, + * and their value has been preserved. * * @param string $text Create a hash from this text. * -- cgit v1.2.3