2020-06-06 14:01:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shaarli\Front\Controller\Admin;
|
|
|
|
|
|
|
|
use Shaarli\Bookmark\Bookmark;
|
|
|
|
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
|
|
|
|
use Shaarli\Formatter\BookmarkMarkdownFormatter;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Render\TemplatePage;
|
2020-06-06 14:01:03 +02:00
|
|
|
use Shaarli\Thumbnailer;
|
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PostBookmarkController
|
|
|
|
*
|
|
|
|
* Slim controller used to handle Shaarli create or edit bookmarks.
|
|
|
|
*/
|
2020-06-13 15:37:02 +02:00
|
|
|
class ManageShaareController extends ShaarliAdminController
|
2020-06-06 14:01:03 +02:00
|
|
|
{
|
|
|
|
/**
|
2020-06-13 13:08:01 +02:00
|
|
|
* GET /admin/add-shaare - Displays the form used to create a new bookmark from an URL
|
2020-06-06 14:01:03 +02:00
|
|
|
*/
|
|
|
|
public function addShaare(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$this->assignView(
|
|
|
|
'pagetitle',
|
|
|
|
t('Shaare a new link') .' - '. $this->container->conf->get('general.title', 'Shaarli')
|
|
|
|
);
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
return $response->write($this->render(TemplatePage::ADDLINK));
|
2020-06-06 14:01:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-13 13:08:01 +02:00
|
|
|
* GET /admin/shaare - Displays the bookmark form for creation.
|
2020-06-13 15:37:02 +02:00
|
|
|
* Note that if the URL is found in existing bookmarks, then it will be in edit mode.
|
2020-06-06 14:01:03 +02:00
|
|
|
*/
|
|
|
|
public function displayCreateForm(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$url = cleanup_url($request->getParam('post'));
|
|
|
|
|
|
|
|
$linkIsNew = false;
|
|
|
|
// Check if URL is not already in database (in this case, we will edit the existing link)
|
|
|
|
$bookmark = $this->container->bookmarkService->findByUrl($url);
|
|
|
|
if (null === $bookmark) {
|
|
|
|
$linkIsNew = true;
|
|
|
|
// Get shaare data if it was provided in URL (e.g.: by the bookmarklet).
|
|
|
|
$title = $request->getParam('title');
|
|
|
|
$description = $request->getParam('description');
|
|
|
|
$tags = $request->getParam('tags');
|
|
|
|
$private = filter_var($request->getParam('private'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
|
|
|
// If this is an HTTP(S) link, we try go get the page to extract
|
|
|
|
// the title (otherwise we will to straight to the edit form.)
|
2020-09-25 13:29:36 +02:00
|
|
|
if (true !== $this->container->conf->get('general.enable_async_metadata', true)
|
|
|
|
&& empty($title)
|
|
|
|
&& strpos(get_url_scheme($url) ?: '', 'http') !== false
|
|
|
|
) {
|
|
|
|
$metadata = $this->container->metadataRetriever->retrieve($url);
|
2020-06-06 14:01:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 13:29:36 +02:00
|
|
|
if (empty($url)) {
|
|
|
|
$metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: '));
|
2020-06-06 14:01:03 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 17:30:18 +02:00
|
|
|
$link = [
|
2020-09-25 13:29:36 +02:00
|
|
|
'title' => $title ?? $metadata['title'] ?? '',
|
2020-06-06 14:01:03 +02:00
|
|
|
'url' => $url ?? '',
|
2020-09-25 13:29:36 +02:00
|
|
|
'description' => $description ?? $metadata['description'] ?? '',
|
|
|
|
'tags' => $tags ?? $metadata['tags'] ?? '',
|
2020-06-06 14:01:03 +02:00
|
|
|
'private' => $private,
|
2020-10-06 17:30:18 +02:00
|
|
|
];
|
2020-06-06 14:01:03 +02:00
|
|
|
} else {
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter('raw');
|
|
|
|
$link = $formatter->format($bookmark);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->displayForm($link, $linkIsNew, $request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-13 13:08:01 +02:00
|
|
|
* GET /admin/shaare/{id} - Displays the bookmark form in edition mode.
|
2020-06-06 14:01:03 +02:00
|
|
|
*/
|
|
|
|
public function displayEditForm(Request $request, Response $response, array $args): Response
|
|
|
|
{
|
2020-06-13 15:37:02 +02:00
|
|
|
$id = $args['id'] ?? '';
|
2020-06-06 14:01:03 +02:00
|
|
|
try {
|
|
|
|
if (false === ctype_digit($id)) {
|
|
|
|
throw new BookmarkNotFoundException();
|
|
|
|
}
|
2020-06-13 15:37:02 +02:00
|
|
|
$bookmark = $this->container->bookmarkService->get((int) $id); // Read database
|
2020-06-06 14:01:03 +02:00
|
|
|
} catch (BookmarkNotFoundException $e) {
|
2020-06-13 15:37:02 +02:00
|
|
|
$this->saveErrorMessage(sprintf(
|
|
|
|
t('Bookmark with identifier %s could not be found.'),
|
|
|
|
$id
|
|
|
|
));
|
2020-06-06 14:01:03 +02:00
|
|
|
|
2020-06-13 13:08:01 +02:00
|
|
|
return $this->redirect($response, '/');
|
2020-06-06 14:01:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter('raw');
|
|
|
|
$link = $formatter->format($bookmark);
|
|
|
|
|
|
|
|
return $this->displayForm($link, false, $request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-13 13:08:01 +02:00
|
|
|
* POST /admin/shaare
|
2020-06-06 14:01:03 +02:00
|
|
|
*/
|
|
|
|
public function save(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$this->checkToken($request);
|
|
|
|
|
|
|
|
// lf_id should only be present if the link exists.
|
2020-09-30 15:31:34 +02:00
|
|
|
$id = $request->getParam('lf_id') !== null ? intval(escape($request->getParam('lf_id'))) : null;
|
2020-06-06 14:01:03 +02:00
|
|
|
if (null !== $id && true === $this->container->bookmarkService->exists($id)) {
|
|
|
|
// Edit
|
|
|
|
$bookmark = $this->container->bookmarkService->get($id);
|
|
|
|
} else {
|
|
|
|
// New link
|
|
|
|
$bookmark = new Bookmark();
|
|
|
|
}
|
|
|
|
|
|
|
|
$bookmark->setTitle($request->getParam('lf_title'));
|
|
|
|
$bookmark->setDescription($request->getParam('lf_description'));
|
|
|
|
$bookmark->setUrl($request->getParam('lf_url'), $this->container->conf->get('security.allowed_protocols', []));
|
|
|
|
$bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN));
|
|
|
|
$bookmark->setTagsString($request->getParam('lf_tags'));
|
|
|
|
|
|
|
|
if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
|
|
|
|
&& false === $bookmark->isNote()
|
|
|
|
) {
|
|
|
|
$bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl()));
|
|
|
|
}
|
|
|
|
$this->container->bookmarkService->addOrSet($bookmark, false);
|
|
|
|
|
|
|
|
// To preserve backward compatibility with 3rd parties, plugins still use arrays
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter('raw');
|
|
|
|
$data = $formatter->format($bookmark);
|
2020-07-26 14:43:10 +02:00
|
|
|
$this->executePageHooks('save_link', $data);
|
2020-06-06 14:01:03 +02:00
|
|
|
|
|
|
|
$bookmark->fromArray($data);
|
|
|
|
$this->container->bookmarkService->set($bookmark);
|
|
|
|
|
|
|
|
// If we are called from the bookmarklet, we must close the popup:
|
|
|
|
if ($request->getParam('source') === 'bookmarklet') {
|
|
|
|
return $response->write('<script>self.close();</script>');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($request->getParam('returnurl'))) {
|
|
|
|
$this->container->environment['HTTP_REFERER'] = escape($request->getParam('returnurl'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirectFromReferer(
|
|
|
|
$request,
|
|
|
|
$response,
|
2020-09-22 14:04:10 +02:00
|
|
|
['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'],
|
2020-06-06 14:01:03 +02:00
|
|
|
$bookmark->getShortUrl()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:08:01 +02:00
|
|
|
/**
|
2020-06-13 19:40:32 +02:00
|
|
|
* GET /admin/shaare/delete - Delete one or multiple bookmarks (depending on `id` query parameter).
|
2020-06-13 13:08:01 +02:00
|
|
|
*/
|
2020-06-06 14:01:03 +02:00
|
|
|
public function deleteBookmark(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$this->checkToken($request);
|
|
|
|
|
2020-06-13 15:37:02 +02:00
|
|
|
$ids = escape(trim($request->getParam('id') ?? ''));
|
|
|
|
if (empty($ids) || strpos($ids, ' ') !== false) {
|
2020-06-06 14:01:03 +02:00
|
|
|
// multiple, space-separated ids provided
|
2020-06-13 15:37:02 +02:00
|
|
|
$ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
|
2020-06-06 14:01:03 +02:00
|
|
|
} else {
|
|
|
|
$ids = [$ids];
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert at least one id is given
|
|
|
|
if (0 === count($ids)) {
|
|
|
|
$this->saveErrorMessage(t('Invalid bookmark ID provided.'));
|
|
|
|
|
|
|
|
return $this->redirectFromReferer($request, $response, [], ['delete-shaare']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter('raw');
|
2020-06-13 15:37:02 +02:00
|
|
|
$count = 0;
|
2020-06-06 14:01:03 +02:00
|
|
|
foreach ($ids as $id) {
|
2020-06-13 15:37:02 +02:00
|
|
|
try {
|
|
|
|
$bookmark = $this->container->bookmarkService->get((int) $id);
|
|
|
|
} catch (BookmarkNotFoundException $e) {
|
|
|
|
$this->saveErrorMessage(sprintf(
|
|
|
|
t('Bookmark with identifier %s could not be found.'),
|
|
|
|
$id
|
|
|
|
));
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:01:03 +02:00
|
|
|
$data = $formatter->format($bookmark);
|
2020-07-26 14:43:10 +02:00
|
|
|
$this->executePageHooks('delete_link', $data);
|
2020-06-06 14:01:03 +02:00
|
|
|
$this->container->bookmarkService->remove($bookmark, false);
|
2020-06-13 15:37:02 +02:00
|
|
|
++ $count;
|
2020-06-06 14:01:03 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 15:37:02 +02:00
|
|
|
if ($count > 0) {
|
|
|
|
$this->container->bookmarkService->save();
|
|
|
|
}
|
2020-06-06 14:01:03 +02:00
|
|
|
|
|
|
|
// If we are called from the bookmarklet, we must close the popup:
|
|
|
|
if ($request->getParam('source') === 'bookmarklet') {
|
|
|
|
return $response->write('<script>self.close();</script>');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't redirect to where we were previously because the datastore has changed.
|
2020-06-13 13:08:01 +02:00
|
|
|
return $this->redirect($response, '/');
|
2020-06-06 14:01:03 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:40:32 +02:00
|
|
|
/**
|
|
|
|
* GET /admin/shaare/visibility
|
|
|
|
*
|
|
|
|
* Change visibility (public/private) of one or multiple bookmarks (depending on `id` query parameter).
|
|
|
|
*/
|
|
|
|
public function changeVisibility(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$this->checkToken($request);
|
|
|
|
|
|
|
|
$ids = trim(escape($request->getParam('id') ?? ''));
|
|
|
|
if (empty($ids) || strpos($ids, ' ') !== false) {
|
|
|
|
// multiple, space-separated ids provided
|
|
|
|
$ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
|
|
|
|
} else {
|
|
|
|
// only a single id provided
|
|
|
|
$ids = [$ids];
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert at least one id is given
|
|
|
|
if (0 === count($ids)) {
|
|
|
|
$this->saveErrorMessage(t('Invalid bookmark ID provided.'));
|
|
|
|
|
|
|
|
return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert that the visibility is valid
|
|
|
|
$visibility = $request->getParam('newVisibility');
|
|
|
|
if (null === $visibility || false === in_array($visibility, ['public', 'private'], true)) {
|
|
|
|
$this->saveErrorMessage(t('Invalid visibility provided.'));
|
|
|
|
|
|
|
|
return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
|
|
|
|
} else {
|
|
|
|
$isPrivate = $visibility === 'private';
|
|
|
|
}
|
|
|
|
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter('raw');
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
try {
|
|
|
|
$bookmark = $this->container->bookmarkService->get((int) $id);
|
|
|
|
} catch (BookmarkNotFoundException $e) {
|
|
|
|
$this->saveErrorMessage(sprintf(
|
|
|
|
t('Bookmark with identifier %s could not be found.'),
|
|
|
|
$id
|
|
|
|
));
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$bookmark->setPrivate($isPrivate);
|
|
|
|
|
|
|
|
// To preserve backward compatibility with 3rd parties, plugins still use arrays
|
|
|
|
$data = $formatter->format($bookmark);
|
2020-07-26 14:43:10 +02:00
|
|
|
$this->executePageHooks('save_link', $data);
|
2020-06-13 19:40:32 +02:00
|
|
|
$bookmark->fromArray($data);
|
|
|
|
|
|
|
|
$this->container->bookmarkService->set($bookmark, false);
|
|
|
|
++$count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
$this->container->bookmarkService->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirectFromReferer($request, $response, ['/visibility'], ['change_visibility']);
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:15:40 +02:00
|
|
|
/**
|
|
|
|
* GET /admin/shaare/{id}/pin - Pin or unpin a bookmark.
|
|
|
|
*/
|
|
|
|
public function pinBookmark(Request $request, Response $response, array $args): Response
|
|
|
|
{
|
|
|
|
$this->checkToken($request);
|
|
|
|
|
|
|
|
$id = $args['id'] ?? '';
|
|
|
|
try {
|
|
|
|
if (false === ctype_digit($id)) {
|
|
|
|
throw new BookmarkNotFoundException();
|
|
|
|
}
|
|
|
|
$bookmark = $this->container->bookmarkService->get((int) $id); // Read database
|
|
|
|
} catch (BookmarkNotFoundException $e) {
|
|
|
|
$this->saveErrorMessage(sprintf(
|
|
|
|
t('Bookmark with identifier %s could not be found.'),
|
|
|
|
$id
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter('raw');
|
|
|
|
|
|
|
|
$bookmark->setSticky(!$bookmark->isSticky());
|
|
|
|
|
|
|
|
// To preserve backward compatibility with 3rd parties, plugins still use arrays
|
|
|
|
$data = $formatter->format($bookmark);
|
2020-07-26 14:43:10 +02:00
|
|
|
$this->executePageHooks('save_link', $data);
|
2020-06-15 08:15:40 +02:00
|
|
|
$bookmark->fromArray($data);
|
|
|
|
|
|
|
|
$this->container->bookmarkService->set($bookmark);
|
|
|
|
|
|
|
|
return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
|
|
|
|
}
|
|
|
|
|
2020-06-13 15:37:02 +02:00
|
|
|
/**
|
|
|
|
* Helper function used to display the shaare form whether it's a new or existing bookmark.
|
|
|
|
*
|
|
|
|
* @param array $link data used in template, either from parameters or from the data store
|
|
|
|
*/
|
2020-06-06 14:01:03 +02:00
|
|
|
protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$tags = $this->container->bookmarkService->bookmarksCountPerTag();
|
|
|
|
if ($this->container->conf->get('formatter') === 'markdown') {
|
|
|
|
$tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1;
|
|
|
|
}
|
|
|
|
|
2020-10-06 17:30:18 +02:00
|
|
|
$data = escape([
|
2020-06-06 14:01:03 +02:00
|
|
|
'link' => $link,
|
|
|
|
'link_is_new' => $isNew,
|
2020-10-06 17:30:18 +02:00
|
|
|
'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '',
|
2020-06-06 14:01:03 +02:00
|
|
|
'source' => $request->getParam('source') ?? '',
|
|
|
|
'tags' => $tags,
|
|
|
|
'default_private_links' => $this->container->conf->get('privacy.default_private_links', false),
|
2020-09-25 13:29:36 +02:00
|
|
|
'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true),
|
|
|
|
'retrieve_description' => $this->container->conf->get('general.retrieve_description', false),
|
2020-10-06 17:30:18 +02:00
|
|
|
]);
|
2020-06-06 14:01:03 +02:00
|
|
|
|
2020-07-26 14:43:10 +02:00
|
|
|
$this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK);
|
2020-06-06 14:01:03 +02:00
|
|
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$this->assignView($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$editLabel = false === $isNew ? t('Edit') .' ' : '';
|
|
|
|
$this->assignView(
|
|
|
|
'pagetitle',
|
|
|
|
$editLabel . t('Shaare') .' - '. $this->container->conf->get('general.title', 'Shaarli')
|
|
|
|
);
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
return $response->write($this->render(TemplatePage::EDIT_LINK));
|
2020-06-06 14:01:03 +02:00
|
|
|
}
|
|
|
|
}
|