DataObjects/Events.php
[Pman.Core] / Images.php
index 13ba397..51ec17f 100644 (file)
 require_once  'Pman.php';
 class Pman_Core_Images extends Pman
 {
+    
+    // tables that do not need authentication checks before serving.
+    var $public_image_tables = array();
+    
+    
     function getAuth()
     {
         parent::getAuth(); // load company!
@@ -44,7 +49,8 @@ class Pman_Core_Images extends Pman
         $au = $this->getAuthUser();
         
         if (!$au) {
-            die("Access denied");
+            $this->authUser = false;
+            return true;//die("Access denied");
         }
         
         $this->authUser = $au;
@@ -55,7 +61,7 @@ class Pman_Core_Images extends Pman
     var $as_mimetype = false;
     var $method = 'inline';
     
-    function get($s) // determin what to serve!!!!
+    function get($s, $opts=array()) // determin what to serve!!!!
     {
         // for testing only.
         //if (!empty($_GET['_post'])) {
@@ -85,10 +91,12 @@ class Pman_Core_Images extends Pman
             $id = empty($bits[3]) ? 0 :   $bits[3];
             
         } else if (!empty($bits[0]) && $bits[0] == 'events') {
-            
+            if (!$this->authUser) {
+                $this->imgErr("no-authentication-events",$s);
+            }
             $this->downloadEvent($bits);
+            $this->imgErr("unknown file",$s);
             
-            die ("unknown file?"); 
             
         } else {
         
@@ -96,9 +104,16 @@ class Pman_Core_Images extends Pman
         }
         
         if (strpos($id,':') > 0) {  // id format  tablename:id:-imgtype
+            
+            if (!$this->authUser) {
+                $this->imgErr("not-authenticated-using-colon-format",$s);
+                
+            }
+            
             $onbits = explode(':', $id);
             if ((count($onbits) < 2)   || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
-                die("Bad url");
+                $this->imgErr("bad-url",$s);
+                
             }
             //DB_DataObject::debugLevel(1);
             $img = DB_DataObject::factory('Images');
@@ -113,8 +128,8 @@ class Pman_Core_Images extends Pman
             }
             $img->limit(1);
             if (!$img->find(true)) {
-                header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
-                urlencode("no images for that item: " . htmlspecialchars($id)));
+                $this->imgErr("no images for that item: " . htmlspecialchars($id),$s);
+                
             }
             
             $id = $img->id;
@@ -125,20 +140,20 @@ class Pman_Core_Images extends Pman
         
         // depreciated - should use ontable:onid:type here...
         if (!empty($_REQUEST['ontable'])) {
-
+            
+            if (!$this->authUser) {
+                die("authentication required");
+            }
+            
             //DB_DataObjecT::debugLevel(1);
-            $img = DB_DataObjecT::factory('Images');
+            $img = DB_DataObject::factory('Images');
             $img->setFrom($_REQUEST);
-            // use imgtype now...
-           // if (!empty($_REQUEST['query']['filename'])){
-           //     $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
-           // }
+           
             
             
             $img->limit(1);
             if (!$img->find(true)) {
-                header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='. 
-                    urlencode("No file exists"));
+                $this->imgErr("No file exists",$s);
             } 
             $id = $img->id;
             
@@ -149,27 +164,48 @@ class Pman_Core_Images extends Pman
         $img = DB_DataObjecT::factory('Images');
          
         if (!$id || !$img->get($id)) {
-             
-            header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
-                urlencode("image has been removed or deleted."));
+             $this->imgErr("image has been removed or deleted.",$s);
+        }
+        
+        if (!$this->authUser && !in_array($img->ontable,$this->public_image_tables)) {
+           
+            if ($img->ontable != 'core_company') {
+                $this->imgErr("not-authenticated {$img->ontable}",$s);
+            }
+            if ($img->imgtype != 'LOGO') {
+                $this->imgErr("not-logo",$s);
+            }
+            $comp  = $img->object();
+            if ($comp->comptype != 'OWNER') {
+                $this->imgErr("not-owner-company",$s);
+            }
+            return $this->serve($img);
+        
             
         }
         
+        
         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->imgErr("access to this image/file has been denied.",$s);
+            
         }
         
         $this->serve($img);
         exit;
     }
     
+    function imgErr($reason,$path) {
+        header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
+            urlencode($reason) .'&path='.urlencode($path));
+        exit;
+    }
+    
     function hasPermission($img) 
     {
         return true;
     }
     
-    function post()
+    function post($v)
     {
         
         if (!$this->authUser) {
@@ -232,7 +268,7 @@ class Pman_Core_Images extends Pman
         }
 //        print_r($img);exit;
         $x = $img->toFileConvert();
-        if (empty($this->as_mimetype)) {
+        if (empty($this->as_mimetype) || $img->mimetype == 'image/gif') {
             $this->as_mimetype  = $img->mimetype;
         }
         if (!$this->thumb) {
@@ -243,12 +279,14 @@ 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..
         
-        
-        
         // 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($img->mimetype == 'image/gif'){
+            $fn = $img->getStoreName() . '.'. $this->size . '.gif';
+        }
+        
         if (!file_exists($fn)) {
             $fn = $img->getStoreName()  . '.'. $this->size . '.'. $img->fileExt();
             // if it's an image, convert into the same type for thumbnail..
@@ -271,7 +309,8 @@ class Pman_Core_Images extends Pman
     }
     function validateSize()
     {
-        if (($this->authUser && !empty($this->authUser->company_id) && $this->authUser->company()->comptype=='OWNER') || $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']) {
+        if (($this->authUser && !empty($this->authUser->company_id) && $this->authUser->company()->comptype=='OWNER')
+            || $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']) {
             return true;
         }
         
@@ -302,10 +341,15 @@ class Pman_Core_Images extends Pman
         
         require_once $ff->project . '.php';
         
-        $project = new $ff->project();
-        
-        if(isset($project::$Pman_Core_Images_Size)){
-            $sizes = $project::$Pman_Core_Images_Size;
+        $project = str_replace('/', '_', $project);
+         
+        $pr_obj = new $project;
+         
+       // var_dump($pr_obj->Pman_Core_Images_Size);
+        if(isset($pr_obj->Pman_Core_Images_Size)){
+            $sizes = $pr_obj->Pman_Core_Images_Size;
+            
+            
         }
         
         if (!in_array($this->size, $sizes)) {
@@ -360,7 +404,7 @@ class Pman_Core_Images extends Pman
                 $html = self::replaceImgUrl($html, $baseURL, $img, $attr,  'src' );
                 continue;
             }
-            if (false !== strpos($attr['src'], '//')) {
+            if (false !== strpos($attr['src'], '//') && false === strpos($attr['src'], $baseURL)) {
                 // contains an absolute path.. that is probably not us...
                 continue;
             }
@@ -368,8 +412,6 @@ class Pman_Core_Images extends Pman
             
             $html = self::replaceImgUrl($html, $baseURL, $img, $attr,  'src' );
             
-            
-            
         }