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/tmp/';
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
));
69 if ($this->type
== 'text/plain') { // Workaround a bug of php 5.2.0+etch10 (mime_content_type fallback is 'text/plain')
70 $this->type
= preg_replace('/;.*/', '', trim(shell_exec('file -bi ' . escapeshellarg($this->filename
))));
75 public function upload(array &$file)
77 if (@$file['error']) {
78 PlUpload
::$lastError = 'Erreur de téléchargement de ' . $file['name'] . ' : ';
79 switch ($file['error']) {
80 case UPLOAD_ERR_INI_SIZE
: case UPLOAD_ERR_FORM_SIZE
:
81 PlUpload
::$lastError .= 'le fichier est trop gros (limite : ' . ini_get('upload_max_filesize') . ')';
83 case UPLOAD_ERR_PARTIAL
: case UPLOAD_ERR_NO_FILE
:
84 PlUpload
::$lastError .= 'le fichier n\'a pas été transmis intégralement';
87 PlUpload
::$lastError .= 'erreur interne';
92 if (!is_uploaded_file($file['tmp_name'])) {
94 } else if (!move_uploaded_file($file['tmp_name'], $this->filename
)) {
97 $this->checkContentType();
101 public function copyFrom($filename)
103 if (!copy($filename, $this->filename
)) {
106 $this->checkContentType();
110 public function download($url)
112 if (!$url || @parse_url
($url) === false
) {
113 trigger_error('malformed URL given', E_USER_NOTICE
);
116 $data = file_get_contents($url);
120 if (!file_put_contents($this->filename
, $data)) {
123 $this->checkContentType();
127 static public function &get(array &$file, $forlife, $category, $uniq = false
)
129 $upload = new PlUpload($forlife, $category, $uniq ? null
: $file['name']);
130 if (!$upload->upload($file)) {
138 @unlink
($this->filename
);
142 public function rename($fn)
144 if (!$this->file_id
) {
147 $filename = $this->makeFilename($fn);
148 if (rename($this->filename
)) {
149 $this->filename
= $filename;
150 $this->file_id
= $fn;
157 public function exists()
159 return file_exists($this->filename
);
162 static public function listRawFiles($forlife = '*', $category = '*', $uniq = false
, $basename = false
)
165 $filename = $globals->spoolroot
. '/spool/tmp/';
166 $filename .= $forlife . '--' . $category;
170 $files = glob($filename);
172 $files = array_map('basename', $files);
177 static public function listFilenames($forlife = '*', $category = '*')
179 $files = PlUpload
::listRawFiles($forlife, $category, false
, true
);
180 foreach ($files as &$name) {
181 list($forlife, $cat, $fn) = explode('--', $name, 3);
187 static public function &listFiles($forlife = '*', $category = '*', $uniq = false
)
190 $files = PlUpload
::listRawFiles($forlife, $category, $uniq, true
);
191 foreach ($files as $name) {
192 list($forlife, $cat, $fn) = explode('--', $name, 3);
193 $res[$fn] = new PlUpload($forlife, $cat, $fn);
198 static public function clear($user = '*', $category = '*', $uniq = false
)
200 $files = PlUpload
::listRawFiles($user, $category, $uniq, false
);
201 array_map('unlink', $files);
204 public function contentType()
209 public function isType($type, $subtype = null
)
211 list($mytype, $mysubtype) = explode('/', $this->type
);
212 if ($mytype != $type ||
($subtype && $mysubtype != $subtype)) {
218 public function imageInfo()
222 $tmpmap = array (IMG_GIF
=> 'gif', IMG_JPG
=> 'jpeg', IMG_PNG
=> 'png', IMG_WBMP
=> 'bmp', IMG_XPM
=> 'xpm');
224 $supported = imagetypes();
225 foreach ($tmpmap as $type=>$mime) {
226 if ($supported & $type) {
231 $array = getimagesize($this->filename
);
232 $array[2] = @$map[$array[2]];
234 list($image, $type) = explode('/', $array['mime']);
238 trigger_error('unknown image type', E_USER_NOTICE
);
244 public function resizeImage($max_x = -1, $max_y = -1, $min_x = 0, $min_y = 0, $maxsize = -1)
246 if (!$this->exists() ||
strpos($this->type
, 'image/') !== 0) {
247 trigger_error('not an image', E_USER_NOTICE
);
250 $image_infos = $this->imageInfo();
252 trigger_error('invalid image', E_USER_NOTICE
);
255 list($this->x
, $this->y
, $mimetype) = $image_infos;
262 if ($maxsize == -1) {
263 $maxsize = filesize($this->filename
);
265 if (filesize($this->filename
) > $maxsize ||
$this->x
> $max_x ||
$this->y
> $max_y
266 ||
$this->x
< $min_x ||
$this->y
< $min_y) {
267 $img = imagecreatefromstring(file_get_contents($this->filename
));
269 trigger_error('too large image, can\'t be resized', E_USER_NOTICE
);
276 $ny = intval($ny*$max_x/$nx);
280 $nx = intval($nx*$max_y/$ny);
284 $ny = intval($ny*$min_x/$nx);
288 $nx = intval($nx * $min_y/$ny);
294 $img2 = imagecreatetruecolor($nx, $ny);
295 imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $this->x
, $this->y
);
296 imagejpeg($img2, $this->filename
, $comp);
299 } while (filesize($this->filename
) > $maxsize && $comp > 0);
300 $this->type
= 'image/jpeg';
307 public function getContents()
309 if ($this->exists()) {
310 return file_get_contents($this->filename
);
315 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: