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