3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
8 * @license http://opensource.org/licenses/MIT see COPYING file
15 * Changing pictures URL in article content
17 public static function filterPicture($content, $url, $id)
20 $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice
21 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER
);
22 foreach($matches as $i => $link) {
23 $link[1] = trim($link[1]);
24 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) {
25 $absolute_path = self
::_getAbsoluteLink($link[2], $url);
26 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
27 $directory = self
::_createAssetsDirectory($id);
28 $fullpath = $directory . '/' . $filename;
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
35 if (self
::_downloadPictures($absolute_path, $fullpath) === true) {
36 $content = str_replace($matches[$i][2], Tools
::getPocheUrl() . $fullpath, $content);
39 $processing_pictures[] = $absolute_path;
49 private static function _getAbsoluteLink($relativeLink, $url)
51 /* return if already absolute URL */
52 if (parse_url($relativeLink, PHP_URL_SCHEME
) != '') return $relativeLink;
54 /* queries and anchors */
55 if ($relativeLink[0]=='#' || $relativeLink[0]=='?') return $url . $relativeLink;
57 /* parse base URL and convert to local variables:
58 $scheme, $host, $path */
59 extract(parse_url($url));
61 /* remove non-directory element from path */
62 $path = preg_replace('#/[^/]*$#', '', $path);
64 /* destroy path if relative url points to root */
65 if ($relativeLink[0] == '/') $path = '';
67 /* dirty absolute URL */
68 $abs = $host . $path . '/' . $relativeLink;
70 /* replace '//' or '/./' or '/foo/../' with '/' */
71 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
72 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
74 /* absolute URL is ready! */
75 return $scheme.'://'.$abs;
79 * Downloading pictures
81 * @return bool true if the download and processing is OK, false else
83 private static function _downloadPictures($absolute_path, $fullpath)
85 $rawdata = Tools
::getFile($absolute_path);
86 $fullpath = urldecode($fullpath);
88 if(file_exists($fullpath)) {
93 $file_ext = strrchr($fullpath, '.');
94 $whitelist = array(".jpg",".jpeg",".gif",".png");
95 if (!(in_array($file_ext, $whitelist))) {
96 Tools
::logm('processed image with not allowed extension. Skipping ' . $fullpath);
101 $imageinfo = getimagesize($absolute_path);
102 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') {
103 Tools
::logm('processed image with bad header. Skipping ' . $fullpath);
108 $im = imagecreatefromstring($rawdata);
110 Tools
::logm('error while regenerating image ' . $fullpath);
114 switch ($imageinfo['mime']) {
116 $result = imagegif($im, $fullpath);
120 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
123 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
132 * Create a directory for an article
134 * @param $id ID of the article
137 private static function _createAssetsDirectory($id)
139 $assets_path = ABS_PATH
;
140 if (!is_dir($assets_path)) {
141 mkdir($assets_path, 0715);
144 $article_directory = $assets_path . $id;
145 if (!is_dir($article_directory)) {
146 mkdir($article_directory, 0715);
149 return $article_directory;
153 * Remove the directory
158 public static function removeDirectory($directory)
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");
165 return rmdir($directory);