DataObjects/core.sql
[Pman.Core] / Images.php
index 1707e7d..9554939 100644 (file)
@@ -206,13 +206,33 @@ class Pman_Core_Images extends Pman
             die("invalid scale - ".$this->size);
         }
     }
+    /**
+     * replace image urls
+     *
+     * The idea of this code was to replace urls for images when you have an admin
+     * and a distribution page. with different urls.
+     *
+     * it may be usefull later if things like embedded images in emails. but
+     * I think it's proably better not to use this.
+     *
+     * The key problem being how to determine if we are replacing 'our' images or some external one..
+     * 
+     *
+     */
     
     
-    
-    function replaceImg($html)
+    static function replaceImageURLS($html)
     {
-        preg_match_all('/<img[^>]+>/i',$html, $result); 
-
+        
+        $ff = HTML_FlexyFramework::get();
+        if (!isset($ff->Pman_Images['public_baseURL'])) {
+            return $html;
+        }
+        //var_dump($ff->Pman_Images['public_baseURL']);
+        $baseURL = $ff->Pman_Images['public_baseURL'];
+        
+        preg_match_all('/<img\s+[^>]+>/i',$html, $result); 
+        //print_r($result);
         $matches = array_unique($result[0]);
         foreach($matches as $img) {
             $imatch = array();
@@ -223,76 +243,100 @@ class Pman_Core_Images extends Pman
             foreach($imatch[1] as $i=>$key) {
                 $attr[$key] = $imatch[2][$i];
             }
-            print_R($attr);
-            // see if it's an image url..
-            // Images/{ID}/fullname.xxxx
-            // Images/Thumb/200/{ID}/fullname.xxxx
-            // Images/Download/{ID}/fullname.xxxx
-            $umatch  = false;
-            if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr['src'], $umatch))  {
+            if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
                 continue;
             }
-            $id = $umatch[2];
-            $imgObj = DB_DataObject::factory('Images');
-            if (!$imgObj->get($id)) {
-                continue;
-            }
-            $type = explode('/', $umatch[1]);
-            $thumbsize = -1;
-            $new_thumbsize = -1;
-            
-            if (count($type) > 2 && $type[1] == 'Thumb') {
-                $thumbsize = $type[2];
-                $provider = 'Images/Thumb';
-            } else {
-                $provider = $type;
-            }
+            $html = self::replaceImgUrl($html, $baseURL, $img, $attr,  'src' );
+        }
+        
+        $result = array();
+        preg_match_all('/<a\s+[^>]+>/i',$html, $result); 
+
+        $matches = array_unique($result[0]);
+        foreach($matches as $img) {
+            $imatch = array();
+            preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
+            // build a keymap
+            $attr =  array();
             
-            if (!empty($attr['width']) || !empty($attr['height']) )
-            {
-                // no support for %...
-                $new_thumbsize =
-                    (empty($attr['width']) ? '0' : $attr['width'] * 1) .
-                    'x' .
-                    (empty($attr['height']) ? '0' : $attr['height'] * 1);
-                
-                
+            foreach($imatch[1] as $i=>$key) {
+                $attr[$key] = $imatch[2][$i];
             }
-            if ($new_thumbsize != $thumbsize) {
-                // change in size..
-                // need to regenerate it..
-                if (!$new_thumbsize) {
-                    $type = array('Image');
-                } else {
-                    
-                    $type = array('Image', 'Thumb', $new_thumbsize);
-                    
-                    $fc = $imgObj->toFileConvert();
-                    // make sure it's available..
-                    $fc->convert($imgObj->mimetype, $new_thumbsize);
-                    
-                }
-                
+            if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) { 
+                continue;
             }
+            $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
+        }
+        
+        return $html;
+    }
+    static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name) 
+    {
+        
+        //print_R($attr);
+        // see if it's an image url..
+        // 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;
+        }
+        $id = $umatch[2];
+        $img = DB_DataObject::factory('Images');
+        if (!$img->get($id)) {
+            return $html;
+        }
+        $type = explode('/', $umatch[1]);
+        $thumbsize = -1;
+         
+        if (count($type) > 2 && $type[1] == 'Thumb') {
+            $thumbsize = $type[2];
+            $provider = '/Images/Thumb';
+        } else {
+            $provider = '/'.$umatch[1];
+        }
+        
+        if (!empty($attr['width']) || !empty($attr['height']) )
+        {
+            // no support for %...
+            $thumbsize =
+                (empty($attr['width']) ? '0' : $attr['width'] * 1) .
+                'x' .
+                (empty($attr['height']) ? '0' : $attr['height'] * 1);
+             $provider = '/Images/Thumb';
             
+        }
+        
+        if ($thumbsize !== -1) {
+            // change in size..
+            // need to regenerate it..
             
-            // finally replace the original TAG with the new version..
-            
-            $new_img = str_replace(
-                'src="'. $attr['src'] . '"',
-                'src="'. htmlspecialchars($imgObj->URL($new_thumbsize , $provider, $baseURL)) . '"',
-                $img
-            );
-            
-            
-            $html = str_replace($img, $new_img);
-            
+            $type = array('Images', 'Thumb', $thumbsize);
+                
+            $fc = $img->toFileConvert();
+            // make sure it's available..
+            $fc->convert($img->mimetype, $thumbsize);
             
-            // make an image url..
             
+        } else {
+            $provider = $provider == 'Images/Thumb' ? 'Images' : $provider; 
         }
-        return $html;
         
+        
+        // finally replace the original TAG with the new version..
+        
+        $new_tag = str_replace(
+            $attr_name. '="'. $attr_url . '"',
+            $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
+            $tag
+        );
+        
+        
+        return str_replace($tag, $new_tag, $html);
+         
     }
     
 }