]>
Commit | Line | Data |
---|---|---|
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; | |
9 | use Shaarli\Formatter\BookmarkMarkdownFormatter; | |
10 | use Shaarli\Render\TemplatePage; | |
11 | use Shaarli\Thumbnailer; | |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
15 | class ShaarePublishController extends ShaarliAdminController | |
16 | { | |
17 | /** | |
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. | |
20 | */ | |
21 | public function displayCreateForm(Request $request, Response $response): Response | |
22 | { | |
23 | $url = cleanup_url($request->getParam('post')); | |
24 | $link = $this->buildLinkDataFromUrl($request, $url); | |
25 | ||
26 | return $this->displayForm($link, $link['linkIsNew'], $request, $response); | |
27 | } | |
28 | ||
29 | /** | |
30 | * POST /admin/shaare-batch - Displays multiple creation/edit forms from bulk add in add-link page. | |
31 | */ | |
32 | public function displayCreateBatchForms(Request $request, Response $response): Response | |
33 | { | |
34 | $urls = array_map('cleanup_url', explode(PHP_EOL, $request->getParam('urls'))); | |
35 | ||
36 | $links = []; | |
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'; | |
42 | ||
43 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); | |
44 | ||
45 | $links[] = $data; | |
46 | } | |
47 | ||
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)); | |
51 | ||
52 | return $response->write($this->render(TemplatePage::EDIT_LINK_BATCH)); | |
53 | } | |
54 | ||
55 | /** | |
56 | * GET /admin/shaare/{id} - Displays the bookmark form in edition mode. | |
57 | */ | |
58 | public function displayEditForm(Request $request, Response $response, array $args): Response | |
59 | { | |
60 | $id = $args['id'] ?? ''; | |
61 | try { | |
62 | if (false === ctype_digit($id)) { | |
63 | throw new BookmarkNotFoundException(); | |
64 | } | |
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.'), | |
69 | $id | |
70 | )); | |
71 | ||
72 | return $this->redirect($response, '/'); | |
73 | } | |
74 | ||
75 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
76 | $link = $formatter->format($bookmark); | |
77 | ||
78 | return $this->displayForm($link, false, $request, $response); | |
79 | } | |
80 | ||
81 | /** | |
82 | * POST /admin/shaare | |
83 | */ | |
84 | public function save(Request $request, Response $response): Response | |
85 | { | |
86 | $this->checkToken($request); | |
87 | ||
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)) { | |
91 | // Edit | |
92 | $bookmark = $this->container->bookmarkService->get($id); | |
93 | } else { | |
94 | // New link | |
95 | $bookmark = new Bookmark(); | |
96 | } | |
97 | ||
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')); | |
103 | ||
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() | |
107 | ) { | |
108 | $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl())); | |
109 | } | |
110 | $this->container->bookmarkService->addOrSet($bookmark, false); | |
111 | ||
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); | |
116 | ||
117 | $bookmark->fromArray($data); | |
118 | $this->container->bookmarkService->set($bookmark); | |
119 | ||
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') { | |
124 | return $response; | |
125 | } | |
126 | ||
127 | if (!empty($request->getParam('returnurl'))) { | |
128 | $this->container->environment['HTTP_REFERER'] = escape($request->getParam('returnurl')); | |
129 | } | |
130 | ||
131 | return $this->redirectFromReferer( | |
132 | $request, | |
133 | $response, | |
134 | ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'], | |
135 | $bookmark->getShortUrl() | |
136 | ); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Helper function used to display the shaare form whether it's a new or existing bookmark. | |
141 | * | |
142 | * @param array $link data used in template, either from parameters or from the data store | |
143 | */ | |
144 | protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response | |
145 | { | |
146 | $data = $this->buildFormData($link, $isNew, $request); | |
147 | ||
148 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); | |
149 | ||
150 | foreach ($data as $key => $value) { | |
151 | $this->assignView($key, $value); | |
152 | } | |
153 | ||
154 | $editLabel = false === $isNew ? t('Edit') .' ' : ''; | |
155 | $this->assignView( | |
156 | 'pagetitle', | |
157 | $editLabel . t('Shaare') .' - '. $this->container->conf->get('general.title', 'Shaarli') | |
158 | ); | |
159 | ||
160 | return $response->write($this->render(TemplatePage::EDIT_LINK)); | |
161 | } | |
162 | ||
163 | protected function buildLinkDataFromUrl(Request $request, string $url): array | |
164 | { | |
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); | |
174 | } else { | |
175 | $private = $this->container->conf->get('privacy.default_private_links', false); | |
176 | } | |
177 | ||
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) | |
181 | && empty($title) | |
182 | && strpos(get_url_scheme($url) ?: '', 'http') !== false | |
183 | ) { | |
184 | $metadata = $this->container->metadataRetriever->retrieve($url); | |
185 | } | |
186 | ||
187 | if (empty($url)) { | |
188 | $metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: ')); | |
189 | } | |
190 | ||
191 | return [ | |
192 | 'title' => $title ?? $metadata['title'] ?? '', | |
193 | 'url' => $url ?? '', | |
194 | 'description' => $description ?? $metadata['description'] ?? '', | |
195 | 'tags' => $tags ?? $metadata['tags'] ?? '', | |
196 | 'private' => $private, | |
197 | 'linkIsNew' => true, | |
198 | ]; | |
199 | } | |
200 | ||
201 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
202 | $link = $formatter->format($bookmark); | |
203 | $link['linkIsNew'] = false; | |
204 | ||
205 | return $link; | |
206 | } | |
207 | ||
208 | protected function buildFormData(array $link, bool $isNew, Request $request): array | |
209 | { | |
210 | $tags = $this->container->bookmarkService->bookmarksCountPerTag(); | |
211 | if ($this->container->conf->get('formatter') === 'markdown') { | |
212 | $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; | |
213 | } | |
214 | ||
215 | return escape([ | |
216 | 'link' => $link, | |
217 | 'link_is_new' => $isNew, | |
218 | 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '', | |
219 | 'source' => $request->getParam('source') ?? '', | |
220 | 'tags' => $tags, | |
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), | |
224 | ]); | |
225 | } | |
226 | } |