aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2017-06-09 13:51:26 +0200
committerGitHub <noreply@github.com>2017-06-09 13:51:26 +0200
commita8993999893fb665b5e96597f9b649a2a8b940a2 (patch)
tree0c99b2f80a1279b17f44f2b9927f09e40a2001a2 /src/Wallabag
parent5c762616c378d20b96eda0f0727eb4c760d4119d (diff)
parent1f7018e1fe369b326150c388b56b8b8c26017234 (diff)
downloadwallabag-a8993999893fb665b5e96597f9b649a2a8b940a2.tar.gz
wallabag-a8993999893fb665b5e96597f9b649a2a8b940a2.tar.zst
wallabag-a8993999893fb665b5e96597f9b649a2a8b940a2.zip
Merge pull request #3192 from wallabag/validate-content-fields
Validate language & preview picture fields
Diffstat (limited to 'src/Wallabag')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php83
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml1
2 files changed, 73 insertions, 11 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index d5820e66..0c971863 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -7,6 +7,9 @@ use Psr\Log\LoggerInterface;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Tools\Utils; 8use Wallabag\CoreBundle\Tools\Utils;
9use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; 9use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
10use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint;
11use Symfony\Component\Validator\Constraints\Url as UrlConstraint;
12use Symfony\Component\Validator\Validator\ValidatorInterface;
10 13
11/** 14/**
12 * This kind of proxy class take care of getting the content from an url 15 * This kind of proxy class take care of getting the content from an url
@@ -16,15 +19,17 @@ class ContentProxy
16{ 19{
17 protected $graby; 20 protected $graby;
18 protected $tagger; 21 protected $tagger;
22 protected $validator;
19 protected $logger; 23 protected $logger;
20 protected $mimeGuesser; 24 protected $mimeGuesser;
21 protected $fetchingErrorMessage; 25 protected $fetchingErrorMessage;
22 protected $eventDispatcher; 26 protected $eventDispatcher;
23 27
24 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage) 28 public function __construct(Graby $graby, RuleBasedTagger $tagger, ValidatorInterface $validator, LoggerInterface $logger, $fetchingErrorMessage)
25 { 29 {
26 $this->graby = $graby; 30 $this->graby = $graby;
27 $this->tagger = $tagger; 31 $this->tagger = $tagger;
32 $this->validator = $validator;
28 $this->logger = $logger; 33 $this->logger = $logger;
29 $this->mimeGuesser = new MimeTypeExtensionGuesser(); 34 $this->mimeGuesser = new MimeTypeExtensionGuesser();
30 $this->fetchingErrorMessage = $fetchingErrorMessage; 35 $this->fetchingErrorMessage = $fetchingErrorMessage;
@@ -113,7 +118,24 @@ class ContentProxy
113 $entry->setHeaders($content['all_headers']); 118 $entry->setHeaders($content['all_headers']);
114 } 119 }
115 120
116 $entry->setLanguage(isset($content['language']) ? $content['language'] : ''); 121 $this->validateAndSetLanguage(
122 $entry,
123 isset($content['language']) ? $content['language'] : ''
124 );
125
126 $this->validateAndSetPreviewPicture(
127 $entry,
128 isset($content['open_graph']['og_image']) ? $content['open_graph']['og_image'] : ''
129 );
130
131 // if content is an image, define it as a preview too
132 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
133 $this->validateAndSetPreviewPicture(
134 $entry,
135 $content['url']
136 );
137 }
138
117 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); 139 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
118 $entry->setReadingTime(Utils::getReadingTime($html)); 140 $entry->setReadingTime(Utils::getReadingTime($html));
119 141
@@ -122,15 +144,6 @@ class ContentProxy
122 $entry->setDomainName($domainName); 144 $entry->setDomainName($domainName);
123 } 145 }
124 146
125 if (!empty($content['open_graph']['og_image'])) {
126 $entry->setPreviewPicture($content['open_graph']['og_image']);
127 }
128
129 // if content is an image define as a preview too
130 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
131 $entry->setPreviewPicture($content['url']);
132 }
133
134 try { 147 try {
135 $this->tagger->tag($entry); 148 $this->tagger->tag($entry);
136 } catch (\Exception $e) { 149 } catch (\Exception $e) {
@@ -152,4 +165,52 @@ class ContentProxy
152 { 165 {
153 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); 166 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
154 } 167 }
168
169 /**
170 * Use a Symfony validator to ensure the language is well formatted.
171 *
172 * @param Entry $entry
173 * @param string $value Language to validate
174 */
175 private function validateAndSetLanguage($entry, $value)
176 {
177 // some lang are defined as fr-FR, es-ES.
178 // replacing - by _ might increase language support
179 $value = str_replace('-', '_', $value);
180
181 $errors = $this->validator->validate(
182 $value,
183 (new LocaleConstraint())
184 );
185
186 if (0 === count($errors)) {
187 $entry->setLanguage($value);
188
189 return;
190 }
191
192 $this->logger->warning('Language validation failed. '.(string) $errors);
193 }
194
195 /**
196 * Use a Symfony validator to ensure the preview picture is a real url.
197 *
198 * @param Entry $entry
199 * @param string $value URL to validate
200 */
201 private function validateAndSetPreviewPicture($entry, $value)
202 {
203 $errors = $this->validator->validate(
204 $value,
205 (new UrlConstraint())
206 );
207
208 if (0 === count($errors)) {
209 $entry->setPreviewPicture($value);
210
211 return;
212 }
213
214 $this->logger->warning('PreviewPicture validation failed. '.(string) $errors);
215 }
155} 216}
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index a9b0d2d5..2ae5d27f 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -90,6 +90,7 @@ services:
90 arguments: 90 arguments:
91 - "@wallabag_core.graby" 91 - "@wallabag_core.graby"
92 - "@wallabag_core.rule_based_tagger" 92 - "@wallabag_core.rule_based_tagger"
93 - "@validator"
93 - "@logger" 94 - "@logger"
94 - '%wallabag_core.fetching_error_message%' 95 - '%wallabag_core.fetching_error_message%'
95 96