]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/DownloadImages.php
Merge remote-tracking branch 'origin/master' into 2.4
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / DownloadImages.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Helper;
4
5 use GuzzleHttp\Client;
6 use GuzzleHttp\Message\Response;
7 use Psr\Log\LoggerInterface;
8 use Symfony\Component\DomCrawler\Crawler;
9 use Symfony\Component\Finder\Finder;
10 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
11
12 class DownloadImages
13 {
14 const REGENERATE_PICTURES_QUALITY = 80;
15
16 private $client;
17 private $baseFolder;
18 private $logger;
19 private $mimeGuesser;
20 private $wallabagUrl;
21
22 public function __construct(Client $client, $baseFolder, $wallabagUrl, LoggerInterface $logger)
23 {
24 $this->client = $client;
25 $this->baseFolder = $baseFolder;
26 $this->wallabagUrl = rtrim($wallabagUrl, '/');
27 $this->logger = $logger;
28 $this->mimeGuesser = new MimeTypeExtensionGuesser();
29
30 $this->setFolder();
31 }
32
33 /**
34 * Process the html and extract images URLs from it.
35 *
36 * @param string $html
37 *
38 * @return string[]
39 */
40 public static function extractImagesUrlsFromHtml($html)
41 {
42 $crawler = new Crawler($html);
43 $imagesCrawler = $crawler
44 ->filterXpath('//img');
45 $imagesUrls = $imagesCrawler
46 ->extract(['src']);
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);
64
65 $relativePath = $this->getRelativePath($entryId);
66
67 // download and save the image to the folder
68 foreach ($imagesUrls as $image) {
69 $imagePath = $this->processSingleImage($entryId, $image, $url, $relativePath);
70
71 if (false === $imagePath) {
72 continue;
73 }
74
75 // if image contains "&" and we can't find it in the html it might be because it's encoded as &amp;
76 if (false !== stripos($image, '&') && false === stripos($html, $image)) {
77 $image = str_replace('&', '&amp;', $image);
78 }
79
80 $html = str_replace($image, $imagePath, $html);
81 }
82
83 return $html;
84 }
85
86 /**
87 * Process a single image:
88 * - retrieve it
89 * - re-saved it (for security reason)
90 * - return the new local path.
91 *
92 * @param int $entryId ID of the entry
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 */
99 public function processSingleImage($entryId, $imagePath, $url, $relativePath = null)
100 {
101 if (null === $imagePath) {
102 return false;
103 }
104
105 if (null === $relativePath) {
106 $relativePath = $this->getRelativePath($entryId);
107 }
108
109 $this->logger->debug('DownloadImages: working on image: ' . $imagePath);
110
111 $folderPath = $this->baseFolder . '/' . $relativePath;
112
113 // build image path
114 $absolutePath = $this->getAbsoluteLink($url, $imagePath);
115 if (false === $absolutePath) {
116 $this->logger->error('DownloadImages: Can not determine the absolute path for that image, skipping.');
117
118 return false;
119 }
120
121 try {
122 $res = $this->client->get($absolutePath);
123 } catch (\Exception $e) {
124 $this->logger->error('DownloadImages: Can not retrieve image, skipping.', ['exception' => $e]);
125
126 return false;
127 }
128
129 $ext = $this->getExtensionFromResponse($res, $imagePath);
130 if (false === $res) {
131 return false;
132 }
133
134 $hashImage = hash('crc32', $absolutePath);
135 $localPath = $folderPath . '/' . $hashImage . '.' . $ext;
136
137 try {
138 $im = imagecreatefromstring($res->getBody());
139 } catch (\Exception $e) {
140 $im = false;
141 }
142
143 if (false === $im) {
144 $this->logger->error('DownloadImages: Error while regenerating image', ['path' => $localPath]);
145
146 return false;
147 }
148
149 switch ($ext) {
150 case 'gif':
151 // use Imagick if available to keep GIF animation
152 if (class_exists('\\Imagick')) {
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 }
162 } else {
163 imagegif($im, $localPath);
164 }
165
166 $this->logger->debug('DownloadImages: Re-creating gif');
167 break;
168 case 'jpeg':
169 case 'jpg':
170 imagejpeg($im, $localPath, self::REGENERATE_PICTURES_QUALITY);
171 $this->logger->debug('DownloadImages: Re-creating jpg');
172 break;
173 case 'png':
174 imagealphablending($im, false);
175 imagesavealpha($im, true);
176 imagepng($im, $localPath, ceil(self::REGENERATE_PICTURES_QUALITY / 100 * 9));
177 $this->logger->debug('DownloadImages: Re-creating png');
178 }
179
180 imagedestroy($im);
181
182 return $this->wallabagUrl . '/assets/images/' . $relativePath . '/' . $hashImage . '.' . $ext;
183 }
184
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);
193 $folderPath = $this->baseFolder . '/' . $relativePath;
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
208 /**
209 * Get images urls from the srcset image attribute.
210 *
211 * @param Crawler $imagesCrawler
212 *
213 * @return array An array of urls
214 */
215 private static function getSrcsetUrls(Crawler $imagesCrawler)
216 {
217 $urls = [];
218 $iterator = $imagesCrawler
219 ->getIterator();
220 while ($iterator->valid()) {
221 $srcsetAttribute = $iterator->current()->getAttribute('srcset');
222 if ('' !== $srcsetAttribute) {
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);
228 $srcset = \call_user_func_array('array_merge', $matches);
229 $srcsetUrls = array_map(function ($src) {
230 return trim(explode(' ', $src, 2)[0]);
231 }, $srcset);
232 $urls = array_merge($srcsetUrls, $urls);
233 }
234 $iterator->next();
235 }
236
237 return $urls;
238 }
239
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
251 /**
252 * Generate the folder where we are going to save images based on the entry url.
253 *
254 * @param int $entryId ID of the entry
255 *
256 * @return string
257 */
258 private function getRelativePath($entryId)
259 {
260 $hashId = hash('crc32', $entryId);
261 $relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId;
262 $folderPath = $this->baseFolder . '/' . $relativePath;
263
264 if (!file_exists($folderPath)) {
265 mkdir($folderPath, 0777, true);
266 }
267
268 $this->logger->debug('DownloadImages: Folder used for that Entry id', ['folder' => $folderPath, 'entryId' => $entryId]);
269
270 return $relativePath;
271 }
272
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;
288 }
289
290 $base = new \SimplePie_IRI($base);
291
292 // remove '//' in URL path (causes URLs not to resolve properly)
293 if (isset($base->ipath)) {
294 $base->ipath = preg_replace('!//+!', '/', $base->ipath);
295 }
296
297 if ($absolute = \SimplePie_IRI::absolutize($base, $url)) {
298 return $absolute->get_uri();
299 }
300
301 $this->logger->error('DownloadImages: Can not make an absolute link', ['base' => $base, 'url' => $url]);
302
303 return false;
304 }
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
338 if (!\in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
339 $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping: ' . $imagePath);
340
341 return false;
342 }
343
344 return $ext;
345 }
346 }