* Made everything here global too to work with new include system
[squirrelmail.git] / functions / imap_messages.php
index 98449ad9e079b567580e442a0dd9fd2b9f538364..2c47d5abf5ae5f80dbb2ae67875176eea3225fa9 100755 (executable)
@@ -2,10 +2,14 @@
    /**
     **  imap_messages.php
     **
-    **  This implements functions that manipulate messages 
+    **  This implements functions that manipulate messages
+    **
+    **  $Id$
     **/
 
-   if (!isset($mime_php)) include "../functions/mime.php";
+   if (defined ('imap_messages_php'))
+       return;
+   define ('imap_messages_php', true);
 
    /******************************************************************************
     **  Copies specified messages to specified folder
@@ -51,7 +55,8 @@
     **  Returns some general header information -- FROM, DATE, and SUBJECT
     ******************************************************************************/
    class small_header {
-      var $from, $subject, $date, $to, $priority, $message_id;
+      var $from = '', $subject = '', $date = '', $to = '', 
+          $priority = 0, $message_id = 0, $cc = '';
    }
         
    function sqimap_get_small_header ($imap_stream, $id, $sent) {
       $from = _("Unknown Sender");
       $priority = "0";
       $messageid = "<>";
+      $cc = "";
+      $to = "";
+      $date = "";
+      $type[0] = "";
+      $type[1] = "";
 
       $g = 0;
       for ($i = 0; $i < count($read); $i++) {
@@ -89,6 +99,8 @@
             if ($pos = strpos($type, ";"))
                $type = substr($type, 0, $pos);
             $type = explode("/", $type);
+           if (! isset($type[1]))
+               $type[1] = '';
          }
          
       }
 
       fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
       $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
-      preg_match("/([0-9]+)\)\s*$/i", $read[0], $regs);
+      eregi("([0-9]+)[^0-9]*$", $read[0], $regs);
       $size = $regs[1];
       
       $header = new small_header;
       if ($sent == true)
-         $header->from = (trim($to) != '')? $to : _('(only Cc/Bcc)');
+         $header->from = (trim($to) != '')? $to : _("(only Cc/Bcc)");
       else   
          $header->from = $from;
 
       $header->size = $size;
       $header->type0 = $type[0];
       $header->type1 = $type[1];
-
+      
       return $header;
    }
 
       fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
       if (ereg("FLAGS(.*)", $read[0], $regs))
-          return explode(" ", trim(ereg_replace('[\(\)\\]', '', $regs[1])));
+          return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
       return Array('None');
    }
 
     ******************************************************************************/
    function sqimap_get_message ($imap_stream, $id, $mailbox) {
       $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
-      $msg = sqimap_get_message_body($imap_stream, &$header);
-      return $msg;
+      return sqimap_get_message_body($imap_stream, $header);
    }
 
    /******************************************************************************
       $hdr->type1 = "plain";
       $hdr->charset = "us-ascii";
 
-      preg_match("/\{([0-9]+)\}/", $read[$i], $regs);
-      preg_match("/[0-9]+/", $regs[0], $regs);
-
       while ($i < count($read)) {
          if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
             $hdr->mime = true;
             }
 
             /** Detect the boundary of a multipart message **/
-            if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {                             
+            if (eregi('boundary="([^"]+)"', $line, $regs)) {                             
                $hdr->boundary = $regs[1];                                             
             }
 
             if (strpos(strtolower(trim($line)), "charset=")) {
                $pos = strpos($line, "charset=") + 8;
                $charset = trim($line);
-               if (strpos($line, " ", $pos) > 0) {
-                  $charset = substr($charset, $pos, strpos($line, " ", $pos));
+               if (strpos($line, ";", $pos) > 0) {
+                  $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
                } else {
                   $charset = substr($charset, $pos);
                }
 
          }
 
-         else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {   
+         else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
             /** Add better dontent-disposition support **/
             
             $line = $read[$i];
          /** FROM **/
          else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
             $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
-            if ($hdr->replyto == "")
+            if (! isset($hdr->replyto) || $hdr->replyto == "")
                $hdr->replyto = $hdr->from;
             $i++;
          }
          else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
             $d = substr($read[$i], 5);
             $d = trim($d);
-            $d = ereg_replace("  ", " ", $d);
-            $d = explode(" ", $d);
+            $d = strtr($d, array('  ' => ' '));
+            $d = explode(' ', $d);
             $hdr->date = getTimeStamp($d);
             $i++;
          }
     ******************************************************************************/
    function sqimap_get_message_body ($imap_stream, &$header) {
       $id = $header->id;
-      return decodeMime($imap_stream, $body, &$header);
+      return decodeMime($imap_stream, $header);
    }