155a8a4bc56eed7f94fbfa64017213f42c9147be
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 /** Class to store per user and per category files
33 static public $lastError;
40 public function __construct($forlife, $category, $filename = null
)
42 $this->file_id
= $filename;
43 $this->category
= $category;
44 $this->forlife
= $forlife;
45 $this->filename
= $this->makeFilename($this->file_id
);
46 $this->checkContentType();
49 private function makeFilename($file_id)
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
);
58 $filename .= $this->forlife
. '--' . $this->category
;
60 $filename .= '--' . $file_id;
65 private function checkContentType()
67 if ($this->exists()) {
68 $this->type
= trim(mime_content_type($this->filename
));
72 public function upload(array &$file)
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') . ')';
80 case UPLOAD_ERR_PARTIAL
: case UPLOAD_ERR_NO_FILE
:
81 PlUpload
::$lastError .= 'le fichier n\'a pas été transmis intégralement';
84 PlUpload
::$lastError .= 'erreur interne';
89 if (!is_uploaded_file($file['tmp_name'])) {
91 } else if (!move_uploaded_file($file['tmp_name'], $this->filename
)) {
94 $this->checkContentType();
98 public function copyFrom($filename)
100 if (!copy($filename, $this->filename
)) {
103 $this->checkContentType();
107 public function download($url)
109 if (!$url || @parse_url
($url) === false
) {
110 trigger_error('malformed URL given', E_USER_NOTICE
);
113 $data = file_get_contents($url);
117 if (!file_put_contents($this->filename
, $data)) {
120 $this->checkContentType();
124 static public function &get(array &$file, $forlife, $category, $uniq = false
)
126 $upload = new PlUpload($forlife, $category, $uniq ? null
: $file['name']);
127 if (!$upload->upload($file)) {
135 @unlink
($this->filename
);
139 public function rename($fn)
141 if (!$this->file_id
) {
144 $filename = $this->makeFilename($fn);
145 if (rename($this->filename
)) {
146 $this->filename
= $filename;
147 $this->file_id
= $fn;
154 public function exists()
156 return file_exists($this->filename
);
159 static public function listRawFiles($forlife = '*', $category = '*', $uniq = false
, $basename = false
)
162 $filename = $globals->spoolroot
. '/spool/uploads/temp/';
163 $filename .= $forlife . '--' . $category;
167 $files = glob($filename);
169 $files = array_map('basename', $files);
174 static public function listFilenames($forlife = '*', $category = '*')
176 $files = PlUpload
::listRawFiles($forlife, $category, false
, true
);
177 foreach ($files as &$name) {
178 list($forlife, $cat, $fn) = explode('--', $name, 3);
184 static public function &listFiles($forlife = '*', $category = '*', $uniq = false
)
187 $files = PlUpload
::listRawFiles($forlife, $category, $uniq, true
);
188 foreach ($files as $name) {
189 list($forlife, $cat, $fn) = explode('--', $name, 3);
190 $res[$fn] = new PlUpload($forlife, $cat, $fn);
195 static public function clear($user = '*', $category = '*', $uniq = false
)
197 $files = PlUpload
::listRawFiles($user, $category, $uniq, false
);
198 array_map('unlink', $files);
201 public function contentType()
206 public function isType($type, $subtype = null
)
208 list($mytype, $mysubtype) = explode('/', $this->type
);
209 if ($mytype != $type ||
($subtype && $mysubtype != $subtype)) {
215 public function imageInfo()
219 $tmpmap = array (IMG_GIF
=> 'gif', IMG_JPG
=> 'jpeg', IMG_PNG
=> 'png', IMG_WBMP
=> 'bmp', IMG_XPM
=> 'xpm');
221 $supported = imagetypes();
222 foreach ($tmpmap as $type=>$mime) {
223 if ($supported & $type) {
228 $array = getimagesize($this->filename
);
229 $array[2] = @$map[$array[2]];
231 list($image, $type) = explode('/', $array['mime']);
235 trigger_error('unknown image type', E_USER_NOTICE
);
241 public function resizeImage($max_x = -1, $max_y = -1, $min_x = 0, $min_y = 0, $maxsize = -1)
243 if (!$this->exists() ||
strpos($this->type
, 'image/') !== 0) {
244 trigger_error('not an image', E_USER_NOTICE
);
247 $image_infos = $this->imageInfo();
249 trigger_error('invalid image', E_USER_NOTICE
);
252 list($this->x
, $this->y
, $mimetype) = $image_infos;
259 if ($maxsize == -1) {
260 $maxsize = filesize($this->filename
);
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
));
266 trigger_error('too large image, can\'t be resized', E_USER_NOTICE
);
273 $ny = intval($ny*$max_x/$nx);
277 $nx = intval($nx*$max_y/$ny);
281 $ny = intval($ny*$min_x/$nx);
285 $nx = intval($nx * $min_y/$ny);
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);
296 } while (filesize($this->filename
) > $maxsize && $comp > 0);
297 $this->type
= 'image/jpeg';
304 public function getContents()
306 if ($this->exists()) {
307 return file_get_contents($this->filename
);
312 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: