alecpl
2008-12-11 81c7b2cd11d117802cea6ad6e374bc197ace1f22
program/lib/imap.inc
@@ -72,6 +72,9 @@
      - added 3rd argument in iil_StartsWith* functions
      - fix iil_C_FetchPartHeader() in some cases by use of iil_C_HandlePartBody()
      - allow iil_C_HandlePartBody() to fetch whole message
      - optimize iil_C_FetchHeaders() to use only one FETCH command
      - added 4th argument to iil_Connect()
      - allow setting rootdir and delimiter before connect
********************************************************/
@@ -475,13 +478,14 @@
function iil_C_NameSpace(&$conn) {
   global $my_prefs;
   if (isset($my_prefs['rootdir']) && is_string($my_prefs['rootdir'])) {
          $conn->rootdir = $my_prefs['rootdir'];
      return true;
   }
   
   if (!iil_C_GetCapability($conn, 'NAMESPACE')) {
       return false;
   }
   if ($my_prefs["rootdir"]) {
       return true;
   }
    
   iil_PutLine($conn->fp, "ns1 NAMESPACE");
@@ -509,12 +513,13 @@
    
   $conn->rootdir       = $first_userspace[0];
   $conn->delimiter     = $first_userspace[1];
   $my_prefs["rootdir"] = substr($conn->rootdir, 0, -1);
   $my_prefs['rootdir'] = substr($conn->rootdir, 0, -1);
   $my_prefs['delimiter'] = $conn->delimiter;
   
   return true;
}
function iil_Connect($host, $user, $password) {
function iil_Connect($host, $user, $password, $options=null) {
   global $iil_error, $iil_errornum;
   global $ICL_SSL, $ICL_PORT;
   global $IMAP_NO_CACHE;
@@ -522,18 +527,23 @@
   
   $iil_error = '';
   $iil_errornum = 0;
   //set auth method
   $auth_method = 'plain';
   if (func_num_args() >= 4) {
      $auth_array = func_get_arg(3);
      if (is_array($auth_array)) {
         $auth_method = $auth_array['imap'];
          }
      if (empty($auth_method)) {
              $auth_method = "plain";
          }
   // set some imap options
   if (is_array($options)) {
      foreach($options as $optkey => $optval) {
         if ($optkey == 'imap') {
            $auth_method = $optval;
         } else if ($optkey == 'rootdir') {
                $my_prefs['rootdir'] = $optval;
         } else if ($optkey == 'delimiter') {
                $my_prefs['delimiter'] = $optval;
         }
      }
   }
   if (empty($auth_method))
          $auth_method = 'plain';
   $message = "INITIAL: $auth_method\n";
      
   $result = false;
@@ -585,7 +595,7 @@
   $conn->fp = fsockopen($host, $ICL_PORT, $errno, $errstr, 10);
   if (!$conn->fp) {
          $iil_error = "Could not connect to $host at port $ICL_PORT: $errstr";
          $iil_errornum = -1;
          $iil_errornum = -2;
      return false;
   }
@@ -1610,7 +1620,6 @@
{
   global $IMAP_USE_INTERNAL_DATE;
   
   $c      = 0;
   $result = array();
   $fp     = $conn->fp;
   
@@ -1648,11 +1657,10 @@
      }
   }
   /* FETCH date,from,subject headers */
   $key     = 'fh' . ($c++);
   $prefix     = $uidfetch?' UID':'';
   $request  = $key . $prefix;
   $request .= " FETCH $message_set (BODY.PEEK[HEADER.FIELDS ";
   /* FETCH uid, size, flags and headers */
   $key       = 'FH12';
   $request  = $key . ($uidfetch ? ' UID' : '') . " FETCH $message_set ";
   $request .= "(UID RFC822.SIZE FLAGS INTERNALDATE BODY.PEEK[HEADER.FIELDS ";
   $request .= "(DATE FROM TO SUBJECT REPLY-TO IN-REPLY-TO CC BCC ";
   $request .= "CONTENT-TRANSFER-ENCODING CONTENT-TYPE MESSAGE-ID ";
   $request .= "REFERENCES DISPOSITION-NOTIFICATION-TO X-PRIORITY)])";
@@ -1661,7 +1669,7 @@
      return false;
   }
   do {
      $line = chop(iil_ReadLine($fp, 200));
      $line = chop(iil_ReadLine($fp, 1024));
      $a    = explode(' ', $line);
      if (($line[0] == '*') && ($a[2] == 'FETCH')) {
         $id = $a[1];
@@ -1672,15 +1680,100 @@
         $result[$id]->messageID = 'mid:' . $id;
         /*
             Sample reply line:
             * 321 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen)
             INTERNALDATE "16-Nov-2008 21:08:46 +0100" BODY[HEADER.FIELDS ...
         */
         if (preg_match('/^\* [0-9]+ FETCH \((.*) BODY\[HEADER/', $line, $matches)) {
            $str = $matches[1];
            //swap parents with quotes, then explode
            $str = eregi_replace("[()]", "\"", $str);
            $a = iil_ExplodeQuotedString(' ', $str);
            //did we get the right number of replies?
            $parts_count = count($a);
            if ($parts_count>=8) {
               for ($i=0; $i<$parts_count; $i=$i+2) {
                  if (strcasecmp($a[$i],'UID') == 0)
                     $result[$id]->uid = $a[$i+1];
                  else if (strcasecmp($a[$i],'RFC822.SIZE') == 0)
                     $result[$id]->size = $a[$i+1];
                  else if (strcasecmp($a[$i],'INTERNALDATE') == 0)
                     $time_str = $a[$i+1];
                  else if (strcasecmp($a[$i],'FLAGS') == 0)
                     $flags_str = $a[$i+1];
               }
               // process flags
               $flags_str = eregi_replace('[\\\"]', '', $flags_str);
               $flags_a   = explode(' ', $flags_str);
               if (is_array($flags_a)) {
                  reset($flags_a);
                  while (list(,$val)=each($flags_a)) {
                     if (strcasecmp($val,'Seen') == 0) {
                         $result[$id]->seen = true;
                     } else if (strcasecmp($val, 'Deleted') == 0) {
                         $result[$id]->deleted=true;
                     } else if (strcasecmp($val, 'Recent') == 0) {
                         $result[$id]->recent = true;
                     } else if (strcasecmp($val, 'Answered') == 0) {
                         $result[$id]->answered = true;
                     } else if (strcasecmp($val, '$Forwarded') == 0) {
                         $result[$id]->forwarded = true;
                     } else if (strcasecmp($val, 'Draft') == 0) {
                         $result[$id]->is_draft = true;
                     } else if (strcasecmp($val, '$MDNSent') == 0) {
                         $result[$id]->mdn_sent = true;
                     } else if (strcasecmp($val, 'Flagged') == 0) {
                          $result[$id]->flagged = true;
                     }
                  }
                  $result[$id]->flags = $flags_a;
               }
               $time_str = str_replace('"', '', $time_str);
               // if time is gmt...
                              $time_str = str_replace('GMT','+0000',$time_str);
               //get timezone
               $time_str      = substr($time_str, 0, -1);
               $time_zone_str = substr($time_str, -5); // extract timezone
               $time_str      = substr($time_str, 1, -6); // remove quotes
               $time_zone     = (float)substr($time_zone_str, 1, 2); // get first two digits
               if ($time_zone_str[3] != '0') {
                        $time_zone += 0.5;  //handle half hour offset
               }
               if ($time_zone_str[0] == '-') {
                       $time_zone = $time_zone * -1.0; //minus?
               }
               //calculate timestamp
                                        $timestamp     = strtotime($time_str); //return's server's time
               $timestamp    -= $time_zone * 3600; //compensate for tz, get GMT
               $result[$id]->internaldate = $time_str;
               $result[$id]->timestamp = $timestamp;
               $result[$id]->date = $time_str;
            }
         }
         /*
            Start parsing headers.  The problem is, some header "lines" take up multiple lines.
            So, we'll read ahead, and if the one we're reading now is a valid header, we'll
            process the previous line.  Otherwise, we'll keep adding the strings until we come
            to the next valid header line.
         */
         $i     = 0;
         $lines = array();
         do {
            $line = chop(iil_ReadLine($fp, 300), "\r\n");
            if (ord($line[0])<=32) {
                $lines[$i] .= (empty($lines[$i])?'':"\n").trim($line);
            } else {
@@ -1689,7 +1782,7 @@
            }
            /* 
               The preg_match below works around communigate imap, which outputs " UID <number>)".
               Without this, the while statement continues on and gets the "fh0 OK completed" message.
               Without this, the while statement continues on and gets the "FH0 OK completed" message.
               If this loop gets the ending message, then the outer loop does not receive it from radline on line 1249.  
               This in causes the if statement on line 1278 to never be true, which causes the headers to end up missing
               If the if statement was changed to pick up the fh0 from this loop, then it causes the outer loop to spin
@@ -1702,7 +1795,7 @@
            }
         // patch from "Maksim Rubis" <siburny@hotmail.com>
         } while (trim($line[0]) != ')' && strncmp($line, $key, strlen($key)));
         if (strncmp($line, $key, strlen($key))) { 
            //process header, fill iilBasicHeader obj.
            //   initialize
@@ -1722,8 +1815,10 @@
               
               switch ($field) {
               case 'date';
                  $result[$id]->date = $string;
                  $result[$id]->timestamp = iil_StrToTime($string);
                  if (!$IMAP_USE_INTERNAL_DATE) {
                     $result[$id]->date = $string;
                     $result[$id]->timestamp = iil_StrToTime($string);
                  }
                  break;
               case 'from':
                  $result[$id]->from = $string;
@@ -1776,128 +1871,23 @@
                  break;
               } // end switch ()
            } // end while ()
            if ($conn->do_cache) {
               $uid = $result[$id]->uid;
               $conn->cache[$mailbox][$uid] = $result[$id];
               $conn->cache_dirty[$mailbox] = true;
            }
         } else {
            $a = explode(' ', $line);
         }
      }
   } while (strcmp($a[0], $key) != 0);
   /*
      FETCH uid, size, flags
      Sample reply line: "* 3 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen \Deleted))"
   */
   $command_key = 'fh' . ($c++);
   $request     = $command_key . $prefix;
   $request    .= " FETCH $message_set (UID RFC822.SIZE FLAGS INTERNALDATE)";
   if (!iil_PutLine($fp, $request)) {
       return false;
   }
   do {
      $line = chop(iil_ReadLine($fp, 200));
      //$a = explode(' ', $line);
      //if (($line[0]=="*") && ($a[2]=="FETCH")) {
      if ($line[0] == '*') {
         //echo "<!-- $line //-->\n";
         //get outter most parens
         $open_pos = strpos($line, "(") + 1;
         $close_pos = strrpos($line, ")");
         if ($open_pos && $close_pos) {
            //extract ID from pre-paren
            $pre_str = substr($line, 0, $open_pos);
            $pre_a = explode(' ', $line);
            $id = $pre_a[1];
            //get data
            $len = $close_pos - $open_pos;
            $str = substr($line, $open_pos, $len);
            //swap parents with quotes, then explode
            $str = eregi_replace("[()]", "\"", $str);
            $a = iil_ExplodeQuotedString(' ', $str);
            //did we get the right number of replies?
            $parts_count = count($a);
            if ($parts_count>=8) {
               for ($i=0;$i<$parts_count;$i=$i+2) {
                  if (strcasecmp($a[$i],"UID") == 0) $result[$id]->uid=$a[$i+1];
                  else if (strcasecmp($a[$i],"RFC822.SIZE") == 0) $result[$id]->size=$a[$i+1];
                  else if (strcasecmp($a[$i],"INTERNALDATE") == 0) $time_str = $a[$i+1];
                  else if (strcasecmp($a[$i],"FLAGS") == 0) $flags_str = $a[$i+1];
               }
               // process flags
               $flags_str = eregi_replace('[\\\"]', '', $flags_str);
               $flags_a   = explode(' ', $flags_str);
               if (is_array($flags_a)) {
                  reset($flags_a);
                  while (list($key,$val)=each($flags_a)) {
                     if (strcasecmp($val,'Seen') == 0) {
                         $result[$id]->seen = true;
                     } else if (strcasecmp($val, 'Deleted') == 0) {
                         $result[$id]->deleted=true;
                     } else if (strcasecmp($val, 'Recent') == 0) {
                         $result[$id]->recent = true;
                     } else if (strcasecmp($val, 'Answered') == 0) {
                         $result[$id]->answered = true;
                     } else if (strcasecmp($val, '$Forwarded') == 0) {
                         $result[$id]->forwarded = true;
                     } else if (strcasecmp($val, 'Draft') == 0) {
                         $result[$id]->is_draft = true;
                     } else if (strcasecmp($val, '$MDNSent') == 0) {
                         $result[$id]->mdn_sent = true;
                     } else if (strcasecmp($val, 'Flagged') == 0) {
                          $result[$id]->flagged = true;
                     }
                  }
                  $result[$id]->flags = $flags_a;
               }
               // if time is gmt...
               $time_str = str_replace('GMT','+0000',$time_str);
               //get timezone
               $time_str      = substr($time_str, 0, -1);
               $time_zone_str = substr($time_str, -5); //extract timezone
               $time_str      = substr($time_str, 1, -6); //remove quotes
               $time_zone     = (float)substr($time_zone_str, 1, 2); //get first two digits
               if ($time_zone_str[3] != '0') {
                   $time_zone += 0.5;  //handle half hour offset
               }
               if ($time_zone_str[0] == '-') {
                  $time_zone = $time_zone * -1.0; //minus?
               }
               $result[$id]->internaldate = $time_str;
               if ($IMAP_USE_INTERNAL_DATE || empty($result[$id]->date)) {
                  //calculate timestamp
                  $timestamp     = strtotime($time_str); //return's server's time
                  $na_timestamp  = $timestamp;
                  $timestamp    -= $time_zone * 3600; //compensate for tz, get GMT
                  $result[$id]->timestamp = $timestamp;
                  $result[$id]->date = $time_str;
               }
               if ($conn->do_cache) {
                  $uid = $result[$id]->uid;
                  $conn->cache[$mailbox][$uid] = $result[$id];
                  $conn->cache_dirty[$mailbox] = true;
               }
               //echo "<!-- ID: $id : $time_str -- local: $na_timestamp (".date("F j, Y, g:i a", $na_timestamp).") tz: $time_zone -- GMT: ".$timestamp." (".date("F j, Y, g:i a", $timestamp).")  //-->\n";
            } else {
               //echo "<!-- ERROR: $id : $str //-->\n";
            }
         }
      }
   } while (strpos($line, $command_key) === false);
   return $result;
}
function iil_C_FetchHeader(&$conn, $mailbox, $id, $uidfetch=false) {
   $fp = $conn->fp;
   $a  = iil_C_FetchHeaders($conn, $mailbox, $id, $uidfetch);
   if (is_array($a)) {
      return array_shift($a);
@@ -2157,8 +2147,14 @@
 * @see iil_Connect()
 */
function iil_C_GetHierarchyDelimiter(&$conn) {
   global $my_prefs;
   if ($conn->delimiter) {
        return $conn->delimiter;
          return $conn->delimiter;
   }
   if (!empty($my_prefs['delimiter'])) {
           return ($conn->delimiter = $my_prefs['delimiter']);
   }
    
   $fp        = $conn->fp;
@@ -2373,7 +2369,7 @@
   $part = empty($part) ? 'HEADER' : $part.'.MIME';
   return iil_C_HandlePartBody(&$conn, $mailbox, $id, $part, 1);
   return iil_C_HandlePartBody($conn, $mailbox, $id, $part, 1);
}
function iil_C_HandlePartBody(&$conn, $mailbox, $id, $part='', $mode=1, $file=NULL) {
@@ -2385,7 +2381,7 @@
   
   $fp     = $conn->fp;
   $result = false;
   if (iil_C_Select($conn, $mailbox)) {
          $reply_key = '* ' . $id;