aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php2
-rw-r--r--src/Wallabag/CoreBundle/Helper/DownloadImages.php77
2 files changed, 42 insertions, 37 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index bbad705f..8ed11205 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -76,7 +76,7 @@ class ContentProxy
76 } 76 }
77 77
78 if (true) { 78 if (true) {
79 $this->logger->log('debug','Starting to download images'); 79 $this->logger->log('debug', 'Starting to download images');
80 $downloadImages = new DownloadImages($html, $url, $this->logger); 80 $downloadImages = new DownloadImages($html, $url, $this->logger);
81 $html = $downloadImages->process(); 81 $html = $downloadImages->process();
82 } 82 }
diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php
index 14f0aa1b..e23e0c55 100644
--- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php
+++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php
@@ -8,31 +8,35 @@ use Symfony\Component\DomCrawler\Crawler;
8define('REGENERATE_PICTURES_QUALITY', 75); 8define('REGENERATE_PICTURES_QUALITY', 75);
9define('HTTP_PORT', 80); 9define('HTTP_PORT', 80);
10define('SSL_PORT', 443); 10define('SSL_PORT', 443);
11define('BASE_URL',''); 11define('BASE_URL', '');
12 12
13class DownloadImages { 13class DownloadImages
14{
14 private $folder; 15 private $folder;
15 private $url; 16 private $url;
16 private $html; 17 private $html;
17 private $fileName; 18 private $fileName;
18 private $logger; 19 private $logger;
19 20
20 public function __construct($html, $url, Logger $logger) { 21 public function __construct($html, $url, Logger $logger)
22 {
21 $this->html = $html; 23 $this->html = $html;
22 $this->url = $url; 24 $this->url = $url;
23 $this->setFolder(); 25 $this->setFolder();
24 $this->logger = $logger; 26 $this->logger = $logger;
25 } 27 }
26 28
27 public function setFolder($folder = "assets/images") { 29 public function setFolder($folder = 'assets/images')
30 {
28 // if folder doesn't exist, attempt to create one and store the folder name in property $folder 31 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
29 if(!file_exists($folder)) { 32 if (!file_exists($folder)) {
30 mkdir($folder); 33 mkdir($folder);
31 } 34 }
32 $this->folder = $folder; 35 $this->folder = $folder;
33 } 36 }
34 37
35 public function process() { 38 public function process()
39 {
36 //instantiate the symfony DomCrawler Component 40 //instantiate the symfony DomCrawler Component
37 $crawler = new Crawler($this->html); 41 $crawler = new Crawler($this->html);
38 // create an array of all scrapped image links 42 // create an array of all scrapped image links
@@ -48,15 +52,16 @@ class DownloadImages {
48 // Checks 52 // Checks
49 $absolute_path = self::getAbsoluteLink($image, $this->url); 53 $absolute_path = self::getAbsoluteLink($image, $this->url);
50 $filename = basename(parse_url($absolute_path, PHP_URL_PATH)); 54 $filename = basename(parse_url($absolute_path, PHP_URL_PATH));
51 $fullpath = $this->folder."/".$filename; 55 $fullpath = $this->folder.'/'.$filename;
52 self::checks($file, $fullpath, $absolute_path); 56 self::checks($file, $fullpath, $absolute_path);
53 $this->html = str_replace($image, self::getPocheUrl() . '/' . $fullpath, $this->html); 57 $this->html = str_replace($image, self::getPocheUrl().'/'.$fullpath, $this->html);
54 } 58 }
55 59
56 return $this->html; 60 return $this->html;
57 } 61 }
58 62
59 private function checks($rawdata, $fullpath, $absolute_path) { 63 private function checks($rawdata, $fullpath, $absolute_path)
64 {
60 $fullpath = urldecode($fullpath); 65 $fullpath = urldecode($fullpath);
61 66
62 if (file_exists($fullpath)) { 67 if (file_exists($fullpath)) {
@@ -64,30 +69,30 @@ class DownloadImages {
64 } 69 }
65 70
66 // check extension 71 // check extension
67 $this->logger->log('debug','Checking extension'); 72 $this->logger->log('debug', 'Checking extension');
68 73
69 $file_ext = strrchr($fullpath, '.'); 74 $file_ext = strrchr($fullpath, '.');
70 $whitelist = array('.jpg', '.jpeg', '.gif', '.png'); 75 $whitelist = array('.jpg', '.jpeg', '.gif', '.png');
71 if (!(in_array($file_ext, $whitelist))) { 76 if (!(in_array($file_ext, $whitelist))) {
72 $this->logger->log('debug','processed image with not allowed extension. Skipping '.$fullpath); 77 $this->logger->log('debug', 'processed image with not allowed extension. Skipping '.$fullpath);
73 78
74 return false; 79 return false;
75 } 80 }
76 81
77 // check headers 82 // check headers
78 $this->logger->log('debug','Checking headers'); 83 $this->logger->log('debug', 'Checking headers');
79 $imageinfo = getimagesize($absolute_path); 84 $imageinfo = getimagesize($absolute_path);
80 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/jpg' && $imageinfo['mime'] != 'image/png') { 85 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); 86 $this->logger->log('debug', 'processed image with bad header. Skipping '.$fullpath);
82 87
83 return false; 88 return false;
84 } 89 }
85 90
86 // regenerate image 91 // regenerate image
87 $this->logger->log('debug','regenerating image'); 92 $this->logger->log('debug', 'regenerating image');
88 $im = imagecreatefromstring($rawdata); 93 $im = imagecreatefromstring($rawdata);
89 if ($im === false) { 94 if ($im === false) {
90 $this->logger->log('error','error while regenerating image '.$fullpath); 95 $this->logger->log('error', 'error while regenerating image '.$fullpath);
91 96
92 return false; 97 return false;
93 } 98 }
@@ -95,15 +100,15 @@ class DownloadImages {
95 switch ($imageinfo['mime']) { 100 switch ($imageinfo['mime']) {
96 case 'image/gif': 101 case 'image/gif':
97 $result = imagegif($im, $fullpath); 102 $result = imagegif($im, $fullpath);
98 $this->logger->log('debug','Re-creating gif'); 103 $this->logger->log('debug', 'Re-creating gif');
99 break; 104 break;
100 case 'image/jpeg': 105 case 'image/jpeg':
101 case 'image/jpg': 106 case 'image/jpg':
102 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY); 107 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY);
103 $this->logger->log('debug','Re-creating jpg'); 108 $this->logger->log('debug', 'Re-creating jpg');
104 break; 109 break;
105 case 'image/png': 110 case 'image/png':
106 $this->logger->log('debug','Re-creating png'); 111 $this->logger->log('debug', 'Re-creating png');
107 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY / 100 * 9)); 112 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY / 100 * 9));
108 break; 113 break;
109 } 114 }
@@ -150,24 +155,24 @@ class DownloadImages {
150 155
151 public static function getPocheUrl() 156 public static function getPocheUrl()
152 { 157 {
153 $baseUrl = ""; 158 $baseUrl = '';
154 $https = (!empty($_SERVER['HTTPS']) 159 $https = (!empty($_SERVER['HTTPS'])
155 && (strtolower($_SERVER['HTTPS']) == 'on')) 160 && (strtolower($_SERVER['HTTPS']) == 'on'))
156 || (isset($_SERVER["SERVER_PORT"]) 161 || (isset($_SERVER['SERVER_PORT'])
157 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection. 162 && $_SERVER['SERVER_PORT'] == '443') // HTTPS detection.
158 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection 163 || (isset($_SERVER['SERVER_PORT']) //Custom HTTPS port detection
159 && $_SERVER["SERVER_PORT"] == SSL_PORT) 164 && $_SERVER['SERVER_PORT'] == SSL_PORT)
160 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) 165 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
161 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'); 166 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
162 $serverport = (!isset($_SERVER["SERVER_PORT"]) 167 $serverport = (!isset($_SERVER['SERVER_PORT'])
163 || $_SERVER["SERVER_PORT"] == '80' 168 || $_SERVER['SERVER_PORT'] == '80'
164 || $_SERVER["SERVER_PORT"] == HTTP_PORT 169 || $_SERVER['SERVER_PORT'] == HTTP_PORT
165 || ($https && $_SERVER["SERVER_PORT"] == '443') 170 || ($https && $_SERVER['SERVER_PORT'] == '443')
166 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection 171 || ($https && $_SERVER['SERVER_PORT'] == SSL_PORT) //Custom HTTPS port detection
167 ? '' : ':' . $_SERVER["SERVER_PORT"]); 172 ? '' : ':'.$_SERVER['SERVER_PORT']);
168 173
169 if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) { 174 if (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) {
170 $serverport = ':' . $_SERVER["HTTP_X_FORWARDED_PORT"]; 175 $serverport = ':'.$_SERVER['HTTP_X_FORWARDED_PORT'];
171 } 176 }
172 // $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); 177 // $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
173 // if (!isset($_SERVER["HTTP_HOST"])) { 178 // if (!isset($_SERVER["HTTP_HOST"])) {
@@ -178,12 +183,12 @@ class DownloadImages {
178 $serverport = ''; 183 $serverport = '';
179 } 184 }
180 // check if BASE_URL is configured 185 // check if BASE_URL is configured
181 if(BASE_URL) { 186 if (BASE_URL) {
182 $baseUrl = BASE_URL; 187 $baseUrl = BASE_URL;
183 } else { 188 } else {
184 $baseUrl = 'http' . ($https ? 's' : '') . '://' . $host . $serverport; 189 $baseUrl = 'http'.($https ? 's' : '').'://'.$host.$serverport;
185 } 190 }
186 return $baseUrl; 191
187 192 return $baseUrl;
188 } 193 }
189} 194}