flag perm failure
[Pman.Core] / DataObjects / Images.php
index 58e10a7..4f226cd 100644 (file)
@@ -38,6 +38,12 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
                 $tn.filename LIKE '%{$this->escape($q['search']['filename'])}%' OR $tn.title LIKE '%{$this->escape($q['search']['filename'])}%'
             ");
         }
+
+        if(!empty($q['_to_base64']) && !empty($q['image_id'])) {
+            $i = DB_DataObject::factory("Images");
+            $i->get($q['image_id']);
+            $roo->jok($i->toBase64());
+        }
         
 
     }
@@ -53,7 +59,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         
         $o = $this->object();
         //print_r($o);
-        if (method_exists($o, 'checkPerm')) {
+        if ($o &&  method_exists($o, 'checkPerm')) {
             // edit permissions on related object needed...
             return $o->checkPerm( $lvl == 'S' ? 'S' : 'E' , $au);
             
@@ -197,7 +203,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     function getStoreName() 
     {
         $opts = HTML_FlexyFramework::get()->Pman;
-        $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
+        $fn = preg_replace('/[^a-z0-9_\.]+/i', '_', $this->filename);
         return implode( '/', array(
             $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
         ));
@@ -209,7 +215,42 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
      */
     function exists()
     {
-        return file_exists($this->getStoreName());
+        clearstatcache();
+        //var_dump($this->getStoreName());
+        $ret =  file_exists($this->getStoreName());
+        if (!$ret) {
+            return $this->canFix();
+        }
+        return $ret;
+    }
+    
+    function canFix() {
+        // look for the image in the folder, with matching id.
+        // this is problematic..
+        $fn = $this->getStoreName();
+        if (file_exists($fn . '-really-missing')) {
+            return false;
+        }
+        if (!file_exists(dirname($fn))) {
+            return false;
+        }
+        foreach( scandir(dirname($fn)) as $n) {
+            if (empty($n) || $n[0] == '.') {
+                continue;
+            }
+            $bits = explode('-', $n);
+            if ($bits[0] != $this->id) {
+                continue;
+            }
+            if (preg_match('/\.[0-9]+x[0-9]]+\.jpeg$/', $n)) {
+                continue;
+            }
+            copy(dirname($fn). '/'.  $n, $fn);
+            clearstatcache();
+            return true;
+        }
+        // fixme - flag it as bad
+        touch($fn . '-really-missing');
     }
     
     
@@ -497,7 +538,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             
             
         }
-        $ret['shorten_name'] = $ret['filename'] = $this->shorten_name();
+        $ret['shorten_name']   = $this->shorten_name();
         
         return $ret;
     }
@@ -660,6 +701,8 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
      */
     function toFileConvert()
     {
+        $fn = $this->getStoreName();
+        
         require_once 'File/Convert.php';
         $fc = new File_Convert($this->getStoreName(), $this->mimetype);
         return $fc;
@@ -913,7 +956,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         }
         
         $file = $this->getStoreName();
-        
+
         if(!file_exists($file)){
             return false;
         }