aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/legacy
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-18 10:01:06 +0100
committerGitHub <noreply@github.com>2020-01-18 10:01:06 +0100
commit3fb29fdda04ca86e04422d49b86cf646d53c4f9d (patch)
treeadf8512f93f5559ba87d0c9931969ae4ebea7133 /application/legacy
parent796c4c57d085ae4589b53dfe8369ae9ba30ffdaf (diff)
parente26e2060f5470ce8bf4c5973284bae07b8af170a (diff)
downloadShaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.gz
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.zst
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.zip
Store bookmarks as PHP objects and add a service layer to retriā€¦ (#1307)
Store bookmarks as PHP objects and add a service layer to retrieve them
Diffstat (limited to 'application/legacy')
-rw-r--r--application/legacy/LegacyLinkDB.php580
-rw-r--r--application/legacy/LegacyLinkFilter.php451
-rw-r--r--application/legacy/LegacyUpdater.php617
3 files changed, 1648 insertions, 0 deletions
diff --git a/application/legacy/LegacyLinkDB.php b/application/legacy/LegacyLinkDB.php
new file mode 100644
index 00000000..7ccf5e54
--- /dev/null
+++ b/application/legacy/LegacyLinkDB.php
@@ -0,0 +1,580 @@
1<?php
2
3namespace Shaarli\Legacy;
4
5use ArrayAccess;
6use Countable;
7use DateTime;
8use Iterator;
9use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
10use Shaarli\Exceptions\IOException;
11use Shaarli\FileUtils;
12
13/**
14 * Data storage for bookmarks.
15 *
16 * This object behaves like an associative array.
17 *
18 * Example:
19 * $myLinks = new LinkDB();
20 * echo $myLinks[350]['title'];
21 * foreach ($myLinks as $link)
22 * echo $link['title'].' at url '.$link['url'].'; description:'.$link['description'];
23 *
24 * Available keys:
25 * - id: primary key, incremental integer identifier (persistent)
26 * - description: description of the entry
27 * - created: creation date of this entry, DateTime object.
28 * - updated: last modification date of this entry, DateTime object.
29 * - private: Is this link private? 0=no, other value=yes
30 * - tags: tags attached to this entry (separated by spaces)
31 * - title Title of the link
32 * - url URL of the link. Used for displayable bookmarks.
33 * Can be absolute or relative in the database but the relative bookmarks
34 * will be converted to absolute ones in templates.
35 * - real_url Raw URL in stored in the DB (absolute or relative).
36 * - shorturl Permalink smallhash
37 *
38 * Implements 3 interfaces:
39 * - ArrayAccess: behaves like an associative array;
40 * - Countable: there is a count() method;
41 * - Iterator: usable in foreach () loops.
42 *
43 * ID mechanism:
44 * ArrayAccess is implemented in a way that will allow to access a link
45 * with the unique identifier ID directly with $link[ID].
46 * Note that it's not the real key of the link array attribute.
47 * This mechanism is in place to have persistent link IDs,
48 * even though the internal array is reordered by date.
49 * Example:
50 * - DB: link #1 (2010-01-01) link #2 (2016-01-01)
51 * - Order: #2 #1
52 * - Import bookmarks containing: link #3 (2013-01-01)
53 * - New DB: link #1 (2010-01-01) link #2 (2016-01-01) link #3 (2013-01-01)
54 * - Real order: #2 #3 #1
55 *
56 * @deprecated
57 */
58class LegacyLinkDB implements Iterator, Countable, ArrayAccess
59{
60 // Links are stored as a PHP serialized string
61 private $datastore;
62
63 // Link date storage format
64 const LINK_DATE_FORMAT = 'Ymd_His';
65
66 // List of bookmarks (associative array)
67 // - key: link date (e.g. "20110823_124546"),
68 // - value: associative array (keys: title, description...)
69 private $links;
70
71 // List of all recorded URLs (key=url, value=link offset)
72 // for fast reserve search (url-->link offset)
73 private $urls;
74
75 /**
76 * @var array List of all bookmarks IDS mapped with their array offset.
77 * Map: id->offset.
78 */
79 protected $ids;
80
81 // List of offset keys (for the Iterator interface implementation)
82 private $keys;
83
84 // Position in the $this->keys array (for the Iterator interface)
85 private $position;
86
87 // Is the user logged in? (used to filter private bookmarks)
88 private $loggedIn;
89
90 // Hide public bookmarks
91 private $hidePublicLinks;
92
93 /**
94 * Creates a new LinkDB
95 *
96 * Checks if the datastore exists; else, attempts to create a dummy one.
97 *
98 * @param string $datastore datastore file path.
99 * @param boolean $isLoggedIn is the user logged in?
100 * @param boolean $hidePublicLinks if true all bookmarks are private.
101 */
102 public function __construct(
103 $datastore,
104 $isLoggedIn,
105 $hidePublicLinks
106 ) {
107
108 $this->datastore = $datastore;
109 $this->loggedIn = $isLoggedIn;
110 $this->hidePublicLinks = $hidePublicLinks;
111 $this->check();
112 $this->read();
113 }
114
115 /**
116 * Countable - Counts elements of an object
117 */
118 public function count()
119 {
120 return count($this->links);
121 }
122
123 /**
124 * ArrayAccess - Assigns a value to the specified offset
125 */
126 public function offsetSet($offset, $value)
127 {
128 // TODO: use exceptions instead of "die"
129 if (!$this->loggedIn) {
130 die(t('You are not authorized to add a link.'));
131 }
132 if (!isset($value['id']) || empty($value['url'])) {
133 die(t('Internal Error: A link should always have an id and URL.'));
134 }
135 if (($offset !== null && !is_int($offset)) || !is_int($value['id'])) {
136 die(t('You must specify an integer as a key.'));
137 }
138 if ($offset !== null && $offset !== $value['id']) {
139 die(t('Array offset and link ID must be equal.'));
140 }
141
142 // If the link exists, we reuse the real offset, otherwise new entry
143 $existing = $this->getLinkOffset($offset);
144 if ($existing !== null) {
145 $offset = $existing;
146 } else {
147 $offset = count($this->links);
148 }
149 $this->links[$offset] = $value;
150 $this->urls[$value['url']] = $offset;
151 $this->ids[$value['id']] = $offset;
152 }
153
154 /**
155 * ArrayAccess - Whether or not an offset exists
156 */
157 public function offsetExists($offset)
158 {
159 return array_key_exists($this->getLinkOffset($offset), $this->links);
160 }
161
162 /**
163 * ArrayAccess - Unsets an offset
164 */
165 public function offsetUnset($offset)
166 {
167 if (!$this->loggedIn) {
168 // TODO: raise an exception
169 die('You are not authorized to delete a link.');
170 }
171 $realOffset = $this->getLinkOffset($offset);
172 $url = $this->links[$realOffset]['url'];
173 unset($this->urls[$url]);
174 unset($this->ids[$realOffset]);
175 unset($this->links[$realOffset]);
176 }
177
178 /**
179 * ArrayAccess - Returns the value at specified offset
180 */
181 public function offsetGet($offset)
182 {
183 $realOffset = $this->getLinkOffset($offset);
184 return isset($this->links[$realOffset]) ? $this->links[$realOffset] : null;
185 }
186
187 /**
188 * Iterator - Returns the current element
189 */
190 public function current()
191 {
192 return $this[$this->keys[$this->position]];
193 }
194
195 /**
196 * Iterator - Returns the key of the current element
197 */
198 public function key()
199 {
200 return $this->keys[$this->position];
201 }
202
203 /**
204 * Iterator - Moves forward to next element
205 */
206 public function next()
207 {
208 ++$this->position;
209 }
210
211 /**
212 * Iterator - Rewinds the Iterator to the first element
213 *
214 * Entries are sorted by date (latest first)
215 */
216 public function rewind()
217 {
218 $this->keys = array_keys($this->ids);
219 $this->position = 0;
220 }
221
222 /**
223 * Iterator - Checks if current position is valid
224 */
225 public function valid()
226 {
227 return isset($this->keys[$this->position]);
228 }
229
230 /**
231 * Checks if the DB directory and file exist
232 *
233 * If no DB file is found, creates a dummy DB.
234 */
235 private function check()
236 {
237 if (file_exists($this->datastore)) {
238 return;
239 }
240
241 // Create a dummy database for example
242 $this->links = array();
243 $link = array(
244 'id' => 1,
245 'title' => t('The personal, minimalist, super-fast, database free, bookmarking service'),
246 'url' => 'https://shaarli.readthedocs.io',
247 'description' => t(
248 'Welcome to Shaarli! This is your first public bookmark. '
249 . 'To edit or delete me, you must first login.
250
251To learn how to use Shaarli, consult the link "Documentation" at the bottom of this page.
252
253You use the community supported version of the original Shaarli project, by Sebastien Sauvage.'
254 ),
255 'private' => 0,
256 'created' => new DateTime(),
257 'tags' => 'opensource software',
258 'sticky' => false,
259 );
260 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
261 $this->links[1] = $link;
262
263 $link = array(
264 'id' => 0,
265 'title' => t('My secret stuff... - Pastebin.com'),
266 'url' => 'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
267 'description' => t('Shhhh! I\'m a private link only YOU can see. You can delete me too.'),
268 'private' => 1,
269 'created' => new DateTime('1 minute ago'),
270 'tags' => 'secretstuff',
271 'sticky' => false,
272 );
273 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
274 $this->links[0] = $link;
275
276 // Write database to disk
277 $this->write();
278 }
279
280 /**
281 * Reads database from disk to memory
282 */
283 private function read()
284 {
285 // Public bookmarks are hidden and user not logged in => nothing to show
286 if ($this->hidePublicLinks && !$this->loggedIn) {
287 $this->links = array();
288 return;
289 }
290
291 $this->urls = [];
292 $this->ids = [];
293 $this->links = FileUtils::readFlatDB($this->datastore, []);
294
295 $toremove = array();
296 foreach ($this->links as $key => &$link) {
297 if (!$this->loggedIn && $link['private'] != 0) {
298 // Transition for not upgraded databases.
299 unset($this->links[$key]);
300 continue;
301 }
302
303 // Sanitize data fields.
304 sanitizeLink($link);
305
306 // Remove private tags if the user is not logged in.
307 if (!$this->loggedIn) {
308 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
309 }
310
311 $link['real_url'] = $link['url'];
312
313 $link['sticky'] = isset($link['sticky']) ? $link['sticky'] : false;
314
315 // To be able to load bookmarks before running the update, and prepare the update
316 if (!isset($link['created'])) {
317 $link['id'] = $link['linkdate'];
318 $link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']);
319 if (!empty($link['updated'])) {
320 $link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']);
321 }
322 $link['shorturl'] = smallHash($link['linkdate']);
323 }
324
325 $this->urls[$link['url']] = $key;
326 $this->ids[$link['id']] = $key;
327 }
328 }
329
330 /**
331 * Saves the database from memory to disk
332 *
333 * @throws IOException the datastore is not writable
334 */
335 private function write()
336 {
337 $this->reorder();
338 FileUtils::writeFlatDB($this->datastore, $this->links);
339 }
340
341 /**
342 * Saves the database from memory to disk
343 *
344 * @param string $pageCacheDir page cache directory
345 */
346 public function save($pageCacheDir)
347 {
348 if (!$this->loggedIn) {
349 // TODO: raise an Exception instead
350 die('You are not authorized to change the database.');
351 }
352
353 $this->write();
354
355 invalidateCaches($pageCacheDir);
356 }
357
358 /**
359 * Returns the link for a given URL, or False if it does not exist.
360 *
361 * @param string $url URL to search for
362 *
363 * @return mixed the existing link if it exists, else 'false'
364 */
365 public function getLinkFromUrl($url)
366 {
367 if (isset($this->urls[$url])) {
368 return $this->links[$this->urls[$url]];
369 }
370 return false;
371 }
372
373 /**
374 * Returns the shaare corresponding to a smallHash.
375 *
376 * @param string $request QUERY_STRING server parameter.
377 *
378 * @return array $filtered array containing permalink data.
379 *
380 * @throws BookmarkNotFoundException if the smallhash is malformed or doesn't match any link.
381 */
382 public function filterHash($request)
383 {
384 $request = substr($request, 0, 6);
385 $linkFilter = new LegacyLinkFilter($this->links);
386 return $linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, $request);
387 }
388
389 /**
390 * Returns the list of articles for a given day.
391 *
392 * @param string $request day to filter. Format: YYYYMMDD.
393 *
394 * @return array list of shaare found.
395 */
396 public function filterDay($request)
397 {
398 $linkFilter = new LegacyLinkFilter($this->links);
399 return $linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, $request);
400 }
401
402 /**
403 * Filter bookmarks according to search parameters.
404 *
405 * @param array $filterRequest Search request content. Supported keys:
406 * - searchtags: list of tags
407 * - searchterm: term search
408 * @param bool $casesensitive Optional: Perform case sensitive filter
409 * @param string $visibility return only all/private/public bookmarks
410 * @param bool $untaggedonly return only untagged bookmarks
411 *
412 * @return array filtered bookmarks, all bookmarks if no suitable filter was provided.
413 */
414 public function filterSearch(
415 $filterRequest = array(),
416 $casesensitive = false,
417 $visibility = 'all',
418 $untaggedonly = false
419 ) {
420
421 // Filter link database according to parameters.
422 $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
423 $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
424
425 // Search tags + fullsearch - blank string parameter will return all bookmarks.
426 $type = LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT; // == "vuotext"
427 $request = [$searchtags, $searchterm];
428
429 $linkFilter = new LegacyLinkFilter($this);
430 return $linkFilter->filter($type, $request, $casesensitive, $visibility, $untaggedonly);
431 }
432
433 /**
434 * Returns the list tags appearing in the bookmarks with the given tags
435 *
436 * @param array $filteringTags tags selecting the bookmarks to consider
437 * @param string $visibility process only all/private/public bookmarks
438 *
439 * @return array tag => linksCount
440 */
441 public function linksCountPerTag($filteringTags = [], $visibility = 'all')
442 {
443 $links = $this->filterSearch(['searchtags' => $filteringTags], false, $visibility);
444 $tags = [];
445 $caseMapping = [];
446 foreach ($links as $link) {
447 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) {
448 if (empty($tag)) {
449 continue;
450 }
451 // The first case found will be displayed.
452 if (!isset($caseMapping[strtolower($tag)])) {
453 $caseMapping[strtolower($tag)] = $tag;
454 $tags[$caseMapping[strtolower($tag)]] = 0;
455 }
456 $tags[$caseMapping[strtolower($tag)]]++;
457 }
458 }
459
460 /*
461 * Formerly used arsort(), which doesn't define the sort behaviour for equal values.
462 * Also, this function doesn't produce the same result between PHP 5.6 and 7.
463 *
464 * So we now use array_multisort() to sort tags by DESC occurrences,
465 * then ASC alphabetically for equal values.
466 *
467 * @see https://github.com/shaarli/Shaarli/issues/1142
468 */
469 $keys = array_keys($tags);
470 $tmpTags = array_combine($keys, $keys);
471 array_multisort($tags, SORT_DESC, $tmpTags, SORT_ASC, $tags);
472 return $tags;
473 }
474
475 /**
476 * Rename or delete a tag across all bookmarks.
477 *
478 * @param string $from Tag to rename
479 * @param string $to New tag. If none is provided, the from tag will be deleted
480 *
481 * @return array|bool List of altered bookmarks or false on error
482 */
483 public function renameTag($from, $to)
484 {
485 if (empty($from)) {
486 return false;
487 }
488 $delete = empty($to);
489 // True for case-sensitive tag search.
490 $linksToAlter = $this->filterSearch(['searchtags' => $from], true);
491 foreach ($linksToAlter as $key => &$value) {
492 $tags = preg_split('/\s+/', trim($value['tags']));
493 if (($pos = array_search($from, $tags)) !== false) {
494 if ($delete) {
495 unset($tags[$pos]); // Remove tag.
496 } else {
497 $tags[$pos] = trim($to);
498 }
499 $value['tags'] = trim(implode(' ', array_unique($tags)));
500 $this[$value['id']] = $value;
501 }
502 }
503
504 return $linksToAlter;
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();
514 foreach ($this->links as $link) {
515 $linkDays[$link['created']->format('Ymd')] = 0;
516 }
517 $linkDays = array_keys($linkDays);
518 sort($linkDays);
519
520 return $linkDays;
521 }
522
523 /**
524 * Reorder bookmarks 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 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
536 return $a['sticky'] ? -1 : 1;
537 }
538 if ($a['created'] == $b['created']) {
539 return $a['id'] < $b['id'] ? 1 * $order : -1 * $order;
540 }
541 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
542 });
543
544 $this->urls = [];
545 $this->ids = [];
546 foreach ($this->links as $key => $link) {
547 $this->urls[$link['url']] = $key;
548 $this->ids[$link['id']] = $key;
549 }
550 }
551
552 /**
553 * Return the next key for link creation.
554 * E.g. If the last ID is 597, the next will be 598.
555 *
556 * @return int next ID.
557 */
558 public function getNextId()
559 {
560 if (!empty($this->ids)) {
561 return max(array_keys($this->ids)) + 1;
562 }
563 return 0;
564 }
565
566 /**
567 * Returns a link offset in bookmarks array from its unique ID.
568 *
569 * @param int $id Persistent ID of a link.
570 *
571 * @return int Real offset in local array, or null if doesn't exist.
572 */
573 protected function getLinkOffset($id)
574 {
575 if (isset($this->ids[$id])) {
576 return $this->ids[$id];
577 }
578 return null;
579 }
580}
diff --git a/application/legacy/LegacyLinkFilter.php b/application/legacy/LegacyLinkFilter.php
new file mode 100644
index 00000000..7cf93d60
--- /dev/null
+++ b/application/legacy/LegacyLinkFilter.php
@@ -0,0 +1,451 @@
1<?php
2
3namespace Shaarli\Legacy;
4
5use Exception;
6use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
7
8/**
9 * Class LinkFilter.
10 *
11 * Perform search and filter operation on link data list.
12 *
13 * @deprecated
14 */
15class LegacyLinkFilter
16{
17 /**
18 * @var string permalinks.
19 */
20 public static $FILTER_HASH = 'permalink';
21
22 /**
23 * @var string text search.
24 */
25 public static $FILTER_TEXT = 'fulltext';
26
27 /**
28 * @var string tag filter.
29 */
30 public static $FILTER_TAG = 'tags';
31
32 /**
33 * @var string filter by day.
34 */
35 public static $FILTER_DAY = 'FILTER_DAY';
36
37 /**
38 * @var string Allowed characters for hashtags (regex syntax).
39 */
40 public static $HASHTAG_CHARS = '\p{Pc}\p{N}\p{L}\p{Mn}';
41
42 /**
43 * @var LegacyLinkDB all available links.
44 */
45 private $links;
46
47 /**
48 * @param LegacyLinkDB $links initialization.
49 */
50 public function __construct($links)
51 {
52 $this->links = $links;
53 }
54
55 /**
56 * Filter links according to parameters.
57 *
58 * @param string $type Type of filter (eg. tags, permalink, etc.).
59 * @param mixed $request Filter content.
60 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
61 * @param string $visibility Optional: return only all/private/public links
62 * @param string $untaggedonly Optional: return only untagged links. Applies only if $type includes FILTER_TAG
63 *
64 * @return array filtered link list.
65 */
66 public function filter($type, $request, $casesensitive = false, $visibility = 'all', $untaggedonly = false)
67 {
68 if (!in_array($visibility, ['all', 'public', 'private'])) {
69 $visibility = 'all';
70 }
71
72 switch ($type) {
73 case self::$FILTER_HASH:
74 return $this->filterSmallHash($request);
75 case self::$FILTER_TAG | self::$FILTER_TEXT: // == "vuotext"
76 $noRequest = empty($request) || (empty($request[0]) && empty($request[1]));
77 if ($noRequest) {
78 if ($untaggedonly) {
79 return $this->filterUntagged($visibility);
80 }
81 return $this->noFilter($visibility);
82 }
83 if ($untaggedonly) {
84 $filtered = $this->filterUntagged($visibility);
85 } else {
86 $filtered = $this->links;
87 }
88 if (!empty($request[0])) {
89 $filtered = (new LegacyLinkFilter($filtered))->filterTags($request[0], $casesensitive, $visibility);
90 }
91 if (!empty($request[1])) {
92 $filtered = (new LegacyLinkFilter($filtered))->filterFulltext($request[1], $visibility);
93 }
94 return $filtered;
95 case self::$FILTER_TEXT:
96 return $this->filterFulltext($request, $visibility);
97 case self::$FILTER_TAG:
98 if ($untaggedonly) {
99 return $this->filterUntagged($visibility);
100 } else {
101 return $this->filterTags($request, $casesensitive, $visibility);
102 }
103 case self::$FILTER_DAY:
104 return $this->filterDay($request);
105 default:
106 return $this->noFilter($visibility);
107 }
108 }
109
110 /**
111 * Unknown filter, but handle private only.
112 *
113 * @param string $visibility Optional: return only all/private/public links
114 *
115 * @return array filtered links.
116 */
117 private function noFilter($visibility = 'all')
118 {
119 if ($visibility === 'all') {
120 return $this->links;
121 }
122
123 $out = array();
124 foreach ($this->links as $key => $value) {
125 if ($value['private'] && $visibility === 'private') {
126 $out[$key] = $value;
127 } elseif (!$value['private'] && $visibility === 'public') {
128 $out[$key] = $value;
129 }
130 }
131
132 return $out;
133 }
134
135 /**
136 * Returns the shaare corresponding to a smallHash.
137 *
138 * @param string $smallHash permalink hash.
139 *
140 * @return array $filtered array containing permalink data.
141 *
142 * @throws BookmarkNotFoundException if the smallhash doesn't match any link.
143 */
144 private function filterSmallHash($smallHash)
145 {
146 $filtered = array();
147 foreach ($this->links as $key => $l) {
148 if ($smallHash == $l['shorturl']) {
149 // Yes, this is ugly and slow
150 $filtered[$key] = $l;
151 return $filtered;
152 }
153 }
154
155 if (empty($filtered)) {
156 throw new BookmarkNotFoundException();
157 }
158
159 return $filtered;
160 }
161
162 /**
163 * Returns the list of links corresponding to a full-text search
164 *
165 * Searches:
166 * - in the URLs, title and description;
167 * - are case-insensitive;
168 * - terms surrounded by quotes " are exact terms search.
169 * - terms starting with a dash - are excluded (except exact terms).
170 *
171 * Example:
172 * print_r($mydb->filterFulltext('hollandais'));
173 *
174 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
175 * - allows to perform searches on Unicode text
176 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
177 *
178 * @param string $searchterms search query.
179 * @param string $visibility Optional: return only all/private/public links.
180 *
181 * @return array search results.
182 */
183 private function filterFulltext($searchterms, $visibility = 'all')
184 {
185 if (empty($searchterms)) {
186 return $this->noFilter($visibility);
187 }
188
189 $filtered = array();
190 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8');
191 $exactRegex = '/"([^"]+)"/';
192 // Retrieve exact search terms.
193 preg_match_all($exactRegex, $search, $exactSearch);
194 $exactSearch = array_values(array_filter($exactSearch[1]));
195
196 // Remove exact search terms to get AND terms search.
197 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
198 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
199
200 // Filter excluding terms and update andSearch.
201 $excludeSearch = array();
202 $andSearch = array();
203 foreach ($explodedSearchAnd as $needle) {
204 if ($needle[0] == '-' && strlen($needle) > 1) {
205 $excludeSearch[] = substr($needle, 1);
206 } else {
207 $andSearch[] = $needle;
208 }
209 }
210
211 $keys = array('title', 'description', 'url', 'tags');
212
213 // Iterate over every stored link.
214 foreach ($this->links as $id => $link) {
215 // ignore non private links when 'privatonly' is on.
216 if ($visibility !== 'all') {
217 if (!$link['private'] && $visibility === 'private') {
218 continue;
219 } elseif ($link['private'] && $visibility === 'public') {
220 continue;
221 }
222 }
223
224 // Concatenate link fields to search across fields.
225 // Adds a '\' separator for exact search terms.
226 $content = '';
227 foreach ($keys as $key) {
228 $content .= mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8') . '\\';
229 }
230
231 // Be optimistic
232 $found = true;
233
234 // First, we look for exact term search
235 for ($i = 0; $i < count($exactSearch) && $found; $i++) {
236 $found = strpos($content, $exactSearch[$i]) !== false;
237 }
238
239 // Iterate over keywords, if keyword is not found,
240 // no need to check for the others. We want all or nothing.
241 for ($i = 0; $i < count($andSearch) && $found; $i++) {
242 $found = strpos($content, $andSearch[$i]) !== false;
243 }
244
245 // Exclude terms.
246 for ($i = 0; $i < count($excludeSearch) && $found; $i++) {
247 $found = strpos($content, $excludeSearch[$i]) === false;
248 }
249
250 if ($found) {
251 $filtered[$id] = $link;
252 }
253 }
254
255 return $filtered;
256 }
257
258 /**
259 * generate a regex fragment out of a tag
260 *
261 * @param string $tag to to generate regexs from. may start with '-' to negate, contain '*' as wildcard
262 *
263 * @return string generated regex fragment
264 */
265 private static function tag2regex($tag)
266 {
267 $len = strlen($tag);
268 if (!$len || $tag === "-" || $tag === "*") {
269 // nothing to search, return empty regex
270 return '';
271 }
272 if ($tag[0] === "-") {
273 // query is negated
274 $i = 1; // use offset to start after '-' character
275 $regex = '(?!'; // create negative lookahead
276 } else {
277 $i = 0; // start at first character
278 $regex = '(?='; // use positive lookahead
279 }
280 $regex .= '.*(?:^| )'; // before tag may only be a space or the beginning
281 // iterate over string, separating it into placeholder and content
282 for (; $i < $len; $i++) {
283 if ($tag[$i] === '*') {
284 // placeholder found
285 $regex .= '[^ ]*?';
286 } else {
287 // regular characters
288 $offset = strpos($tag, '*', $i);
289 if ($offset === false) {
290 // no placeholder found, set offset to end of string
291 $offset = $len;
292 }
293 // subtract one, as we want to get before the placeholder or end of string
294 $offset -= 1;
295 // we got a tag name that we want to search for. escape any regex characters to prevent conflicts.
296 $regex .= preg_quote(substr($tag, $i, $offset - $i + 1), '/');
297 // move $i on
298 $i = $offset;
299 }
300 }
301 $regex .= '(?:$| ))'; // after the tag may only be a space or the end
302 return $regex;
303 }
304
305 /**
306 * Returns the list of links associated with a given list of tags
307 *
308 * You can specify one or more tags, separated by space or a comma, e.g.
309 * print_r($mydb->filterTags('linux programming'));
310 *
311 * @param string $tags list of tags separated by commas or blank spaces.
312 * @param bool $casesensitive ignore case if false.
313 * @param string $visibility Optional: return only all/private/public links.
314 *
315 * @return array filtered links.
316 */
317 public function filterTags($tags, $casesensitive = false, $visibility = 'all')
318 {
319 // get single tags (we may get passed an array, even though the docs say different)
320 $inputTags = $tags;
321 if (!is_array($tags)) {
322 // we got an input string, split tags
323 $inputTags = preg_split('/(?:\s+)|,/', $inputTags, -1, PREG_SPLIT_NO_EMPTY);
324 }
325
326 if (!count($inputTags)) {
327 // no input tags
328 return $this->noFilter($visibility);
329 }
330
331 // build regex from all tags
332 $re = '/^' . implode(array_map("self::tag2regex", $inputTags)) . '.*$/';
333 if (!$casesensitive) {
334 // make regex case insensitive
335 $re .= 'i';
336 }
337
338 // create resulting array
339 $filtered = array();
340
341 // iterate over each link
342 foreach ($this->links as $key => $link) {
343 // check level of visibility
344 // ignore non private links when 'privateonly' is on.
345 if ($visibility !== 'all') {
346 if (!$link['private'] && $visibility === 'private') {
347 continue;
348 } elseif ($link['private'] && $visibility === 'public') {
349 continue;
350 }
351 }
352 $search = $link['tags']; // build search string, start with tags of current link
353 if (strlen(trim($link['description'])) && strpos($link['description'], '#') !== false) {
354 // description given and at least one possible tag found
355 $descTags = array();
356 // find all tags in the form of #tag in the description
357 preg_match_all(
358 '/(?<![' . self::$HASHTAG_CHARS . '])#([' . self::$HASHTAG_CHARS . ']+?)\b/sm',
359 $link['description'],
360 $descTags
361 );
362 if (count($descTags[1])) {
363 // there were some tags in the description, add them to the search string
364 $search .= ' ' . implode(' ', $descTags[1]);
365 }
366 };
367 // match regular expression with search string
368 if (!preg_match($re, $search)) {
369 // this entry does _not_ match our regex
370 continue;
371 }
372 $filtered[$key] = $link;
373 }
374 return $filtered;
375 }
376
377 /**
378 * Return only links without any tag.
379 *
380 * @param string $visibility return only all/private/public links.
381 *
382 * @return array filtered links.
383 */
384 public function filterUntagged($visibility)
385 {
386 $filtered = [];
387 foreach ($this->links as $key => $link) {
388 if ($visibility !== 'all') {
389 if (!$link['private'] && $visibility === 'private') {
390 continue;
391 } elseif ($link['private'] && $visibility === 'public') {
392 continue;
393 }
394 }
395
396 if (empty(trim($link['tags']))) {
397 $filtered[$key] = $link;
398 }
399 }
400
401 return $filtered;
402 }
403
404 /**
405 * Returns the list of articles for a given day, chronologically sorted
406 *
407 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
408 * print_r($mydb->filterDay('20120125'));
409 *
410 * @param string $day day to filter.
411 *
412 * @return array all link matching given day.
413 *
414 * @throws Exception if date format is invalid.
415 */
416 public function filterDay($day)
417 {
418 if (!checkDateFormat('Ymd', $day)) {
419 throw new Exception('Invalid date format');
420 }
421
422 $filtered = array();
423 foreach ($this->links as $key => $l) {
424 if ($l['created']->format('Ymd') == $day) {
425 $filtered[$key] = $l;
426 }
427 }
428
429 // sort by date ASC
430 return array_reverse($filtered, true);
431 }
432
433 /**
434 * Convert a list of tags (str) to an array. Also
435 * - handle case sensitivity.
436 * - accepts spaces commas as separator.
437 *
438 * @param string $tags string containing a list of tags.
439 * @param bool $casesensitive will convert everything to lowercase if false.
440 *
441 * @return array filtered tags string.
442 */
443 public static function tagsStrToArray($tags, $casesensitive)
444 {
445 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
446 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8');
447 $tagsOut = str_replace(',', ' ', $tagsOut);
448
449 return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY);
450 }
451}
diff --git a/application/legacy/LegacyUpdater.php b/application/legacy/LegacyUpdater.php
new file mode 100644
index 00000000..3a5de79f
--- /dev/null
+++ b/application/legacy/LegacyUpdater.php
@@ -0,0 +1,617 @@
1<?php
2
3namespace Shaarli\Legacy;
4
5use Exception;
6use RainTPL;
7use ReflectionClass;
8use ReflectionException;
9use ReflectionMethod;
10use Shaarli\ApplicationUtils;
11use Shaarli\Bookmark\Bookmark;
12use Shaarli\Bookmark\BookmarkArray;
13use Shaarli\Bookmark\LinkDB;
14use Shaarli\Bookmark\BookmarkFilter;
15use Shaarli\Bookmark\BookmarkIO;
16use Shaarli\Config\ConfigJson;
17use Shaarli\Config\ConfigManager;
18use Shaarli\Config\ConfigPhp;
19use Shaarli\Exceptions\IOException;
20use Shaarli\Thumbnailer;
21use Shaarli\Updater\Exception\UpdaterException;
22
23/**
24 * Class updater.
25 * Used to update stuff when a new Shaarli's version is reached.
26 * Update methods are ran only once, and the stored in a JSON file.
27 *
28 * @deprecated
29 */
30class LegacyUpdater
31{
32 /**
33 * @var array Updates which are already done.
34 */
35 protected $doneUpdates;
36
37 /**
38 * @var LegacyLinkDB instance.
39 */
40 protected $linkDB;
41
42 /**
43 * @var ConfigManager $conf Configuration Manager instance.
44 */
45 protected $conf;
46
47 /**
48 * @var bool True if the user is logged in, false otherwise.
49 */
50 protected $isLoggedIn;
51
52 /**
53 * @var array $_SESSION
54 */
55 protected $session;
56
57 /**
58 * @var ReflectionMethod[] List of current class methods.
59 */
60 protected $methods;
61
62 /**
63 * Object constructor.
64 *
65 * @param array $doneUpdates Updates which are already done.
66 * @param LegacyLinkDB $linkDB LinkDB instance.
67 * @param ConfigManager $conf Configuration Manager instance.
68 * @param boolean $isLoggedIn True if the user is logged in.
69 * @param array $session $_SESSION (by reference)
70 *
71 * @throws ReflectionException
72 */
73 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn, &$session = [])
74 {
75 $this->doneUpdates = $doneUpdates;
76 $this->linkDB = $linkDB;
77 $this->conf = $conf;
78 $this->isLoggedIn = $isLoggedIn;
79 $this->session = &$session;
80
81 // Retrieve all update methods.
82 $class = new ReflectionClass($this);
83 $this->methods = $class->getMethods();
84 }
85
86 /**
87 * Run all new updates.
88 * Update methods have to start with 'updateMethod' and return true (on success).
89 *
90 * @return array An array containing ran updates.
91 *
92 * @throws UpdaterException If something went wrong.
93 */
94 public function update()
95 {
96 $updatesRan = array();
97
98 // If the user isn't logged in, exit without updating.
99 if ($this->isLoggedIn !== true) {
100 return $updatesRan;
101 }
102
103 if ($this->methods === null) {
104 throw new UpdaterException(t('Couldn\'t retrieve updater class methods.'));
105 }
106
107 foreach ($this->methods as $method) {
108 // Not an update method or already done, pass.
109 if (!startsWith($method->getName(), 'updateMethod')
110 || in_array($method->getName(), $this->doneUpdates)
111 ) {
112 continue;
113 }
114
115 try {
116 $method->setAccessible(true);
117 $res = $method->invoke($this);
118 // Update method must return true to be considered processed.
119 if ($res === true) {
120 $updatesRan[] = $method->getName();
121 }
122 } catch (Exception $e) {
123 throw new UpdaterException($method, $e);
124 }
125 }
126
127 $this->doneUpdates = array_merge($this->doneUpdates, $updatesRan);
128
129 return $updatesRan;
130 }
131
132 /**
133 * @return array Updates methods already processed.
134 */
135 public function getDoneUpdates()
136 {
137 return $this->doneUpdates;
138 }
139
140 /**
141 * Move deprecated options.php to config.php.
142 *
143 * Milestone 0.9 (old versioning) - shaarli/Shaarli#41:
144 * options.php is not supported anymore.
145 */
146 public function updateMethodMergeDeprecatedConfigFile()
147 {
148 if (is_file($this->conf->get('resource.data_dir') . '/options.php')) {
149 include $this->conf->get('resource.data_dir') . '/options.php';
150
151 // Load GLOBALS into config
152 $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS);
153 $allowedKeys[] = 'config';
154 foreach ($GLOBALS as $key => $value) {
155 if (in_array($key, $allowedKeys)) {
156 $this->conf->set($key, $value);
157 }
158 }
159 $this->conf->write($this->isLoggedIn);
160 unlink($this->conf->get('resource.data_dir') . '/options.php');
161 }
162
163 return true;
164 }
165
166 /**
167 * Move old configuration in PHP to the new config system in JSON format.
168 *
169 * Will rename 'config.php' into 'config.save.php' and create 'config.json.php'.
170 * It will also convert legacy setting keys to the new ones.
171 */
172 public function updateMethodConfigToJson()
173 {
174 // JSON config already exists, nothing to do.
175 if ($this->conf->getConfigIO() instanceof ConfigJson) {
176 return true;
177 }
178
179 $configPhp = new ConfigPhp();
180 $configJson = new ConfigJson();
181 $oldConfig = $configPhp->read($this->conf->getConfigFile() . '.php');
182 rename($this->conf->getConfigFileExt(), $this->conf->getConfigFile() . '.save.php');
183 $this->conf->setConfigIO($configJson);
184 $this->conf->reload();
185
186 $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING);
187 foreach (ConfigPhp::$ROOT_KEYS as $key) {
188 $this->conf->set($legacyMap[$key], $oldConfig[$key]);
189 }
190
191 // Set sub config keys (config and plugins)
192 $subConfig = array('config', 'plugins');
193 foreach ($subConfig as $sub) {
194 foreach ($oldConfig[$sub] as $key => $value) {
195 if (isset($legacyMap[$sub . '.' . $key])) {
196 $configKey = $legacyMap[$sub . '.' . $key];
197 } else {
198 $configKey = $sub . '.' . $key;
199 }
200 $this->conf->set($configKey, $value);
201 }
202 }
203
204 try {
205 $this->conf->write($this->isLoggedIn);
206 return true;
207 } catch (IOException $e) {
208 error_log($e->getMessage());
209 return false;
210 }
211 }
212
213 /**
214 * Escape settings which have been manually escaped in every request in previous versions:
215 * - general.title
216 * - general.header_link
217 * - redirector.url
218 *
219 * @return bool true if the update is successful, false otherwise.
220 */
221 public function updateMethodEscapeUnescapedConfig()
222 {
223 try {
224 $this->conf->set('general.title', escape($this->conf->get('general.title')));
225 $this->conf->set('general.header_link', escape($this->conf->get('general.header_link')));
226 $this->conf->write($this->isLoggedIn);
227 } catch (Exception $e) {
228 error_log($e->getMessage());
229 return false;
230 }
231 return true;
232 }
233
234 /**
235 * Update the database to use the new ID system, which replaces linkdate primary keys.
236 * Also, creation and update dates are now DateTime objects (done by LinkDB).
237 *
238 * Since this update is very sensitve (changing the whole database), the datastore will be
239 * automatically backed up into the file datastore.<datetime>.php.
240 *
241 * LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash),
242 * which will be saved by this method.
243 *
244 * @return bool true if the update is successful, false otherwise.
245 */
246 public function updateMethodDatastoreIds()
247 {
248 $first = 'update';
249 foreach ($this->linkDB as $key => $link) {
250 $first = $key;
251 break;
252 }
253
254 // up to date database
255 if (is_int($first)) {
256 return true;
257 }
258
259 $save = $this->conf->get('resource.data_dir') . '/datastore.' . date('YmdHis') . '.php';
260 copy($this->conf->get('resource.datastore'), $save);
261
262 $links = array();
263 foreach ($this->linkDB as $offset => $value) {
264 $links[] = $value;
265 unset($this->linkDB[$offset]);
266 }
267 $links = array_reverse($links);
268 $cpt = 0;
269 foreach ($links as $l) {
270 unset($l['linkdate']);
271 $l['id'] = $cpt;
272 $this->linkDB[$cpt++] = $l;
273 }
274
275 $this->linkDB->save($this->conf->get('resource.page_cache'));
276 $this->linkDB->reorder();
277
278 return true;
279 }
280
281 /**
282 * Rename tags starting with a '-' to work with tag exclusion search.
283 */
284 public function updateMethodRenameDashTags()
285 {
286 $linklist = $this->linkDB->filterSearch();
287 foreach ($linklist as $key => $link) {
288 $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']);
289 $link['tags'] = implode(' ', array_unique(BookmarkFilter::tagsStrToArray($link['tags'], true)));
290 $this->linkDB[$key] = $link;
291 }
292 $this->linkDB->save($this->conf->get('resource.page_cache'));
293 return true;
294 }
295
296 /**
297 * Initialize API settings:
298 * - api.enabled: true
299 * - api.secret: generated secret
300 */
301 public function updateMethodApiSettings()
302 {
303 if ($this->conf->exists('api.secret')) {
304 return true;
305 }
306
307 $this->conf->set('api.enabled', true);
308 $this->conf->set(
309 'api.secret',
310 generate_api_secret(
311 $this->conf->get('credentials.login'),
312 $this->conf->get('credentials.salt')
313 )
314 );
315 $this->conf->write($this->isLoggedIn);
316 return true;
317 }
318
319 /**
320 * New setting: theme name. If the default theme is used, nothing to do.
321 *
322 * If the user uses a custom theme, raintpl_tpl dir is updated to the parent directory,
323 * and the current theme is set as default in the theme setting.
324 *
325 * @return bool true if the update is successful, false otherwise.
326 */
327 public function updateMethodDefaultTheme()
328 {
329 // raintpl_tpl isn't the root template directory anymore.
330 // We run the update only if this folder still contains the template files.
331 $tplDir = $this->conf->get('resource.raintpl_tpl');
332 $tplFile = $tplDir . '/linklist.html';
333 if (!file_exists($tplFile)) {
334 return true;
335 }
336
337 $parent = dirname($tplDir);
338 $this->conf->set('resource.raintpl_tpl', $parent);
339 $this->conf->set('resource.theme', trim(str_replace($parent, '', $tplDir), '/'));
340 $this->conf->write($this->isLoggedIn);
341
342 // Dependency injection gore
343 RainTPL::$tpl_dir = $tplDir;
344
345 return true;
346 }
347
348 /**
349 * Move the file to inc/user.css to data/user.css.
350 *
351 * Note: Due to hardcoded paths, it's not unit testable. But one line of code should be fine.
352 *
353 * @return bool true if the update is successful, false otherwise.
354 */
355 public function updateMethodMoveUserCss()
356 {
357 if (!is_file('inc/user.css')) {
358 return true;
359 }
360
361 return rename('inc/user.css', 'data/user.css');
362 }
363
364 /**
365 * * `markdown_escape` is a new setting, set to true as default.
366 *
367 * If the markdown plugin was already enabled, escaping is disabled to avoid
368 * breaking existing entries.
369 */
370 public function updateMethodEscapeMarkdown()
371 {
372 if ($this->conf->exists('security.markdown_escape')) {
373 return true;
374 }
375
376 if (in_array('markdown', $this->conf->get('general.enabled_plugins'))) {
377 $this->conf->set('security.markdown_escape', false);
378 } else {
379 $this->conf->set('security.markdown_escape', true);
380 }
381 $this->conf->write($this->isLoggedIn);
382
383 return true;
384 }
385
386 /**
387 * Add 'http://' to Piwik URL the setting is set.
388 *
389 * @return bool true if the update is successful, false otherwise.
390 */
391 public function updateMethodPiwikUrl()
392 {
393 if (!$this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) {
394 return true;
395 }
396
397 $this->conf->set('plugins.PIWIK_URL', 'http://' . $this->conf->get('plugins.PIWIK_URL'));
398 $this->conf->write($this->isLoggedIn);
399
400 return true;
401 }
402
403 /**
404 * Use ATOM feed as default.
405 */
406 public function updateMethodAtomDefault()
407 {
408 if (!$this->conf->exists('feed.show_atom') || $this->conf->get('feed.show_atom') === true) {
409 return true;
410 }
411
412 $this->conf->set('feed.show_atom', true);
413 $this->conf->write($this->isLoggedIn);
414
415 return true;
416 }
417
418 /**
419 * Update updates.check_updates_branch setting.
420 *
421 * If the current major version digit matches the latest branch
422 * major version digit, we set the branch to `latest`,
423 * otherwise we'll check updates on the `stable` branch.
424 *
425 * No update required for the dev version.
426 *
427 * Note: due to hardcoded URL and lack of dependency injection, this is not unit testable.
428 *
429 * FIXME! This needs to be removed when we switch to first digit major version
430 * instead of the second one since the versionning process will change.
431 */
432 public function updateMethodCheckUpdateRemoteBranch()
433 {
434 if (SHAARLI_VERSION === 'dev' || $this->conf->get('updates.check_updates_branch') === 'latest') {
435 return true;
436 }
437
438 // Get latest branch major version digit
439 $latestVersion = ApplicationUtils::getLatestGitVersionCode(
440 'https://raw.githubusercontent.com/shaarli/Shaarli/latest/shaarli_version.php',
441 5
442 );
443 if (preg_match('/(\d+)\.\d+$/', $latestVersion, $matches) === false) {
444 return false;
445 }
446 $latestMajor = $matches[1];
447
448 // Get current major version digit
449 preg_match('/(\d+)\.\d+$/', SHAARLI_VERSION, $matches);
450 $currentMajor = $matches[1];
451
452 if ($currentMajor === $latestMajor) {
453 $branch = 'latest';
454 } else {
455 $branch = 'stable';
456 }
457 $this->conf->set('updates.check_updates_branch', $branch);
458 $this->conf->write($this->isLoggedIn);
459 return true;
460 }
461
462 /**
463 * Reset history store file due to date format change.
464 */
465 public function updateMethodResetHistoryFile()
466 {
467 if (is_file($this->conf->get('resource.history'))) {
468 unlink($this->conf->get('resource.history'));
469 }
470 return true;
471 }
472
473 /**
474 * Save the datastore -> the link order is now applied when bookmarks are saved.
475 */
476 public function updateMethodReorderDatastore()
477 {
478 $this->linkDB->save($this->conf->get('resource.page_cache'));
479 return true;
480 }
481
482 /**
483 * Change privateonly session key to visibility.
484 */
485 public function updateMethodVisibilitySession()
486 {
487 if (isset($_SESSION['privateonly'])) {
488 unset($_SESSION['privateonly']);
489 $_SESSION['visibility'] = 'private';
490 }
491 return true;
492 }
493
494 /**
495 * Add download size and timeout to the configuration file
496 *
497 * @return bool true if the update is successful, false otherwise.
498 */
499 public function updateMethodDownloadSizeAndTimeoutConf()
500 {
501 if ($this->conf->exists('general.download_max_size')
502 && $this->conf->exists('general.download_timeout')
503 ) {
504 return true;
505 }
506
507 if (!$this->conf->exists('general.download_max_size')) {
508 $this->conf->set('general.download_max_size', 1024 * 1024 * 4);
509 }
510
511 if (!$this->conf->exists('general.download_timeout')) {
512 $this->conf->set('general.download_timeout', 30);
513 }
514
515 $this->conf->write($this->isLoggedIn);
516 return true;
517 }
518
519 /**
520 * * Move thumbnails management to WebThumbnailer, coming with new settings.
521 */
522 public function updateMethodWebThumbnailer()
523 {
524 if ($this->conf->exists('thumbnails.mode')) {
525 return true;
526 }
527
528 $thumbnailsEnabled = extension_loaded('gd') && $this->conf->get('thumbnail.enable_thumbnails', true);
529 $this->conf->set('thumbnails.mode', $thumbnailsEnabled ? Thumbnailer::MODE_ALL : Thumbnailer::MODE_NONE);
530 $this->conf->set('thumbnails.width', 125);
531 $this->conf->set('thumbnails.height', 90);
532 $this->conf->remove('thumbnail');
533 $this->conf->write(true);
534
535 if ($thumbnailsEnabled) {
536 $this->session['warnings'][] = t(
537 'You have enabled or changed thumbnails mode. <a href="?do=thumbs_update">Please synchronize them</a>.'
538 );
539 }
540
541 return true;
542 }
543
544 /**
545 * Set sticky = false on all bookmarks
546 *
547 * @return bool true if the update is successful, false otherwise.
548 */
549 public function updateMethodSetSticky()
550 {
551 foreach ($this->linkDB as $key => $link) {
552 if (isset($link['sticky'])) {
553 return true;
554 }
555 $link['sticky'] = false;
556 $this->linkDB[$key] = $link;
557 }
558
559 $this->linkDB->save($this->conf->get('resource.page_cache'));
560
561 return true;
562 }
563
564 /**
565 * Remove redirector settings.
566 */
567 public function updateMethodRemoveRedirector()
568 {
569 $this->conf->remove('redirector');
570 $this->conf->write(true);
571 return true;
572 }
573
574 /**
575 * Migrate the legacy arrays to Bookmark objects.
576 * Also make a backup of the datastore.
577 */
578 public function updateMethodMigrateDatabase()
579 {
580 $save = $this->conf->get('resource.data_dir') . '/datastore.' . date('YmdHis') . '_1.php';
581 if (! copy($this->conf->get('resource.datastore'), $save)) {
582 die('Could not backup the datastore.');
583 }
584
585 $linksArray = new BookmarkArray();
586 foreach ($this->linkDB as $key => $link) {
587 $linksArray[$key] = (new Bookmark())->fromArray($link);
588 }
589 $linksIo = new BookmarkIO($this->conf);
590 $linksIo->write($linksArray);
591
592 return true;
593 }
594
595 /**
596 * Write the `formatter` setting in config file.
597 * Use markdown if the markdown plugin is enabled, the default one otherwise.
598 * Also remove markdown plugin setting as it is now integrated to the core.
599 */
600 public function updateMethodFormatterSetting()
601 {
602 if (!$this->conf->exists('formatter') || $this->conf->get('formatter') === 'default') {
603 $enabledPlugins = $this->conf->get('general.enabled_plugins');
604 if (($pos = array_search('markdown', $enabledPlugins)) !== false) {
605 $formatter = 'markdown';
606 unset($enabledPlugins[$pos]);
607 $this->conf->set('general.enabled_plugins', array_values($enabledPlugins));
608 } else {
609 $formatter = 'default';
610 }
611 $this->conf->set('formatter', $formatter);
612 $this->conf->write(true);
613 }
614
615 return true;
616 }
617}