Getting ready for 1.4.0 RC1
[squirrelmail.git] / class / mime / Rfc822Header.class.php
index 19a012cbb7aef0ff6f97086d0dff2a52326f58cd..ff07b0cb8cc20b3d7a6170b539b1e210ede08029 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * Rfc822Header.class.php
  *
- * Copyright (c) 2002 The SquirrelMail Project Team
+ * Copyright (c) 2003 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This contains functions needed to handle mime messages.
@@ -26,6 +26,7 @@ class Rfc822Header {
         $bcc = array(),
         $in_reply_to = '',
         $message_id = '',
+       $references = '',
         $mime = false,
         $content_type = '',
         $disposition = '',
@@ -50,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 == '') {
@@ -141,6 +144,9 @@ class Rfc822Header {
             case 'message-id':
                 $this->message_id = $value;
                 break;
+           case 'references':
+               $this->references = $value;
+               break;
             case 'disposition-notification-to':
                 $this->dnt = $this->parseAddress($value);
                 break;
@@ -189,9 +195,24 @@ class Rfc822Header {
                 break;
         }
     }
-
+    /*
+     * parseAddress: recursive function for parsing address strings and store 
+     *               them in an address stucture object.
+     *               input: $address = string
+     *                      $ar      = boolean (return array instead of only the
+     *                                 first element)
+     *                      $addr_ar = array with parsed addresses
+     *                      $group   = string
+     *                      $host    = string (default domainname in case of 
+     *                                 addresses without a domainname)
+     *                      $lookup  = callback function (for lookup address
+     *                                 strings which are probably nicks
+     *                                 (without @ ) ) 
+     *               output: array with addressstructure objects or only one
+     *                       address_structure object.
+     */
     function parseAddress
-    ($address, $ar=false, $addr_ar = array(), $group = '') {
+    ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
         $pos = 0;
         $j = strlen($address);
         $name = '';
@@ -245,7 +266,24 @@ class Rfc822Header {
                         $addr_structure->mailbox = substr($addr, 0, $at);
                         $addr_structure->host = substr($addr, $at+1);
                     } else {
-                        $addr_structure->mailbox = $addr;
+                       /* if lookup function */
+                       if ($lookup) {
+                           $aAddr = call_user_func_array($lookup,array($addr));
+                           if (isset($aAddr['email'])) {
+                               $at = strpos($aAddr['email'], '@');
+                               $addr_structure->mailbox = substr($aAddr['email'], 0, $at);
+                               $addr_structure->host = substr($aAddr['email'], $at+1);
+                               if (isset($aAddr['name'])) {
+                                   $addr_structure->personal = $aAddr['name'];
+                               }
+                           }
+                       }
+                       if (!$addr_structure->mailbox) {
+                           $addr_structure->mailbox = trim($addr);
+                           if ($host) {
+                               $addr_structure->host = $host;
+                           }
+                       }
                     }
                     $address = trim(substr($address, $pos+1));
                     $j = strlen($address);
@@ -288,13 +326,34 @@ class Rfc822Header {
             $addr_structure->mailbox = trim(substr($addr, 0, $at));
             $addr_structure->host = trim(substr($addr, $at+1));
         } else {
-            $addr_structure->mailbox = trim($addr);
+           /* if lookup function */
+           if ($lookup) {
+               $aAddr = call_user_func_array($lookup,array($addr));
+               if (isset($aAddr['email'])) {
+                   $at = strpos($aAddr['email'], '@');
+                    $addr_structure->mailbox = substr($aAddr['email'], 0, $at);
+                    $addr_structure->host = substr($aAddr['email'], $at+1);
+                   if (isset($aAddr['name'])) {
+                       $addr_structure->personal = $aAddr['name'];
+                   }
+               }
+           }
+           if (!$addr_structure->mailbox) {
+                $addr_structure->mailbox = trim($addr);
+               if ($host) {
+                   $addr_structure->host = $host;
+               }
+           }
         }
         if ($group && $addr == '') { /* no addresses found in group */
-            $name = "$group: Undisclosed recipients;";
+            $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) {
@@ -350,7 +409,7 @@ class Rfc822Header {
         $props = '';
         if ($pos > 0) {
             $name = trim(substr($value, 0, $pos));
-            $props = trim(substr($type, $pos+1));
+            $props = trim(substr($value, $pos+1));
         } else {
             $name = $value;
         }
@@ -445,6 +504,65 @@ 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 {
+           if (!is_array($this->cc)) $this->cc = array();
+           $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;