diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-05-11 08:14:29 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-05-31 13:59:45 +0200 |
commit | e668a8124c46d47add4248963d77f3b29b37b3ce (patch) | |
tree | b9b56d19b06fe268d025c3591119470162efc99a /src | |
parent | 4423b88c5b2c2d530b0a83a822f521a61ca4d4b8 (diff) | |
download | wallabag-e668a8124c46d47add4248963d77f3b29b37b3ce.tar.gz wallabag-e668a8124c46d47add4248963d77f3b29b37b3ce.tar.zst wallabag-e668a8124c46d47add4248963d77f3b29b37b3ce.zip |
Allow other fields to be send using API
Entry API can now have these new fields:
- content
- language
- preview_picture
- published_at
Re-use the ContentProxy to be able to do the same using the web UI (in the future).
htmLawed is used to clean stuff from content, I hope it’ll be enough to avoid security breach.
Lower content validation when we want to update an entry with content already defined. Before, language & content_type were required. If there weren’t provided, we re-fetched the content using graby. I think these fields aren’t required for an entry to be created. So I removed them.
Which means some import from the v1 export won’t be re-fetched since they provide content, url & title.
Also, remove liberation link from Readability import to avoid overlaping import (from wallabag v1, which had the same link)
Diffstat (limited to 'src')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 46 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 30 |
2 files changed, 54 insertions, 22 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 31bb67fd..dfd04fb4 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -280,6 +280,10 @@ class EntryRestController extends WallabagRestController | |||
280 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | 280 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, |
281 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, | 281 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, |
282 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, | 282 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, |
283 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, | ||
284 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | ||
285 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | ||
286 | * {"name"="published_at", "dataType"="datetime", "format"="YYYY-MM-DDTHH:II:SS+TZ", "required"=false, "description"="Published date of the entry"}, | ||
283 | * } | 287 | * } |
284 | * ) | 288 | * ) |
285 | * | 289 | * |
@@ -293,30 +297,42 @@ class EntryRestController extends WallabagRestController | |||
293 | $title = $request->request->get('title'); | 297 | $title = $request->request->get('title'); |
294 | $isArchived = $request->request->get('archive'); | 298 | $isArchived = $request->request->get('archive'); |
295 | $isStarred = $request->request->get('starred'); | 299 | $isStarred = $request->request->get('starred'); |
300 | $content = $request->request->get('content'); | ||
301 | $language = $request->request->get('language'); | ||
302 | $picture = $request->request->get('preview_picture'); | ||
303 | $publishedAt = $request->request->get('published_at'); | ||
296 | 304 | ||
297 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); | 305 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); |
298 | 306 | ||
299 | if (false === $entry) { | 307 | if (false === $entry) { |
300 | $entry = new Entry($this->getUser()); | 308 | $entry = new Entry($this->getUser()); |
301 | try { | ||
302 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | ||
303 | $entry, | ||
304 | $url | ||
305 | ); | ||
306 | } catch (\Exception $e) { | ||
307 | $this->get('logger')->error('Error while saving an entry', [ | ||
308 | 'exception' => $e, | ||
309 | 'entry' => $entry, | ||
310 | ]); | ||
311 | $entry->setUrl($url); | ||
312 | } | ||
313 | } | 309 | } |
314 | 310 | ||
315 | if (!is_null($title)) { | 311 | try { |
316 | $entry->setTitle($title); | 312 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( |
313 | $entry, | ||
314 | $url, | ||
315 | [ | ||
316 | 'title' => $title, | ||
317 | 'html' => $content, | ||
318 | 'url' => $url, | ||
319 | 'language' => $language, | ||
320 | 'date' => $publishedAt, | ||
321 | // faking the preview picture | ||
322 | 'open_graph' => [ | ||
323 | 'og_image' => $picture, | ||
324 | ], | ||
325 | ] | ||
326 | ); | ||
327 | } catch (\Exception $e) { | ||
328 | $this->get('logger')->error('Error while saving an entry', [ | ||
329 | 'exception' => $e, | ||
330 | 'entry' => $entry, | ||
331 | ]); | ||
332 | $entry->setUrl($url); | ||
317 | } | 333 | } |
318 | 334 | ||
319 | $tags = $request->request->get('tags', ''); | 335 | $tags = $request->request->get('tags', []); |
320 | if (!empty($tags)) { | 336 | if (!empty($tags)) { |
321 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | 337 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
322 | } | 338 | } |
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index 4b3e6fbb..e06ad3d6 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php | |||
@@ -45,6 +45,18 @@ class ContentProxy | |||
45 | */ | 45 | */ |
46 | public function updateEntry(Entry $entry, $url, array $content = []) | 46 | public function updateEntry(Entry $entry, $url, array $content = []) |
47 | { | 47 | { |
48 | // ensure content is a bit cleaned up | ||
49 | if (!empty($content['html'])) { | ||
50 | $content['html'] = htmLawed($content['html'], [ | ||
51 | 'safe' => 1, | ||
52 | // which means: do not remove iframe elements | ||
53 | 'elements' => '*+iframe', | ||
54 | 'deny_attribute' => 'style', | ||
55 | 'comment' => 1, | ||
56 | 'cdata' => 1, | ||
57 | ]); | ||
58 | } | ||
59 | |||
48 | // do we have to fetch the content or the provided one is ok? | 60 | // do we have to fetch the content or the provided one is ok? |
49 | if (empty($content) || false === $this->validateContent($content)) { | 61 | if (empty($content) || false === $this->validateContent($content)) { |
50 | $fetchedContent = $this->graby->fetchContent($url); | 62 | $fetchedContent = $this->graby->fetchContent($url); |
@@ -57,7 +69,7 @@ class ContentProxy | |||
57 | } | 69 | } |
58 | 70 | ||
59 | $title = $content['title']; | 71 | $title = $content['title']; |
60 | if (!$title && isset($content['open_graph']['og_title'])) { | 72 | if (!$title && !empty($content['open_graph']['og_title'])) { |
61 | $title = $content['open_graph']['og_title']; | 73 | $title = $content['open_graph']['og_title']; |
62 | } | 74 | } |
63 | 75 | ||
@@ -65,7 +77,7 @@ class ContentProxy | |||
65 | if (false === $html) { | 77 | if (false === $html) { |
66 | $html = $this->fetchingErrorMessage; | 78 | $html = $this->fetchingErrorMessage; |
67 | 79 | ||
68 | if (isset($content['open_graph']['og_description'])) { | 80 | if (!empty($content['open_graph']['og_description'])) { |
69 | $html .= '<p><i>But we found a short description: </i></p>'; | 81 | $html .= '<p><i>But we found a short description: </i></p>'; |
70 | $html .= $content['open_graph']['og_description']; | 82 | $html .= $content['open_graph']['og_description']; |
71 | } | 83 | } |
@@ -76,8 +88,12 @@ class ContentProxy | |||
76 | $entry->setContent($html); | 88 | $entry->setContent($html); |
77 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); | 89 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); |
78 | 90 | ||
79 | if (isset($content['date']) && null !== $content['date'] && '' !== $content['date']) { | 91 | if (!empty($content['date'])) { |
80 | $entry->setPublishedAt(new \DateTime($content['date'])); | 92 | try { |
93 | $entry->setPublishedAt(new \DateTime($content['date'])); | ||
94 | } catch (\Exception $e) { | ||
95 | $this->logger->warn('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]); | ||
96 | } | ||
81 | } | 97 | } |
82 | 98 | ||
83 | if (!empty($content['authors'])) { | 99 | if (!empty($content['authors'])) { |
@@ -97,12 +113,12 @@ class ContentProxy | |||
97 | $entry->setDomainName($domainName); | 113 | $entry->setDomainName($domainName); |
98 | } | 114 | } |
99 | 115 | ||
100 | if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) { | 116 | if (!empty($content['open_graph']['og_image'])) { |
101 | $entry->setPreviewPicture($content['open_graph']['og_image']); | 117 | $entry->setPreviewPicture($content['open_graph']['og_image']); |
102 | } | 118 | } |
103 | 119 | ||
104 | // if content is an image define as a preview too | 120 | // if content is an image define as a preview too |
105 | if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { | 121 | if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { |
106 | $entry->setPreviewPicture($content['url']); | 122 | $entry->setPreviewPicture($content['url']); |
107 | } | 123 | } |
108 | 124 | ||
@@ -128,6 +144,6 @@ class ContentProxy | |||
128 | */ | 144 | */ |
129 | private function validateContent(array $content) | 145 | private function validateContent(array $content) |
130 | { | 146 | { |
131 | return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']); | 147 | return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); |
132 | } | 148 | } |
133 | } | 149 | } |