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', '');
21 public function __construct($html, $url, Logger
$logger)
26 $this->logger
= $logger;
29 public function setFolder($folder = 'assets/images')
31 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
32 if (!file_exists($folder)) {
35 $this->folder
= $folder;
38 public function process()
40 //instantiate the symfony DomCrawler Component
41 $crawler = new Crawler($this->html
);
42 // create an array of all scrapped image links
43 $this->logger
->log('debug', 'Finding images inside document');
45 ->filterXpath('//img')
46 ->extract(array('src'));
48 // download and save the image to the folder
49 foreach ($result as $image) {
50 $file = file_get_contents($image);
53 $absolute_path = self
::getAbsoluteLink($image, $this->url
);
54 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
55 $fullpath = $this->folder
.'/'.$filename;
56 self
::checks($file, $fullpath, $absolute_path);
57 $this->html
= str_replace($image, self
::getPocheUrl().'/'.$fullpath, $this->html
);
63 private function checks($rawdata, $fullpath, $absolute_path)
65 $fullpath = urldecode($fullpath);
67 if (file_exists($fullpath)) {
72 $this->logger
->log('debug', 'Checking extension');
74 $file_ext = strrchr($fullpath, '.');
75 $whitelist = array('.jpg', '.jpeg', '.gif', '.png');
76 if (!(in_array($file_ext, $whitelist))) {
77 $this->logger
->log('debug', 'processed image with not allowed extension. Skipping '.$fullpath);
83 $this->logger
->log('debug', 'Checking headers');
84 $imageinfo = getimagesize($absolute_path);
85 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/jpg' && $imageinfo['mime'] != 'image/png') {
86 $this->logger
->log('debug', 'processed image with bad header. Skipping '.$fullpath);
92 $this->logger
->log('debug', 'regenerating image');
93 $im = imagecreatefromstring($rawdata);
95 $this->logger
->log('error', 'error while regenerating image '.$fullpath);
100 switch ($imageinfo['mime']) {
102 $result = imagegif($im, $fullpath);
103 $this->logger
->log('debug', 'Re-creating gif');
107 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
108 $this->logger
->log('debug', 'Re-creating jpg');
111 $this->logger
->log('debug', 'Re-creating png');
112 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
120 private static function getAbsoluteLink($relativeLink, $url)
122 /* return if already absolute URL */
123 if (parse_url($relativeLink, PHP_URL_SCHEME
) != '') {
124 return $relativeLink;
127 /* queries and anchors */
128 if ($relativeLink[0] == '#' || $relativeLink[0] == '?') {
129 return $url.$relativeLink;
132 /* parse base URL and convert to local variables:
133 $scheme, $host, $path */
134 extract(parse_url($url));
136 /* remove non-directory element from path */
137 $path = preg_replace('#/[^/]*$#', '', $path);
139 /* destroy path if relative url points to root */
140 if ($relativeLink[0] == '/') {
144 /* dirty absolute URL */
145 $abs = $host.$path.'/'.$relativeLink;
147 /* replace '//' or '/./' or '/foo/../' with '/' */
148 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
149 for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {
152 /* absolute URL is ready! */
153 return $scheme.'://'.$abs;
156 public static function getPocheUrl()
159 $https = (!empty($_SERVER['HTTPS'])
160 && (strtolower($_SERVER['HTTPS']) == 'on'))
161 || (isset($_SERVER['SERVER_PORT'])
162 && $_SERVER['SERVER_PORT'] == '443') // HTTPS detection.
163 || (isset($_SERVER['SERVER_PORT']) //Custom HTTPS port detection
164 && $_SERVER['SERVER_PORT'] == SSL_PORT
)
165 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
166 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
167 $serverport = (!isset($_SERVER['SERVER_PORT'])
168 || $_SERVER['SERVER_PORT'] == '80'
169 || $_SERVER['SERVER_PORT'] == HTTP_PORT
170 || ($https && $_SERVER['SERVER_PORT'] == '443')
171 || ($https && $_SERVER['SERVER_PORT'] == SSL_PORT
) //Custom HTTPS port detection
172 ? '' : ':'.$_SERVER['SERVER_PORT']);
174 if (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) {
175 $serverport = ':'.$_SERVER['HTTP_X_FORWARDED_PORT'];
177 // $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
178 // if (!isset($_SERVER["HTTP_HOST"])) {
179 // return $scriptname;
181 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
182 if (strpos($host, ':') !== false) {
185 // check if BASE_URL is configured
189 $baseUrl = 'http'.($https ? 's' : '').'://'.$host.$serverport;