diff options
Diffstat (limited to 'application/bookmark/BookmarkFileService.php')
-rw-r--r-- | application/bookmark/BookmarkFileService.php | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php index eb7899bf..6666a251 100644 --- a/application/bookmark/BookmarkFileService.php +++ b/application/bookmark/BookmarkFileService.php | |||
@@ -69,7 +69,7 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
69 | } else { | 69 | } else { |
70 | try { | 70 | try { |
71 | $this->bookmarks = $this->bookmarksIO->read(); | 71 | $this->bookmarks = $this->bookmarksIO->read(); |
72 | } catch (EmptyDataStoreException|DatastoreNotInitializedException $e) { | 72 | } catch (EmptyDataStoreException | DatastoreNotInitializedException $e) { |
73 | $this->bookmarks = new BookmarkArray(); | 73 | $this->bookmarks = new BookmarkArray(); |
74 | 74 | ||
75 | if ($this->isLoggedIn) { | 75 | if ($this->isLoggedIn) { |
@@ -85,25 +85,29 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
85 | if (! $this->bookmarks instanceof BookmarkArray) { | 85 | if (! $this->bookmarks instanceof BookmarkArray) { |
86 | $this->migrate(); | 86 | $this->migrate(); |
87 | exit( | 87 | exit( |
88 | 'Your data store has been migrated, please reload the page.'. PHP_EOL . | 88 | 'Your data store has been migrated, please reload the page.' . PHP_EOL . |
89 | 'If this message keeps showing up, please delete data/updates.txt file.' | 89 | 'If this message keeps showing up, please delete data/updates.txt file.' |
90 | ); | 90 | ); |
91 | } | 91 | } |
92 | } | 92 | } |
93 | 93 | ||
94 | $this->bookmarkFilter = new BookmarkFilter($this->bookmarks); | 94 | $this->bookmarkFilter = new BookmarkFilter($this->bookmarks, $this->conf); |
95 | } | 95 | } |
96 | 96 | ||
97 | /** | 97 | /** |
98 | * @inheritDoc | 98 | * @inheritDoc |
99 | */ | 99 | */ |
100 | public function findByHash(string $hash): Bookmark | 100 | public function findByHash(string $hash, string $privateKey = null): Bookmark |
101 | { | 101 | { |
102 | $bookmark = $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_HASH, $hash); | 102 | $bookmark = $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_HASH, $hash); |
103 | // PHP 7.3 introduced array_key_first() to avoid this hack | 103 | // PHP 7.3 introduced array_key_first() to avoid this hack |
104 | $first = reset($bookmark); | 104 | $first = reset($bookmark); |
105 | if (! $this->isLoggedIn && $first->isPrivate()) { | 105 | if ( |
106 | throw new Exception('Not authorized'); | 106 | !$this->isLoggedIn |
107 | && $first->isPrivate() | ||
108 | && (empty($privateKey) || $privateKey !== $first->getAdditionalContentEntry('private_key')) | ||
109 | ) { | ||
110 | throw new BookmarkNotFoundException(); | ||
107 | } | 111 | } |
108 | 112 | ||
109 | return $first; | 113 | return $first; |
@@ -162,7 +166,8 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
162 | } | 166 | } |
163 | 167 | ||
164 | $bookmark = $this->bookmarks[$id]; | 168 | $bookmark = $this->bookmarks[$id]; |
165 | if (($bookmark->isPrivate() && $visibility != 'all' && $visibility != 'private') | 169 | if ( |
170 | ($bookmark->isPrivate() && $visibility != 'all' && $visibility != 'private') | ||
166 | || (! $bookmark->isPrivate() && $visibility != 'all' && $visibility != 'public') | 171 | || (! $bookmark->isPrivate() && $visibility != 'all' && $visibility != 'public') |
167 | ) { | 172 | ) { |
168 | throw new Exception('Unauthorized'); | 173 | throw new Exception('Unauthorized'); |
@@ -262,7 +267,8 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
262 | } | 267 | } |
263 | 268 | ||
264 | $bookmark = $this->bookmarks[$id]; | 269 | $bookmark = $this->bookmarks[$id]; |
265 | if (($bookmark->isPrivate() && $visibility != 'all' && $visibility != 'private') | 270 | if ( |
271 | ($bookmark->isPrivate() && $visibility != 'all' && $visibility != 'private') | ||
266 | || (! $bookmark->isPrivate() && $visibility != 'all' && $visibility != 'public') | 272 | || (! $bookmark->isPrivate() && $visibility != 'all' && $visibility != 'public') |
267 | ) { | 273 | ) { |
268 | return false; | 274 | return false; |
@@ -304,7 +310,8 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
304 | $caseMapping = []; | 310 | $caseMapping = []; |
305 | foreach ($bookmarks as $bookmark) { | 311 | foreach ($bookmarks as $bookmark) { |
306 | foreach ($bookmark->getTags() as $tag) { | 312 | foreach ($bookmark->getTags() as $tag) { |
307 | if (empty($tag) | 313 | if ( |
314 | empty($tag) | ||
308 | || (! $this->isLoggedIn && startsWith($tag, '.')) | 315 | || (! $this->isLoggedIn && startsWith($tag, '.')) |
309 | || $tag === BookmarkMarkdownFormatter::NO_MD_TAG | 316 | || $tag === BookmarkMarkdownFormatter::NO_MD_TAG |
310 | || in_array($tag, $filteringTags, true) | 317 | || in_array($tag, $filteringTags, true) |
@@ -340,26 +347,42 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
340 | /** | 347 | /** |
341 | * @inheritDoc | 348 | * @inheritDoc |
342 | */ | 349 | */ |
343 | public function days(): array | 350 | public function findByDate( |
344 | { | 351 | \DateTimeInterface $from, |
345 | $bookmarkDays = []; | 352 | \DateTimeInterface $to, |
346 | foreach ($this->search() as $bookmark) { | 353 | ?\DateTimeInterface &$previous, |
347 | $bookmarkDays[$bookmark->getCreated()->format('Ymd')] = 0; | 354 | ?\DateTimeInterface &$next |
355 | ): array { | ||
356 | $out = []; | ||
357 | $previous = null; | ||
358 | $next = null; | ||
359 | |||
360 | foreach ($this->search([], null, false, false, true) as $bookmark) { | ||
361 | if ($to < $bookmark->getCreated()) { | ||
362 | $next = $bookmark->getCreated(); | ||
363 | } elseif ($from < $bookmark->getCreated() && $to > $bookmark->getCreated()) { | ||
364 | $out[] = $bookmark; | ||
365 | } else { | ||
366 | if ($previous !== null) { | ||
367 | break; | ||
368 | } | ||
369 | $previous = $bookmark->getCreated(); | ||
370 | } | ||
348 | } | 371 | } |
349 | $bookmarkDays = array_keys($bookmarkDays); | ||
350 | sort($bookmarkDays); | ||
351 | 372 | ||
352 | return array_map('strval', $bookmarkDays); | 373 | return $out; |
353 | } | 374 | } |
354 | 375 | ||
355 | /** | 376 | /** |
356 | * @inheritDoc | 377 | * @inheritDoc |
357 | */ | 378 | */ |
358 | public function filterDay(string $request) | 379 | public function getLatest(): ?Bookmark |
359 | { | 380 | { |
360 | $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; | 381 | foreach ($this->search([], null, false, false, true) as $bookmark) { |
382 | return $bookmark; | ||
383 | } | ||
361 | 384 | ||
362 | return $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_DAY, $request, false, $visibility); | 385 | return null; |
363 | } | 386 | } |
364 | 387 | ||
365 | /** | 388 | /** |
@@ -386,14 +409,14 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
386 | false | 409 | false |
387 | ); | 410 | ); |
388 | $updater = new LegacyUpdater( | 411 | $updater = new LegacyUpdater( |
389 | UpdaterUtils::read_updates_file($this->conf->get('resource.updates')), | 412 | UpdaterUtils::readUpdatesFile($this->conf->get('resource.updates')), |
390 | $bookmarkDb, | 413 | $bookmarkDb, |
391 | $this->conf, | 414 | $this->conf, |
392 | true | 415 | true |
393 | ); | 416 | ); |
394 | $newUpdates = $updater->update(); | 417 | $newUpdates = $updater->update(); |
395 | if (! empty($newUpdates)) { | 418 | if (! empty($newUpdates)) { |
396 | UpdaterUtils::write_updates_file( | 419 | UpdaterUtils::writeUpdatesFile( |
397 | $this->conf->get('resource.updates'), | 420 | $this->conf->get('resource.updates'), |
398 | $updater->getDoneUpdates() | 421 | $updater->getDoneUpdates() |
399 | ); | 422 | ); |