X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FLinkDB.php;h=f771ac8bf7b1677c99c67ac468ea33009c69a6cc;hb=9ecdeb54528eaebf12edc6af7a4082420a9899ee;hp=1e16fef179fefa843ef7f10eb62d3471e593928c;hpb=bba021defce15184c986d5e3db138bdbdd4df5d5;p=github%2Fshaarli%2FShaarli.git diff --git a/application/LinkDB.php b/application/LinkDB.php index 1e16fef1..f771ac8b 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -57,18 +57,25 @@ class LinkDB implements Iterator, Countable, ArrayAccess // Hide public links private $_hidePublicLinks; + // link redirector set in user settings. + private $_redirector; + /** * Creates a new LinkDB * * Checks if the datastore exists; else, attempts to create a dummy one. * - * @param $isLoggedIn is the user logged in? + * @param string $datastore datastore file path. + * @param boolean $isLoggedIn is the user logged in? + * @param boolean $hidePublicLinks if true all links are private. + * @param string $redirector link redirector set in user settings. */ - function __construct($datastore, $isLoggedIn, $hidePublicLinks) + function __construct($datastore, $isLoggedIn, $hidePublicLinks, $redirector = '') { $this->_datastore = $datastore; $this->_loggedIn = $isLoggedIn; $this->_hidePublicLinks = $hidePublicLinks; + $this->_redirector = $redirector; $this->_checkDB(); $this->_readDB(); } @@ -212,11 +219,7 @@ You use the community supported version of the original Shaarli project, by Seba $this->_links[$link['linkdate']] = $link; // Write database to disk - // TODO: raise an exception if the file is not write-able - file_put_contents( - $this->_datastore, - self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix - ); + $this->writeDB(); } /** @@ -263,28 +266,62 @@ You use the community supported version of the original Shaarli project, by Seba // Escape links data foreach($this->_links as &$link) { - sanitizeLink($link); + sanitizeLink($link); + // Do not use the redirector for internal links (Shaarli note URL starting with a '?'). + if (!empty($this->_redirector) && !startsWith($link['url'], '?')) { + $link['real_url'] = $this->_redirector . urlencode($link['url']); + } + else { + $link['real_url'] = $link['url']; + } } } /** * Saves the database from memory to disk + * + * @throws IOException the datastore is not writable */ - public function savedb() + private function writeDB() { - if (!$this->_loggedIn) { - // TODO: raise an Exception instead - die('You are not authorized to change the database.'); + if (is_file($this->_datastore) && !is_writeable($this->_datastore)) { + // The datastore exists but is not writeable + throw new IOException($this->_datastore); + } else if (!is_file($this->_datastore) && !is_writeable(dirname($this->_datastore))) { + // The datastore does not exist and its parent directory is not writeable + throw new IOException(dirname($this->_datastore)); } + file_put_contents( $this->_datastore, self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix ); - invalidateCaches(); + + } + + /** + * Saves the database from memory to disk + * + * @param string $pageCacheDir page cache directory + */ + public function savedb($pageCacheDir) + { + if (!$this->_loggedIn) { + // TODO: raise an Exception instead + die('You are not authorized to change the database.'); + } + + $this->writeDB(); + + invalidateCaches($pageCacheDir); } /** * Returns the link for a given URL, or False if it does not exist. + * + * @param string $url URL to search for + * + * @return mixed the existing link if it exists, else 'false' */ public function getLinkFromUrl($url) { @@ -439,4 +476,3 @@ You use the community supported version of the original Shaarli project, by Seba return $linkDays; } } -?>