]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
6 | use Psr\Log\LoggerInterface as Logger; | |
7 | use Wallabag\CoreBundle\Entity\Entry; | |
8 | use Wallabag\CoreBundle\Entity\Tag; | |
9 | use Wallabag\CoreBundle\Tools\Utils; | |
10 | use Wallabag\CoreBundle\Repository\TagRepository; | |
11 | ||
12 | /** | |
13 | * This kind of proxy class take care of getting the content from an url | |
14 | * and update the entry with what it found. | |
15 | */ | |
16 | class ContentProxy | |
17 | { | |
18 | protected $graby; | |
19 | protected $tagger; | |
20 | protected $logger; | |
21 | protected $tagRepository; | |
22 | ||
23 | public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger) | |
24 | { | |
25 | $this->graby = $graby; | |
26 | $this->tagger = $tagger; | |
27 | $this->logger = $logger; | |
28 | $this->tagRepository = $tagRepository; | |
29 | } | |
30 | ||
31 | /** | |
32 | * Fetch content using graby and hydrate given entry with results information. | |
33 | * In case we couldn't find content, we'll try to use Open Graph data. | |
34 | * | |
35 | * We can also force the content, in case of an import from the v1 for example, so the function won't | |
36 | * fetch the content from the website but rather use information given with the $content parameter. | |
37 | * | |
38 | * @param Entry $entry Entry to update | |
39 | * @param string $url Url to grab content for | |
40 | * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url | |
41 | * | |
42 | * @return Entry | |
43 | */ | |
44 | public function updateEntry(Entry $entry, $url, array $content = []) | |
45 | { | |
46 | // do we have to fetch the content or the provided one is ok? | |
47 | if (empty($content) || false === $this->validateContent($content)) { | |
48 | $content = $this->graby->fetchContent($url); | |
49 | } | |
50 | ||
51 | $title = $content['title']; | |
52 | if (!$title && isset($content['open_graph']['og_title'])) { | |
53 | $title = $content['open_graph']['og_title']; | |
54 | } | |
55 | ||
56 | $html = $content['html']; | |
57 | if (false === $html) { | |
58 | $html = '<p>Unable to retrieve readable content.</p>'; | |
59 | ||
60 | if (isset($content['open_graph']['og_description'])) { | |
61 | $html .= '<p><i>But we found a short description: </i></p>'; | |
62 | $html .= $content['open_graph']['og_description']; | |
63 | } | |
64 | } | |
65 | ||
66 | $entry->setUrl($content['url'] ?: $url); | |
67 | $entry->setTitle($title); | |
68 | $entry->setContent($html); | |
69 | $entry->setLanguage($content['language']); | |
70 | $entry->setMimetype($content['content_type']); | |
71 | $entry->setReadingTime(Utils::getReadingTime($html)); | |
72 | ||
73 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | |
74 | if (false !== $domainName) { | |
75 | $entry->setDomainName($domainName); | |
76 | } | |
77 | ||
78 | if (isset($content['open_graph']['og_image'])) { | |
79 | $entry->setPreviewPicture($content['open_graph']['og_image']); | |
80 | } | |
81 | ||
82 | try { | |
83 | $this->tagger->tag($entry); | |
84 | } catch (\Exception $e) { | |
85 | $this->logger->error('Error while trying to automatically tag an entry.', [ | |
86 | 'entry_url' => $url, | |
87 | 'error_msg' => $e->getMessage(), | |
88 | ]); | |
89 | } | |
90 | ||
91 | return $entry; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Assign some tags to an entry. | |
96 | * | |
97 | * @param Entry $entry | |
98 | * @param array|string $tags An array of tag or a string coma separated of tag | |
99 | * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed | |
100 | * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101 | |
101 | */ | |
102 | public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = []) | |
103 | { | |
104 | if (!is_array($tags)) { | |
105 | $tags = explode(',', $tags); | |
106 | } | |
107 | ||
108 | // keeps only Tag entity from the "not yet flushed entities" | |
109 | $tagsNotYetFlushed = []; | |
110 | foreach ($entitiesReady as $entity) { | |
111 | if ($entity instanceof Tag) { | |
112 | $tagsNotYetFlushed[$entity->getLabel()] = $entity; | |
113 | } | |
114 | } | |
115 | ||
116 | foreach ($tags as $label) { | |
117 | $label = trim($label); | |
118 | ||
119 | // avoid empty tag | |
120 | if (0 === strlen($label)) { | |
121 | continue; | |
122 | } | |
123 | ||
124 | if (isset($tagsNotYetFlushed[$label])) { | |
125 | $tagEntity = $tagsNotYetFlushed[$label]; | |
126 | } else { | |
127 | $tagEntity = $this->tagRepository->findOneByLabel($label); | |
128 | ||
129 | if (is_null($tagEntity)) { | |
130 | $tagEntity = new Tag(); | |
131 | $tagEntity->setLabel($label); | |
132 | } | |
133 | } | |
134 | ||
135 | // only add the tag on the entry if the relation doesn't exist | |
136 | if (false === $entry->getTags()->contains($tagEntity)) { | |
137 | $entry->addTag($tagEntity); | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | /** | |
143 | * Validate that the given content as enough value to be used | |
144 | * instead of fetch the content from the url. | |
145 | * | |
146 | * @param array $content | |
147 | * | |
148 | * @return bool true if valid otherwise false | |
149 | */ | |
150 | private function validateContent(array $content) | |
151 | { | |
152 | return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Changing pictures URL in article content. | |
157 | */ | |
158 | public static function filterPicture($content, $url, $id) | |
159 | { | |
160 | $matches = array(); | |
161 | $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice | |
162 | preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER); | |
163 | foreach ($matches as $i => $link) { | |
164 | $link[1] = trim($link[1]); | |
165 | if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) { | |
166 | $absolute_path = self::_getAbsoluteLink($link[2], $url); | |
167 | $filename = basename(parse_url($absolute_path, PHP_URL_PATH)); | |
168 | $directory = self::_createAssetsDirectory($id); | |
169 | $fullpath = $directory.'/'.$filename; | |
170 | ||
171 | if (in_array($absolute_path, $processing_pictures) === true) { | |
172 | // replace picture's URL only if processing is OK : already processing -> go to next picture | |
173 | continue; | |
174 | } | |
175 | ||
176 | if (self::_downloadPictures($absolute_path, $fullpath) === true) { | |
177 | $content = str_replace($matches[$i][2], Tools::getPocheUrl().$fullpath, $content); | |
178 | } | |
179 | ||
180 | $processing_pictures[] = $absolute_path; | |
181 | } | |
182 | } | |
183 | ||
184 | return $content; | |
185 | } | |
186 | ||
187 | /** | |
188 | * Get absolute URL. | |
189 | */ | |
190 | private static function _getAbsoluteLink($relativeLink, $url) | |
191 | { | |
192 | /* return if already absolute URL */ | |
193 | if (parse_url($relativeLink, PHP_URL_SCHEME) != '') { | |
194 | return $relativeLink; | |
195 | } | |
196 | ||
197 | /* queries and anchors */ | |
198 | if ($relativeLink[0] == '#' || $relativeLink[0] == '?') { | |
199 | return $url.$relativeLink; | |
200 | } | |
201 | ||
202 | /* parse base URL and convert to local variables: | |
203 | $scheme, $host, $path */ | |
204 | extract(parse_url($url)); | |
205 | ||
206 | /* remove non-directory element from path */ | |
207 | $path = preg_replace('#/[^/]*$#', '', $path); | |
208 | ||
209 | /* destroy path if relative url points to root */ | |
210 | if ($relativeLink[0] == '/') { | |
211 | $path = ''; | |
212 | } | |
213 | ||
214 | /* dirty absolute URL */ | |
215 | $abs = $host.$path.'/'.$relativeLink; | |
216 | ||
217 | /* replace '//' or '/./' or '/foo/../' with '/' */ | |
218 | $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); | |
219 | for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) { | |
220 | } | |
221 | ||
222 | /* absolute URL is ready! */ | |
223 | return $scheme.'://'.$abs; | |
224 | } | |
225 | ||
226 | /** | |
227 | * Downloading pictures. | |
228 | * | |
229 | * @return bool true if the download and processing is OK, false else | |
230 | */ | |
231 | private static function _downloadPictures($absolute_path, $fullpath) | |
232 | { | |
233 | $rawdata = Tools::getFile($absolute_path); | |
234 | $fullpath = urldecode($fullpath); | |
235 | ||
236 | if (file_exists($fullpath)) { | |
237 | unlink($fullpath); | |
238 | } | |
239 | ||
240 | // check extension | |
241 | $file_ext = strrchr($fullpath, '.'); | |
242 | $whitelist = array('.jpg', '.jpeg', '.gif', '.png'); | |
243 | if (!(in_array($file_ext, $whitelist))) { | |
244 | Tools::logm('processed image with not allowed extension. Skipping '.$fullpath); | |
245 | ||
246 | return false; | |
247 | } | |
248 | ||
249 | // check headers | |
250 | $imageinfo = getimagesize($absolute_path); | |
251 | if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/jpg' && $imageinfo['mime'] != 'image/png') { | |
252 | Tools::logm('processed image with bad header. Skipping '.$fullpath); | |
253 | ||
254 | return false; | |
255 | } | |
256 | ||
257 | // regenerate image | |
258 | $im = imagecreatefromstring($rawdata); | |
259 | if ($im === false) { | |
260 | Tools::logm('error while regenerating image '.$fullpath); | |
261 | ||
262 | return false; | |
263 | } | |
264 | ||
265 | switch ($imageinfo['mime']) { | |
266 | case 'image/gif': | |
267 | $result = imagegif($im, $fullpath); | |
268 | break; | |
269 | case 'image/jpeg': | |
270 | case 'image/jpg': | |
271 | $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY); | |
272 | break; | |
273 | case 'image/png': | |
274 | $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY / 100 * 9)); | |
275 | break; | |
276 | } | |
277 | imagedestroy($im); | |
278 | ||
279 | return $result; | |
280 | } | |
281 | ||
282 | /** | |
283 | * Create a directory for an article. | |
284 | * | |
285 | * @param $id ID of the article | |
286 | * | |
287 | * @return string | |
288 | */ | |
289 | private static function _createAssetsDirectory($id) | |
290 | { | |
291 | $assets_path = ABS_PATH; | |
292 | if (!is_dir($assets_path)) { | |
293 | mkdir($assets_path, 0715); | |
294 | } | |
295 | ||
296 | $article_directory = $assets_path.$id; | |
297 | if (!is_dir($article_directory)) { | |
298 | mkdir($article_directory, 0715); | |
299 | } | |
300 | ||
301 | return $article_directory; | |
302 | } | |
303 | ||
304 | /** | |
305 | * Remove the directory. | |
306 | * | |
307 | * @param $directory | |
308 | * | |
309 | * @return bool | |
310 | */ | |
311 | public static function removeDirectory($directory) | |
312 | { | |
313 | if (is_dir($directory)) { | |
314 | $files = array_diff(scandir($directory), array('.', '..')); | |
315 | foreach ($files as $file) { | |
316 | (is_dir("$directory/$file")) ? self::removeDirectory("$directory/$file") : unlink("$directory/$file"); | |
317 | } | |
318 | ||
319 | return rmdir($directory); | |
320 | } | |
321 | } | |
322 | } |