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