2 /***************************************************************************
3 * Copyright (C) 2003-2010 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($hruid, $category, $filename = null
)
42 $this->file_id
= $filename;
43 $this->category
= $category;
44 $this->hruid
= $hruid;
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->hruid
. '--' . $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 if (file_exists($url)) {
117 $data = file_get_contents($url);
124 if (!file_put_contents($this->filename
, $data)) {
127 $this->checkContentType();
131 static public function &get(array &$file, $hruid, $category, $uniq = false
)
133 $upload = new PlUpload($hruid, $category, $uniq ? null
: $file['name']);
134 if (!$upload->upload($file)) {
142 @unlink
($this->filename
);
146 public function rename($fn)
148 if (!$this->file_id
) {
151 $filename = $this->makeFilename($fn);
152 if (rename($this->filename
)) {
153 $this->filename
= $filename;
154 $this->file_id
= $fn;
161 public function exists()
163 return file_exists($this->filename
);
166 static public function listRawFiles($hruid = '*', $category = '*', $uniq = false
, $basename = false
)
169 $filename = $globals->spoolroot
. '/spool/tmp/';
170 $filename .= $hruid . '--' . $category;
174 $files = glob($filename);
176 $files = array_map('basename', $files);
181 static public function listFilenames($hruid = '*', $category = '*')
183 $files = PlUpload
::listRawFiles($hruid, $category, false
, true
);
184 foreach ($files as &$name) {
185 list($hruid, $cat, $fn) = explode('--', $name, 3);
191 static public function &listFiles($hruid = '*', $category = '*', $uniq = false
)
194 $files = PlUpload
::listRawFiles($hruid, $category, $uniq, true
);
195 foreach ($files as $name) {
196 list($hruid, $cat, $fn) = explode('--', $name, 3);
197 $res[$fn] = new PlUpload($hruid, $cat, $fn);
202 static public function clear($user = '*', $category = '*', $uniq = false
)
204 $files = PlUpload
::listRawFiles($user, $category, $uniq, false
);
205 array_map('unlink', $files);
208 public function contentType()
213 public function isType($type, $subtype = null
)
215 list($mytype, $mysubtype) = explode('/', $this->type
);
216 if ($mytype != $type ||
($subtype && $mysubtype != $subtype)) {
222 public function imageInfo()
226 $tmpmap = array (IMG_GIF
=> 'gif', IMG_JPG
=> 'jpeg', IMG_PNG
=> 'png', IMG_WBMP
=> 'bmp', IMG_XPM
=> 'xpm');
228 $supported = imagetypes();
229 foreach ($tmpmap as $type=>$mime) {
230 if ($supported & $type) {
235 $array = getimagesize($this->filename
);
236 $array[2] = @$map[$array[2]];
238 list($image, $type) = explode('/', $array['mime']);
242 trigger_error('unknown image type', E_USER_NOTICE
);
248 public function resizeImage($max_x = -1, $max_y = -1, $min_x = 0, $min_y = 0, $maxsize = -1)
250 if (!$this->exists() ||
strpos($this->type
, 'image/') !== 0) {
251 trigger_error('not an image', E_USER_NOTICE
);
254 $image_infos = $this->imageInfo();
256 trigger_error('invalid image', E_USER_NOTICE
);
259 list($this->x
, $this->y
, $mimetype) = $image_infos;
266 if ($maxsize == -1) {
267 $maxsize = filesize($this->filename
);
269 if (filesize($this->filename
) > $maxsize ||
$this->x
> $max_x ||
$this->y
> $max_y
270 ||
$this->x
< $min_x ||
$this->y
< $min_y) {
271 $img = imagecreatefromstring(file_get_contents($this->filename
));
273 trigger_error('too large image, can\'t be resized', E_USER_NOTICE
);
280 $ny = intval($ny*$max_x/$nx);
284 $nx = intval($nx*$max_y/$ny);
288 $ny = intval($ny*$min_x/$nx);
292 $nx = intval($nx * $min_y/$ny);
298 $img2 = imagecreatetruecolor($nx, $ny);
299 imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $this->x
, $this->y
);
300 imagejpeg($img2, $this->filename
, $comp);
303 } while (filesize($this->filename
) > $maxsize && $comp > 0);
304 $this->type
= 'image/jpeg';
311 public function getContents()
313 if ($this->exists()) {
314 return file_get_contents($this->filename
);
319 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: