3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas Lœuillet <nicolas@loeuillet.org>
8 * @license http://www.wtfpl.net/ see COPYING file
12 * On modifie les URLS des images dans le corps de l'article
14 function filtre_picture($content, $url, $id)
17 $processing_pictures = array(); // list of processing image to avoid processing the same pictures twice
18 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER
);
19 foreach($matches as $i => $link) {
20 $link[1] = trim($link[1]);
21 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) {
22 $absolute_path = get_absolute_link($link[2],$url);
23 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
24 $directory = create_assets_directory($id);
25 $fullpath = $directory . '/' . $filename;
27 if (in_array($absolute_path, $processing_pictures) === true) {
28 // replace picture's URL only if processing is OK : already processing -> go to next picture
32 if (download_pictures($absolute_path, $fullpath) === true) {
33 $content = str_replace($matches[$i][2], $fullpath, $content);
36 $processing_pictures[] = $absolute_path;
45 * Retourne le lien absolu
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;
51 /* queries and anchors */
52 if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link;
54 /* parse base URL and convert to local variables:
55 $scheme, $host, $path */
56 extract(parse_url($url));
58 /* remove non-directory element from path */
59 $path = preg_replace('#/[^/]*$#', '', $path);
61 /* destroy path if relative url points to root */
62 if ($relative_link[0] == '/') $path = '';
64 /* dirty absolute URL */
65 $abs = $host . $path . '/' . $relative_link;
67 /* replace '//' or '/./' or '/foo/../' with '/' */
68 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
69 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
71 /* absolute URL is ready! */
72 return $scheme.'://'.$abs;
76 * Téléchargement des images
78 * @return bool true if the download and processing is OK, false else
80 function download_pictures($absolute_path, $fullpath)
82 $rawdata = Tools
::getFile($absolute_path);
83 $fullpath = urldecode($fullpath);
85 if(file_exists($fullpath)) {
90 $file_ext = strrchr($fullpath, '.');
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);
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);
105 $im = imagecreatefromstring($rawdata);
107 Tools
::logm('error while regenerating image ' . $fullpath);
111 switch ($imageinfo['mime']) {
113 $result = imagegif($im, $fullpath);
117 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
120 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
129 * Crée un répertoire de médias pour l'article
131 function create_assets_directory($id)
133 $assets_path = ABS_PATH
;
134 if(!is_dir($assets_path)) {
135 mkdir($assets_path, 0715);
138 $article_directory = $assets_path . $id;
139 if(!is_dir($article_directory)) {
140 mkdir($article_directory, 0715);
143 return $article_directory;
147 * Suppression du répertoire d'images
149 function remove_directory($directory)
151 if(is_dir($directory)) {
152 $files = array_diff(scandir($directory), array('.','..'));
153 foreach ($files as $file) {
154 (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file");
156 return rmdir($directory);