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