Merge branch 'master' of http://git.roojs.com:8081/Pman.Core
[Pman.Core] / Images.php
index 8b6bd11..3e2680f 100644 (file)
@@ -14,7 +14,7 @@
  *      ontable (req) tablename.
  *      filename
  *      (other table args)
- *      as (serve as a type) = eg. mimetype.
+ *      as (serve as a type) = eg. ?as=audio/mpeg 
  * 
  * args: generic
  *     as :(serve as a type) = eg. mimetype.
@@ -59,6 +59,7 @@ class Pman_Core_Images extends Pman
         
         $bits= explode('/', $s);
         $id = 0;
+        
         // without id as first part...
         if (!empty($bits[0]) && $bits[0] == 'Thumb') {
             $this->thumb = true;
@@ -76,7 +77,24 @@ class Pman_Core_Images extends Pman
             $this->size = empty($bits[2]) ? '0x0' : $bits[2];
             $id = empty($bits[3]) ? 0 :   $bits[3];
             
+        } else if (!empty($bits[0]) && $bits[0] == 'events') {
+            $popts = PEAR::getStaticProperty('Pman','options');
+            
+            header ('Content-Type: image/jpeg');
+            if(!empty($bits[2]) && $bits[2] == 'download'){
+                $file = "{$popts['event_log_dir']}/{$bits[1]}";
+                header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
+                ob_clean();
+                flush();
+                readfile($file);
+            }else{
+                $file = "{$popts['event_log_dir']}/{$bits[1]}.jpg";
+                $fh = fopen($file,'r');
+                echo fread($fh,filesize($file));
+            }
+            exit;
         } else {
+        
             $id = empty($bits[0]) ? 0 :  $bits[0];
         }
         
@@ -140,6 +158,57 @@ class Pman_Core_Images extends Pman
         $this->serve($img);
         exit;
     }
+    
+    
+    function post()
+    {
+        
+        if (!$this->authUser) {
+            $this->jerr("image conversion only allowed by registered users");
+        }
+        // converts a posted string (eg.svg)
+        // into another type..
+        if (empty($_REQUEST['as'])) {
+           $this->jerr("missing target type");
+        }
+        if (empty($_REQUEST['mimetype'])) {
+            $this->jerr("missing mimetype");
+        }
+        if (empty($_REQUEST['data'])) {
+            $this->jerr("missing data");
+        }
+        
+        
+        $this->as_mimetype = $_REQUEST['as'];
+        $this->mimetype = $_REQUEST['mimetype'];
+        require_once 'File/MimeType.php';
+        $y = new File_MimeType();
+        $src_ext = $y->toExt( $this->mimetype );
+        
+        
+        $tmp = $this->tempName($src_ext);
+        file_put_contents($tmp, $_REQUEST['data']);
+        
+        require_once 'File/Convert.php';
+        $cv = new File_Convert($tmp, $this->mimetype);
+        
+        $fn = $cv->convert(
+                $this->as_mimetype ,
+                empty($_REQUEST['width']) ? 0 : $_REQUEST['width'],
+                empty($_REQUEST['height']) ? 0 : $_REQUEST['height']
+        );
+        if (!empty($_REQUEST['as_data'])) {
+            $this->jok(base64_encode(file_get_contents($fn)));
+        }
+        
+        $cv->serve('attachment');
+        exit;
+        
+        
+        
+    }
+    
+    
  
     function serve($img)
     {
@@ -162,7 +231,22 @@ class Pman_Core_Images extends Pman
         }
         //echo "SKALING?  $this->size";
         // acutally if we generated the image, then we do not need to validate the size..
-        $this->validateSize();
+        
+        
+        
+        // if the mimetype is not converted..
+        // then the filename should be original.{size}.jpeg
+        $fn = $img->getStoreName() . '.'. $this->size . '.jpeg'; // thumbs are currenly all jpeg.!???
+        
+        if (!file_exists($fn)) {
+            $fn = $img->getStoreName()  . '.'. $this->size . '.'. $img->fileExt();
+            $this->as_mimetype = $img->mimetype;
+        }
+        
+        if (!file_exists($fn)) {            
+            
+            $this->validateSize();
+        }
         
         $x->convert( $this->as_mimetype, $this->size);
         $x->serve();
@@ -221,17 +305,18 @@ class Pman_Core_Images extends Pman
      */
     
     
-    static function replaceImgURLS($html)
+    static function replaceImageURLS($html)
     {
         
         $ff = HTML_FlexyFramework::get();
-        if (!isset($ff->Pman_Image['public_baseURL'])) {
+        if (!isset($ff->Pman_Images['public_baseURL'])) {
             return $html;
         }
-        $baseURL = $ff->Pman_Image['public_baseURL'];
+        //var_dump($ff->Pman_Images['public_baseURL']);
+        $baseURL = $ff->Pman_Images['public_baseURL'];
         
-        preg_match_all('/<img\w[^>]+>/i',$html, $result); 
-
+        preg_match_all('/<img\s+[^>]+>/i',$html, $result); 
+        //print_r($result);
         $matches = array_unique($result[0]);
         foreach($matches as $img) {
             $imatch = array();
@@ -245,11 +330,11 @@ class Pman_Core_Images extends Pman
             if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
                 continue;
             }
-            $html = self::replaceImgUrl($html, $baseURL, $img, $attr, $attr['src'] );
+            $html = self::replaceImgUrl($html, $baseURL, $img, $attr,  'src' );
         }
         
         $result = array();
-        preg_match_all('/<a\w[^>]+>/i',$html, $result); 
+        preg_match_all('/<a\s+[^>]+>/i',$html, $result); 
 
         $matches = array_unique($result[0]);
         foreach($matches as $img) {
@@ -264,12 +349,12 @@ class Pman_Core_Images extends Pman
             if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) { 
                 continue;
             }
-            $html = self::replaceImgUrl($html, $baseURL, $img, $attr, $attr['href'] );
+            $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
         }
         
         return $html;
     }
-    static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_url
+    static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name
     {
         
         //print_R($attr);
@@ -277,9 +362,11 @@ class Pman_Core_Images extends Pman
         // Images/{ID}/fullname.xxxx
         // Images/Thumb/200/{ID}/fullname.xxxx
         // Images/Download/{ID}/fullname.xxxx
+        
+        $attr_url = $attr[$attr_name];
         $umatch  = false;
         if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch))  {
-            continue;
+            return $html;
         }
         $id = $umatch[2];
         $img = DB_DataObject::factory('Images');
@@ -288,48 +375,46 @@ class Pman_Core_Images extends Pman
         }
         $type = explode('/', $umatch[1]);
         $thumbsize = -1;
-        $new_thumbsize = -1;
-        
+         
         if (count($type) > 2 && $type[1] == 'Thumb') {
             $thumbsize = $type[2];
-            $provider = 'Images/Thumb';
+            $provider = '/Images/Thumb';
         } else {
-            $provider = $umatch[1];
+            $provider = '/'.$umatch[1];
         }
         
         if (!empty($attr['width']) || !empty($attr['height']) )
         {
             // no support for %...
-            $new_thumbsize =
+            $thumbsize =
                 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
                 'x' .
                 (empty($attr['height']) ? '0' : $attr['height'] * 1);
-            
+             $provider = '/Images/Thumb';
             
         }
-        if ($new_thumbsize != $thumbsize) {
+        
+        if ($thumbsize !== -1) {
             // change in size..
             // need to regenerate it..
-            if (!$new_thumbsize) {
-                $type = array('Image');
-            } else {
-                
-                $type = array('Image', 'Thumb', $new_thumbsize);
-                
-                $fc = $img->toFileConvert();
-                // make sure it's available..
-                $fc->convert($img->mimetype, $new_thumbsize);
+            
+            $type = array('Images', 'Thumb', $thumbsize);
                 
-            }
+            $fc = $img->toFileConvert();
+            // make sure it's available..
+            $fc->convert($img->mimetype, $thumbsize);
+            
             
+        } else {
+            $provider = $provider == 'Images/Thumb' ? 'Images' : $provider; 
         }
         
         
         // finally replace the original TAG with the new version..
         
         $new_tag = str_replace(
-            'src="'. $attr['src'] . '"',
-            'src="'. htmlspecialchars($img->URL($new_thumbsize , $provider, $baseURL)) . '"',
+            $attr_name. '="'. $attr_url . '"',
+            $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
             $tag
         );