X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fplupload.php;h=e903e92bb2d9d02a0f951934227be25968f489a1;hb=refs%2Fheads%2Fcore%2Fmaster;hp=8844bc4d900babf52bf584b47bede45afd3d7f48;hpb=fac5d9eba8c038a7c973b89ef16d32357d224371;p=platal.git diff --git a/classes/plupload.php b/classes/plupload.php index 8844bc4..e903e92 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,13 +49,13 @@ 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; } @@ -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; } @@ -93,7 +113,11 @@ class PlUpload trigger_error('malformed URL given', E_USER_NOTICE); return false; } - $data = file_get_contents($url); + if (file_exists($url)) { + $data = file_get_contents($url); + } else { + return false; + } if (!$data) { return false; } @@ -104,9 +128,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; } @@ -139,11 +163,11 @@ class PlUpload return file_exists($this->filename); } - static public function listRawFiles($forlife = '*', $category = '*', $uniq = false, $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 .= '--*'; } @@ -154,23 +178,23 @@ class PlUpload return $files; } - static public function listFilenames($forlife = '*', $category = '*') + static public function listFilenames($hruid = '*', $category = '*') { - $files = PlUpload::listRawFiles($forlife, $category, false, true); + $files = PlUpload::listRawFiles($hruid, $category, false, true); foreach ($files as &$name) { - list($forlife, $cat, $fn) = explode('--', $name, 3); + list($hruid, $cat, $fn) = explode('--', $name, 3); $name = $fn; } return $files; } - static public function &listFiles($forlife = '*', $category = '*', $uniq = false) + static public function &listFiles($hruid = '*', $category = '*', $uniq = false) { $res = array(); - $files = PlUpload::listRawFiles($forlife, $category, $uniq, true); + $files = PlUpload::listRawFiles($hruid, $category, $uniq, true); foreach ($files as $name) { - list($forlife, $cat, $fn) = explode('--', $name, 3); - $res[$fn] = new PlUpload($forlife, $cat, $fn); + list($hruid, $cat, $fn) = explode('--', $name, 3); + $res[$fn] = new PlUpload($hruid, $cat, $fn); } return $res; } @@ -211,6 +235,10 @@ class PlUpload $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; } @@ -288,5 +316,5 @@ class PlUpload return null; } } -// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: ?>