X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fplupload.php;h=7654830e5b2b64cce9bdd25fd7936617e362d505;hb=c291ca5486bce705cd3dfd81065cbd2d92b96da5;hp=d5f428f6b42dec530d7095c763974e7815793243;hpb=abe7e055f16e6f6e9605c2965164169babbf9b7f;p=platal.git diff --git a/classes/plupload.php b/classes/plupload.php index d5f428f..7654830 100644 --- a/classes/plupload.php +++ b/classes/plupload.php @@ -1,6 +1,6 @@ file_id = $filename; $this->category = $category; - $this->forlife = $forlife; + $this->hruid = $hruid; $this->filename = $this->makeFilename($this->file_id); $this->checkContentType(); } @@ -47,15 +49,15 @@ class PlUpload private function makeFilename($file_id) { global $globals; - $filename = $globals->spoolroot . '/spool/uploads/temp/'; + $filename = $globals->spoolroot . '/spool/tmp/'; if (!file_exists($filename)) { if (!mkdir($filename)) { trigger_error('can\'t create upload directory: ' . $filename, E_USER_ERROR); } } - $filename .= $this->forlife . '-' . $this->category; + $filename .= $this->hruid . '--' . $this->category; if ($file_id) { - $filename .= '-' . $file_id; + $filename .= '--' . $file_id; } return $filename; } @@ -64,17 +66,35 @@ class PlUpload { if ($this->exists()) { $this->type = trim(mime_content_type($this->filename)); + if ($this->type == 'text/plain') { // Workaround a bug of php 5.2.0+etch10 (mime_content_type fallback is 'text/plain') + $this->type = preg_replace('/;.*/', '', trim(shell_exec('file -bi ' . escapeshellarg($this->filename)))); + } } } public function upload(array &$file) { + if (@$file['error']) { + PlUpload::$lastError = 'Erreur de téléchargement de ' . $file['name'] . ' : '; + switch ($file['error']) { + case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: + PlUpload::$lastError .= 'le fichier est trop gros (limite : ' . ini_get('upload_max_filesize') . ')'; + break; + case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: + PlUpload::$lastError .= 'le fichier n\'a pas été transmis intégralement'; + break; + default: + PlUpload::$lastError .= 'erreur interne'; + break; + } + return false; + } if (!is_uploaded_file($file['tmp_name'])) { return false; } else if (!move_uploaded_file($file['tmp_name'], $this->filename)) { return false; } - $this->checkContentType(); + $this->checkContentType(); return true; } @@ -89,6 +109,10 @@ class PlUpload public function download($url) { + if (!$url || @parse_url($url) === false) { + trigger_error('malformed URL given', E_USER_NOTICE); + return false; + } $data = file_get_contents($url); if (!$data) { return false; @@ -100,9 +124,9 @@ class PlUpload return true; } - static public function &get(array &$file, $forlife, $category, $uniq = false) + static public function &get(array &$file, $hruid, $category, $uniq = false) { - $upload = new PlUpload($forlife, $category, $uniq ? null : $file['name']); + $upload = new PlUpload($hruid, $category, $uniq ? null : $file['name']); if (!$upload->upload($file)) { $upload = null; } @@ -112,7 +136,7 @@ class PlUpload public function rm() { @unlink($this->filename); - clearstatcache(); + @clearstatcache(); } public function rename($fn) @@ -135,22 +159,46 @@ class PlUpload return file_exists($this->filename); } - static public function listFiles($forlife = '*', $category = '*', $basename = false) + static public function listRawFiles($hruid = '*', $category = '*', $uniq = false, $basename = false) { global $globals; - $filename = $globals->spoolroot . '/spool/uploads/temp/'; - $filename .= $forlife . '-' . $category; + $filename = $globals->spoolroot . '/spool/tmp/'; + $filename .= $hruid . '--' . $category; + if (!$uniq) { + $filename .= '--*'; + } $files = glob($filename); if ($basename) { - array_walk($files, 'basename'); + $files = array_map('basename', $files); + } + return $files; + } + + static public function listFilenames($hruid = '*', $category = '*') + { + $files = PlUpload::listRawFiles($hruid, $category, false, true); + foreach ($files as &$name) { + list($hruid, $cat, $fn) = explode('--', $name, 3); + $name = $fn; } return $files; } - static public function clear($user = '*', $category = '*') + static public function &listFiles($hruid = '*', $category = '*', $uniq = false) + { + $res = array(); + $files = PlUpload::listRawFiles($hruid, $category, $uniq, true); + foreach ($files as $name) { + list($hruid, $cat, $fn) = explode('--', $name, 3); + $res[$fn] = new PlUpload($hruid, $cat, $fn); + } + return $res; + } + + static public function clear($user = '*', $category = '*', $uniq = false) { - $files = PlUpload::listFiles($user, $category, false); - array_walk($files, 'unlink'); + $files = PlUpload::listRawFiles($user, $category, $uniq, false); + array_map('unlink', $files); } public function contentType() @@ -158,9 +206,39 @@ class PlUpload return $this->type; } + public function isType($type, $subtype = null) + { + list($mytype, $mysubtype) = explode('/', $this->type); + if ($mytype != $type || ($subtype && $mysubtype != $subtype)) { + return false; + } + return true; + } + public function imageInfo() { - return getimagesize($this->filename); + static $map; + if (!isset($map)) { + $tmpmap = array (IMG_GIF => 'gif', IMG_JPG => 'jpeg', IMG_PNG => 'png', IMG_WBMP => 'bmp', IMG_XPM => 'xpm'); + $map = array(); + $supported = imagetypes(); + foreach ($tmpmap as $type=>$mime) { + if ($supported & $type) { + $map[$type] = $mime; + } + } + } + $array = getimagesize($this->filename); + $array[2] = @$map[$array[2]]; + if (!$array[2]) { + list($image, $type) = explode('/', $array['mime']); + $array[2] = $type; + } + if (!$array[2]) { + trigger_error('unknown image type', E_USER_NOTICE); + return null; + } + return $array; } public function resizeImage($max_x = -1, $max_y = -1, $min_x = 0, $min_y = 0, $maxsize = -1) @@ -170,15 +248,11 @@ class PlUpload return false; } $image_infos = $this->imageInfo(); - if (empty($image_infos)) { + if (!$image_infos) { trigger_error('invalid image', E_USER_NOTICE); return false; } list($this->x, $this->y, $mimetype) = $image_infos; - if ($mimetype < 1 || $mimetype > 3) { // 1 - gif, 2 - jpeg, 3 - png - trigger_error('unknown image type', E_USER_NOTICE); - return false; - } if ($max_x == -1) { $max_x = $this->x; }