aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/pochePictures.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/poche/pochePictures.php')
-rw-r--r--inc/poche/pochePictures.php262
1 files changed, 136 insertions, 126 deletions
diff --git a/inc/poche/pochePictures.php b/inc/poche/pochePictures.php
index 7c319a85..7a914f90 100644
--- a/inc/poche/pochePictures.php
+++ b/inc/poche/pochePictures.php
@@ -5,154 +5,164 @@
5 * @category wallabag 5 * @category wallabag
6 * @author Nicolas Lœuillet <nicolas@loeuillet.org> 6 * @author Nicolas Lœuillet <nicolas@loeuillet.org>
7 * @copyright 2013 7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file 8 * @license http://opensource.org/licenses/MIT see COPYING file
9 */ 9 */
10 10
11/** 11
12 * On modifie les URLS des images dans le corps de l'article 12final class Picture
13 */
14function filtre_picture($content, $url, $id)
15{ 13{
16 $matches = array(); 14 /**
17 $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice 15 * Changing pictures URL in article content
18 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER); 16 */
19 foreach($matches as $i => $link) { 17 public static function filterPicture($content, $url, $id)
20 $link[1] = trim($link[1]); 18 {
21 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) { 19 $matches = array();
22 $absolute_path = get_absolute_link($link[2],$url); 20 $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice
23 $filename = basename(parse_url($absolute_path, PHP_URL_PATH)); 21 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER);
24 $directory = create_assets_directory($id); 22 foreach($matches as $i => $link) {
25 $fullpath = $directory . '/' . $filename; 23 $link[1] = trim($link[1]);
26 24 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) {
27 if (in_array($absolute_path, $processing_pictures) === true) { 25 $absolute_path = self::_getAbsoluteLink($link[2], $url);
28 // replace picture's URL only if processing is OK : already processing -> go to next picture 26 $filename = basename(parse_url($absolute_path, PHP_URL_PATH));
29 continue; 27 $directory = self::_createAssetsDirectory($id);
28 $fullpath = $directory . '/' . $filename;
29
30 if (in_array($absolute_path, $processing_pictures) === true) {
31 // replace picture's URL only if processing is OK : already processing -> go to next picture
32 continue;
33 }
34
35 if (self::_downloadPictures($absolute_path, $fullpath) === true) {
36 $content = str_replace($matches[$i][2], $fullpath, $content);
37 }
38
39 $processing_pictures[] = $absolute_path;
30 } 40 }
31
32 if (download_pictures($absolute_path, $fullpath) === true) {
33 $content = str_replace($matches[$i][2], $fullpath, $content);
34 }
35
36 $processing_pictures[] = $absolute_path;
37 } 41 }
38 42
43 return $content;
39 } 44 }
40 45
41 return $content; 46 /**
42} 47 * Get absolute URL
48 */
49 private static function _getAbsoluteLink($relativeLink, $url)
50 {
51 /* return if already absolute URL */
52 if (parse_url($relativeLink, PHP_URL_SCHEME) != '') return $relativeLink;
43 53
44/** 54 /* queries and anchors */
45 * Retourne le lien absolu 55 if ($relativeLink[0]=='#' || $relativeLink[0]=='?') return $url . $relativeLink;
46 */
47function get_absolute_link($relative_link, $url) {
48 /* return if already absolute URL */
49 if (parse_url($relative_link, PHP_URL_SCHEME) != '') return $relative_link;
50 56
51 /* queries and anchors */ 57 /* parse base URL and convert to local variables:
52 if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link; 58 $scheme, $host, $path */
59 extract(parse_url($url));
53 60
54 /* parse base URL and convert to local variables: 61 /* remove non-directory element from path */
55 $scheme, $host, $path */ 62 $path = preg_replace('#/[^/]*$#', '', $path);
56 extract(parse_url($url));
57 63
58 /* remove non-directory element from path */ 64 /* destroy path if relative url points to root */
59 $path = preg_replace('#/[^/]*$#', '', $path); 65 if ($relativeLink[0] == '/') $path = '';
60 66
61 /* destroy path if relative url points to root */ 67 /* dirty absolute URL */
62 if ($relative_link[0] == '/') $path = ''; 68 $abs = $host . $path . '/' . $relativeLink;
63 69
64 /* dirty absolute URL */ 70 /* replace '//' or '/./' or '/foo/../' with '/' */
65 $abs = $host . $path . '/' . $relative_link; 71 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
72 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
66 73
67 /* replace '//' or '/./' or '/foo/../' with '/' */ 74 /* absolute URL is ready! */
68 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); 75 return $scheme.'://'.$abs;
69 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} 76 }
70 77
71 /* absolute URL is ready! */ 78 /**
72 return $scheme.'://'.$abs; 79 * Downloading pictures
73} 80 *
81 * @return bool true if the download and processing is OK, false else
82 */
83 private static function _downloadPictures($absolute_path, $fullpath)
84 {
85 $rawdata = Tools::getFile($absolute_path);
86 $fullpath = urldecode($fullpath);
87
88 if(file_exists($fullpath)) {
89 unlink($fullpath);
90 }
74 91
75/** 92 // check extension
76 * Téléchargement des images 93 $file_ext = strrchr($fullpath, '.');
77 * 94 $whitelist = array(".jpg",".jpeg",".gif",".png");
78 * @return bool true if the download and processing is OK, false else 95 if (!(in_array($file_ext, $whitelist))) {
79 */ 96 Tools::logm('processed image with not allowed extension. Skipping ' . $fullpath);
80function download_pictures($absolute_path, $fullpath) 97 return false;
81{ 98 }
82 $rawdata = Tools::getFile($absolute_path);
83 $fullpath = urldecode($fullpath);
84 99
85 if(file_exists($fullpath)) { 100 // check headers
86 unlink($fullpath); 101 $imageinfo = getimagesize($absolute_path);
87 } 102 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') {
88 103 Tools::logm('processed image with bad header. Skipping ' . $fullpath);
89 // check extension 104 return false;
90 $file_ext = strrchr($fullpath, '.'); 105 }
91 $whitelist = array(".jpg",".jpeg",".gif",".png");
92 if (!(in_array($file_ext, $whitelist))) {
93 Tools::logm('processed image with not allowed extension. Skipping ' . $fullpath);
94 return false;
95 }
96
97 // check headers
98 $imageinfo = getimagesize($absolute_path);
99 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') {
100 Tools::logm('processed image with bad header. Skipping ' . $fullpath);
101 return false;
102 }
103
104 // regenerate image
105 $im = imagecreatefromstring($rawdata);
106 if ($im === false) {
107 Tools::logm('error while regenerating image ' . $fullpath);
108 return false;
109 }
110
111 switch ($imageinfo['mime']) {
112 case 'image/gif':
113 $result = imagegif($im, $fullpath);
114 break;
115 case 'image/jpeg':
116 case 'image/jpg':
117 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY);
118 break;
119 case 'image/png':
120 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY / 100 * 9));
121 break;
122 }
123 imagedestroy($im);
124
125 return $result;
126}
127 106
128/** 107 // regenerate image
129 * Crée un répertoire de médias pour l'article 108 $im = imagecreatefromstring($rawdata);
130 */ 109 if ($im === false) {
131function create_assets_directory($id) 110 Tools::logm('error while regenerating image ' . $fullpath);
132{ 111 return false;
133 $assets_path = ABS_PATH; 112 }
134 if(!is_dir($assets_path)) { 113
135 mkdir($assets_path, 0715); 114 switch ($imageinfo['mime']) {
136 } 115 case 'image/gif':
116 $result = imagegif($im, $fullpath);
117 break;
118 case 'image/jpeg':
119 case 'image/jpg':
120 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY);
121 break;
122 case 'image/png':
123 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY / 100 * 9));
124 break;
125 }
126 imagedestroy($im);
137 127
138 $article_directory = $assets_path . $id; 128 return $result;
139 if(!is_dir($article_directory)) {
140 mkdir($article_directory, 0715);
141 } 129 }
142 130
143 return $article_directory; 131 /**
144} 132 * Create a directory for an article
133 *
134 * @param $id ID of the article
135 * @return string
136 */
137 private static function _createAssetsDirectory($id)
138 {
139 $assets_path = ABS_PATH;
140 if (!is_dir($assets_path)) {
141 mkdir($assets_path, 0715);
142 }
145 143
146/** 144 $article_directory = $assets_path . $id;
147 * Suppression du répertoire d'images 145 if (!is_dir($article_directory)) {
148 */ 146 mkdir($article_directory, 0715);
149function remove_directory($directory) 147 }
150{ 148
151 if(is_dir($directory)) { 149 return $article_directory;
152 $files = array_diff(scandir($directory), array('.','..')); 150 }
153 foreach ($files as $file) { 151
154 (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file"); 152 /**
153 * Remove the directory
154 *
155 * @param $directory
156 * @return bool
157 */
158 public static function removeDirectory($directory)
159 {
160 if (is_dir($directory)) {
161 $files = array_diff(scandir($directory), array('.','..'));
162 foreach ($files as $file) {
163 (is_dir("$directory/$file")) ? self::removeDirectory("$directory/$file") : unlink("$directory/$file");
164 }
165 return rmdir($directory);
155 } 166 }
156 return rmdir($directory);
157 } 167 }
158} 168} \ No newline at end of file