]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/pochePictures.php
WHAT. A. BIG. REFACTOR. + new license (we moved to MIT one)
[github/wallabag/wallabag.git] / inc / poche / pochePictures.php
1 <?php
2 /**
3 * wallabag, self hostable application allowing you to not miss any content anymore
4 *
5 * @category wallabag
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
7 * @copyright 2013
8 * @license http://opensource.org/licenses/MIT see COPYING file
9 */
10
11
12 final class Picture
13 {
14 private function __construct()
15 {
16
17 }
18
19 /**
20 * Changing pictures URL in article content
21 */
22 public static function filterPicture($content, $url, $id)
23 {
24 $matches = array();
25 $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice
26 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER);
27 foreach($matches as $i => $link) {
28 $link[1] = trim($link[1]);
29 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) {
30 $absolute_path = self::_getAbsoluteLink($link[2], $url);
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;
45 }
46 }
47
48 return $content;
49 }
50
51 /**
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;
58
59 /* queries and anchors */
60 if ($relativeLink[0]=='#' || $relativeLink[0]=='?') return $url . $relativeLink;
61
62 /* parse base URL and convert to local variables:
63 $scheme, $host, $path */
64 extract(parse_url($url));
65
66 /* remove non-directory element from path */
67 $path = preg_replace('#/[^/]*$#', '', $path);
68
69 /* destroy path if relative url points to root */
70 if ($relativeLink[0] == '/') $path = '';
71
72 /* dirty absolute URL */
73 $abs = $host . $path . '/' . $relativeLink;
74
75 /* replace '//' or '/./' or '/foo/../' with '/' */
76 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
77 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
78
79 /* absolute URL is ready! */
80 return $scheme.'://'.$abs;
81 }
82
83 /**
84 * Downloading pictures
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 }
96
97 // check extension
98 $file_ext = strrchr($fullpath, '.');
99 $whitelist = array(".jpg",".jpeg",".gif",".png");
100 if (!(in_array($file_ext, $whitelist))) {
101 Tools::logm('processed image with not allowed extension. Skipping ' . $fullpath);
102 return false;
103 }
104
105 // check headers
106 $imageinfo = getimagesize($absolute_path);
107 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') {
108 Tools::logm('processed image with bad header. Skipping ' . $fullpath);
109 return false;
110 }
111
112 // regenerate image
113 $im = imagecreatefromstring($rawdata);
114 if ($im === false) {
115 Tools::logm('error while regenerating image ' . $fullpath);
116 return false;
117 }
118
119 switch ($imageinfo['mime']) {
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);
132
133 return $result;
134 }
135
136 /**
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 }
148
149 $article_directory = $assets_path . $id;
150 if (!is_dir($article_directory)) {
151 mkdir($article_directory, 0715);
152 }
153
154 return $article_directory;
155 }
156
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);
171 }
172 }
173 }