3 namespace Wallabag\CoreBundle\Helper
;
5 use Psr\Log\LoggerInterface
;
6 use Symfony\Component\DomCrawler\Crawler
;
8 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser
;
12 const REGENERATE_PICTURES_QUALITY
= 80;
20 public function __construct(Client
$client, $baseFolder, LoggerInterface
$logger)
22 $this->client
= $client;
23 $this->baseFolder
= $baseFolder;
24 $this->logger
= $logger;
25 $this->mimeGuesser
= new MimeTypeExtensionGuesser();
31 * Since we can't inject CraueConfig service because it'll generate a circular reference when injected in the subscriber
32 * we use a different way to inject the current wallabag url.
34 * @param string $url Usually from `$config->get('wallabag_url')`
36 public function setWallabagUrl($url)
38 $this->wallabagUrl
= rtrim($url, '/');
42 * Setup base folder where all images are going to be saved.
44 private function setFolder()
46 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
47 if (!file_exists($this->baseFolder
)) {
48 mkdir($this->baseFolder
, 0777, true);
53 * Process the html and extract image from it, save them to local and return the updated html.
56 * @param string $url Used as a base path for relative image and folder
60 public function processHtml($html, $url)
62 $crawler = new Crawler($html);
64 ->filterXpath('//img')
65 ->extract(array('src'));
67 $relativePath = $this->getRelativePath($url);
69 // download and save the image to the folder
70 foreach ($result as $image) {
71 $imagePath = $this->processSingleImage($image, $url, $relativePath);
73 if (false === $imagePath) {
77 $html = str_replace($image, $imagePath, $html);
84 * Process a single image:
86 * - re-saved it (for security reason)
87 * - return the new local path.
89 * @param string $imagePath Path to the image to retrieve
90 * @param string $url Url from where the image were found
91 * @param string $relativePath Relative local path to saved the image
93 * @return string Relative url to access the image from the web
95 public function processSingleImage($imagePath, $url, $relativePath = null)
97 if (null == $relativePath) {
98 $relativePath = $this->getRelativePath($url);
101 $folderPath = $this->baseFolder
.'/'.$relativePath;
104 $absolutePath = $this->getAbsoluteLink($url, $imagePath);
105 if (false === $absolutePath) {
106 $this->logger
->log('error', 'Can not determine the absolute path for that image, skipping.');
112 $res = $this->client
->get($absolutePath);
113 } catch (\Exception
$e) {
114 $this->logger
->log('error', 'Can not retrieve image, skipping.', ['exception' => $e]);
119 $ext = $this->mimeGuesser
->guess($res->getHeader('content-type'));
120 $this->logger
->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
121 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
122 $this->logger
->log('error', 'Processed image with not allowed extension. Skipping '.$imagePath);
126 $hashImage = hash('crc32', $absolutePath);
127 $localPath = $folderPath.'/'.$hashImage.'.'.$ext;
130 $im = imagecreatefromstring($res->getBody());
131 } catch (\Exception
$e) {
136 $this->logger
->log('error', 'Error while regenerating image', ['path' => $localPath]);
143 $result = imagegif($im, $localPath);
144 $this->logger
->log('debug', 'Re-creating gif');
148 $result = imagejpeg($im, $localPath, self
::REGENERATE_PICTURES_QUALITY
);
149 $this->logger
->log('debug', 'Re-creating jpg');
152 $result = imagepng($im, $localPath, ceil(self
::REGENERATE_PICTURES_QUALITY
/ 100 * 9));
153 $this->logger
->log('debug', 'Re-creating png');
158 return $this->wallabagUrl
.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
162 * Generate the folder where we are going to save images based on the entry url.
168 private function getRelativePath($url)
170 $hashUrl = hash('crc32', $url);
171 $relativePath = $hashUrl[0].'/'.$hashUrl[1].'/'.$hashUrl;
172 $folderPath = $this->baseFolder
.'/'.$relativePath;
174 if (!file_exists($folderPath)) {
175 mkdir($folderPath, 0777, true);
178 $this->logger
->log('debug', 'Folder used for that url', ['folder' => $folderPath, 'url' => $url]);
180 return $relativePath;
184 * Make an $url absolute based on the $base.
186 * @see Graby->makeAbsoluteStr
188 * @param string $base Base url
189 * @param string $url Url to make it absolute
191 * @return false|string
193 private function getAbsoluteLink($base, $url)
195 if (preg_match('!^https?://!i', $url)) {
200 $base = new \
SimplePie_IRI($base);
202 // remove '//' in URL path (causes URLs not to resolve properly)
203 if (isset($base->ipath
)) {
204 $base->ipath
= preg_replace('!//+!', '/', $base->ipath
);
207 if ($absolute = \SimplePie_IRI
::absolutize($base, $url)) {
208 return $absolute->get_uri();
211 $this->logger
->log('error', 'Can not make an absolute link', ['base' => $base, 'url' => $url]);