2 /***************************************************************************
3 * Copyright (C) 2003-2007 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
38 public function __construct($forlife, $category, $filename = null
)
40 $this->file_id
= $filename;
41 $this->category
= $category;
42 $this->forlife
= $forlife;
43 $this->filename
= $this->makeFilename($this->file_id
);
44 $this->checkContentType();
47 private function makeFilename($file_id)
50 $filename = $globals->spoolroot
. '/spool/uploads/temp/';
51 if (!file_exists($filename)) {
52 if (!mkdir($filename)) {
53 trigger_error('can\'t create upload directory: ' . $filename, E_USER_ERROR
);
56 $filename .= $this->forlife
. '-' . $this->category
;
58 $filename .= '-' . $file_id;
63 private function checkContentType()
65 if ($this->exists()) {
66 $this->type
= trim(mime_content_type($this->filename
));
70 public function upload(array &$file)
72 if (!is_uploaded_file($file['tmp_name'])) {
74 } else if (!move_uploaded_file($file['tmp_name'], $this->filename
)) {
77 $this->checkContentType();
81 public function copyFrom($filename)
83 if (!copy($filename, $this->filename
)) {
86 $this->checkContentType();
90 public function download($url)
92 if (!$url || @parse_url
($url) === false
) {
93 trigger_error('malformed URL given', E_USER_NOTICE
);
96 $data = file_get_contents($url);
100 if (!file_put_contents($this->filename
, $data)) {
103 $this->checkContentType();
107 static public function &get(array &$file, $forlife, $category, $uniq = false
)
109 $upload = new PlUpload($forlife, $category, $uniq ? null
: $file['name']);
110 if (!$upload->upload($file)) {
118 @unlink
($this->filename
);
122 public function rename($fn)
124 if (!$this->file_id
) {
127 $filename = $this->makeFilename($fn);
128 if (rename($this->filename
)) {
129 $this->filename
= $filename;
130 $this->file_id
= $fn;
137 public function exists()
139 return file_exists($this->filename
);
142 static public function listRawFiles($forlife = '*', $category = '*', $uniq = false
, $basename = false
)
145 $filename = $globals->spoolroot
. '/spool/uploads/temp/';
146 $filename .= $forlife . '-' . $category;
150 $files = glob($filename);
152 $files = array_map('basename', $files);
157 static public function listFilenames($forlife = '*', $category = '*')
159 $files = PlUpload
::listRawFiles($forlife, $category, false
, true
);
160 foreach ($files as &$name) {
161 list($forlife, $cat, $fn) = explode('-', $name, 3);
167 static public function &listFiles($forlife = '*', $category = '*', $uniq = false
)
170 $files = PlUpload
::listRawFiles($forlife, $category, $uniq, true
);
171 foreach ($files as $name) {
172 list($forlife, $cat, $fn) = explode('-', $name, 3);
173 $res[$fn] = new PlUpload($forlife, $cat, $fn);
178 static public function clear($user = '*', $category = '*', $uniq = false
)
180 $files = PlUpload
::listRawFiles($user, $category, $uniq, false
);
181 array_map('unlink', $files);
184 public function contentType()
189 public function isType($type, $subtype = null
)
191 list($mytype, $mysubtype) = explode('/', $this->type
);
192 if ($mytype != $type ||
($subtype && $mysubtype != $subtype)) {
198 public function imageInfo()
202 $map = array (1 => 'gif', 2 => 'jpeg', 3 => 'png');
204 $array = getimagesize($this->filename
);
205 $array[2] = @$map[$array[2]];
207 trigger_error('unknown image type', E_USER_NOTICE
);
213 public function resizeImage($max_x = -1, $max_y = -1, $min_x = 0, $min_y = 0, $maxsize = -1)
215 if (!$this->exists() ||
strpos($this->type
, 'image/') !== 0) {
216 trigger_error('not an image', E_USER_NOTICE
);
219 $image_infos = $this->imageInfo();
221 trigger_error('invalid image', E_USER_NOTICE
);
224 list($this->x
, $this->y
, $mimetype) = $image_infos;
231 if ($maxsize == -1) {
232 $maxsize = filesize($this->filename
);
234 if (filesize($this->filename
) > $maxsize ||
$this->x
> $max_x ||
$this->y
> $max_y
235 ||
$this->x
< $min_x ||
$this->y
< $min_y) {
236 $img = imagecreatefromstring(file_get_contents($this->filename
));
238 trigger_error('too large image, can\'t be resized', E_USER_NOTICE
);
245 $ny = intval($ny*$max_x/$nx);
249 $nx = intval($nx*$max_y/$ny);
253 $ny = intval($ny*$min_x/$nx);
257 $nx = intval($nx * $min_y/$ny);
263 $img2 = imagecreatetruecolor($nx, $ny);
264 imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $this->x
, $this->y
);
265 imagejpeg($img2, $this->filename
, $comp);
268 } while (filesize($this->filename
) > $maxsize && $comp > 0);
269 $this->type
= 'image/jpeg';
276 public function getContents()
278 if ($this->exists()) {
279 return file_get_contents($this->filename
);
284 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: