Release plat/al core v1.1.13
[platal.git] / classes / plupload.php
index b3dbb56..e903e92 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  */
 class PlUpload
 {
-    private $forlife;
+    private $hruid;
     private $category;
     private $file_id;
 
     private $filename;
     private $type;
 
+    static public $lastError;
+
     /** For images
      */
     private $x;
     private $y;
 
-    public function __construct($forlife, $category, $filename = null)
+    public function __construct($hruid, $category, $filename = null)
     {
         $this->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;
     }
 
@@ -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;
         }
@@ -116,7 +140,7 @@ class PlUpload
     public function rm()
     {
         @unlink($this->filename);
-        clearstatcache();
+        @clearstatcache();
     }
 
     public function rename($fn)
@@ -139,13 +163,13 @@ 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 .= '-*';
+            $filename .= '--*';
         }
         $files = glob($filename);
         if ($basename) {
@@ -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;
     }
@@ -199,11 +223,22 @@ class PlUpload
     {
         static $map;
         if (!isset($map)) {
-            $map = array (1 => 'gif', 2 => 'jpeg', 3 => 'png');
+            $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;
         }
@@ -281,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:
 ?>