]>
Commit | Line | Data |
---|---|---|
5d8de758 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
7 | use Shaarli\Bookmark\Bookmark; | |
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
c609944c | 9 | use Shaarli\Formatter\BookmarkFormatter; |
5d8de758 A |
10 | use Shaarli\Formatter\BookmarkMarkdownFormatter; |
11 | use Shaarli\Render\TemplatePage; | |
12 | use Shaarli\Thumbnailer; | |
13 | use Slim\Http\Request; | |
14 | use Slim\Http\Response; | |
15 | ||
16 | class ShaarePublishController extends ShaarliAdminController | |
17 | { | |
c609944c A |
18 | /** |
19 | * @var BookmarkFormatter[] Statically cached instances of formatters | |
20 | */ | |
21 | protected $formatters = []; | |
22 | ||
23 | /** | |
24 | * @var array Statically cached bookmark's tags counts | |
25 | */ | |
26 | protected $tags; | |
27 | ||
5d8de758 A |
28 | /** |
29 | * GET /admin/shaare - Displays the bookmark form for creation. | |
30 | * Note that if the URL is found in existing bookmarks, then it will be in edit mode. | |
31 | */ | |
32 | public function displayCreateForm(Request $request, Response $response): Response | |
33 | { | |
34 | $url = cleanup_url($request->getParam('post')); | |
35 | $link = $this->buildLinkDataFromUrl($request, $url); | |
36 | ||
37 | return $this->displayForm($link, $link['linkIsNew'], $request, $response); | |
38 | } | |
39 | ||
40 | /** | |
41 | * POST /admin/shaare-batch - Displays multiple creation/edit forms from bulk add in add-link page. | |
42 | */ | |
43 | public function displayCreateBatchForms(Request $request, Response $response): Response | |
44 | { | |
45 | $urls = array_map('cleanup_url', explode(PHP_EOL, $request->getParam('urls'))); | |
46 | ||
47 | $links = []; | |
48 | foreach ($urls as $url) { | |
34c8f558 A |
49 | if (empty($url)) { |
50 | continue; | |
51 | } | |
5d8de758 A |
52 | $link = $this->buildLinkDataFromUrl($request, $url); |
53 | $data = $this->buildFormData($link, $link['linkIsNew'], $request); | |
54 | $data['token'] = $this->container->sessionManager->generateToken(); | |
55 | $data['source'] = 'batch'; | |
56 | ||
57 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); | |
58 | ||
59 | $links[] = $data; | |
60 | } | |
61 | ||
62 | $this->assignView('links', $links); | |
63 | $this->assignView('batch_mode', true); | |
64 | $this->assignView('async_metadata', $this->container->conf->get('general.enable_async_metadata', true)); | |
65 | ||
66 | return $response->write($this->render(TemplatePage::EDIT_LINK_BATCH)); | |
67 | } | |
68 | ||
69 | /** | |
70 | * GET /admin/shaare/{id} - Displays the bookmark form in edition mode. | |
71 | */ | |
72 | public function displayEditForm(Request $request, Response $response, array $args): Response | |
73 | { | |
74 | $id = $args['id'] ?? ''; | |
75 | try { | |
76 | if (false === ctype_digit($id)) { | |
77 | throw new BookmarkNotFoundException(); | |
78 | } | |
79 | $bookmark = $this->container->bookmarkService->get((int) $id); // Read database | |
80 | } catch (BookmarkNotFoundException $e) { | |
81 | $this->saveErrorMessage(sprintf( | |
82 | t('Bookmark with identifier %s could not be found.'), | |
83 | $id | |
84 | )); | |
85 | ||
86 | return $this->redirect($response, '/'); | |
87 | } | |
88 | ||
c609944c | 89 | $formatter = $this->getFormatter('raw'); |
5d8de758 A |
90 | $link = $formatter->format($bookmark); |
91 | ||
92 | return $this->displayForm($link, false, $request, $response); | |
93 | } | |
94 | ||
95 | /** | |
96 | * POST /admin/shaare | |
97 | */ | |
98 | public function save(Request $request, Response $response): Response | |
99 | { | |
100 | $this->checkToken($request); | |
101 | ||
102 | // lf_id should only be present if the link exists. | |
103 | $id = $request->getParam('lf_id') !== null ? intval(escape($request->getParam('lf_id'))) : null; | |
104 | if (null !== $id && true === $this->container->bookmarkService->exists($id)) { | |
105 | // Edit | |
106 | $bookmark = $this->container->bookmarkService->get($id); | |
107 | } else { | |
108 | // New link | |
109 | $bookmark = new Bookmark(); | |
110 | } | |
111 | ||
112 | $bookmark->setTitle($request->getParam('lf_title')); | |
113 | $bookmark->setDescription($request->getParam('lf_description')); | |
114 | $bookmark->setUrl($request->getParam('lf_url'), $this->container->conf->get('security.allowed_protocols', [])); | |
115 | $bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN)); | |
b3bd8c3e A |
116 | $bookmark->setTagsString( |
117 | $request->getParam('lf_tags'), | |
118 | $this->container->conf->get('general.tags_separator', ' ') | |
119 | ); | |
5d8de758 | 120 | |
53054b2b A |
121 | if ( |
122 | $this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | |
5d8de758 A |
123 | && true !== $this->container->conf->get('general.enable_async_metadata', true) |
124 | && $bookmark->shouldUpdateThumbnail() | |
125 | ) { | |
126 | $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl())); | |
127 | } | |
128 | $this->container->bookmarkService->addOrSet($bookmark, false); | |
129 | ||
130 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | |
c609944c | 131 | $formatter = $this->getFormatter('raw'); |
5d8de758 A |
132 | $data = $formatter->format($bookmark); |
133 | $this->executePageHooks('save_link', $data); | |
134 | ||
b3bd8c3e | 135 | $bookmark->fromArray($data, $this->container->conf->get('general.tags_separator', ' ')); |
5d8de758 A |
136 | $this->container->bookmarkService->set($bookmark); |
137 | ||
138 | // If we are called from the bookmarklet, we must close the popup: | |
139 | if ($request->getParam('source') === 'bookmarklet') { | |
140 | return $response->write('<script>self.close();</script>'); | |
141 | } elseif ($request->getParam('source') === 'batch') { | |
142 | return $response; | |
143 | } | |
144 | ||
145 | if (!empty($request->getParam('returnurl'))) { | |
114a43b2 | 146 | $this->container->environment['HTTP_REFERER'] = $request->getParam('returnurl'); |
5d8de758 A |
147 | } |
148 | ||
149 | return $this->redirectFromReferer( | |
150 | $request, | |
151 | $response, | |
53054b2b A |
152 | ['/admin/add-shaare', '/admin/shaare'], |
153 | ['addlink', 'post', 'edit_link'], | |
5d8de758 A |
154 | $bookmark->getShortUrl() |
155 | ); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Helper function used to display the shaare form whether it's a new or existing bookmark. | |
160 | * | |
161 | * @param array $link data used in template, either from parameters or from the data store | |
162 | */ | |
163 | protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response | |
164 | { | |
165 | $data = $this->buildFormData($link, $isNew, $request); | |
166 | ||
167 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); | |
168 | ||
169 | foreach ($data as $key => $value) { | |
170 | $this->assignView($key, $value); | |
171 | } | |
172 | ||
53054b2b | 173 | $editLabel = false === $isNew ? t('Edit') . ' ' : ''; |
5d8de758 A |
174 | $this->assignView( |
175 | 'pagetitle', | |
53054b2b | 176 | $editLabel . t('Shaare') . ' - ' . $this->container->conf->get('general.title', 'Shaarli') |
5d8de758 A |
177 | ); |
178 | ||
179 | return $response->write($this->render(TemplatePage::EDIT_LINK)); | |
180 | } | |
181 | ||
182 | protected function buildLinkDataFromUrl(Request $request, string $url): array | |
183 | { | |
184 | // Check if URL is not already in database (in this case, we will edit the existing link) | |
185 | $bookmark = $this->container->bookmarkService->findByUrl($url); | |
186 | if (null === $bookmark) { | |
187 | // Get shaare data if it was provided in URL (e.g.: by the bookmarklet). | |
188 | $title = $request->getParam('title'); | |
189 | $description = $request->getParam('description'); | |
190 | $tags = $request->getParam('tags'); | |
25e90d8d A |
191 | if ($request->getParam('private') !== null) { |
192 | $private = filter_var($request->getParam('private'), FILTER_VALIDATE_BOOLEAN); | |
193 | } else { | |
194 | $private = $this->container->conf->get('privacy.default_private_links', false); | |
195 | } | |
5d8de758 A |
196 | |
197 | // If this is an HTTP(S) link, we try go get the page to extract | |
198 | // the title (otherwise we will to straight to the edit form.) | |
53054b2b A |
199 | if ( |
200 | true !== $this->container->conf->get('general.enable_async_metadata', true) | |
5d8de758 A |
201 | && empty($title) |
202 | && strpos(get_url_scheme($url) ?: '', 'http') !== false | |
203 | ) { | |
204 | $metadata = $this->container->metadataRetriever->retrieve($url); | |
205 | } | |
206 | ||
207 | if (empty($url)) { | |
208 | $metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: ')); | |
209 | } | |
210 | ||
211 | return [ | |
212 | 'title' => $title ?? $metadata['title'] ?? '', | |
213 | 'url' => $url ?? '', | |
214 | 'description' => $description ?? $metadata['description'] ?? '', | |
215 | 'tags' => $tags ?? $metadata['tags'] ?? '', | |
216 | 'private' => $private, | |
217 | 'linkIsNew' => true, | |
218 | ]; | |
219 | } | |
220 | ||
c609944c | 221 | $formatter = $this->getFormatter('raw'); |
5d8de758 A |
222 | $link = $formatter->format($bookmark); |
223 | $link['linkIsNew'] = false; | |
224 | ||
225 | return $link; | |
226 | } | |
227 | ||
228 | protected function buildFormData(array $link, bool $isNew, Request $request): array | |
229 | { | |
6a3a78d0 | 230 | $link['tags'] = $link['tags'] !== null && strlen($link['tags']) > 0 |
b3bd8c3e A |
231 | ? $link['tags'] . $this->container->conf->get('general.tags_separator', ' ') |
232 | : $link['tags'] | |
233 | ; | |
234 | ||
5d8de758 A |
235 | return escape([ |
236 | 'link' => $link, | |
237 | 'link_is_new' => $isNew, | |
238 | 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '', | |
239 | 'source' => $request->getParam('source') ?? '', | |
c609944c | 240 | 'tags' => $this->getTags(), |
5d8de758 A |
241 | 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), |
242 | 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true), | |
243 | 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false), | |
244 | ]); | |
245 | } | |
c609944c A |
246 | |
247 | /** | |
248 | * Memoize formatterFactory->getFormatter() calls. | |
249 | */ | |
250 | protected function getFormatter(string $type): BookmarkFormatter | |
251 | { | |
252 | if (!array_key_exists($type, $this->formatters) || $this->formatters[$type] === null) { | |
253 | $this->formatters[$type] = $this->container->formatterFactory->getFormatter($type); | |
254 | } | |
255 | ||
256 | return $this->formatters[$type]; | |
257 | } | |
258 | ||
259 | /** | |
260 | * Memoize bookmarkService->bookmarksCountPerTag() calls. | |
261 | */ | |
262 | protected function getTags(): array | |
263 | { | |
264 | if ($this->tags === null) { | |
265 | $this->tags = $this->container->bookmarkService->bookmarksCountPerTag(); | |
266 | ||
267 | if ($this->container->conf->get('formatter') === 'markdown') { | |
268 | $this->tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; | |
269 | } | |
270 | } | |
271 | ||
272 | return $this->tags; | |
273 | } | |
5d8de758 | 274 | } |