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
11 namespace Wallabag\Wallabag
;
16 * Changing pictures URL in article content
18 public static function filterPicture($content, $url, $id)
21 $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice
22 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER
);
23 foreach($matches as $i => $link) {
24 $link[1] = trim($link[1]);
25 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) {
26 $absolute_path = self
::_getAbsoluteLink($link[2], $url);
27 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
28 $directory = self
::_createAssetsDirectory($id);
29 $fullpath = $directory . '/' . $filename;
31 if (in_array($absolute_path, $processing_pictures) === true) {
32 // replace picture's URL only if processing is OK : already processing -> go to next picture
36 if (self
::_downloadPictures($absolute_path, $fullpath) === true) {
37 $content = str_replace($matches[$i][2], Tools
::getPocheUrl() . $fullpath, $content);
40 $processing_pictures[] = $absolute_path;
50 private static function _getAbsoluteLink($relativeLink, $url)
52 /* return if already absolute URL */
53 if (parse_url($relativeLink, PHP_URL_SCHEME
) != '') return $relativeLink;
55 /* queries and anchors */
56 if ($relativeLink[0]=='#' || $relativeLink[0]=='?') return $url . $relativeLink;
58 /* parse base URL and convert to local variables:
59 $scheme, $host, $path */
60 extract(parse_url($url));
62 /* remove non-directory element from path */
63 $path = preg_replace('#/[^/]*$#', '', $path);
65 /* destroy path if relative url points to root */
66 if ($relativeLink[0] == '/') $path = '';
68 /* dirty absolute URL */
69 $abs = $host . $path . '/' . $relativeLink;
71 /* replace '//' or '/./' or '/foo/../' with '/' */
72 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
73 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
75 /* absolute URL is ready! */
76 return $scheme.'://'.$abs;
80 * Downloading pictures
82 * @return bool true if the download and processing is OK, false else
84 private static function _downloadPictures($absolute_path, $fullpath)
86 $rawdata = Tools
::getFile($absolute_path);
87 $fullpath = urldecode($fullpath);
89 if(file_exists($fullpath)) {
94 $file_ext = strrchr($fullpath, '.');
95 $whitelist = array(".jpg",".jpeg",".gif",".png");
96 if (!(in_array($file_ext, $whitelist))) {
97 Tools
::logm('processed image with not allowed extension. Skipping ' . $fullpath);
102 $imageinfo = getimagesize($absolute_path);
103 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') {
104 Tools
::logm('processed image with bad header. Skipping ' . $fullpath);
109 $im = imagecreatefromstring($rawdata);
111 Tools
::logm('error while regenerating image ' . $fullpath);
115 switch ($imageinfo['mime']) {
117 $result = imagegif($im, $fullpath);
121 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
124 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
133 * Create a directory for an article
135 * @param $id ID of the article
138 private static function _createAssetsDirectory($id)
140 $assets_path = ABS_PATH
;
141 if (!is_dir($assets_path)) {
142 mkdir($assets_path, 0715);
145 $article_directory = $assets_path . $id;
146 if (!is_dir($article_directory)) {
147 mkdir($article_directory, 0715);
150 return $article_directory;
154 * Remove the directory
159 public static function removeDirectory($directory)
161 if (is_dir($directory)) {
162 $files = array_diff(scandir($directory), array('.','..'));
163 foreach ($files as $file) {
164 (is_dir("$directory/$file")) ? self::removeDirectory("$directory/$file") : unlink("$directory/$file");
166 return rmdir($directory);