Fixes and improvements
[platal.git] / classes / plupload.php
index d5f428f..ce143a6 100644 (file)
@@ -89,6 +89,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;
@@ -112,7 +116,7 @@ class PlUpload
     public function rm()
     {
         @unlink($this->filename);
-        clearstatcache();
+        @clearstatcache();
     }
 
     public function rename($fn)
@@ -135,22 +139,46 @@ class PlUpload
         return file_exists($this->filename);
     }
 
-    static public function listFiles($forlife = '*', $category = '*', $basename = false)
+    static public function listRawFiles($forlife = '*', $category = '*', $uniq = false, $basename = false)
     {
         global $globals;
         $filename = $globals->spoolroot . '/spool/uploads/temp/';
         $filename .= $forlife . '-' . $category;
+        if (!$uniq) {
+            $filename .= '-*';
+        }
         $files = glob($filename);
         if ($basename) {
-            array_walk($files, 'basename');
+            $files = array_map('basename', $files);
         }
         return $files;
     }
 
-    static public function clear($user = '*', $category = '*')
+    static public function listFilenames($forlife = '*', $category = '*')
     {
-        $files = PlUpload::listFiles($user, $category, false);
-        array_walk($files, 'unlink');
+        $files = PlUpload::listRawFiles($forlife, $category, false, true);
+        foreach ($files as &$name) {
+            list($forlife, $cat, $fn) = explode('-', $name, 3);
+            $name = $fn;
+        }
+        return $files;
+    }
+
+    static public function &listFiles($forlife = '*', $category = '*', $uniq = false)
+    {
+        $res   = array();
+        $files = PlUpload::listRawFiles($forlife, $category, $uniq, true);
+        foreach ($files as $name) {
+            list($forlife, $cat, $fn) = explode('-', $name, 3);
+            $res[$fn] = new PlUpload($forlife, $cat, $fn);
+        }
+        return $res;
+    }
+
+    static public function clear($user = '*', $category = '*', $uniq = false)
+    {
+        $files = PlUpload::listRawFiles($user, $category, $uniq, false);
+        array_map('unlink', $files);
     }
 
     public function contentType()
@@ -158,9 +186,35 @@ 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]) {
+            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 +224,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;
         }