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
14 private function __construct()
20 * Changing pictures URL in article content
22 public static function filterPicture($content, $url, $id)
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;
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
40 if (self
::_downloadPictures($absolute_path, $fullpath) === true) {
41 $content = str_replace($matches[$i][2], $fullpath, $content);
44 $processing_pictures[] = $absolute_path;
54 private static function _getAbsoluteLink($relativeLink, $url)
56 /* return if already absolute URL */
57 if (parse_url($relativeLink, PHP_URL_SCHEME
) != '') return $relativeLink;
59 /* queries and anchors */
60 if ($relativeLink[0]=='#' || $relativeLink[0]=='?') return $url . $relativeLink;
62 /* parse base URL and convert to local variables:
63 $scheme, $host, $path */
64 extract(parse_url($url));
66 /* remove non-directory element from path */
67 $path = preg_replace('#/[^/]*$#', '', $path);
69 /* destroy path if relative url points to root */
70 if ($relativeLink[0] == '/') $path = '';
72 /* dirty absolute URL */
73 $abs = $host . $path . '/' . $relativeLink;
75 /* replace '//' or '/./' or '/foo/../' with '/' */
76 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
77 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
79 /* absolute URL is ready! */
80 return $scheme.'://'.$abs;
84 * Downloading pictures
86 * @return bool true if the download and processing is OK, false else
88 private static function _downloadPictures($absolute_path, $fullpath)
90 $rawdata = Tools
::getFile($absolute_path);
91 $fullpath = urldecode($fullpath);
93 if(file_exists($fullpath)) {
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);
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);
113 $im = imagecreatefromstring($rawdata);
115 Tools
::logm('error while regenerating image ' . $fullpath);
119 switch ($imageinfo['mime']) {
121 $result = imagegif($im, $fullpath);
125 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
128 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
137 * Create a directory for an article
139 * @param $id ID of the article
142 private static function _createAssetsDirectory($id)
144 $assets_path = ABS_PATH
;
145 if (!is_dir($assets_path)) {
146 mkdir($assets_path, 0715);
149 $article_directory = $assets_path . $id;
150 if (!is_dir($article_directory)) {
151 mkdir($article_directory, 0715);
154 return $article_directory;
158 * Remove the directory
163 public static function removeDirectory($directory)
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");
170 return rmdir($directory);