DataObjects/Core_domain.php
[Pman.Core] / Images.php
index e776d10..13ba397 100644 (file)
@@ -42,9 +42,11 @@ class Pman_Core_Images extends Pman
         parent::getAuth(); // load company!
         //return true;
         $au = $this->getAuthUser();
-        //if (!$au) {
-        //    die("Access denied");
-       // }
+        
+        if (!$au) {
+            die("Access denied");
+        }
+        
         $this->authUser = $au;
         
         return true;
@@ -55,7 +57,11 @@ class Pman_Core_Images extends Pman
     
     function get($s) // determin what to serve!!!!
     {
-        print_r($s);exit;
+        // for testing only.
+        //if (!empty($_GET['_post'])) {
+        //   return $this->post();
+        //}
+        
         $this->as_mimetype = empty($_REQUEST['as']) ? '' : $_REQUEST['as'];
         
         $bits= explode('/', $s);
@@ -80,42 +86,10 @@ class Pman_Core_Images extends Pman
             
         } else if (!empty($bits[0]) && $bits[0] == 'events') {
             
-            $popts = PEAR::getStaticProperty('Pman','options');
-            $ev = DB_DAtaObject::Factory('events');
-            if (!$ev->get($bits[1])) {
-                die("could not find event id");
-            }
-            // technically same user only.. -- normally www-data..
-            if (function_exists('posix_getpwuid')) {
-                $uinfo = posix_getpwuid( posix_getuid () ); 
-                $user = $uinfo['name'];
-            } else {
-                $user = getenv('USERNAME'); // windows.
-            }
-            $ff = HTML_FlexyFramework::get();
-            $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/',strtotime($ev->event_when)). $ev->id . ".json";
-            $filesJ = json_decode(file_get_contents($file));
-         
-            //print_r($filesJ);
-         
-            foreach($filesJ->FILES as $k=>$f){
-                if ($f->tmp_name != $bits[2]) {
-                    continue;
-                }
-                
-                $src = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/', strtotime($ev->event_when)).  $f->tmp_name ;
-                if (!file_exists($src)) {
-                    die("file was not saved");
-                }
-                header ('Content-Type: ' . $f->type);
+            $this->downloadEvent($bits);
             
-                header("Content-Disposition: attachment; filename=\"".basename($f->name)."\";" );
-                @ob_clean();
-                flush();
-                readfile($src);
-                exit;
-            }
             die ("unknown file?"); 
+            
         } else {
         
             $id = empty($bits[0]) ? 0 :  $bits[0];
@@ -133,7 +107,7 @@ class Pman_Core_Images extends Pman
             if (empty($_REQUEST['anytype'])) {
                 $img->whereAdd("mimetype like 'image/%'");
             }
-            
+            $img->orderBy('title ASC'); /// spurious ordering... (curretnly used by shipping project)
             if (isset($onbits[2])) {
                 $img->imgtype = $onbits[2];
             }
@@ -180,10 +154,20 @@ class Pman_Core_Images extends Pman
                 urlencode("image has been removed or deleted."));
             
         }
+        
+        if(!$this->hasPermission($img)){
+            header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
+                urlencode("access to this image/file has been denied."));
+        }
+        
         $this->serve($img);
         exit;
     }
     
+    function hasPermission($img) 
+    {
+        return true;
+    }
     
     function post()
     {
@@ -273,8 +257,7 @@ class Pman_Core_Images extends Pman
             }
         }
         
-        if (!file_exists($fn)) {            
-            
+        if (!file_exists($fn)) {    
             $this->validateSize();
         }
         
@@ -288,12 +271,12 @@ class Pman_Core_Images extends Pman
     }
     function validateSize()
     {
-        
-        if ($this->authUser && $this->authUser->company_id && $this->authUser->company()->comptype=='OWNER') {
+        if (($this->authUser && !empty($this->authUser->company_id) && $this->authUser->company()->comptype=='OWNER') || $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']) {
             return true;
         }
         
-        // DEFAULT allowed - override with $cfg['sizes'];
+        
+        $ff = HTML_FlexyFramework::get();
         
         $sizes = array(
                 '100', 
@@ -304,21 +287,26 @@ class Pman_Core_Images extends Pman
                 '200x0',
                 '200x200',  
                 '400x0',
-                '300x100', // logo on login.
+                '300x100',
                 '500'
             );
         
-        // this should be configurable...
-        $ff = HTML_FlexyFramework::get();
         $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
                 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
         
-        
-        
         if (!empty($cfg['sizes'])) {
             $sizes = array_merge($sizes , $cfg['sizes']);
         }
         
+        $project = $ff->project;
+        
+        require_once $ff->project . '.php';
+        
+        $project = new $ff->project();
+        
+        if(isset($project::$Pman_Core_Images_Size)){
+            $sizes = $project::$Pman_Core_Images_Size;
+        }
         
         if (!in_array($this->size, $sizes)) {
             die("invalid scale - ".$this->size);
@@ -361,12 +349,30 @@ class Pman_Core_Images extends Pman
             foreach($imatch[1] as $i=>$key) {
                 $attr[$key] = $imatch[2][$i];
             }
-            if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
+            // does it contain baseURL??? --- well what about relative paths...
+            //print_R($attr);
+            
+            if (empty($attr['src'])) {
+                continue;
+            }
+            if (0 !== strpos($attr['src'], $baseURL)) {
+                // it starts with our 'new' baseURL?
+                $html = self::replaceImgUrl($html, $baseURL, $img, $attr,  'src' );
+                continue;
+            }
+            if (false !== strpos($attr['src'], '//')) {
+                // contains an absolute path.. that is probably not us...
                 continue;
             }
+            // what about mailto or data... - just ignore?? for images...
+            
             $html = self::replaceImgUrl($html, $baseURL, $img, $attr,  'src' );
+            
+            
+            
         }
         
+        
         $result = array();
         preg_match_all('/<a\s+[^>]+>/i',$html, $result); 
 
@@ -402,7 +408,14 @@ class Pman_Core_Images extends Pman
         if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch))  {
             return $html;
         }
+        
         $id = $umatch[2];
+        $hash = '';
+        if (!empty($umatch[3]) && strpos($umatch[3],'#')) {
+            $hash = '#'. array_pop(explode('#',$umatch[3]));
+        }
+        
+        
         $img = DB_DataObject::factory('Images');
         if (!$img->get($id)) {
             return $html;
@@ -448,7 +461,7 @@ class Pman_Core_Images extends Pman
         
         $new_tag = str_replace(
             $attr_name. '="'. $attr_url . '"',
-            $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
+            $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . $hash .'"',
             $tag
         );
         
@@ -457,4 +470,43 @@ class Pman_Core_Images extends Pman
          
     }
     
+    function downloadEvent($bits)
+    {
+        $popts = PEAR::getStaticProperty('Pman','options');
+        $ev = DB_DAtaObject::Factory('events');
+        if (!$ev->get($bits[1])) {
+            die("could not find event id");
+        }
+        // technically same user only.. -- normally www-data..
+        if (function_exists('posix_getpwuid')) {
+            $uinfo = posix_getpwuid( posix_getuid () ); 
+            $user = $uinfo['name'];
+        } else {
+            $user = getenv('USERNAME'); // windows.
+        }
+        $ff = HTML_FlexyFramework::get();
+        $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/',strtotime($ev->event_when)). $ev->id . ".json";
+        $filesJ = json_decode(file_get_contents($file));
+
+        //print_r($filesJ);
+
+        foreach($filesJ->FILES as $k=>$f){
+            if ($f->tmp_name != $bits[2]) {
+                continue;
+            }
+
+            $src = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/', strtotime($ev->event_when)).  $f->tmp_name ;
+            if (!file_exists($src)) {
+                die("file was not saved");
+            }
+            header ('Content-Type: ' . $f->type);
+
+            header("Content-Disposition: attachment; filename=\"".basename($f->name)."\";" );
+            @ob_clean();
+            flush();
+            readfile($src);
+            exit;
+        }
+    }
+    
 }