]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/pochePictures.php
f10cc25e929be32ea78144e59c16b879f8fd1629
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 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER
);
18 foreach($matches as $i => $link) {
19 $link[1] = trim($link[1]);
20 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1])) {
21 $absolute_path = get_absolute_link($link[2],$url);
22 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
23 $directory = create_assets_directory($id);
24 $fullpath = $directory . '/' . $filename;
25 download_pictures($absolute_path, $fullpath);
26 $content = str_replace($matches[$i][2], $fullpath, $content);
35 * Retourne le lien absolu
37 function get_absolute_link($relative_link, $url) {
38 /* return if already absolute URL */
39 if (parse_url($relative_link, PHP_URL_SCHEME
) != '') return $relative_link;
41 /* queries and anchors */
42 if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link;
44 /* parse base URL and convert to local variables:
45 $scheme, $host, $path */
46 extract(parse_url($url));
48 /* remove non-directory element from path */
49 $path = preg_replace('#/[^/]*$#', '', $path);
51 /* destroy path if relative url points to root */
52 if ($relative_link[0] == '/') $path = '';
54 /* dirty absolute URL */
55 $abs = $host . $path . '/' . $relative_link;
57 /* replace '//' or '/./' or '/foo/../' with '/' */
58 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
59 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
61 /* absolute URL is ready! */
62 return $scheme.'://'.$abs;
66 * Téléchargement des images
68 function download_pictures($absolute_path, $fullpath)
70 $rawdata = Tools
::getFile($absolute_path);
71 $fullpath = urldecode($fullpath);
73 if(file_exists($fullpath)) {
78 $file_ext = strrchr($fullpath, '.');
79 $whitelist = array(".jpg",".jpeg",".gif",".png");
80 if (!(in_array($file_ext, $whitelist))) {
81 Tools
::logm('processed image with not allowed extension. Skipping ' . $fullpath);
84 $imageinfo = getimagesize($absolute_path);
85 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') {
86 Tools
::logm('processed image with bad header. Skipping ' . $fullpath);
89 $im = imagecreatefromstring($rawdata);
91 switch ($imageinfo['mime']) {
93 imagegif($im, $fullpath);
97 imagejpeg($im, $fullpath); // default quality is 75%
100 imagepng($im, $fullpath);
105 Tools
::logm('error while regenerating image ' . $fullpath);
112 * Crée un répertoire de médias pour l'article
114 function create_assets_directory($id)
116 $assets_path = ABS_PATH
;
117 if(!is_dir($assets_path)) {
118 mkdir($assets_path, 0715);
121 $article_directory = $assets_path . $id;
122 if(!is_dir($article_directory)) {
123 mkdir($article_directory, 0715);
126 return $article_directory;
130 * Suppression du répertoire d'images
132 function remove_directory($directory)
134 if(is_dir($directory)) {
135 $files = array_diff(scandir($directory), array('.','..'));
136 foreach ($files as $file) {
137 (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file");
139 return rmdir($directory);