fix for getEntity and the return of a non-object
[squirrelmail.git] / class / mime.class.php
index 98b9e602dab1a16874a28d8b14372bb14b59dc89..8d1def3d81cb4a7afa55a7ceda5150600840f5bc 100644 (file)
@@ -60,7 +60,7 @@ class rfc822_header
             {
                 $field = substr($line,0,$pos);
                 $value = trim(substr($line,$pos+1));
-                if(!preg_match('/^X.*/',$value)) {
+                if(!preg_match('/^X.*/i',$field)) {
                     $value = $this->stripComments($value);
                 }
                 $this->parseField($field,$value);
@@ -717,10 +717,13 @@ class message
           $msg = $msg->entities[0];
        }
 
-       if (isset($msg->entities[($ent_a[$cnt-1])-1]))
+        if (isset($msg->entities[($ent_a[$cnt-1])-1]))
        {
-           $msg = $msg->entities[($ent_a[$cnt-1]-1)];
-        }
+           if (is_object($msg->entities[($ent_a[$cnt-1])-1]))
+           {
+               $msg = $msg->entities[($ent_a[$cnt-1]-1)];
+           }
+       }
 
         return $msg;
     }
@@ -848,6 +851,7 @@ class message
                 /* multipart properties */
                 $i++;
                 $res = $this->parseProperties($read,$i);
+
                 $arg_a[] = $res[0];
                 $i = $res[1];
                 $arg_no++;
@@ -1511,60 +1515,72 @@ class message
 
     function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html'))
     {
-       $found = false;    
-       $type = $this->type0.'/'.$this->type1;
-       if ( $type == 'multipart/alternative')
-       {
-           $msg = $this->findAlternativeEntity($alt_order);
-           if (count($msg->entities) == 0)
-           {
-               $entity[] = $msg->entity_id;
-           } else 
-           {
-               $entity = $msg->findDisplayEntity($entity, $alt_order);
-           }
-           $found = true;          
-       } else if ( $type == 'multipart/related') 
-       {
+        $found = false;
+        $type = $this->type0.'/'.$this->type1;
+        if ( $type == 'multipart/alternative')
+        {
+            $msg = $this->findAlternativeEntity($alt_order);
+            if (count($msg->entities) == 0) 
+            {
+                $entity[] = $msg->entity_id;
+            } else 
+            {
+                $entity = $msg->findDisplayEntity($entity, $alt_order);
+            }
+            $found = true;
+        } else if ( $type == 'multipart/related') 
+        {
             $msgs = $this->findRelatedEntity();
-           for ($i = 0; $i < count($msgs); $i++)
-           {
-               $msg = $msgs[$i];
-               if (count($msg->entities) == 0)
-               {
-                   $entity[] = $msg->entity_id;
-               } else
-               {
-                   $entity = $msg->findDisplayEntity($entity,$alt_order);
-               }
-               $found = true;          
-           }
-       } else if ( $this->type0 == 'text' &&
-             ( $this->type1 == 'plain' ||
-               $this->type1 == 'html' ||
-              $this->type1 == 'message') &&
-               isset($this->entity_id) ) 
-       {
-         if (count($this->entities) == 0) 
-          {
-           if (strtolower($this->header->disposition->name) != 'attachment') 
-           {
-                $entity[] = $this->entity_id;
-           }
-         }
-       } 
-       $i = 0;
-       while ( isset($this->entities[$i]) &&  !$found &&
-             (strtolower($this->entities[$i]->header->disposition->name) 
-               != 'attachment') &&
-             ($this->entities[$i]->type0 != 'message' && 
-             $this->entities[$i]->type1 != 'rfc822' )
-            )
-       {
-           $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order);
-           $i++;
-       }
-       return( $entity );
+            foreach ($msgs as $msg)
+            {
+                if (count($msg->entities) == 0) 
+                {
+                    $entity[] = $msg->entity_id;
+                } else 
+                {
+                    $entity = $msg->findDisplayEntity($entity,$alt_order);
+                }
+            }
+            if (count($msgs) > 0) {
+                $found = true;
+            }
+        } else if ($this->type0 == 'text' &&
+                   ($this->type1 == 'plain' ||
+                    $this->type1 == 'html'  ||
+                    $this->type1 == 'message') &&
+                   isset($this->entity_id) ) 
+        {
+            if (count($this->entities) == 0) 
+            {
+                if (strtolower($this->header->disposition->name) != 'attachment') 
+                {
+                    $entity[] = $this->entity_id;
+                }
+            }
+        }
+        $i = 0;
+        if(!$found) {
+            foreach ($this->entities as $ent) {
+                if(strtolower($ent->header->disposition->name) != 'attachment' &&
+                   ($ent->type0 != 'message' && $ent->type1 != 'rfc822'))
+                {
+                    $entity = $ent->findDisplayEntity($entity, $alt_order);
+                }
+            }
+        }
+        /*
+        while ( isset($this->entities[$i]) && !$found &&
+                (strtolower($this->entities[$i]->header->disposition->name)
+                 != 'attachment') &&
+                ($this->entities[$i]->type0 != 'message' &&
+                 $this->entities[$i]->type1 != 'rfc822' )
+              )
+        {
+            $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order);
+            $i++;
+        }
+        */
+        return( $entity );
     }
 
     function findAlternativeEntity ($alt_order)
@@ -1573,38 +1589,38 @@ class message
         * viewable message supported by SM.
         */
         $best_view = 0;
-        $ent_id = 0;
-        $k = 0; 
-        for ($i = 0; $i < count($this->entities); $i ++)
-       {
-            $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
-           if ($type == 'multipart/related')
-           {
-              $type = $this->entities[$i]->header->getParameter('type');
-           }
-           for ($j = $k; $j < count($alt_order); $j++)
-           {
-               if ($alt_order[$j] == $type && $j > $best_view)
-               {
-                   $best_view = $j;
-                   $ent_id = $i;
-                   $k = $j;
-               }
-           }
+        $entity = array();
+        $altcount = count($alt_order);
+        foreach($this->entities as $ent)
+        {
+            $type = $ent->header->type0.'/'.$ent->header->type1;
+            if ($type == 'multipart/related')
+            {
+                $type = $ent->header->getParameter('type');
+            }
+            for ($j = $best_view; $j < $altcount; $j++)
+            {
+                if ($alt_order[$j] == $type && $j >= $best_view)
+                {
+                    $best_view = $j;
+                    $entity = $ent;
+                }
+            }
         }
-        return $this->entities[$ent_id];
+        return $entity;
     }
     
     function findRelatedEntity ()
     {
         $msgs = array(); 
-        for ($i = 0; $i < count($this->entities); $i ++)
-       {
+        $entcount = count($this->entities);
+        for ($i = 0; $i < $entcount; $i++)
+        {
             $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
             if ($this->header->getParameter('type') == $type)
-           {
-               $msgs[] = $this->entities[$i];
-           }
+            {
+                $msgs[] = $this->entities[$i];
+            }
         }
         return $msgs;
     }
@@ -1622,7 +1638,7 @@ class message
            $exclude = false;
            foreach ($exclude_id as $excl)
            {
-               if ($entity->entity_id == $excl)
+               if ($entity->entity_id === $excl)
               {
                  $exclude = true;
               }
@@ -1669,6 +1685,17 @@ class disposition
        $this->name = $name;
        $this->properties = array();
     }
+
+    function getProperty($par)
+    {
+        $value = strtolower($par);
+        if (isset($this->properties[$par]))
+       {
+           return $this->properties[$par];
+        }
+        return '';
+    }
+
 }
 
 class language