thomascube
2006-03-27 3062b3ef1883147c79fbdc8832ccb1a232a75f28
program/lib/imap.inc
@@ -23,8 +23,27 @@
      Function containing "_C_" in name require connection handler to be
      passed as one of the parameters.  To obtain connection handler, use
      iil_Connect()
   VERSION:
      IlohaMail-0.9-20050415
   CHANGES:
      File altered by Thomas Bruederli <roundcube@gmail.com>
      to fit enhanced equirements by the RoundCube Webmail:
      - Added list of server capabilites and check these before invoking commands
      - Added junk flag to iilBasicHeader
      - Enhanced error reporting on fsockopen()
      - Additional parameter for SORT command
      - Removed Call-time pass-by-reference because deprecated
      - Parse charset from content-type in iil_C_FetchHeaders()
      - Enhanced heaer sorting
      - Pass message as reference in iil_C_Append (to save memory)
      - Added BCC and REFERENCE to the list of headers to fetch in iil_C_FetchHeaders()
      - Leave messageID unchanged in iil_C_FetchHeaders()
      - Avoid stripslahes in iil_Connect()
      - Removed <br> from error messages (better for logging)
      - Removed some debuggers (echo ...)
********************************************************/
// changed path to work within roundcube webmail
include_once("lib/icl_commons.inc");
@@ -52,6 +71,7 @@
   var $recent;
   var $rootdir;
   var $delimiter;
   var $capability = array();
}
class iilBasicHeader{
@@ -190,7 +210,7 @@
        $conn->errorNum = 0;
        return $conn->fp;
    }else{
        $conn->error .= 'Authentication failed (AUTH): <br>"'.htmlspecialchars($line)."\"";
        $conn->error .= 'Authentication for '.$user.' failed (AUTH): "'.htmlspecialchars($line)."\"";
        $conn->errorNum = -2;
        return false;
    }
@@ -211,7 +231,7 @@
    }else{
        $result=false;
        fclose($conn->fp);
        $conn->error .= 'Authentication failed (LOGIN):<br>"'.htmlspecialchars($line)."\"";
        $conn->error .= 'Authentication for '.$user.' failed (LOGIN): "'.htmlspecialchars($line)."\"";
        $conn->errorNum = -2;
    }
    return $result;
@@ -245,6 +265,9 @@
function iil_C_NameSpace(&$conn){
   global $my_prefs;
   if (!in_array('NAMESPACE', $conn->capability))
     return false;
   
   if ($my_prefs["rootdir"]) return true;
   
@@ -283,8 +306,8 @@
   $iil_errornum = 0;
   
   //strip slashes
   $user = stripslashes($user);
   $password = stripslashes($password);
   // $user = stripslashes($user);
   // $password = stripslashes($password);
   
   //set auth method
   $auth_method = "plain";
@@ -313,9 +336,9 @@
   //echo '<!-- conn sort_field: '.$my_prefs['sort_field'].' //-->';
   
   //check input
   if (empty($host)) $iil_error .= "Invalid host<br>\n";
   if (empty($user)) $iil_error .= "Invalid user<br>\n";
   if (empty($password)) $iil_error .= "Invalid password<br>\n";
   if (empty($host)) $iil_error .= "Invalid host\n";
   if (empty($user)) $iil_error .= "Invalid user\n";
   if (empty($password)) $iil_error .= "Invalid password\n";
   if (!empty($iil_error)) return false;
   if (!$ICL_PORT) $ICL_PORT = 143;
   
@@ -325,16 +348,16 @@
   }
   
   //open socket connection
   $conn->fp = @fsockopen($host, $ICL_PORT);
   $conn->fp = @fsockopen($host, $ICL_PORT, $errno, $errstr, 10);
   if (!$conn->fp){
        $iil_error = "Could not connect to $host at port $ICL_PORT";
        $iil_error = "Could not connect to $host at port $ICL_PORT: $errstr";
        $iil_errornum = -1;
      return false;
   }
   $iil_error.="Socket connection established\r\n";
   $line=iil_ReadLine($conn->fp, 300);
   if (strcasecmp($auth_method, "check")==0){
      //check for supported auth methods
      
@@ -345,9 +368,12 @@
      fputs($conn->fp, "cp01 CAPABILITY\r\n");
      do{
      $line = trim(chop(iil_ReadLine($conn->fp, 100)));
      $conn->message.="$line\n";
         $a = explode(" ", $line);
         if ($line[0]=="*"){
            while ( list($k, $w) = each($a) ){
                if ($w!='*' && $w!='CAPABILITY')
                   $conn->capability[] = $w;
               if ((strcasecmp($w, "AUTH=CRAM_MD5")==0)||
                  (strcasecmp($w, "AUTH=CRAM-MD5")==0)){
                     $auth_method = "auth";
@@ -362,6 +388,7 @@
      //do CRAM-MD5 authentication
      fputs($conn->fp, "a000 AUTHENTICATE CRAM-MD5\r\n");
      $line = trim(chop(iil_ReadLine($conn->fp, 1024)));
      $conn->message.="$line\n";
      if ($line[0]=="+"){
         $conn->message.='Got challenge: '.htmlspecialchars($line)."\n";
         //got a challenge string, try CRAM-5
@@ -599,7 +626,7 @@
   return $time2;
}
function iil_C_Sort(&$conn, $mailbox, $field){
function iil_C_Sort(&$conn, $mailbox, $field, $add=''){
   /*  Do "SELECT" command */
   if (!iil_C_Select($conn, $mailbox)) return false;
   
@@ -610,7 +637,7 @@
   if (!$fields[$field]) return false;
   
   $fp = $conn->fp;
   $command = 's SORT ('.$field.') US-ASCII ALL'."\r\n";
   $command = 's SORT ('.$field.') US-ASCII ALL '."$add\r\n";
   $line = $data = '';
   
   if (!fputs($fp, $command)) return false;
@@ -840,7 +867,7 @@
function iil_C_FetchUIDs(&$conn,$mailbox){
   global $clock;
   
   $num = iil_C_CountMessages(&$conn, $mailbox);
   $num = iil_C_CountMessages($conn, $mailbox);
   if ($num==0) return array();
   $message_set = '1'.($num>1?':'.$num:'');
   
@@ -1184,9 +1211,7 @@
   /* FETCH date,from,subject headers */
   $key="fh".($c++);
   $request=$key." FETCH $message_set (BODY.PEEK[HEADER.FIELDS (DATE FROM TO SUBJECT REPLY-TO IN-REPLY-TO CC CONTENT-TRANSFER-ENCODING CONTENT-TYPE MESSAGE-ID)])\r\n";
    // echo "// $request\n\n";
   $request=$key." FETCH $message_set (BODY.PEEK[HEADER.FIELDS (DATE FROM TO SUBJECT REPLY-TO IN-REPLY-TO CC BCC CONTENT-TRANSFER-ENCODING CONTENT-TYPE MESSAGE-ID REFERENCE)])\r\n";
   if (!fputs($fp, $request)) return false;
   do{
@@ -1212,8 +1237,10 @@
               $i++;
               $lines[$i] = trim(chop($line));
            }
         }while($line[0]!=")");
         }while($line[0]!=")" && strncmp($line, $key, strlen($key)));  // patch from "Maksim Rubis" <siburny@hotmail.com>
         
            if(strncmp($line, $key, strlen($key)))
            {
         //process header, fill iilBasicHeader obj.
         //   initialize
         if (is_array($headers)){
@@ -1235,14 +1262,24 @@
         $result[$id]->subject = str_replace("\n", "", $headers["subject"]);
         $result[$id]->replyto = str_replace("\n", " ", $headers["reply-to"]);
         $result[$id]->cc = str_replace("\n", " ", $headers["cc"]);
         $result[$id]->bcc = str_replace("\n", " ", $headers["bcc"]);
         $result[$id]->encoding = str_replace("\n", " ", $headers["content-transfer-encoding"]);
         $result[$id]->ctype = str_replace("\n", " ", $headers["content-type"]);
         //$result[$id]->in_reply_to = ereg_replace("[\n<>]",'', $headers['in-reply-to']);
         list($result[$id]->ctype,$foo) = explode(";", $headers["content-type"]);
         $result[$id]->in_reply_to = ereg_replace("[\n<>]",'', $headers['in-reply-to']);
         $result[$id]->reference = $headers["reference"];
         list($result[$id]->ctype, $ctype_add) = explode(";", $headers["content-type"]);
         if (preg_match('/charset="?([a-z0-9\-]+)"?/i', $ctype_add, $regs))
            $result[$id]->charset = $regs[1];
         $messageID = $headers["message-id"];
         if ($messageID) $messageID = substr(substr($messageID, 1), 0, strlen($messageID)-2);
         else $messageID = "mid:".$id;
         if (!$messageID) "mid:".$id;
         $result[$id]->messageID = $messageID;
         }
            else {
            $a=explode(" ", $line);
            }
         
      }
   }while(strcmp($a[0], $key)!=0);
@@ -1358,7 +1395,8 @@
   if ($field=="date"||$field=='internaldate') $field="timestamp";
   if (empty($flag)) $flag="ASC";
   $flag=strtoupper($flag);
   $stripArr = ($field=='subject') ? array('Re: ','Fwd: ','Fw: ',"\"") : array("\"");
   $c=count($a);
   if ($c>0){
      /*
@@ -1368,12 +1406,15 @@
         and use that to sort the main array.
      */
                
                // create "index" array
      // create "index" array
      $index=array();
      reset($a);
      while (list($key, $val)=each($a)){
         $data=$a[$key]->$field;
         if (is_string($data)) $data=strtoupper(str_replace("\"", "", $data));
         if (is_string($data))
            $data=strtoupper(str_replace($stripArr, "", $data));
         $index[$key]=$data;
      }
      
@@ -1386,7 +1427,7 @@
      $result=array();
      reset($index);
      while (list($key, $val)=each($index)){
         $result[$i]=$a[$key];
         $result[$key]=$a[$key];
         $i++;
      }
   }
@@ -1438,7 +1479,7 @@
         $line=chop(iil_ReadLine($fp, 100));
         if ($line[0]=="*") $c++;
      }while (!iil_StartsWith($line, "flg"));
      if (iil_ParseResult($line) == 0){
         iil_C_ExpireCachedItems($conn, $mailbox, $messages);
         return $c;
@@ -1539,12 +1580,12 @@
      $result_code=iil_ParseResult($line);
      if ($result_code==0) return $messages;
      else{
         $conn->error = "iil_C_Search: ".$line."<br>\n";
         $conn->error = "iil_C_Search: ".$line."\n";
         return false;
      }
      
   }else{
      $conn->error = "iil_C_Search: Couldn't select \"$folder\" <br>\n";
      $conn->error = "iil_C_Search: Couldn't select \"$folder\"\n";
      return false;
   }
}
@@ -1812,7 +1853,7 @@
        
        if ($result){
         $result = chop($result);
            return substr($result, 0, strlen($result)-1);
            return $result; // substr($result, 0, strlen($result)-1);
        }else return false;
   }else{
      echo "Select failed.";
@@ -1872,7 +1913,7 @@
   }
}
function iil_C_Append(&$conn, $folder, $message){
function iil_C_Append(&$conn, $folder, &$message){
   if (!$folder) return false;
   $fp = $conn->fp;
@@ -1883,24 +1924,20 @@
   if (!$len) return false;
   
   $request="A APPEND \"".$folder."\" (\\Seen) {".$len."}\r\n";
   // echo $request.'<br>';
   if (fputs($fp, $request)){
      $line=iil_ReadLine($fp, 100);
      // echo $line.'<br>';
      $line=iil_ReadLine($fp, 100);
      $sent = fwrite($fp, $message."\r\n");
      flush();
      do{
         $line=iil_ReadLine($fp, 1000);
         //echo $line.'<br>';
      }while($line[0]!="A");
   
      $result = (iil_ParseResult($line)==0);
      if (!$result) $conn->error .= $line."<br>\n";
      if (!$result) $conn->error .= $line."\n";
      return $result;
   
   }else{
      $conn->error .= "Couldn't send command \"$request\"<br>\n";
      $conn->error .= "Couldn't send command \"$request\"\n";
      return false;
   }
}
@@ -1913,7 +1950,7 @@
   $in_fp = false;            
   if (file_exists(realpath($path))) $in_fp = fopen($path, "r");
   if (!$in_fp){ 
      $conn->error .= "Couldn't open $path for reading<br>\n";
      $conn->error .= "Couldn't open $path for reading\n";
      return false;
   }
   
@@ -1940,15 +1977,14 @@
      //read response
      do{
         $line=iil_ReadLine($fp, 1000);
         //echo $line.'<br>';
      }while($line[0]!="A");
         
      $result = (iil_ParseResult($line)==0);
      if (!$result) $conn->error .= $line."<br>\n";
      if (!$result) $conn->error .= $line."\n";
      return $result;
   
   }else{
      $conn->error .= "Couldn't send command \"$request\"<br>\n";
      $conn->error .= "Couldn't send command \"$request\"\n";
      return false;
   }
}