added function to search the to and cc headers and return the best match
[squirrelmail.git] / class / mime / Rfc822Header.class.php
index 8a3e6b2e169d8d37deb840f2774e1b0f4113c8e6..e168a351f614a20fc7a3fd6cb6c67cebc38ea483 100644 (file)
@@ -51,12 +51,14 @@ class Rfc822Header {
             $pos = strpos($line, ':');
             if ($pos > 0) {
                 $field = substr($line, 0, $pos);
-                $value = trim(substr($line, $pos+1));
-                if(!preg_match('/^X.*/i', $field) &&
-                   !preg_match('/^Subject/i', $field)) {
-                    $value = $this->stripComments($value);
-                }
-                $this->parseField($field, $value);
+               if (!strstr($field,' ')) { /* valid field */
+                   $value = trim(substr($line, $pos+1));
+                   if(!preg_match('/^X.*/i', $field) &&
+                       !preg_match('/^Subject/i', $field)) {
+                       $value = $this->stripComments($value);
+                    }
+                   $this->parseField($field, $value);
+               }
             }
         }
         if ($this->content_type == '') {
@@ -305,7 +307,11 @@ class Rfc822Header {
             $name = "$group";
             $addr_structure->personal = $name;
             $addr_ar[] = $addr_structure;
-            return (array($addr_ar, $pos+1));
+            return (array($addr_ar,$pos+1 ));
+       } elseif ($group) {
+            $addr_structure->personal = $name;
+            $addr_ar[] = $addr_structure;
+           return (array($addr_ar,$pos+1 ));
         } else {
             $addr_structure->personal = $name;
             if ($name || $addr) {
@@ -456,6 +462,64 @@ class Rfc822Header {
         }
         return $arr;
     }
+    
+    function findAddress($address, $recurs = false) {
+       $result = false;
+        if (is_array($address)) {
+           $i=0;
+            foreach($address as $argument) {
+                $match = $this->findAddress($argument, true);
+               $last = end($match);
+               if ($match[1]) {
+                   return $i;
+               } else {
+                   if (count($match[0]) && !$result) {
+                       $result = $i;
+                   }
+               }
+               ++$i;   
+            }
+       } else {
+           $srch_addr = $this->parseAddress($address);
+           $results = array();
+           foreach ($this->to as $to) {
+               if ($to->host == $srch_addr->host) {
+                   if ($to->mailbox == $srch_addr->mailbox) {
+                       $results[] = $srch_addr;
+                       if ($to->personal == $srch_addr->personal) {
+                           if ($recurs) {
+                               return array($results, true);
+                           } else {
+                               return true;
+                           }
+                       }
+                   }
+               }
+           }
+           foreach ($this->cc as $cc) {
+               if ($cc->host == $srch_addr->host) {
+                   if ($cc->mailbox == $srch_addr->mailbox) {
+                       $results[] = $srch_addr;
+                       if ($cc->personal == $srch_addr->personal) {
+                           if ($recurs) {
+                               return array($results, true);
+                           } else {
+                               return true;
+                           }
+                       }
+                   }
+               }
+           }
+           if ($recurs) {
+               return array($results, false);
+           } elseif (count($result)) {
+               return true;
+           } else {
+               return false;
+           }   
+       }
+       return $result;
+    }
 
     function getContentType($type0, $type1) {
         $type0 = $this->content_type->type0;