+
+ // Sanitize data fields.
+ sanitizeLink($link);
+
+ // Remove private tags if the user is not logged in.
+ if (! $this->loggedIn) {
+ $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
+ }
+
+ // 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;
+ if ($this->redirectorEncode) {
+ $link['real_url'] .= urlencode(unescape($link['url']));
+ } else {
+ $link['real_url'] .= $link['url'];
+ }
+ }
+ else {
+ $link['real_url'] = $link['url'];
+ }
+ }
+ }
+
+ /**
+ * Saves the database from memory to disk
+ *
+ * @throws IOException the datastore is not writable
+ */
+ private function write()
+ {
+ 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));