Fixes vim mode line.
[platal.git] / classes / plupload.php
CommitLineData
abe7e055 1<?php
2/***************************************************************************
e92ecb8c 3 * Copyright (C) 2003-2011 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 */
24class PlUpload
25{
285cd12d 26 private $hruid;
abe7e055 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
285cd12d 40 public function __construct($hruid, $category, $filename = null)
abe7e055 41 {
42 $this->file_id = $filename;
43 $this->category = $category;
285cd12d 44 $this->hruid = $hruid;
abe7e055 45 $this->filename = $this->makeFilename($this->file_id);
46 $this->checkContentType();
47 }
48
49 private function makeFilename($file_id)
50 {
51 global $globals;
c31c4e6a 52 $filename = $globals->spoolroot . '/spool/tmp/';
abe7e055 53 if (!file_exists($filename)) {
54 if (!mkdir($filename)) {
55 trigger_error('can\'t create upload directory: ' . $filename, E_USER_ERROR);
56 }
57 }
285cd12d 58 $filename .= $this->hruid . '--' . $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));
fa3300aa 69 if ($this->type == 'text/plain') { // Workaround a bug of php 5.2.0+etch10 (mime_content_type fallback is 'text/plain')
19ad5b5d 70 $this->type = preg_replace('/;.*/', '', trim(shell_exec('file -bi ' . escapeshellarg($this->filename))));
fa3300aa 71 }
abe7e055 72 }
73 }
74
75 public function upload(array &$file)
76 {
380d35f4
FB
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') . ')';
82 break;
83 case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE:
84 PlUpload::$lastError .= 'le fichier n\'a pas été transmis intégralement';
85 break;
86 default:
87 PlUpload::$lastError .= 'erreur interne';
88 break;
89 }
90 return false;
91 }
abe7e055 92 if (!is_uploaded_file($file['tmp_name'])) {
93 return false;
94 } else if (!move_uploaded_file($file['tmp_name'], $this->filename)) {
95 return false;
96 }
eaf30d86 97 $this->checkContentType();
abe7e055 98 return true;
99 }
100
101 public function copyFrom($filename)
102 {
103 if (!copy($filename, $this->filename)) {
104 return false;
105 }
106 $this->checkContentType();
107 return true;
108 }
109
110 public function download($url)
111 {
02838718 112 if (!$url || @parse_url($url) === false) {
113 trigger_error('malformed URL given', E_USER_NOTICE);
114 return false;
115 }
adf0fa21
SJ
116 if (file_exists($url)) {
117 $data = file_get_contents($url);
118 } else {
119 return false;
120 }
abe7e055 121 if (!$data) {
122 return false;
123 }
124 if (!file_put_contents($this->filename, $data)) {
125 return false;
126 }
127 $this->checkContentType();
128 return true;
129 }
130
285cd12d 131 static public function &get(array &$file, $hruid, $category, $uniq = false)
abe7e055 132 {
285cd12d 133 $upload = new PlUpload($hruid, $category, $uniq ? null : $file['name']);
abe7e055 134 if (!$upload->upload($file)) {
135 $upload = null;
136 }
137 return $upload;
138 }
139
140 public function rm()
141 {
142 @unlink($this->filename);
f62bd784 143 @clearstatcache();
abe7e055 144 }
145
146 public function rename($fn)
147 {
148 if (!$this->file_id) {
149 return false;
150 }
151 $filename = $this->makeFilename($fn);
152 if (rename($this->filename)) {
153 $this->filename = $filename;
154 $this->file_id = $fn;
155 clearstatcache();
156 return true;
157 }
158 return false;
159 }
160
161 public function exists()
162 {
163 return file_exists($this->filename);
164 }
165
285cd12d 166 static public function listRawFiles($hruid = '*', $category = '*', $uniq = false, $basename = false)
abe7e055 167 {
168 global $globals;
c31c4e6a 169 $filename = $globals->spoolroot . '/spool/tmp/';
285cd12d 170 $filename .= $hruid . '--' . $category;
da419622 171 if (!$uniq) {
fac5d9eb 172 $filename .= '--*';
da419622 173 }
abe7e055 174 $files = glob($filename);
175 if ($basename) {
da419622 176 $files = array_map('basename', $files);
177 }
178 return $files;
179 }
180
285cd12d 181 static public function listFilenames($hruid = '*', $category = '*')
da419622 182 {
285cd12d 183 $files = PlUpload::listRawFiles($hruid, $category, false, true);
da419622 184 foreach ($files as &$name) {
285cd12d 185 list($hruid, $cat, $fn) = explode('--', $name, 3);
da419622 186 $name = $fn;
abe7e055 187 }
188 return $files;
189 }
190
285cd12d 191 static public function &listFiles($hruid = '*', $category = '*', $uniq = false)
da419622 192 {
193 $res = array();
285cd12d 194 $files = PlUpload::listRawFiles($hruid, $category, $uniq, true);
da419622 195 foreach ($files as $name) {
285cd12d
VZ
196 list($hruid, $cat, $fn) = explode('--', $name, 3);
197 $res[$fn] = new PlUpload($hruid, $cat, $fn);
da419622 198 }
199 return $res;
200 }
201
202 static public function clear($user = '*', $category = '*', $uniq = false)
abe7e055 203 {
da419622 204 $files = PlUpload::listRawFiles($user, $category, $uniq, false);
205 array_map('unlink', $files);
abe7e055 206 }
207
208 public function contentType()
209 {
210 return $this->type;
211 }
212
02838718 213 public function isType($type, $subtype = null)
214 {
215 list($mytype, $mysubtype) = explode('/', $this->type);
216 if ($mytype != $type || ($subtype && $mysubtype != $subtype)) {
217 return false;
218 }
219 return true;
220 }
221
abe7e055 222 public function imageInfo()
223 {
02838718 224 static $map;
225 if (!isset($map)) {
9e1b4320 226 $tmpmap = array (IMG_GIF => 'gif', IMG_JPG => 'jpeg', IMG_PNG => 'png', IMG_WBMP => 'bmp', IMG_XPM => 'xpm');
227 $map = array();
228 $supported = imagetypes();
229 foreach ($tmpmap as $type=>$mime) {
230 if ($supported & $type) {
231 $map[$type] = $mime;
232 }
233 }
02838718 234 }
235 $array = getimagesize($this->filename);
236 $array[2] = @$map[$array[2]];
237 if (!$array[2]) {
ff95a302
FB
238 list($image, $type) = explode('/', $array['mime']);
239 $array[2] = $type;
240 }
241 if (!$array[2]) {
02838718 242 trigger_error('unknown image type', E_USER_NOTICE);
243 return null;
244 }
245 return $array;
abe7e055 246 }
247
248 public function resizeImage($max_x = -1, $max_y = -1, $min_x = 0, $min_y = 0, $maxsize = -1)
249 {
250 if (!$this->exists() || strpos($this->type, 'image/') !== 0) {
251 trigger_error('not an image', E_USER_NOTICE);
252 return false;
253 }
254 $image_infos = $this->imageInfo();
02838718 255 if (!$image_infos) {
abe7e055 256 trigger_error('invalid image', E_USER_NOTICE);
257 return false;
258 }
259 list($this->x, $this->y, $mimetype) = $image_infos;
abe7e055 260 if ($max_x == -1) {
261 $max_x = $this->x;
262 }
263 if ($max_y == -1) {
264 $max_y = $this->y;
265 }
266 if ($maxsize == -1) {
267 $maxsize = filesize($this->filename);
268 }
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));
272 if (!$img) {
273 trigger_error('too large image, can\'t be resized', E_USER_NOTICE);
274 return false;
275 }
276
277 $nx = $this->x;
278 $ny = $this->y;
279 if ($nx > $max_x) {
280 $ny = intval($ny*$max_x/$nx);
281 $nx = $max_x;
282 }
283 if ($ny > $max_y) {
284 $nx = intval($nx*$max_y/$ny);
285 $ny = $max_y;
286 }
287 if ($nx < $min_x) {
288 $ny = intval($ny*$min_x/$nx);
289 $nx = $min_x;
290 }
291 if ($ny < $min_y) {
292 $nx = intval($nx * $min_y/$ny);
293 $ny = $min_y;
294 }
295
296 $comp = 90;
297 do {
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);
301 $comp --;
302 clearstatcache();
303 } while (filesize($this->filename) > $maxsize && $comp > 0);
304 $this->type = 'image/jpeg';
305 $this->x = $nx;
306 $this->y = $ny;
307 }
308 return true;
309 }
310
311 public function getContents()
312 {
313 if ($this->exists()) {
314 return file_get_contents($this->filename);
315 }
316 return null;
317 }
318}
fa7ffd66 319// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
abe7e055 320?>