Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process all Shaarli pages through Slim controllers #1511

Merged
merged 61 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
bee3323
Fix all relative link to work with new URL
ArthurHoaro Jan 23, 2020
485b168
Process picwall rendering through Slim controller + UT
ArthurHoaro Jan 26, 2020
b0428aa
Migrate cache purge function to a proper class
ArthurHoaro Jan 23, 2020
8e47af2
Process logout through Slim controller
ArthurHoaro Jan 23, 2020
03340c1
Slim router: handle add tag route
ArthurHoaro May 12, 2020
c266a89
Process tag cloud page through Slim controller
ArthurHoaro Jan 26, 2020
72caf4e
Working version before optimization
ArthurHoaro May 16, 2020
c79473b
Handle tag filtering in the Bookmark service
ArthurHoaro May 16, 2020
3772298
Few optimizations and code readability for tag cloud controller
ArthurHoaro May 16, 2020
60ae241
Process tag list page through Slim controller
ArthurHoaro May 16, 2020
69e29ff
Process daily page through Slim controller
ArthurHoaro May 17, 2020
07f9943
Slim daily: support legacy query parameter
ArthurHoaro May 17, 2020
e3d28be
Slim daily: minor bugfix with empty data
ArthurHoaro May 17, 2020
c4d5be5
Process Daily RSS feed through Slim controller
ArthurHoaro May 17, 2020
029ada5
PHP 7.1 compatibility
ArthurHoaro May 17, 2020
c56a540
Remove legacy handling of /add-tag route
ArthurHoaro May 18, 2020
f4929b1
Make FeedBuilder instance creation independant of the request stack
ArthurHoaro May 18, 2020
7b2ba6e
RSS/ATOM feeds: process through Slim controller
ArthurHoaro May 18, 2020
5ec4708
Process OpenSearch controller through Slim
ArthurHoaro May 20, 2020
dd09ec5
Refactor front controller tests to create container mock using a trait
ArthurHoaro May 20, 2020
893f515
Process remove tag endpoint through Slim controller
ArthurHoaro May 20, 2020
af29005
Process session filters through Slim controllers
ArthurHoaro May 22, 2020
2899ebb
Initialize admin Slim controllers
ArthurHoaro May 22, 2020
ba43064
Process tools page through Slim controller
ArthurHoaro May 22, 2020
ef00f9d
Process password change controller through Slim
ArthurHoaro May 27, 2020
fdedbfd
Test ShaarliAdminController
ArthurHoaro May 27, 2020
4650332
Password change: UT use case with open shaarli
ArthurHoaro May 27, 2020
66063ed
Process configure page through Slim controller
ArthurHoaro May 30, 2020
8eac2e5
Process manage tags page through Slim controller
ArthurHoaro May 30, 2020
c22fa57
Handle shaare creation/edition/deletion through Slim controllers
ArthurHoaro Jun 6, 2020
818b319
Explicitly define base and asset path in templates
ArthurHoaro Jun 13, 2020
9c75f87
Use multi-level routes for existing controllers instead of 1 level ev…
ArthurHoaro Jun 13, 2020
baa6979
Improve ManageTagController coverage and error handling
ArthurHoaro Jun 13, 2020
1ab6754
Fix bookmarklet with new routes
ArthurHoaro Jun 13, 2020
7b8a6f2
Process change visibility action through Slim controller
ArthurHoaro Jun 13, 2020
3447d88
Pin bookmarks through Slim controller
ArthurHoaro Jun 15, 2020
e8a10f3
Use NetscapeBookmarkUtils object instance instead of static calls
ArthurHoaro Jun 17, 2020
c70ff64
Process bookmark exports through Slim controllers
ArthurHoaro Jun 17, 2020
7865734
Process bookmarks import through Slim controller
ArthurHoaro Jun 17, 2020
1b8620b
Process plugins administration page through Slim controllers
ArthurHoaro Jun 20, 2020
764d34a
Process token retrieve through Slim controller
ArthurHoaro Jun 21, 2020
6132d64
Process thumbnail synchronize page through Slim controllers
ArthurHoaro Jun 27, 2020
1a8ac73
Process main page (linklist) through Slim controller
ArthurHoaro Jul 6, 2020
c4ad3d4
Process Shaarli install through Slim controller
Jul 7, 2020
a8c1145
Process login through Slim controller
ArthurHoaro Jul 21, 2020
fabff38
Move PHP and config init to dedicated file
ArthurHoaro Jul 22, 2020
3ee8351
Multiple small fixes
Jul 23, 2020
8e9169c
Update French translation
Jul 23, 2020
87ae3c4
Fix default link and redirection in install controller
ArthurHoaro Jul 24, 2020
204035b
Fix: visitor are allowed to chose nb of links per page
ArthurHoaro Jul 24, 2020
bc58390
Fix: header search action should be on linklist
ArthurHoaro Jul 24, 2020
9fbc422
New basePath: fix officiel plugin paths and vintage template
ArthurHoaro Jul 26, 2020
a285668
Fix redirection after post install login
ArthurHoaro Jul 27, 2020
b725eb0
Fix links per page controller path
ArthurHoaro Jul 27, 2020
301c7ab
Better support for notes permalink
ArthurHoaro Jul 28, 2020
6241231
Include empty basePath in formatting
ArthurHoaro Jul 28, 2020
f7f08ce
Fix basePath in unit tests reference DB
ArthurHoaro Jul 28, 2020
d6e5f04
Remove anonymous permission and initialize bookmarks on login
ArthurHoaro Aug 1, 2020
1a68ae5
Bookmark's thumbnails PHPDoc improvement
ArthurHoaro Aug 1, 2020
bedbb84
Move all admin controller into a dedicated group
ArthurHoaro Aug 13, 2020
0c6fdbe
Move error handling to dedicated controller instead of middleware
ArthurHoaro Aug 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ indent_size = 4
indent_size = 2

[*.php]
max_line_length = 100
max_line_length = 120

[Dockerfile]
max_line_length = 80
Expand Down
3 changes: 1 addition & 2 deletions application/Thumbnailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Shaarli\Config\ConfigManager;
use WebThumbnailer\Application\ConfigManager as WTConfigManager;
use WebThumbnailer\Exception\WebThumbnailerException;
use WebThumbnailer\WebThumbnailer;

/**
Expand Down Expand Up @@ -90,7 +89,7 @@ public function get($url)

try {
return $this->wt->thumbnail($url);
} catch (WebThumbnailerException $e) {
} catch (\Throwable $e) {
// Exceptions are only thrown in debug mode.
error_log(get_class($e) . ': ' . $e->getMessage());
}
Expand Down
14 changes: 9 additions & 5 deletions application/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,14 @@ function endsWith($haystack, $needle, $case = true)
*
* @param mixed $input Data to escape: a single string or an array of strings.
*
* @return string escaped.
* @return string|array escaped.
ArthurHoaro marked this conversation as resolved.
Show resolved Hide resolved
*/
function escape($input)
{
if (null === $input) {
return null;
}

if (is_bool($input)) {
return $input;
}
Expand Down Expand Up @@ -294,15 +298,15 @@ function normalize_spaces($string)
* Requires php-intl to display international datetimes,
* otherwise default format '%c' will be returned.
*
* @param DateTime $date to format.
* @param bool $time Displays time if true.
* @param bool $intl Use international format if true.
* @param DateTimeInterface $date to format.
* @param bool $time Displays time if true.
* @param bool $intl Use international format if true.
*
* @return bool|string Formatted date, or false if the input is invalid.
*/
function format_date($date, $time = true, $intl = true)
{
if (! $date instanceof DateTime) {
if (! $date instanceof DateTimeInterface) {
return false;
}

Expand Down
9 changes: 8 additions & 1 deletion application/api/ApiMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ public function __invoke($request, $response, $next)
$response = $e->getApiResponse();
}

return $response;
return $response
ArthurHoaro marked this conversation as resolved.
Show resolved Hide resolved
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader(
'Access-Control-Allow-Headers',
'X-Requested-With, Content-Type, Accept, Origin, Authorization'
)
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion application/api/ApiUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function formatLink($bookmark, $indexUrl)
if (! $bookmark->isNote()) {
$out['url'] = $bookmark->getUrl();
} else {
$out['url'] = $indexUrl . $bookmark->getUrl();
$out['url'] = rtrim($indexUrl, '/') . '/' . ltrim($bookmark->getUrl(), '/');
}
$out['shorturl'] = $bookmark->getShortUrl();
$out['title'] = $bookmark->getTitle();
Expand Down
25 changes: 13 additions & 12 deletions application/bookmark/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Shaarli\Bookmark;

use DateTime;
use DateTimeInterface;
use Shaarli\Bookmark\Exception\InvalidBookmarkException;

/**
Expand Down Expand Up @@ -36,16 +37,16 @@ class Bookmark
/** @var array List of bookmark's tags */
protected $tags;

/** @var string Thumbnail's URL - false if no thumbnail could be found */
/** @var string|bool|null Thumbnail's URL - initialized at null, false if no thumbnail could be found */
protected $thumbnail;

/** @var bool Set to true if the bookmark is set as sticky */
protected $sticky;

/** @var DateTime Creation datetime */
/** @var DateTimeInterface Creation datetime */
protected $created;

/** @var DateTime Update datetime */
/** @var DateTimeInterface datetime */
protected $updated;

/** @var bool True if the bookmark can only be seen while logged in */
Expand Down Expand Up @@ -100,12 +101,12 @@ public function validate()
|| ! is_int($this->id)
|| empty($this->shortUrl)
|| empty($this->created)
|| ! $this->created instanceof DateTime
|| ! $this->created instanceof DateTimeInterface
) {
throw new InvalidBookmarkException($this);
}
if (empty($this->url)) {
$this->url = '?'. $this->shortUrl;
$this->url = '/shaare/'. $this->shortUrl;
}
if (empty($this->title)) {
$this->title = $this->url;
Expand Down Expand Up @@ -188,7 +189,7 @@ public function getDescription()
/**
* Get the Created.
*
* @return DateTime
* @return DateTimeInterface
*/
public function getCreated()
{
Expand All @@ -198,7 +199,7 @@ public function getCreated()
/**
* Get the Updated.
*
* @return DateTime
* @return DateTimeInterface
*/
public function getUpdated()
{
Expand Down Expand Up @@ -270,7 +271,7 @@ public function setDescription($description)
* Set the Created.
* Note: you shouldn't set this manually except for special cases (like bookmark import)
*
* @param DateTime $created
* @param DateTimeInterface $created
*
* @return Bookmark
*/
Expand All @@ -284,7 +285,7 @@ public function setCreated($created)
/**
* Set the Updated.
*
* @param DateTime $updated
* @param DateTimeInterface $updated
*
* @return Bookmark
*/
Expand Down Expand Up @@ -346,7 +347,7 @@ public function setTags($tags)
/**
* Get the Thumbnail.
*
* @return string|bool
* @return string|bool|null Thumbnail's URL - initialized at null, false if no thumbnail could be found
*/
public function getThumbnail()
{
Expand All @@ -356,7 +357,7 @@ public function getThumbnail()
/**
* Set the Thumbnail.
*
* @param string|bool $thumbnail
* @param string|bool $thumbnail Thumbnail's URL - false if no thumbnail could be found
*
* @return Bookmark
*/
Expand Down Expand Up @@ -405,7 +406,7 @@ public function getTagsString()
public function isNote()
{
// We check empty value to get a valid result if the link has not been saved yet
return empty($this->url) || $this->url[0] === '?';
return empty($this->url) || startsWith($this->url, '/shaare/') || $this->url[0] === '?';
}

/**
Expand Down
38 changes: 28 additions & 10 deletions application/bookmark/BookmarkFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use Exception;
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
use Shaarli\Bookmark\Exception\EmptyDataStoreException;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\BookmarkMarkdownFormatter;
use Shaarli\History;
use Shaarli\Legacy\LegacyLinkDB;
use Shaarli\Legacy\LegacyUpdater;
use Shaarli\Render\PageCacheManager;
use Shaarli\Updater\UpdaterUtils;

/**
Expand Down Expand Up @@ -39,6 +41,9 @@ class BookmarkFileService implements BookmarkServiceInterface
/** @var History instance */
protected $history;

/** @var PageCacheManager instance */
protected $pageCacheManager;

/** @var bool true for logged in users. Default value to retrieve private bookmarks. */
protected $isLoggedIn;

Expand All @@ -49,6 +54,7 @@ public function __construct(ConfigManager $conf, History $history, $isLoggedIn)
{
$this->conf = $conf;
$this->history = $history;
$this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'), $isLoggedIn);
$this->bookmarksIO = new BookmarkIO($this->conf);
$this->isLoggedIn = $isLoggedIn;

Expand All @@ -57,10 +63,16 @@ public function __construct(ConfigManager $conf, History $history, $isLoggedIn)
} else {
try {
$this->bookmarks = $this->bookmarksIO->read();
} catch (EmptyDataStoreException $e) {
} catch (EmptyDataStoreException|DatastoreNotInitializedException $e) {
$this->bookmarks = new BookmarkArray();
if ($isLoggedIn) {
$this->save();

if ($this->isLoggedIn) {
// Datastore file does not exists, we initialize it with default bookmarks.
if ($e instanceof DatastoreNotInitializedException) {
$this->initialize();
} else {
$this->save();
}
}
}

Expand Down Expand Up @@ -88,7 +100,7 @@ public function findByHash($hash)
throw new Exception('Not authorized');
}

return $bookmark;
return $first;
}

/**
Expand Down Expand Up @@ -149,7 +161,7 @@ public function get($id, $visibility = null)
*/
public function set($bookmark, $save = true)
{
if ($this->isLoggedIn !== true) {
if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
Expand All @@ -174,7 +186,7 @@ public function set($bookmark, $save = true)
*/
public function add($bookmark, $save = true)
{
if ($this->isLoggedIn !== true) {
if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
Expand All @@ -199,7 +211,7 @@ public function add($bookmark, $save = true)
*/
public function addOrSet($bookmark, $save = true)
{
if ($this->isLoggedIn !== true) {
if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
Expand All @@ -216,7 +228,7 @@ public function addOrSet($bookmark, $save = true)
*/
public function remove($bookmark, $save = true)
{
if ($this->isLoggedIn !== true) {
if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
Expand Down Expand Up @@ -269,13 +281,14 @@ public function count($visibility = null)
*/
public function save()
{
if (!$this->isLoggedIn) {
if (true !== $this->isLoggedIn) {
// TODO: raise an Exception instead
die('You are not authorized to change the database.');
}

$this->bookmarks->reorder();
$this->bookmarksIO->write($this->bookmarks);
invalidateCaches($this->conf->get('resource.page_cache'));
$this->pageCacheManager->invalidateCaches();
}

/**
Expand All @@ -291,6 +304,7 @@ public function bookmarksCountPerTag($filteringTags = [], $visibility = null)
if (empty($tag)
|| (! $this->isLoggedIn && startsWith($tag, '.'))
|| $tag === BookmarkMarkdownFormatter::NO_MD_TAG
|| in_array($tag, $filteringTags, true)
) {
continue;
}
Expand Down Expand Up @@ -349,6 +363,10 @@ public function initialize()
{
$initializer = new BookmarkInitializer($this);
$initializer->initialize();

if (true === $this->isLoggedIn) {
$this->save();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion application/bookmark/BookmarkFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public function filterDay($day)
throw new Exception('Invalid date format');
}

$filtered = array();
$filtered = [];
foreach ($this->bookmarks as $key => $l) {
if ($l->getCreated()->format('Ymd') == $day) {
$filtered[$key] = $l;
Expand Down
10 changes: 5 additions & 5 deletions application/bookmark/BookmarkIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shaarli\Bookmark;

use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
use Shaarli\Bookmark\Exception\EmptyDataStoreException;
use Shaarli\Bookmark\Exception\NotWritableDataStoreException;
use Shaarli\Config\ConfigManager;
Expand Down Expand Up @@ -52,13 +53,14 @@ public function __construct($conf)
*
* @return BookmarkArray instance
*
* @throws NotWritableDataStoreException Data couldn't be loaded
* @throws EmptyDataStoreException Datastore doesn't exist
* @throws NotWritableDataStoreException Data couldn't be loaded
* @throws EmptyDataStoreException Datastore file exists but does not contain any bookmark
* @throws DatastoreNotInitializedException File does not exists
*/
public function read()
{
if (! file_exists($this->datastore)) {
throw new EmptyDataStoreException();
throw new DatastoreNotInitializedException();
}

if (!is_writable($this->datastore)) {
Expand Down Expand Up @@ -102,7 +104,5 @@ public function write($links)
$this->datastore,
self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix
);

invalidateCaches($this->conf->get('resource.page_cache'));
}
}
Loading