3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
9 use Shaarli\Formatter\BookmarkMarkdownFormatter
;
10 use Shaarli\Render\TemplatePage
;
11 use Shaarli\Thumbnailer
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
15 class ShaarePublishController
extends ShaarliAdminController
18 * GET /admin/shaare - Displays the bookmark form for creation.
19 * Note that if the URL is found in existing bookmarks, then it will be in edit mode.
21 public function displayCreateForm(Request
$request, Response
$response): Response
23 $url = cleanup_url($request->getParam('post'));
24 $link = $this->buildLinkDataFromUrl($request, $url);
26 return $this->displayForm($link, $link['linkIsNew'], $request, $response);
30 * POST /admin/shaare-batch - Displays multiple creation/edit forms from bulk add in add-link page.
32 public function displayCreateBatchForms(Request
$request, Response
$response): Response
34 $urls = array_map('cleanup_url', explode(PHP_EOL
, $request->getParam('urls')));
37 foreach ($urls as $url) {
38 $link = $this->buildLinkDataFromUrl($request, $url);
39 $data = $this->buildFormData($link, $link['linkIsNew'], $request);
40 $data['token'] = $this->container
->sessionManager
->generateToken();
41 $data['source'] = 'batch';
43 $this->executePageHooks('render_editlink', $data, TemplatePage
::EDIT_LINK
);
48 $this->assignView('links', $links);
49 $this->assignView('batch_mode', true);
50 $this->assignView('async_metadata', $this->container
->conf
->get('general.enable_async_metadata', true));
52 return $response->write($this->render(TemplatePage
::EDIT_LINK_BATCH
));
56 * GET /admin/shaare/{id} - Displays the bookmark form in edition mode.
58 public function displayEditForm(Request
$request, Response
$response, array $args): Response
60 $id = $args['id'] ?? '';
62 if (false === ctype_digit($id)) {
63 throw new BookmarkNotFoundException();
65 $bookmark = $this->container
->bookmarkService
->get((int) $id); // Read database
66 } catch (BookmarkNotFoundException
$e) {
67 $this->saveErrorMessage(sprintf(
68 t('Bookmark with identifier %s could not be found.'),
72 return $this->redirect($response, '/');
75 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
76 $link = $formatter->format($bookmark);
78 return $this->displayForm($link, false, $request, $response);
84 public function save(Request
$request, Response
$response): Response
86 $this->checkToken($request);
88 // lf_id should only be present if the link exists.
89 $id = $request->getParam('lf_id') !== null ? intval(escape($request->getParam('lf_id'))) : null;
90 if (null !== $id && true === $this->container
->bookmarkService
->exists($id)) {
92 $bookmark = $this->container
->bookmarkService
->get($id);
95 $bookmark = new Bookmark();
98 $bookmark->setTitle($request->getParam('lf_title'));
99 $bookmark->setDescription($request->getParam('lf_description'));
100 $bookmark->setUrl($request->getParam('lf_url'), $this->container
->conf
->get('security.allowed_protocols', []));
101 $bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN
));
102 $bookmark->setTagsString($request->getParam('lf_tags'));
104 if ($this->container
->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
105 && true !== $this->container
->conf
->get('general.enable_async_metadata', true)
106 && $bookmark->shouldUpdateThumbnail()
108 $bookmark->setThumbnail($this->container
->thumbnailer
->get($bookmark->getUrl()));
110 $this->container
->bookmarkService
->addOrSet($bookmark, false);
112 // To preserve backward compatibility with 3rd parties, plugins still use arrays
113 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
114 $data = $formatter->format($bookmark);
115 $this->executePageHooks('save_link', $data);
117 $bookmark->fromArray($data);
118 $this->container
->bookmarkService
->set($bookmark);
120 // If we are called from the bookmarklet, we must close the popup:
121 if ($request->getParam('source') === 'bookmarklet') {
122 return $response->write('<script>self.close();</script>');
123 } elseif ($request->getParam('source') === 'batch') {
127 if (!empty($request->getParam('returnurl'))) {
128 $this->container
->environment
['HTTP_REFERER'] = escape($request->getParam('returnurl'));
131 return $this->redirectFromReferer(
134 ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'],
135 $bookmark->getShortUrl()
140 * Helper function used to display the shaare form whether it's a new or existing bookmark.
142 * @param array $link data used in template, either from parameters or from the data store
144 protected function displayForm(array $link, bool $isNew, Request
$request, Response
$response): Response
146 $data = $this->buildFormData($link, $isNew, $request);
148 $this->executePageHooks('render_editlink', $data, TemplatePage
::EDIT_LINK
);
150 foreach ($data as $key => $value) {
151 $this->assignView($key, $value);
154 $editLabel = false === $isNew ? t('Edit') .' ' : '';
157 $editLabel . t('Shaare') .' - '. $this->container
->conf
->get('general.title', 'Shaarli')
160 return $response->write($this->render(TemplatePage
::EDIT_LINK
));
163 protected function buildLinkDataFromUrl(Request
$request, string $url): array
165 // Check if URL is not already in database (in this case, we will edit the existing link)
166 $bookmark = $this->container
->bookmarkService
->findByUrl($url);
167 if (null === $bookmark) {
168 // Get shaare data if it was provided in URL (e.g.: by the bookmarklet).
169 $title = $request->getParam('title');
170 $description = $request->getParam('description');
171 $tags = $request->getParam('tags');
172 if ($request->getParam('private') !== null) {
173 $private = filter_var($request->getParam('private'), FILTER_VALIDATE_BOOLEAN
);
175 $private = $this->container
->conf
->get('privacy.default_private_links', false);
178 // If this is an HTTP(S) link, we try go get the page to extract
179 // the title (otherwise we will to straight to the edit form.)
180 if (true !== $this->container
->conf
->get('general.enable_async_metadata', true)
182 && strpos(get_url_scheme($url) ?: '', 'http') !== false
184 $metadata = $this->container
->metadataRetriever
->retrieve($url);
188 $metadata['title'] = $this->container
->conf
->get('general.default_note_title', t('Note: '));
192 'title' => $title ?? $metadata['title'] ?? '',
194 'description' => $description ?? $metadata['description'] ?? '',
195 'tags' => $tags ?? $metadata['tags'] ?? '',
196 'private' => $private,
201 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
202 $link = $formatter->format($bookmark);
203 $link['linkIsNew'] = false;
208 protected function buildFormData(array $link, bool $isNew, Request
$request): array
210 $tags = $this->container
->bookmarkService
->bookmarksCountPerTag();
211 if ($this->container
->conf
->get('formatter') === 'markdown') {
212 $tags[BookmarkMarkdownFormatter
::NO_MD_TAG
] = 1;
217 'link_is_new' => $isNew,
218 'http_referer' => $this->container
->environment
['HTTP_REFERER'] ?? '',
219 'source' => $request->getParam('source') ?? '',
221 'default_private_links' => $this->container
->conf
->get('privacy.default_private_links', false),
222 'async_metadata' => $this->container
->conf
->get('general.enable_async_metadata', true),
223 'retrieve_description' => $this->container
->conf
->get('general.retrieve_description', false),