]>
Commit | Line | Data |
---|---|---|
419214d7 TC |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
7f559418 | 5 | use GuzzleHttp\Client; |
577c0b6d | 6 | use GuzzleHttp\Message\Response; |
f808b016 JB |
7 | use Psr\Log\LoggerInterface; |
8 | use Symfony\Component\DomCrawler\Crawler; | |
e0597476 | 9 | use Symfony\Component\Finder\Finder; |
f808b016 | 10 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; |
419214d7 | 11 | |
156bf627 JB |
12 | class DownloadImages |
13 | { | |
7f559418 JB |
14 | const REGENERATE_PICTURES_QUALITY = 80; |
15 | ||
16 | private $client; | |
17 | private $baseFolder; | |
419214d7 | 18 | private $logger; |
7f559418 | 19 | private $mimeGuesser; |
41ada277 | 20 | private $wallabagUrl; |
419214d7 | 21 | |
e0597476 | 22 | public function __construct(Client $client, $baseFolder, $wallabagUrl, LoggerInterface $logger) |
156bf627 | 23 | { |
7f559418 JB |
24 | $this->client = $client; |
25 | $this->baseFolder = $baseFolder; | |
e0597476 | 26 | $this->wallabagUrl = rtrim($wallabagUrl, '/'); |
419214d7 | 27 | $this->logger = $logger; |
7f559418 JB |
28 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); |
29 | ||
30 | $this->setFolder(); | |
419214d7 TC |
31 | } |
32 | ||
7f559418 | 33 | /** |
423efade | 34 | * Process the html and extract images URLs from it. |
7f559418 JB |
35 | * |
36 | * @param string $html | |
7f559418 | 37 | * |
423efade | 38 | * @return string[] |
7f559418 | 39 | */ |
423efade | 40 | public static function extractImagesUrlsFromHtml($html) |
156bf627 | 41 | { |
7f559418 | 42 | $crawler = new Crawler($html); |
c15bb5ad S |
43 | $imagesCrawler = $crawler |
44 | ->filterXpath('//img'); | |
45 | $imagesUrls = $imagesCrawler | |
9bf7752f | 46 | ->extract(['src']); |
423efade | 47 | $imagesSrcsetUrls = self::getSrcsetUrls($imagesCrawler); |
48 | ||
49 | return array_unique(array_merge($imagesUrls, $imagesSrcsetUrls)); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Process the html and extract image from it, save them to local and return the updated html. | |
54 | * | |
55 | * @param int $entryId ID of the entry | |
56 | * @param string $html | |
57 | * @param string $url Used as a base path for relative image and folder | |
58 | * | |
59 | * @return string | |
60 | */ | |
61 | public function processHtml($entryId, $html, $url) | |
62 | { | |
63 | $imagesUrls = self::extractImagesUrlsFromHtml($html); | |
419214d7 | 64 | |
e0597476 | 65 | $relativePath = $this->getRelativePath($entryId); |
7f559418 | 66 | |
419214d7 | 67 | // download and save the image to the folder |
c15bb5ad | 68 | foreach ($imagesUrls as $image) { |
e0597476 | 69 | $imagePath = $this->processSingleImage($entryId, $image, $url, $relativePath); |
7f559418 JB |
70 | |
71 | if (false === $imagePath) { | |
72 | continue; | |
73 | } | |
74 | ||
9bf7752f | 75 | // if image contains "&" and we can't find it in the html it might be because it's encoded as & |
fcad69a4 JB |
76 | if (false !== stripos($image, '&') && false === stripos($html, $image)) { |
77 | $image = str_replace('&', '&', $image); | |
78 | } | |
79 | ||
7f559418 | 80 | $html = str_replace($image, $imagePath, $html); |
419214d7 TC |
81 | } |
82 | ||
7f559418 | 83 | return $html; |
419214d7 TC |
84 | } |
85 | ||
7f559418 JB |
86 | /** |
87 | * Process a single image: | |
88 | * - retrieve it | |
89 | * - re-saved it (for security reason) | |
90 | * - return the new local path. | |
91 | * | |
e0597476 | 92 | * @param int $entryId ID of the entry |
7f559418 JB |
93 | * @param string $imagePath Path to the image to retrieve |
94 | * @param string $url Url from where the image were found | |
95 | * @param string $relativePath Relative local path to saved the image | |
96 | * | |
97 | * @return string Relative url to access the image from the web | |
98 | */ | |
e0597476 | 99 | public function processSingleImage($entryId, $imagePath, $url, $relativePath = null) |
156bf627 | 100 | { |
3fbbe0d9 S |
101 | if (null === $imagePath) { |
102 | return false; | |
103 | } | |
104 | ||
e0597476 JB |
105 | if (null === $relativePath) { |
106 | $relativePath = $this->getRelativePath($entryId); | |
419214d7 TC |
107 | } |
108 | ||
f808b016 | 109 | $this->logger->debug('DownloadImages: working on image: ' . $imagePath); |
e0597476 | 110 | |
f808b016 | 111 | $folderPath = $this->baseFolder . '/' . $relativePath; |
419214d7 | 112 | |
7f559418 JB |
113 | // build image path |
114 | $absolutePath = $this->getAbsoluteLink($url, $imagePath); | |
115 | if (false === $absolutePath) { | |
e0597476 | 116 | $this->logger->error('DownloadImages: Can not determine the absolute path for that image, skipping.'); |
419214d7 TC |
117 | |
118 | return false; | |
119 | } | |
120 | ||
48656e0e JB |
121 | try { |
122 | $res = $this->client->get($absolutePath); | |
123 | } catch (\Exception $e) { | |
e0597476 | 124 | $this->logger->error('DownloadImages: Can not retrieve image, skipping.', ['exception' => $e]); |
48656e0e JB |
125 | |
126 | return false; | |
127 | } | |
7f559418 | 128 | |
577c0b6d JB |
129 | $ext = $this->getExtensionFromResponse($res, $imagePath); |
130 | if (false === $res) { | |
419214d7 TC |
131 | return false; |
132 | } | |
577c0b6d | 133 | |
7f559418 | 134 | $hashImage = hash('crc32', $absolutePath); |
f808b016 | 135 | $localPath = $folderPath . '/' . $hashImage . '.' . $ext; |
7f559418 JB |
136 | |
137 | try { | |
138 | $im = imagecreatefromstring($res->getBody()); | |
139 | } catch (\Exception $e) { | |
140 | $im = false; | |
141 | } | |
419214d7 | 142 | |
48656e0e | 143 | if (false === $im) { |
e0597476 | 144 | $this->logger->error('DownloadImages: Error while regenerating image', ['path' => $localPath]); |
419214d7 TC |
145 | |
146 | return false; | |
147 | } | |
148 | ||
7f559418 JB |
149 | switch ($ext) { |
150 | case 'gif': | |
9306c2a3 JB |
151 | // use Imagick if available to keep GIF animation |
152 | if (class_exists('\\Imagick')) { | |
844fd9fa JB |
153 | try { |
154 | $imagick = new \Imagick(); | |
155 | $imagick->readImageBlob($res->getBody()); | |
156 | $imagick->setImageFormat('gif'); | |
157 | $imagick->writeImages($localPath, true); | |
158 | } catch (\Exception $e) { | |
159 | // if Imagick fail, fallback to the default solution | |
160 | imagegif($im, $localPath); | |
161 | } | |
9306c2a3 JB |
162 | } else { |
163 | imagegif($im, $localPath); | |
164 | } | |
165 | ||
e0597476 | 166 | $this->logger->debug('DownloadImages: Re-creating gif'); |
419214d7 | 167 | break; |
7f559418 JB |
168 | case 'jpeg': |
169 | case 'jpg': | |
001cc716 | 170 | imagejpeg($im, $localPath, self::REGENERATE_PICTURES_QUALITY); |
e0597476 | 171 | $this->logger->debug('DownloadImages: Re-creating jpg'); |
419214d7 | 172 | break; |
7f559418 | 173 | case 'png': |
7a3260ae KD |
174 | imagealphablending($im, false); |
175 | imagesavealpha($im, true); | |
001cc716 | 176 | imagepng($im, $localPath, ceil(self::REGENERATE_PICTURES_QUALITY / 100 * 9)); |
e0597476 | 177 | $this->logger->debug('DownloadImages: Re-creating png'); |
419214d7 | 178 | } |
7f559418 | 179 | |
419214d7 TC |
180 | imagedestroy($im); |
181 | ||
f808b016 | 182 | return $this->wallabagUrl . '/assets/images/' . $relativePath . '/' . $hashImage . '.' . $ext; |
419214d7 TC |
183 | } |
184 | ||
e0597476 JB |
185 | /** |
186 | * Remove all images for the given entry id. | |
187 | * | |
188 | * @param int $entryId ID of the entry | |
189 | */ | |
190 | public function removeImages($entryId) | |
191 | { | |
192 | $relativePath = $this->getRelativePath($entryId); | |
f808b016 | 193 | $folderPath = $this->baseFolder . '/' . $relativePath; |
e0597476 JB |
194 | |
195 | $finder = new Finder(); | |
196 | $finder | |
197 | ->files() | |
198 | ->ignoreDotFiles(true) | |
199 | ->in($folderPath); | |
200 | ||
201 | foreach ($finder as $file) { | |
202 | @unlink($file->getRealPath()); | |
203 | } | |
204 | ||
205 | @rmdir($folderPath); | |
206 | } | |
207 | ||
c15bb5ad S |
208 | /** |
209 | * Get images urls from the srcset image attribute. | |
210 | * | |
211 | * @param Crawler $imagesCrawler | |
212 | * | |
213 | * @return array An array of urls | |
214 | */ | |
423efade | 215 | private static function getSrcsetUrls(Crawler $imagesCrawler) |
c15bb5ad S |
216 | { |
217 | $urls = []; | |
218 | $iterator = $imagesCrawler | |
219 | ->getIterator(); | |
220 | while ($iterator->valid()) { | |
221 | $srcsetAttribute = $iterator->current()->getAttribute('srcset'); | |
222 | if ('' !== $srcsetAttribute) { | |
e6f12c07 S |
223 | // Couldn't start with " OR ' OR a white space |
224 | // Could be one or more white space | |
225 | // Must be one or more digits followed by w OR x | |
226 | $pattern = "/(?:[^\"'\s]+\s*(?:\d+[wx])+)/"; | |
227 | preg_match_all($pattern, $srcsetAttribute, $matches); | |
2a1ceb67 | 228 | $srcset = \call_user_func_array('array_merge', $matches); |
c15bb5ad | 229 | $srcsetUrls = array_map(function ($src) { |
e6f12c07 | 230 | return trim(explode(' ', $src, 2)[0]); |
c15bb5ad S |
231 | }, $srcset); |
232 | $urls = array_merge($srcsetUrls, $urls); | |
233 | } | |
234 | $iterator->next(); | |
235 | } | |
236 | ||
237 | return $urls; | |
238 | } | |
239 | ||
f808b016 JB |
240 | /** |
241 | * Setup base folder where all images are going to be saved. | |
242 | */ | |
243 | private function setFolder() | |
244 | { | |
245 | // if folder doesn't exist, attempt to create one and store the folder name in property $folder | |
246 | if (!file_exists($this->baseFolder)) { | |
247 | mkdir($this->baseFolder, 0755, true); | |
248 | } | |
249 | } | |
250 | ||
7f559418 JB |
251 | /** |
252 | * Generate the folder where we are going to save images based on the entry url. | |
253 | * | |
e0597476 | 254 | * @param int $entryId ID of the entry |
7f559418 JB |
255 | * |
256 | * @return string | |
257 | */ | |
e0597476 | 258 | private function getRelativePath($entryId) |
419214d7 | 259 | { |
e0597476 | 260 | $hashId = hash('crc32', $entryId); |
f808b016 JB |
261 | $relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId; |
262 | $folderPath = $this->baseFolder . '/' . $relativePath; | |
419214d7 | 263 | |
7f559418 JB |
264 | if (!file_exists($folderPath)) { |
265 | mkdir($folderPath, 0777, true); | |
419214d7 TC |
266 | } |
267 | ||
e0597476 | 268 | $this->logger->debug('DownloadImages: Folder used for that Entry id', ['folder' => $folderPath, 'entryId' => $entryId]); |
419214d7 | 269 | |
7f559418 JB |
270 | return $relativePath; |
271 | } | |
419214d7 | 272 | |
7f559418 JB |
273 | /** |
274 | * Make an $url absolute based on the $base. | |
275 | * | |
276 | * @see Graby->makeAbsoluteStr | |
277 | * | |
278 | * @param string $base Base url | |
279 | * @param string $url Url to make it absolute | |
280 | * | |
281 | * @return false|string | |
282 | */ | |
283 | private function getAbsoluteLink($base, $url) | |
284 | { | |
285 | if (preg_match('!^https?://!i', $url)) { | |
286 | // already absolute | |
287 | return $url; | |
419214d7 TC |
288 | } |
289 | ||
7f559418 | 290 | $base = new \SimplePie_IRI($base); |
419214d7 | 291 | |
7f559418 JB |
292 | // remove '//' in URL path (causes URLs not to resolve properly) |
293 | if (isset($base->ipath)) { | |
294 | $base->ipath = preg_replace('!//+!', '/', $base->ipath); | |
419214d7 TC |
295 | } |
296 | ||
7f559418 JB |
297 | if ($absolute = \SimplePie_IRI::absolutize($base, $url)) { |
298 | return $absolute->get_uri(); | |
94654765 | 299 | } |
156bf627 | 300 | |
e0597476 | 301 | $this->logger->error('DownloadImages: Can not make an absolute link', ['base' => $base, 'url' => $url]); |
48656e0e | 302 | |
7f559418 | 303 | return false; |
94654765 | 304 | } |
577c0b6d JB |
305 | |
306 | /** | |
307 | * Retrieve and validate the extension from the response of the url of the image. | |
308 | * | |
309 | * @param Response $res Guzzle Response | |
310 | * @param string $imagePath Path from the src image from the content (used for log only) | |
311 | * | |
312 | * @return string|false Extension name or false if validation failed | |
313 | */ | |
314 | private function getExtensionFromResponse(Response $res, $imagePath) | |
315 | { | |
316 | $ext = $this->mimeGuesser->guess($res->getHeader('content-type')); | |
317 | $this->logger->debug('DownloadImages: Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]); | |
318 | ||
319 | // ok header doesn't have the extension, try a different way | |
320 | if (empty($ext)) { | |
321 | $types = [ | |
322 | 'jpeg' => "\xFF\xD8\xFF", | |
323 | 'gif' => 'GIF', | |
324 | 'png' => "\x89\x50\x4e\x47\x0d\x0a", | |
325 | ]; | |
326 | $bytes = substr((string) $res->getBody(), 0, 8); | |
327 | ||
328 | foreach ($types as $type => $header) { | |
329 | if (0 === strpos($bytes, $header)) { | |
330 | $ext = $type; | |
331 | break; | |
332 | } | |
333 | } | |
334 | ||
335 | $this->logger->debug('DownloadImages: Checking extension (alternative)', ['ext' => $ext]); | |
336 | } | |
337 | ||
2a1ceb67 | 338 | if (!\in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) { |
f808b016 | 339 | $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping: ' . $imagePath); |
577c0b6d JB |
340 | |
341 | return false; | |
342 | } | |
343 | ||
344 | return $ext; | |
345 | } | |
419214d7 | 346 | } |