3 namespace Wallabag\CoreBundle\Helper
;
5 use Psr\Log\LoggerInterface
as Logger
;
6 use Symfony\Component\DomCrawler\Crawler
;
8 define('REGENERATE_PICTURES_QUALITY', 75);
9 define('HTTP_PORT', 80);
10 define('SSL_PORT', 443);
11 define('BASE_URL','');
13 class DownloadImages
{
20 public function __construct($html, $url, Logger
$logger) {
24 $this->logger
= $logger;
27 public function setFolder($folder = "assets/images") {
28 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
29 if(!file_exists($folder)) {
32 $this->folder
= $folder;
35 public function process() {
36 //instantiate the symfony DomCrawler Component
37 $crawler = new Crawler($this->html
);
38 // create an array of all scrapped image links
39 $this->logger
->log('debug', 'Finding images inside document');
41 ->filterXpath('//img')
42 ->extract(array('src'));
44 // download and save the image to the folder
45 foreach ($result as $image) {
46 $file = file_get_contents($image);
49 $absolute_path = self
::getAbsoluteLink($image, $this->url
);
50 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
51 $fullpath = $this->folder
."/".$filename;
52 self
::checks($file, $fullpath, $absolute_path);
53 $this->html
= str_replace($image, self
::getPocheUrl() . '/' . $fullpath, $this->html
);
59 private function checks($rawdata, $fullpath, $absolute_path) {
60 $fullpath = urldecode($fullpath);
62 if (file_exists($fullpath)) {
67 $this->logger
->log('debug','Checking extension');
69 $file_ext = strrchr($fullpath, '.');
70 $whitelist = array('.jpg', '.jpeg', '.gif', '.png');
71 if (!(in_array($file_ext, $whitelist))) {
72 $this->logger
->log('debug','processed image with not allowed extension. Skipping '.$fullpath);
78 $this->logger
->log('debug','Checking headers');
79 $imageinfo = getimagesize($absolute_path);
80 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/jpg' && $imageinfo['mime'] != 'image/png') {
81 $this->logger
->log('debug','processed image with bad header. Skipping '.$fullpath);
87 $this->logger
->log('debug','regenerating image');
88 $im = imagecreatefromstring($rawdata);
90 $this->logger
->log('error','error while regenerating image '.$fullpath);
95 switch ($imageinfo['mime']) {
97 $result = imagegif($im, $fullpath);
98 $this->logger
->log('debug','Re-creating gif');
102 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
103 $this->logger
->log('debug','Re-creating jpg');
106 $this->logger
->log('debug','Re-creating png');
107 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
115 private static function getAbsoluteLink($relativeLink, $url)
117 /* return if already absolute URL */
118 if (parse_url($relativeLink, PHP_URL_SCHEME
) != '') {
119 return $relativeLink;
122 /* queries and anchors */
123 if ($relativeLink[0] == '#' || $relativeLink[0] == '?') {
124 return $url.$relativeLink;
127 /* parse base URL and convert to local variables:
128 $scheme, $host, $path */
129 extract(parse_url($url));
131 /* remove non-directory element from path */
132 $path = preg_replace('#/[^/]*$#', '', $path);
134 /* destroy path if relative url points to root */
135 if ($relativeLink[0] == '/') {
139 /* dirty absolute URL */
140 $abs = $host.$path.'/'.$relativeLink;
142 /* replace '//' or '/./' or '/foo/../' with '/' */
143 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
144 for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {
147 /* absolute URL is ready! */
148 return $scheme.'://'.$abs;
151 public static function getPocheUrl()
154 $https = (!empty($_SERVER['HTTPS'])
155 && (strtolower($_SERVER['HTTPS']) == 'on'))
156 || (isset($_SERVER["SERVER_PORT"])
157 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
158 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
159 && $_SERVER["SERVER_PORT"] == SSL_PORT
)
160 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
161 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
162 $serverport = (!isset($_SERVER["SERVER_PORT"])
163 || $_SERVER["SERVER_PORT"] == '80'
164 || $_SERVER["SERVER_PORT"] == HTTP_PORT
165 || ($https && $_SERVER["SERVER_PORT"] == '443')
166 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT
) //Custom HTTPS port detection
167 ? '' : ':' . $_SERVER["SERVER_PORT"]);
169 if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
170 $serverport = ':' . $_SERVER["HTTP_X_FORWARDED_PORT"];
172 // $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
173 // if (!isset($_SERVER["HTTP_HOST"])) {
174 // return $scriptname;
176 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
177 if (strpos($host, ':') !== false) {
180 // check if BASE_URL is configured
184 $baseUrl = 'http' . ($https ? 's' : '') . '://' . $host . $serverport;