// 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();
}
$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();
}
/**
// 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
+ */
+ private function writeDB()
+ {
+ 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
+ );
+
+ }
+
/**
* Saves the database from memory to disk
*
// TODO: raise an Exception instead
die('You are not authorized to change the database.');
}
- file_put_contents(
- $this->_datastore,
- self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix
- );
+
+ $this->writeDB();
+
invalidateCaches($pageCacheDir);
}