]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /** | |
3 | * wallabag, self hostable application allowing you to not miss any content anymore | |
4 | * | |
5 | * @category wallabag | |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
7 | * @copyright 2013 | |
8 | * @license http://www.wtfpl.net/ see COPYING file | |
9 | */ | |
10 | ||
11 | /** | |
12 | * On modifie les URLS des images dans le corps de l'article | |
13 | */ | |
14 | function filtre_picture($content, $url, $id) | |
15 | { | |
16 | $matches = array(); | |
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; | |
26 | ||
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 | |
29 | continue; | |
30 | } | |
31 | ||
32 | if (download_pictures($absolute_path, $fullpath) === true) { | |
33 | $content = str_replace($matches[$i][2], $fullpath, $content); | |
34 | } | |
35 | ||
36 | $processing_pictures[] = $absolute_path; | |
37 | } | |
38 | ||
39 | } | |
40 | ||
41 | return $content; | |
42 | } | |
43 | ||
44 | /** | |
45 | * Retourne le lien absolu | |
46 | */ | |
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; | |
50 | ||
51 | /* queries and anchors */ | |
52 | if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link; | |
53 | ||
54 | /* parse base URL and convert to local variables: | |
55 | $scheme, $host, $path */ | |
56 | extract(parse_url($url)); | |
57 | ||
58 | /* remove non-directory element from path */ | |
59 | $path = preg_replace('#/[^/]*$#', '', $path); | |
60 | ||
61 | /* destroy path if relative url points to root */ | |
62 | if ($relative_link[0] == '/') $path = ''; | |
63 | ||
64 | /* dirty absolute URL */ | |
65 | $abs = $host . $path . '/' . $relative_link; | |
66 | ||
67 | /* replace '//' or '/./' or '/foo/../' with '/' */ | |
68 | $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); | |
69 | for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} | |
70 | ||
71 | /* absolute URL is ready! */ | |
72 | return $scheme.'://'.$abs; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Téléchargement des images | |
77 | * | |
78 | * @return bool true if the download and processing is OK, false else | |
79 | */ | |
80 | function download_pictures($absolute_path, $fullpath) | |
81 | { | |
82 | $rawdata = Tools::getFile($absolute_path); | |
83 | $fullpath = urldecode($fullpath); | |
84 | ||
85 | if(file_exists($fullpath)) { | |
86 | unlink($fullpath); | |
87 | } | |
88 | ||
89 | // check extension | |
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); | |
94 | return false; | |
95 | } | |
96 | ||
97 | // check headers | |
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); | |
101 | return false; | |
102 | } | |
103 | ||
104 | // regenerate image | |
105 | $im = imagecreatefromstring($rawdata); | |
106 | if ($im === false) { | |
107 | Tools::logm('error while regenerating image ' . $fullpath); | |
108 | return false; | |
109 | } | |
110 | ||
111 | switch ($imageinfo['mime']) { | |
112 | case 'image/gif': | |
113 | $result = imagegif($im, $fullpath); | |
114 | break; | |
115 | case 'image/jpeg': | |
116 | case 'image/jpg': | |
117 | $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY); | |
118 | break; | |
119 | case 'image/png': | |
120 | $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY / 100 * 9)); | |
121 | break; | |
122 | } | |
123 | imagedestroy($im); | |
124 | ||
125 | return $result; | |
126 | } | |
127 | ||
128 | /** | |
129 | * Crée un répertoire de médias pour l'article | |
130 | */ | |
131 | function create_assets_directory($id) | |
132 | { | |
133 | $assets_path = ABS_PATH; | |
134 | if(!is_dir($assets_path)) { | |
135 | mkdir($assets_path, 0715); | |
136 | } | |
137 | ||
138 | $article_directory = $assets_path . $id; | |
139 | if(!is_dir($article_directory)) { | |
140 | mkdir($article_directory, 0715); | |
141 | } | |
142 | ||
143 | return $article_directory; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Suppression du répertoire d'images | |
148 | */ | |
149 | function remove_directory($directory) | |
150 | { | |
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"); | |
155 | } | |
156 | return rmdir($directory); | |
157 | } | |
158 | } |