]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/LinkDB.php
Add a persistent 'shorturl' key to all links
[github/shaarli/Shaarli.git] / application / LinkDB.php
CommitLineData
ca74886f
V
1<?php
2/**
3 * Data storage for links.
4 *
5 * This object behaves like an associative array.
6 *
7 * Example:
8 * $myLinks = new LinkDB();
29d10882 9 * echo $myLinks[350]['title'];
ca74886f
V
10 * foreach ($myLinks as $link)
11 * echo $link['title'].' at url '.$link['url'].'; description:'.$link['description'];
12 *
13 * Available keys:
29d10882 14 * - id: primary key, incremental integer identifier (persistent)
ca74886f 15 * - description: description of the entry
29d10882
A
16 * - created: creation date of this entry, DateTime object.
17 * - updated: last modification date of this entry, DateTime object.
ca74886f
V
18 * - private: Is this link private? 0=no, other value=yes
19 * - tags: tags attached to this entry (separated by spaces)
20 * - title Title of the link
49e62f22
A
21 * - url URL of the link. Used for displayable links (no redirector, relative, etc.).
22 * Can be absolute or relative.
ca74886f 23 * Relative URLs are permalinks (e.g.'?m-ukcw')
49e62f22 24 * - real_url Absolute processed URL.
d592daea 25 * - shorturl Permalink smallhash
ca74886f
V
26 *
27 * Implements 3 interfaces:
28 * - ArrayAccess: behaves like an associative array;
29 * - Countable: there is a count() method;
30 * - Iterator: usable in foreach () loops.
29d10882
A
31 *
32 * ID mechanism:
33 * ArrayAccess is implemented in a way that will allow to access a link
34 * with the unique identifier ID directly with $link[ID].
35 * Note that it's not the real key of the link array attribute.
36 * This mechanism is in place to have persistent link IDs,
37 * even though the internal array is reordered by date.
38 * Example:
39 * - DB: link #1 (2010-01-01) link #2 (2016-01-01)
40 * - Order: #2 #1
41 * - Import links containing: link #3 (2013-01-01)
42 * - New DB: link #1 (2010-01-01) link #2 (2016-01-01) link #3 (2013-01-01)
43 * - Real order: #2 #3 #1
ca74886f
V
44 */
45class LinkDB implements Iterator, Countable, ArrayAccess
46{
9c8752a2 47 // Links are stored as a PHP serialized string
628b97cb 48 private $datastore;
9c8752a2 49
205a4277
V
50 // Link date storage format
51 const LINK_DATE_FORMAT = 'Ymd_His';
52
9c8752a2
V
53 // Datastore PHP prefix
54 protected static $phpPrefix = '<?php /* ';
55
56 // Datastore PHP suffix
57 protected static $phpSuffix = ' */ ?>';
58
ca74886f
V
59 // List of links (associative array)
60 // - key: link date (e.g. "20110823_124546"),
61 // - value: associative array (keys: title, description...)
628b97cb 62 private $links;
ca74886f 63
29d10882
A
64 // List of all recorded URLs (key=url, value=link offset)
65 // for fast reserve search (url-->link offset)
628b97cb 66 private $urls;
ca74886f 67
29d10882
A
68 /**
69 * @var array List of all links IDS mapped with their array offset.
70 * Map: id->offset.
71 */
72 protected $ids;
73
74 // List of offset keys (for the Iterator interface implementation)
628b97cb 75 private $keys;
ca74886f 76
628b97cb
V
77 // Position in the $this->keys array (for the Iterator interface)
78 private $position;
ca74886f
V
79
80 // Is the user logged in? (used to filter private links)
628b97cb 81 private $loggedIn;
ca74886f 82
9f15ca9e 83 // Hide public links
628b97cb 84 private $hidePublicLinks;
9f15ca9e 85
90e5bd65 86 // link redirector set in user settings.
628b97cb 87 private $redirector;
90e5bd65 88
043eae70
A
89 /**
90 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
91 *
92 * Example:
93 * anonym.to needs clean URL while dereferer.org needs urlencoded URL.
94 *
95 * @var boolean $redirectorEncode parameter: true or false
96 */
97 private $redirectorEncode;
98
ca74886f
V
99 /**
100 * Creates a new LinkDB
101 *
102 * Checks if the datastore exists; else, attempts to create a dummy one.
103 *
043eae70
A
104 * @param string $datastore datastore file path.
105 * @param boolean $isLoggedIn is the user logged in?
106 * @param boolean $hidePublicLinks if true all links are private.
107 * @param string $redirector link redirector set in user settings.
108 * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
ca74886f 109 */
735ed4a9 110 public function __construct(
043eae70
A
111 $datastore,
112 $isLoggedIn,
113 $hidePublicLinks,
114 $redirector = '',
115 $redirectorEncode = true
116 )
ca74886f 117 {
628b97cb
V
118 $this->datastore = $datastore;
119 $this->loggedIn = $isLoggedIn;
120 $this->hidePublicLinks = $hidePublicLinks;
121 $this->redirector = $redirector;
043eae70 122 $this->redirectorEncode = $redirectorEncode === true;
f21abf32
V
123 $this->check();
124 $this->read();
ca74886f
V
125 }
126
127 /**
128 * Countable - Counts elements of an object
129 */
130 public function count()
131 {
628b97cb 132 return count($this->links);
ca74886f
V
133 }
134
135 /**
136 * ArrayAccess - Assigns a value to the specified offset
137 */
138 public function offsetSet($offset, $value)
139 {
140 // TODO: use exceptions instead of "die"
628b97cb 141 if (!$this->loggedIn) {
ca74886f
V
142 die('You are not authorized to add a link.');
143 }
29d10882
A
144 if (!isset($value['id']) || empty($value['url'])) {
145 die('Internal Error: A link should always have an id and URL.');
ca74886f 146 }
29d10882
A
147 if ((! empty($offset) && ! is_int($offset)) || ! is_int($value['id'])) {
148 die('You must specify an integer as a key.');
149 }
150 if (! empty($offset) && $offset !== $value['id']) {
151 die('Array offset and link ID must be equal.');
152 }
153
154 // If the link exists, we reuse the real offset, otherwise new entry
155 $existing = $this->getLinkOffset($offset);
156 if ($existing !== null) {
157 $offset = $existing;
158 } else {
159 $offset = count($this->links);
ca74886f 160 }
628b97cb 161 $this->links[$offset] = $value;
29d10882
A
162 $this->urls[$value['url']] = $offset;
163 $this->ids[$value['id']] = $offset;
ca74886f
V
164 }
165
166 /**
167 * ArrayAccess - Whether or not an offset exists
168 */
169 public function offsetExists($offset)
170 {
29d10882 171 return array_key_exists($this->getLinkOffset($offset), $this->links);
ca74886f
V
172 }
173
174 /**
175 * ArrayAccess - Unsets an offset
176 */
177 public function offsetUnset($offset)
178 {
628b97cb 179 if (!$this->loggedIn) {
ca74886f
V
180 // TODO: raise an exception
181 die('You are not authorized to delete a link.');
182 }
29d10882
A
183 $realOffset = $this->getLinkOffset($offset);
184 $url = $this->links[$realOffset]['url'];
628b97cb 185 unset($this->urls[$url]);
29d10882
A
186 unset($this->ids[$realOffset]);
187 unset($this->links[$realOffset]);
ca74886f
V
188 }
189
190 /**
191 * ArrayAccess - Returns the value at specified offset
192 */
193 public function offsetGet($offset)
194 {
29d10882
A
195 $realOffset = $this->getLinkOffset($offset);
196 return isset($this->links[$realOffset]) ? $this->links[$realOffset] : null;
ca74886f
V
197 }
198
199 /**
200 * Iterator - Returns the current element
201 */
735ed4a9 202 public function current()
ca74886f 203 {
29d10882 204 return $this[$this->keys[$this->position]];
ca74886f
V
205 }
206
207 /**
208 * Iterator - Returns the key of the current element
209 */
735ed4a9 210 public function key()
ca74886f 211 {
628b97cb 212 return $this->keys[$this->position];
ca74886f
V
213 }
214
215 /**
216 * Iterator - Moves forward to next element
217 */
735ed4a9 218 public function next()
ca74886f 219 {
628b97cb 220 ++$this->position;
ca74886f
V
221 }
222
223 /**
224 * Iterator - Rewinds the Iterator to the first element
225 *
226 * Entries are sorted by date (latest first)
227 */
735ed4a9 228 public function rewind()
ca74886f 229 {
29d10882 230 $this->keys = array_keys($this->ids);
628b97cb 231 $this->position = 0;
ca74886f
V
232 }
233
234 /**
235 * Iterator - Checks if current position is valid
236 */
735ed4a9 237 public function valid()
ca74886f 238 {
628b97cb 239 return isset($this->keys[$this->position]);
ca74886f
V
240 }
241
242 /**
243 * Checks if the DB directory and file exist
244 *
245 * If no DB file is found, creates a dummy DB.
246 */
f21abf32 247 private function check()
ca74886f 248 {
628b97cb 249 if (file_exists($this->datastore)) {
ca74886f
V
250 return;
251 }
252
253 // Create a dummy database for example
628b97cb 254 $this->links = array();
ca74886f 255 $link = array(
29d10882 256 'id' => 1,
598376d4
A
257 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone',
258 'url'=>'https://github.com/shaarli/Shaarli/wiki',
259 'description'=>'Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login.
260
261To learn how to use Shaarli, consult the link "Help/documentation" at the bottom of this page.
262
263You use the community supported version of the original Shaarli project, by Sebastien Sauvage.',
ca74886f 264 'private'=>0,
29d10882 265 'created'=> new DateTime(),
ca74886f
V
266 'tags'=>'opensource software'
267 );
d592daea 268 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
29d10882 269 $this->links[1] = $link;
ca74886f
V
270
271 $link = array(
29d10882 272 'id' => 0,
ca74886f
V
273 'title'=>'My secret stuff... - Pastebin.com',
274 'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
598376d4 275 'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.',
ca74886f 276 'private'=>1,
29d10882 277 'created'=> new DateTime('1 minute ago'),
d592daea 278 'tags'=>'secretstuff',
ca74886f 279 );
d592daea 280 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
29d10882 281 $this->links[0] = $link;
ca74886f
V
282
283 // Write database to disk
f21abf32 284 $this->write();
ca74886f
V
285 }
286
287 /**
288 * Reads database from disk to memory
289 */
f21abf32 290 private function read()
ca74886f 291 {
578a84bd 292 // Public links are hidden and user not logged in => nothing to show
628b97cb
V
293 if ($this->hidePublicLinks && !$this->loggedIn) {
294 $this->links = array();
578a84bd 295 return;
296 }
297
ca74886f
V
298 // Read data
299 // Note that gzinflate is faster than gzuncompress.
300 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
628b97cb 301 $this->links = array();
ca74886f 302
628b97cb
V
303 if (file_exists($this->datastore)) {
304 $this->links = unserialize(gzinflate(base64_decode(
305 substr(file_get_contents($this->datastore),
9c8752a2 306 strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
ca74886f
V
307 }
308
29d10882
A
309 $toremove = array();
310 foreach ($this->links as $key => &$link) {
311 if (! $this->loggedIn && $link['private'] != 0) {
312 // Transition for not upgraded databases.
313 $toremove[] = $key;
314 continue;
ca74886f 315 }
195acf9f 316
510377d2 317 // Sanitize data fields.
90e5bd65 318 sanitizeLink($link);
195acf9f
A
319
320 // Remove private tags if the user is not logged in.
628b97cb 321 if (! $this->loggedIn) {
9866b408 322 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
195acf9f
A
323 }
324
90e5bd65 325 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
628b97cb
V
326 if (!empty($this->redirector) && !startsWith($link['url'], '?')) {
327 $link['real_url'] = $this->redirector;
043eae70
A
328 if ($this->redirectorEncode) {
329 $link['real_url'] .= urlencode(unescape($link['url']));
330 } else {
331 $link['real_url'] .= $link['url'];
332 }
90e5bd65
A
333 }
334 else {
335 $link['real_url'] = $link['url'];
336 }
29d10882
A
337
338 // To be able to load links before running the update, and prepare the update
339 if (! isset($link['created'])) {
340 $link['id'] = $link['linkdate'];
d592daea 341 $link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']);
29d10882 342 if (! empty($link['updated'])) {
d592daea 343 $link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']);
29d10882 344 }
d592daea 345 $link['shorturl'] = smallHash($link['linkdate']);
29d10882
A
346 }
347 }
348
349 // If user is not logged in, filter private links.
350 foreach ($toremove as $offset) {
351 unset($this->links[$offset]);
5f85fcd8 352 }
29d10882
A
353
354 $this->reorder();
ca74886f
V
355 }
356
2e28269b
V
357 /**
358 * Saves the database from memory to disk
359 *
360 * @throws IOException the datastore is not writable
361 */
f21abf32 362 private function write()
2e28269b 363 {
628b97cb 364 if (is_file($this->datastore) && !is_writeable($this->datastore)) {
2e28269b 365 // The datastore exists but is not writeable
628b97cb
V
366 throw new IOException($this->datastore);
367 } else if (!is_file($this->datastore) && !is_writeable(dirname($this->datastore))) {
2e28269b 368 // The datastore does not exist and its parent directory is not writeable
628b97cb 369 throw new IOException(dirname($this->datastore));
2e28269b
V
370 }
371
372 file_put_contents(
628b97cb
V
373 $this->datastore,
374 self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
2e28269b
V
375 );
376
377 }
378
ca74886f
V
379 /**
380 * Saves the database from memory to disk
01e48f26
V
381 *
382 * @param string $pageCacheDir page cache directory
ca74886f 383 */
f21abf32 384 public function save($pageCacheDir)
ca74886f 385 {
628b97cb 386 if (!$this->loggedIn) {
ca74886f
V
387 // TODO: raise an Exception instead
388 die('You are not authorized to change the database.');
389 }
2e28269b 390
f21abf32 391 $this->write();
2e28269b 392
01e48f26 393 invalidateCaches($pageCacheDir);
ca74886f
V
394 }
395
396 /**
397 * Returns the link for a given URL, or False if it does not exist.
ef591e7e
GV
398 *
399 * @param string $url URL to search for
400 *
401 * @return mixed the existing link if it exists, else 'false'
ca74886f
V
402 */
403 public function getLinkFromUrl($url)
404 {
628b97cb
V
405 if (isset($this->urls[$url])) {
406 return $this->links[$this->urls[$url]];
ca74886f
V
407 }
408 return false;
409 }
410
411 /**
528a6f8a 412 * Returns the shaare corresponding to a smallHash.
ca74886f 413 *
528a6f8a
A
414 * @param string $request QUERY_STRING server parameter.
415 *
416 * @return array $filtered array containing permalink data.
417 *
418 * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
419 */
420 public function filterHash($request)
421 {
422 $request = substr($request, 0, 6);
628b97cb 423 $linkFilter = new LinkFilter($this->links);
528a6f8a
A
424 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
425 }
426
427 /**
428 * Returns the list of articles for a given day.
429 *
430 * @param string $request day to filter. Format: YYYYMMDD.
431 *
432 * @return array list of shaare found.
433 */
434 public function filterDay($request) {
628b97cb 435 $linkFilter = new LinkFilter($this->links);
528a6f8a
A
436 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
437 }
438
439 /**
440 * Filter links according to search parameters.
441 *
442 * @param array $filterRequest Search request content. Supported keys:
443 * - searchtags: list of tags
444 * - searchterm: term search
822bffce
A
445 * @param bool $casesensitive Optional: Perform case sensitive filter
446 * @param bool $privateonly Optional: Returns private links only if true.
ca74886f 447 *
528a6f8a 448 * @return array filtered links, all links if no suitable filter was provided.
ca74886f 449 */
528a6f8a 450 public function filterSearch($filterRequest = array(), $casesensitive = false, $privateonly = false)
55d0a5c4 451 {
528a6f8a
A
452 // Filter link database according to parameters.
453 $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
454 $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
455
456 // Search tags + fullsearch.
9ccca401 457 if (! empty($searchtags) && ! empty($searchterm)) {
528a6f8a
A
458 $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT;
459 $request = array($searchtags, $searchterm);
460 }
461 // Search by tags.
462 elseif (! empty($searchtags)) {
463 $type = LinkFilter::$FILTER_TAG;
464 $request = $searchtags;
465 }
466 // Fulltext search.
467 elseif (! empty($searchterm)) {
468 $type = LinkFilter::$FILTER_TEXT;
469 $request = $searchterm;
470 }
471 // Otherwise, display without filtering.
472 else {
473 $type = '';
474 $request = '';
475 }
476
29d10882 477 $linkFilter = new LinkFilter($this);
c51fae92 478 return $linkFilter->filter($type, $request, $casesensitive, $privateonly);
ca74886f
V
479 }
480
481 /**
482 * Returns the list of all tags
483 * Output: associative array key=tags, value=0
484 */
485 public function allTags()
486 {
487 $tags = array();
b1eb5d1d 488 $caseMapping = array();
628b97cb 489 foreach ($this->links as $link) {
4b35853d 490 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) {
b1eb5d1d
A
491 if (empty($tag)) {
492 continue;
ca74886f 493 }
b1eb5d1d
A
494 // The first case found will be displayed.
495 if (!isset($caseMapping[strtolower($tag)])) {
496 $caseMapping[strtolower($tag)] = $tag;
497 $tags[$caseMapping[strtolower($tag)]] = 0;
498 }
499 $tags[$caseMapping[strtolower($tag)]]++;
ca74886f
V
500 }
501 }
502 // Sort tags by usage (most used tag first)
503 arsort($tags);
504 return $tags;
505 }
506
507 /**
508 * Returns the list of days containing articles (oldest first)
509 * Output: An array containing days (in format YYYYMMDD).
510 */
511 public function days()
512 {
513 $linkDays = array();
29d10882
A
514 foreach ($this->links as $link) {
515 $linkDays[$link['created']->format('Ymd')] = 0;
ca74886f
V
516 }
517 $linkDays = array_keys($linkDays);
518 sort($linkDays);
510377d2 519
ca74886f
V
520 return $linkDays;
521 }
29d10882
A
522
523 /**
524 * Reorder links by creation date (newest first).
525 *
526 * Also update the urls and ids mapping arrays.
527 *
528 * @param string $order ASC|DESC
529 */
530 public function reorder($order = 'DESC')
531 {
532 $order = $order === 'ASC' ? -1 : 1;
533 // Reorder array by dates.
534 usort($this->links, function($a, $b) use ($order) {
535 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
536 });
537
538 $this->urls = array();
539 $this->ids = array();
540 foreach ($this->links as $key => $link) {
541 $this->urls[$link['url']] = $key;
542 $this->ids[$link['id']] = $key;
543 }
544 }
545
546 /**
547 * Return the next key for link creation.
548 * E.g. If the last ID is 597, the next will be 598.
549 *
550 * @return int next ID.
551 */
552 public function getNextId()
553 {
554 if (!empty($this->ids)) {
555 return max(array_keys($this->ids)) + 1;
556 }
557 return 0;
558 }
559
560 /**
561 * Returns a link offset in links array from its unique ID.
562 *
563 * @param int $id Persistent ID of a link.
564 *
d592daea 565 * @return int Real offset in local array, or null if doesn't exist.
29d10882
A
566 */
567 protected function getLinkOffset($id)
568 {
569 if (isset($this->ids[$id])) {
570 return $this->ids[$id];
571 }
572 return null;
573 }
ca74886f 574}