Commit | Line | Data |
---|---|---|
abe7e055 | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
abe7e055 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | /** Class to store per user and per category files | |
23 | */ | |
24 | class PlUpload | |
25 | { | |
26 | private $forlife; | |
27 | private $category; | |
28 | private $file_id; | |
29 | ||
30 | private $filename; | |
31 | private $type; | |
32 | ||
380d35f4 FB |
33 | static public $lastError; |
34 | ||
abe7e055 | 35 | /** For images |
36 | */ | |
37 | private $x; | |
38 | private $y; | |
39 | ||
40 | public function __construct($forlife, $category, $filename = null) | |
41 | { | |
42 | $this->file_id = $filename; | |
43 | $this->category = $category; | |
44 | $this->forlife = $forlife; | |
45 | $this->filename = $this->makeFilename($this->file_id); | |
46 | $this->checkContentType(); | |
47 | } | |
48 | ||
49 | private function makeFilename($file_id) | |
50 | { | |
51 | global $globals; | |
52 | $filename = $globals->spoolroot . '/spool/uploads/temp/'; | |
53 | if (!file_exists($filename)) { | |
54 | if (!mkdir($filename)) { | |
55 | trigger_error('can\'t create upload directory: ' . $filename, E_USER_ERROR); | |
56 | } | |
57 | } | |
fac5d9eb | 58 | $filename .= $this->forlife . '--' . $this->category; |
abe7e055 | 59 | if ($file_id) { |
fac5d9eb | 60 | $filename .= '--' . $file_id; |
abe7e055 | 61 | } |
62 | return $filename; | |
63 | } | |
64 | ||
65 | private function checkContentType() | |
66 | { | |
67 | if ($this->exists()) { | |
68 | $this->type = trim(mime_content_type($this->filename)); | |
69 | } | |
70 | } | |
71 | ||
72 | public function upload(array &$file) | |
73 | { | |
380d35f4 FB |
74 | if (@$file['error']) { |
75 | PlUpload::$lastError = 'Erreur de téléchargement de ' . $file['name'] . ' : '; | |
76 | switch ($file['error']) { | |
77 | case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: | |
78 | PlUpload::$lastError .= 'le fichier est trop gros (limite : ' . ini_get('upload_max_filesize') . ')'; | |
79 | break; | |
80 | case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: | |
81 | PlUpload::$lastError .= 'le fichier n\'a pas été transmis intégralement'; | |
82 | break; | |
83 | default: | |
84 | PlUpload::$lastError .= 'erreur interne'; | |
85 | break; | |
86 | } | |
87 | return false; | |
88 | } | |
abe7e055 | 89 | if (!is_uploaded_file($file['tmp_name'])) { |
90 | return false; | |
91 | } else if (!move_uploaded_file($file['tmp_name'], $this->filename)) { | |
92 | return false; | |
93 | } | |
eaf30d86 | 94 | $this->checkContentType(); |
abe7e055 | 95 | return true; |
96 | } | |
97 | ||
98 | public function copyFrom($filename) | |
99 | { | |
100 | if (!copy($filename, $this->filename)) { | |
101 | return false; | |
102 | } | |
103 | $this->checkContentType(); | |
104 | return true; | |
105 | } | |
106 | ||
107 | public function download($url) | |
108 | { | |
02838718 | 109 | if (!$url || @parse_url($url) === false) { |
110 | trigger_error('malformed URL given', E_USER_NOTICE); | |
111 | return false; | |
112 | } | |
abe7e055 | 113 | $data = file_get_contents($url); |
114 | if (!$data) { | |
115 | return false; | |
116 | } | |
117 | if (!file_put_contents($this->filename, $data)) { | |
118 | return false; | |
119 | } | |
120 | $this->checkContentType(); | |
121 | return true; | |
122 | } | |
123 | ||
124 | static public function &get(array &$file, $forlife, $category, $uniq = false) | |
125 | { | |
126 | $upload = new PlUpload($forlife, $category, $uniq ? null : $file['name']); | |
127 | if (!$upload->upload($file)) { | |
128 | $upload = null; | |
129 | } | |
130 | return $upload; | |
131 | } | |
132 | ||
133 | public function rm() | |
134 | { | |
135 | @unlink($this->filename); | |
f62bd784 | 136 | @clearstatcache(); |
abe7e055 | 137 | } |
138 | ||
139 | public function rename($fn) | |
140 | { | |
141 | if (!$this->file_id) { | |
142 | return false; | |
143 | } | |
144 | $filename = $this->makeFilename($fn); | |
145 | if (rename($this->filename)) { | |
146 | $this->filename = $filename; | |
147 | $this->file_id = $fn; | |
148 | clearstatcache(); | |
149 | return true; | |
150 | } | |
151 | return false; | |
152 | } | |
153 | ||
154 | public function exists() | |
155 | { | |
156 | return file_exists($this->filename); | |
157 | } | |
158 | ||
da419622 | 159 | static public function listRawFiles($forlife = '*', $category = '*', $uniq = false, $basename = false) |
abe7e055 | 160 | { |
161 | global $globals; | |
162 | $filename = $globals->spoolroot . '/spool/uploads/temp/'; | |
fac5d9eb | 163 | $filename .= $forlife . '--' . $category; |
da419622 | 164 | if (!$uniq) { |
fac5d9eb | 165 | $filename .= '--*'; |
da419622 | 166 | } |
abe7e055 | 167 | $files = glob($filename); |
168 | if ($basename) { | |
da419622 | 169 | $files = array_map('basename', $files); |
170 | } | |
171 | return $files; | |
172 | } | |
173 | ||
174 | static public function listFilenames($forlife = '*', $category = '*') | |
175 | { | |
176 | $files = PlUpload::listRawFiles($forlife, $category, false, true); | |
177 | foreach ($files as &$name) { | |
fac5d9eb | 178 | list($forlife, $cat, $fn) = explode('--', $name, 3); |
da419622 | 179 | $name = $fn; |
abe7e055 | 180 | } |
181 | return $files; | |
182 | } | |
183 | ||
da419622 | 184 | static public function &listFiles($forlife = '*', $category = '*', $uniq = false) |
185 | { | |
186 | $res = array(); | |
187 | $files = PlUpload::listRawFiles($forlife, $category, $uniq, true); | |
188 | foreach ($files as $name) { | |
fac5d9eb | 189 | list($forlife, $cat, $fn) = explode('--', $name, 3); |
da419622 | 190 | $res[$fn] = new PlUpload($forlife, $cat, $fn); |
191 | } | |
192 | return $res; | |
193 | } | |
194 | ||
195 | static public function clear($user = '*', $category = '*', $uniq = false) | |
abe7e055 | 196 | { |
da419622 | 197 | $files = PlUpload::listRawFiles($user, $category, $uniq, false); |
198 | array_map('unlink', $files); | |
abe7e055 | 199 | } |
200 | ||
201 | public function contentType() | |
202 | { | |
203 | return $this->type; | |
204 | } | |
205 | ||
02838718 | 206 | public function isType($type, $subtype = null) |
207 | { | |
208 | list($mytype, $mysubtype) = explode('/', $this->type); | |
209 | if ($mytype != $type || ($subtype && $mysubtype != $subtype)) { | |
210 | return false; | |
211 | } | |
212 | return true; | |
213 | } | |
214 | ||
abe7e055 | 215 | public function imageInfo() |
216 | { | |
02838718 | 217 | static $map; |
218 | if (!isset($map)) { | |
9e1b4320 | 219 | $tmpmap = array (IMG_GIF => 'gif', IMG_JPG => 'jpeg', IMG_PNG => 'png', IMG_WBMP => 'bmp', IMG_XPM => 'xpm'); |
220 | $map = array(); | |
221 | $supported = imagetypes(); | |
222 | foreach ($tmpmap as $type=>$mime) { | |
223 | if ($supported & $type) { | |
224 | $map[$type] = $mime; | |
225 | } | |
226 | } | |
02838718 | 227 | } |
228 | $array = getimagesize($this->filename); | |
229 | $array[2] = @$map[$array[2]]; | |
230 | if (!$array[2]) { | |
ff95a302 FB |
231 | list($image, $type) = explode('/', $array['mime']); |
232 | $array[2] = $type; | |
233 | } | |
234 | if (!$array[2]) { | |
02838718 | 235 | trigger_error('unknown image type', E_USER_NOTICE); |
236 | return null; | |
237 | } | |
238 | return $array; | |
abe7e055 | 239 | } |
240 | ||
241 | public function resizeImage($max_x = -1, $max_y = -1, $min_x = 0, $min_y = 0, $maxsize = -1) | |
242 | { | |
243 | if (!$this->exists() || strpos($this->type, 'image/') !== 0) { | |
244 | trigger_error('not an image', E_USER_NOTICE); | |
245 | return false; | |
246 | } | |
247 | $image_infos = $this->imageInfo(); | |
02838718 | 248 | if (!$image_infos) { |
abe7e055 | 249 | trigger_error('invalid image', E_USER_NOTICE); |
250 | return false; | |
251 | } | |
252 | list($this->x, $this->y, $mimetype) = $image_infos; | |
abe7e055 | 253 | if ($max_x == -1) { |
254 | $max_x = $this->x; | |
255 | } | |
256 | if ($max_y == -1) { | |
257 | $max_y = $this->y; | |
258 | } | |
259 | if ($maxsize == -1) { | |
260 | $maxsize = filesize($this->filename); | |
261 | } | |
262 | if (filesize($this->filename) > $maxsize || $this->x > $max_x || $this->y > $max_y | |
263 | || $this->x < $min_x || $this->y < $min_y) { | |
264 | $img = imagecreatefromstring(file_get_contents($this->filename)); | |
265 | if (!$img) { | |
266 | trigger_error('too large image, can\'t be resized', E_USER_NOTICE); | |
267 | return false; | |
268 | } | |
269 | ||
270 | $nx = $this->x; | |
271 | $ny = $this->y; | |
272 | if ($nx > $max_x) { | |
273 | $ny = intval($ny*$max_x/$nx); | |
274 | $nx = $max_x; | |
275 | } | |
276 | if ($ny > $max_y) { | |
277 | $nx = intval($nx*$max_y/$ny); | |
278 | $ny = $max_y; | |
279 | } | |
280 | if ($nx < $min_x) { | |
281 | $ny = intval($ny*$min_x/$nx); | |
282 | $nx = $min_x; | |
283 | } | |
284 | if ($ny < $min_y) { | |
285 | $nx = intval($nx * $min_y/$ny); | |
286 | $ny = $min_y; | |
287 | } | |
288 | ||
289 | $comp = 90; | |
290 | do { | |
291 | $img2 = imagecreatetruecolor($nx, $ny); | |
292 | imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $this->x, $this->y); | |
293 | imagejpeg($img2, $this->filename, $comp); | |
294 | $comp --; | |
295 | clearstatcache(); | |
296 | } while (filesize($this->filename) > $maxsize && $comp > 0); | |
297 | $this->type = 'image/jpeg'; | |
298 | $this->x = $nx; | |
299 | $this->y = $ny; | |
300 | } | |
301 | return true; | |
302 | } | |
303 | ||
304 | public function getContents() | |
305 | { | |
306 | if ($this->exists()) { | |
307 | return file_get_contents($this->filename); | |
308 | } | |
309 | return null; | |
310 | } | |
311 | } | |
312 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
313 | ?> |