alecpl
2008-04-29 6ee5ed253b92593ac5784300ac425f34f7fd1728
program/lib/imap.inc
@@ -49,17 +49,25 @@
      - Sanity check of $message_set in iil_C_FetchHeaders(), iil_C_FetchHeaderIndex(), iil_C_FetchThreadHeaders()
      - Implemented UID FETCH in iil_C_FetchHeaders()
      - Abort do-loop on socket errors (fgets returns false)
      - $ICL_SSL is not boolean anymore but contains the connection schema (ssl or tls)
      - Removed some debuggers (echo ...)
********************************************************/
/**
 * @todo Possibly clean up more CS.
 * @todo Try to replace most double-quotes with single-quotes.
 * @todo Split this file into smaller files.
 * @todo Refactor code.
 * @todo Replace echo-debugging (make it adhere to config setting and log)
 */
// changed path to work within roundcube webmail
include_once("lib/icl_commons.inc");
include_once 'lib/icl_commons.inc';
if (!$IMAP_USE_HEADER_DATE) {
   $IMAP_USE_INTERNAL_DATE = true;
if (!isset($IMAP_USE_HEADER_DATE) || !$IMAP_USE_HEADER_DATE) {
    $IMAP_USE_INTERNAL_DATE = true;
}
/**
@@ -120,6 +128,7 @@
   var $f;
   var $internaldate;
   var $references;
   var $priority;
   var $mdn_to;
   var $mdn_sent = false;
   var $is_reply = false;
@@ -152,41 +161,48 @@
}
function iil_ReadLine($fp, $size) {
   $line = '';
   if ($fp) {
      do {
         $buffer = fgets($fp, 2048);
         if ($buffer === false) {
            break;
            }
         $line.=$buffer;
      } while ($buffer[strlen($buffer)-1]!="\n");
   }
   return $line;
    $line = '';
    if (!$fp) {
        return $line;
    }
    do {
   // FIXME: hardcode size?
        $buffer = fgets($fp, 2048);
        if ($buffer === false) {
            break;
        }
        $line .= $buffer;
    } while ($buffer[strlen($buffer)-1] != "\n");
    return $line;
}
function iil_MultLine($fp, $line) {
   $line = chop($line);
   if (ereg('\{[0-9]+\}$', $line)) {
      $out = '';
      preg_match_all('/(.*)\{([0-9]+)\}$/', $line, $a);
      $bytes = $a[2][0];
      while (strlen($out)<$bytes) {
         $out.=chop(iil_ReadLine($fp, 1024));
      while (strlen($out) < $bytes) {
          $line = iil_ReadLine($fp, 1024);
         $out .= chop($line);
      }
      $line = $a[1][0]."\"$out\"";
      $line = $a[1][0] . "\"$out\"";
   }
   return $line;
}
function iil_ReadBytes($fp, $bytes) {
   $data = '';
   $len  = 0;
   do {
      $data.=fread($fp, $bytes-$len);
      $len = strlen($data);
   } while ($len<$bytes);
   return $data;
    $data = '';
    $len  = 0;
    do {
        $data .= fread($fp, $bytes-$len);
        if ($len == strlen($data)) {
            break; //nothing was read -> exit to avoid apache lockups
        }
        $len = strlen($data);
    } while ($len < $bytes);
    return $data;
}
function iil_ReadReply($fp) {
@@ -242,8 +258,8 @@
    
    // initialize ipad, opad
    for ($i=0;$i<64;$i++) {
        $ipad .=chr(0x36);
        $opad .=chr(0x5C);
        $ipad .= chr(0x36);
        $opad .= chr(0x5C);
    }
    // pad $pass so it's 64 bytes
    $padLen = 64 - strlen($pass);
@@ -252,13 +268,15 @@
    }
    
    // generate hash
    $hash = md5(iil_xor($pass,$opad) . pack("H*",md5(iil_xor($pass, $ipad) . base64_decode($encChallenge))));
    $hash  = iil_xor($pass,$opad);
    $hash .= pack("H*", md5(iil_xor($pass, $ipad) . base64_decode($encChallenge)));
    $hash  = md5($hash);
    
    // generate reply
    $reply = base64_encode($user . ' ' . $hash);
    $reply = base64_encode('"' . $user . '" "' . $hash . '"');
    
    // send result, get reply
    fputs($conn->fp, $reply."\r\n");
    fputs($conn->fp, $reply . "\r\n");
    $line = iil_ReadLine($conn->fp, 1024);
    
    // process result
@@ -267,7 +285,7 @@
        $conn->errorNum  = 0;
        return $conn->fp;
    }
    $conn->error    .= 'Authentication for '.$user.' failed (AUTH): "';
    $conn->error    .= 'Authentication for ' . $user . ' failed (AUTH): "';
    $conn->error    .= htmlspecialchars($line) . '"';
    $conn->errorNum  = -2;
    return false;
@@ -443,7 +461,7 @@
    
   //check for SSL
   if ($ICL_SSL) {
      $host = "ssl://".$host;
      $host = $ICL_SSL . '://' . $host;
   }
   
   //open socket connection
@@ -461,17 +479,19 @@
      //check for supported auth methods
      
      //default to plain text auth
      $auth_method = "plain";
      $auth_method = 'plain';
         
      //check for CRAM-MD5
      fputs($conn->fp, "cp01 CAPABILITY\r\n");
      do {
          $line = trim(chop(iil_ReadLine($conn->fp, 100)));
          $conn->message.="$line\n";
          $conn->message .= "$line\n";
         $a = explode(' ', $line);
         if ($line[0]=="*") {
            while ( list($k, $w) = each($a) ) {
                if ($w!='*' && $w!='CAPABILITY') {
         if ($line[0] == '*') {
            while (list($k, $w) = each($a)) {
                if ($w != '*' && $w != 'CAPABILITY') {
                   $conn->capability[] = $w;
                    }
               if ((strcasecmp($w, "AUTH=CRAM_MD5") == 0)||
@@ -480,25 +500,29 @@
               }
            }
         }
      } while ($a[0]!="cp01");
      } while ($a[0] != 'cp01');
   }
   if (strcasecmp($auth_method, "auth") == 0) {
   if (strcasecmp($auth_method, 'auth') == 0) {
      $conn->message .= "Trying CRAM-MD5\n";
      //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";
      $conn->message .= "$line\n";
        
      if ($line[0] == "+") {
         $conn->message.='Got challenge: '.htmlspecialchars($line)."\n";
      if ($line[0] == '+') {
         $conn->message .= 'Got challenge: ' . htmlspecialchars($line) . "\n";
         //got a challenge string, try CRAM-5
         $result = iil_C_Authenticate($conn, $user, $password, substr($line,2));
         $conn->message.= "Tried CRAM-MD5: $result \n";
         $conn->message .= "Tried CRAM-MD5: $result \n";
      } else {
         $conn->message.='No challenge ('.htmlspecialchars($line)."), try plain\n";
         $auth = "plain";
         $conn->message .='No challenge ('.htmlspecialchars($line)."), try plain\n";
            $auth = 'plain';
      }
   }
      
@@ -557,22 +581,30 @@
}
function iil_C_LoadCache(&$conn, $folder) {
   if (!$conn->do_cache) return false;
   $key = $folder.".imap";
   if (!$conn->do_cache) {
       return false;
   }
   $key = $folder.'.imap';
   if (!is_array($conn->cache[$folder])) {
      $conn->cache[$folder] = cache_read($conn->user, $conn->host, $key);
      $conn->cache[$folder]       = cache_read($conn->user, $conn->host, $key);
      $conn->cache_dirty[$folder] = false;
   }
}
function iil_C_ExpireCachedItems(&$conn, $folder, $message_set) {
   
   if (!$conn->do_cache) return;   //caching disabled
   if (!is_array($conn->cache[$folder])) return;   //cache not initialized|empty
   if (count($conn->cache[$folder]) == 0) return;   //cache not initialized|empty
   $uids = iil_C_FetchHeaderIndex($conn, $folder, $message_set, "UID");
   if (!$conn->do_cache) {
       return;   //caching disabled
   }
    if (!is_array($conn->cache[$folder])) {
        return;   //cache not initialized|empty
   }
    if (count($conn->cache[$folder]) == 0) {
        return;   //cache not initialized|empty
    }
   $uids = iil_C_FetchHeaderIndex($conn, $folder, $message_set, 'UID');
   $num_removed = 0;
   if (is_array($uids)) {
      //echo "<!-- unsetting: ".implode(",",$uids)." //-->\n";
@@ -603,16 +635,19 @@
}
function iil_ExplodeQuotedString($delimiter, $string) {
   $quotes=explode("\"", $string);
   while ( list($key, $val) = each($quotes))
      if (($key % 2) == 1)
   $quotes=explode('"', $string);
   while ( list($key, $val) = each($quotes)) {
      if (($key % 2) == 1) {
         $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
   $string=implode("\"", $quotes);
        }
    }
   $string=implode('"', $quotes);
   
   $result=explode($delimiter, $string);
   while ( list($key, $val) = each($result) )
      $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
   while ( list($key, $val) = each($result) ) {
      $result[$key] = str_replace('_!@!_', $delimiter, $result[$key]);
   }
   return $result;
}
@@ -628,10 +663,10 @@
      do {
         $line=chop(iil_ReadLine($fp, 300));
         $a=explode(' ', $line);
         if (($a[0] == '*') && (strcasecmp($a[2], "RECENT") == 0)) {
         if (($a[0] == '*') && (strcasecmp($a[2], 'RECENT') == 0)) {
             $result = (int) $a[1];
            }
      } while (!iil_StartsWith($a[0],"a002"));
      } while (!iil_StartsWith($a[0], 'a002'));
      fputs($fp, "a003 LOGOUT\r\n");
      fclose($fp);
@@ -659,14 +694,18 @@
         $line=chop(iil_ReadLine($fp, 300));
         $a=explode(' ', $line);
         if (count($a) == 3) {
            if (strcasecmp($a[2], "EXISTS") == 0) $conn->exists=(int)$a[1];
            if (strcasecmp($a[2], "RECENT") == 0) $conn->recent=(int)$a[1];
            if (strcasecmp($a[2], 'EXISTS') == 0) {
                $conn->exists = (int) $a[1];
            }
                if (strcasecmp($a[2], 'RECENT') == 0) {
                    $conn->recent = (int) $a[1];
                }
         }
      } while (!iil_StartsWith($line, "sel1"));
      } while (!iil_StartsWith($line, 'sel1'));
      $a=explode(' ', $line);
      if (strcasecmp($a[1],"OK") == 0) {
      if (strcasecmp($a[1], 'OK') == 0) {
         $conn->selected = $mailbox;
         return true;
      }
@@ -721,15 +760,15 @@
   
   //replace double spaces with single space
   $str = trim($str);
   $str = str_replace("  ", " ", $str);
   $str = str_replace('  ', ' ', $str);
   
   //strip off day of week
   $pos=strpos($str, " ");
   $pos = strpos($str, ' ');
   if (!is_numeric(substr($str, 0, $pos))) {
       $str = substr($str, $pos+1);
    }
   //explode, take good parts
   $a=explode(' ',$str);
   $a = explode(' ', $str);
   $month_str = $a[1];
   $month     = $IMAP_MONTHS[$month_str];
@@ -738,7 +777,7 @@
   $time      = $a[3];
   $tz_str    = $a[4];
   $tz        = substr($tz_str, 0, 3);
   $ta        = explode(":",$time);
   $ta        = explode(':', $time);
   $hour      = (int)$ta[0]-(int)$tz;
   $minute    = (int)$ta[1];
   $second    = (int)$ta[2];
@@ -750,7 +789,7 @@
}
function iil_C_Sort(&$conn, $mailbox, $field, $add='', $is_uid=FALSE,
    $encoding='US-ASCII') {
    $encoding = 'US-ASCII') {
   /*  Do "SELECT" command */
   if (!iil_C_Select($conn, $mailbox)) {
       return false;
@@ -759,8 +798,8 @@
   if ($field == 'INTERNALDATE') {
       $field = 'ARRIVAL';
    }
   $fields = array('ARRIVAL'=>1,'CC'=>1,'DATE'=>1,'FROM'=>1,'SIZE'=>1,
        'SUBJECT'=>1,'TO'=>1);
   $fields = array('ARRIVAL' => 1,'CC' => 1,'DATE' => 1,
        'FROM' => 1, 'SIZE' => 1, 'SUBJECT' => 1, 'TO' => 1);
   
   if (!$fields[$field]) {
       return false;
@@ -772,16 +811,19 @@
       $add = " $add";
    }
   $fp      = $conn->fp;
   $command = 's '. $is_uid .'SORT ('.$field.') '.$encoding.' ALL'."$add\r\n";
   $line    = $data = '';
   $fp       = $conn->fp;
   $command  = 's ' . $is_uid . 'SORT (' . $field . ') ';
    $command .= $encoding . ' ALL' . "$add\r\n";
   $line     = $data = '';
   
   if (!fputs($fp, $command)) {
       return false;
    }
   do {
      $line = chop(iil_ReadLine($fp, 1024));
      if (iil_StartsWith($line, '* SORT')) $data.=($data?' ':'').substr($line,7);
      if (iil_StartsWith($line, '* SORT')) {
          $data .= ($data?' ':'') . substr($line, 7);
        }
   } while ($line[0]!='s');
   
   if (empty($data)) {
@@ -882,45 +924,54 @@
            $bytes = (int)substr($line, $pos, $end_pos-$pos);
            $received = 0;
            do {
               $line = iil_ReadLine($fp, 0);
               $received+=strlen($line);
               $line = chop($line);
               $line      = iil_ReadLine($fp, 0);
               $received += strlen($line);
               $line      = chop($line);
               
               if ($received>$bytes) break;
               else if (!$line) continue;
               if ($received>$bytes) {
                        break;
               } else if (!$line) {
                        continue;
               }
               list($field, $string) = explode(': ', $line);
               
               list($field,$string)=explode(": ", $line);
               if (strcasecmp($field, "date") == 0)
               if (strcasecmp($field, 'date') == 0) {
                  $result[$id] = iil_StrToTime($string);
               else if ($index_field!="DATE")
               } else if ($index_field != 'DATE') {
                  $result[$id]=strtoupper(str_replace('"', '', $string));
            } while ($line[0]!=")");
                    }
            } while ($line[0] != ')');
         } else {
            //one line response, not expected so ignore            
         }
         */
      } while (!iil_StartsWith($line, $key));
   }else if ($mode == 6) {
      $key     = 'fhi' . ($c++);
      $request = $key." FETCH $message_set (INTERNALDATE)\r\n";
      $request = $key . " FETCH $message_set (INTERNALDATE)\r\n";
      if (!fputs($fp, $request)) {
          return false;
        }
      do {
         $line=chop(iil_ReadLine($fp, 200));
         if ($line[0]=="*") {
            //original: "* 10 FETCH (INTERNALDATE "31-Jul-2002 09:18:02 -0500")"
            $paren_pos = strpos($line, "(");
            $foo = substr($line, 0, $paren_pos);
            $a = explode(' ', $foo);
            $id = $a[1];
         if ($line[0] == '*') {
            /*
             * original:
             * "* 10 FETCH (INTERNALDATE "31-Jul-2002 09:18:02 -0500")"
             */
            $paren_pos = strpos($line, '(');
            $foo       = substr($line, 0, $paren_pos);
            $a         = explode(' ', $foo);
            $id        = $a[1];
            
            $open_pos = strpos($line, "\"") + 1;
            $close_pos = strrpos($line, "\"");
            $open_pos  = strpos($line, '"') + 1;
            $close_pos = strrpos($line, '"');
            if ($open_pos && $close_pos) {
               $len = $close_pos - $open_pos;
               $time_str = substr($line, $open_pos, $len);
               $len         = $close_pos - $open_pos;
               $time_str    = substr($line, $open_pos, $len);
               $result[$id] = strtotime($time_str);
            }
         } else {
@@ -972,23 +1023,21 @@
   }
   //check number of elements...
   list($start_mid,$end_mid) = explode(':', $message_set);
   list($start_mid, $end_mid) = explode(':', $message_set);
   if (is_numeric($start_mid) && is_numeric($end_mid)) {
      //count how many we should have
      $should_have = $end_mid - $start_mid +1;
      
      //if we have less, try and fill in the "gaps"
      if (count($result)<$should_have) {
         for ($i=$start_mid;$i<=$end_mid;$i++) {
      if (count($result) < $should_have) {
         for ($i=$start_mid; $i<=$end_mid; $i++) {
             if (!isset($result[$i])) {
                 $result[$i] = '';
                }
            }
      }
   }
   return $result;   
}
function iil_CompressMessageSet($message_set) {
@@ -1006,17 +1055,21 @@
   }
    
   //separate, then sort
   $ids = explode(',',$message_set);
   $ids = explode(',', $message_set);
   sort($ids);
   
   $result = array();
   $start = $prev = $ids[0];
   $start  = $prev = $ids[0];
   foreach ($ids as $id) {
      $incr = $id - $prev;
      if ($incr>1) {         //found a gap
         if ($start==$prev) $result[] = $prev;   //push single id
         else $result[] = $start.':'.$prev;      //push sequence as start_id:end_id
         $start = $id;                     //start of new sequence
      if ($incr > 1) {         //found a gap
         if ($start == $prev) {
             $result[] = $prev;   //push single id
         } else {
             $result[] = $start . ':' . $prev;   //push sequence as start_id:end_id
         }
            $start = $id;                     //start of new sequence
      }
      $prev = $id;
   }
@@ -1035,12 +1088,12 @@
   if (!is_array($uids) || count($uids) == 0) {
       return array();
    }
   return iil_C_Search($conn, $mailbox, "UID ".implode(",", $uids));
   return iil_C_Search($conn, $mailbox, 'UID ' . implode(',', $uids));
}
function iil_C_UIDToMID(&$conn, $mailbox, $uid) {
   $result = iil_C_UIDsToMIDs($conn, $mailbox, array($uid));
   if (count($result)==1) {
   if (count($result) == 1) {
       return $result[0];
   }
    return false;
@@ -1053,7 +1106,7 @@
   if ($num == 0) {
       return array();
    }
   $message_set = '1'.($num>1?':'.$num:'');
   $message_set = '1' . ($num>1?':' . $num:'');
   
   //if cache not enabled, just call iil_C_FetchHeaderIndex on 'UID' field
   if (!$conn->do_cache)
@@ -1069,17 +1122,17 @@
   }
    
   //was anything cached at all?
   if ($data===false) {
   if ($data === false) {
       $cache_good = -1;
   }
    
   //make sure number of messages were the same
   if ($cache_good>0 && $data['n']!=$num) {
   if ($cache_good > 0 && $data['n'] != $num) {
       $cache_good = -2;
   }
    
   //if everything's okay so far...
   if ($cache_good>0) {
   if ($cache_good > 0) {
      //check UIDs of highest mid with current and cached
      $temp = iil_C_Search($conn, $mailbox, 'UID ' . $data['d'][$num]);
      if (!$temp || !is_array($temp) || $temp[0] != $num) {
@@ -1088,12 +1141,12 @@
   }
   //if cached data's good, return it
   if ($cache_good>0) {
   if ($cache_good > 0) {
      return $data['d'];
   }
   //otherwise, we need to fetch it
   $data      = array('n'=>$num,'d'=>array());
   $data      = array('n' => $num, 'd' => array());
   $data['d'] = iil_C_FetchHeaderIndex($conn, $mailbox, $message_set, 'UID');
    
   cache_write($conn->user, $conn->host, $key, $data);
@@ -1173,16 +1226,19 @@
            $new_thhd = new iilThreadHeader;
            $new_thhd->id = $a[1];
            do {
               $line=chop(iil_ReadLine($fp, 1024),"\r\n");
               $line = chop(iil_ReadLine($fp, 1024), "\r\n");
               if (iil_StartsWithI($line, 'Message-ID:')
                        || (iil_StartsWithI($line,'In-Reply-To:'))
                        || (iil_StartsWithI($line,'SUBJECT:'))) {
                  $pos = strpos($line, ":");
                  $pos        = strpos($line, ':');
                  $field_name = substr($line, 0, $pos);
                  $field_val = substr($line, $pos+1);
                  $field_val  = substr($line, $pos+1);
                  $new[strtoupper($field_name)] = trim($field_val);
               } else if (ereg('^[[:space:]]', $line)) {
                  $new[strtoupper($field_name)].= trim($line);
                  $new[strtoupper($field_name)] .= trim($line);
               }
            } while ($line[0] != ')');
                
@@ -1256,8 +1312,8 @@
      //$new = array('id'=>$id, 'MESSAGE-ID'=>$header['m'], 
      //         'IN-REPLY-TO'=>$header['r'], 'SUBJECT'=>$header['s']);
      $id  = $header->id;
      $new = array('id'=>$id, 'MESSAGE-ID'=>$header->mid,
            'IN-REPLY-TO'=>$header->irt, 'SUBJECT'=>$header->sbj);
      $new = array('id' => $id, 'MESSAGE-ID' => $header->mid,
            'IN-REPLY-TO' => $header->irt, 'SUBJECT' => $header->sbj);
      /* add to message-id -> mid lookup table */
      $mid_to_id[$new['MESSAGE-ID']] = $id;
@@ -1298,7 +1354,7 @@
         }
            $sub_mids[$new['MESSAGE-ID']] = $root_id;
         $result[$root_id][]           = $id;
      }else if (!isset($roots[$sbj])||(!$has_re&&$root_in_root[$root_id])) {
      }else if (!isset($roots[$sbj]) || (!$has_re && $root_in_root[$root_id])) {
         /* try to use In-Reply-To header to find root 
            unless subject contains 'Re:' */
         if ($has_re&&$new['IN-REPLY-TO']) {
@@ -1341,7 +1397,7 @@
   
   //now that we've gone through all the messages,
   //go back and try and link up the stray threads
   if (count($strays)>0) {
   if (count($strays) > 0) {
      foreach ($strays as $id=>$irt) {
         $root_id = $sub_mids[$irt];
         if (!$root_id || $root_id==$id) {
@@ -1407,8 +1463,8 @@
   
   //sort by key, this basically sorts all threads
   ksort($itree);
   $i=0;
   $out=array();
   $i   = 0;
   $out = array();
   foreach ($itree as $k=>$node) {
      $out[$i] = $itree[$k];
      $i++;
@@ -1433,7 +1489,8 @@
   return $t_index;
}
function iil_C_FetchHeaders(&$conn, $mailbox, $message_set, $uidfetch=false) {
function iil_C_FetchHeaders(&$conn, $mailbox, $message_set, $uidfetch=false)
{
   global $IMAP_USE_INTERNAL_DATE;
   
   $c      = 0;
@@ -1442,9 +1499,9 @@
   
   list($from_idx, $to_idx) = explode(':', $message_set);
   if (empty($message_set) || (isset($to_idx)
        && (int)$from_idx > (int)$to_idx)) {
      && (int)$from_idx > (int)$to_idx)) {
      return false;
    }
   }
      
   /*  Do "SELECT" command */
   if (!iil_C_Select($conn, $mailbox)) {
@@ -1463,29 +1520,29 @@
               $result[$id]->id = $id;
            } else {
                $needed_set.=($needed_set ? ',': '') . $id;
                }
            }
         }
         //echo "<!-- iil_C_FetchHeader\nMessage Set: $message_set\nNeeded Set:$needed_set\n//-->\n";
         if ($needed_set) {
             $message_set = iil_CompressMessageSet($needed_set);
            } else {
                return $result;
            }
            $message_set = iil_CompressMessageSet($needed_set);
         } else {
            return $result;
         }
      }
   }
   /* FETCH date,from,subject headers */
   $key      = 'fh' . ($c++);
   $prefix   = $uidfetch?' UID':'';
   $key     = 'fh' . ($c++);
   $prefix     = $uidfetch?' UID':'';
   $request  = $key . $prefix;
    $request .= " FETCH $message_set (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)])\r\n";
   $request .= " FETCH $message_set (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)])\r\n";
   if (!fputs($fp, $request)) {
       return false;
    }
      return false;
   }
   do {
      $line = chop(iil_ReadLine($fp, 200));
      $a    = explode(' ', $line);
@@ -1496,6 +1553,7 @@
         $result[$id]->id        = $id;
         $result[$id]->subject   = '';
         $result[$id]->messageID = 'mid:' . $id;
         /*
            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
@@ -1508,7 +1566,7 @@
            $line = chop(iil_ReadLine($fp, 300), "\r\n");
            if (ord($line[0])<=32) {
                $lines[$i] .= (empty($lines[$i])?'':"\n").trim(chop($line));
            } else{
            } else {
               $i++;
               $lines[$i] = trim(chop($line));
            }
@@ -1522,26 +1580,30 @@
               if (!preg_match("/:/",$line) && preg_match("/\)$/",$line)) break;
               however, unsure how well this would work with all imap clients.
            */
            if (preg_match("/^\s*UID [0-9]+\)$/",$line)) {
            if (preg_match("/^\s*UID [0-9]+\)$/", $line)) {
                break;
                }
         } while (trim($line[0]) != ')' && strncmp($line, $key, strlen($key)));  // patch from "Maksim Rubis" <siburny@hotmail.com>
            }
         // 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
             if (is_array($headers)) {
                reset($headers);
                while ( list($k, $bar) = each($headers) ) $headers[$k] = '';
             }
             //   create array with header field:data
             while ( list($lines_key, $str) = each($lines) ) {
                list($field, $string) = iil_SplitHeaderLine($str);
                $field = strtolower($field);
                switch ($field) {
         if (strncmp($line, $key, strlen($key))) {
            //process header, fill iilBasicHeader obj.
            //   initialize
            if (is_array($headers)) {
               reset($headers);
               while (list($k, $bar) = each($headers)) {
                  $headers[$k] = '';
               }
            }
            //   create array with header field:data
            while ( list($lines_key, $str) = each($lines) ) {
               list($field, $string) = iil_SplitHeaderLine($str);
               $field  = strtolower($field);
                                        $string = ereg_replace("\n[[:space:]]*"," ",$string);
               switch ($field) {
               case 'date';
                  $result[$id]->date = $string;
                  $result[$id]->timestamp = iil_StrToTime($string);
@@ -1550,29 +1612,32 @@
                  $result[$id]->from = $string;
                  break;
               case 'to':
                  $result[$id]->to = str_replace("\n", " ", $string);
                  $result[$id]->to = $string;
                  break;
               case 'subject':
                  $result[$id]->subject = str_replace("\n", '', $string);
                  $result[$id]->subject = $string;
                  break;
               case 'reply-to':
                  $result[$id]->replyto = str_replace("\n", " ", $string);
                  $result[$id]->replyto = $string;
                  break;
               case 'cc':
                  $result[$id]->cc = str_replace("\n", " ", $string);
                  $result[$id]->cc = $string;
                  break;
               case 'bcc':
                  $result[$id]->bcc = str_replace("\n", " ", $string);
                  $result[$id]->bcc = $string;
                  break;
               case 'content-transfer-encoding':
                  $result[$id]->encoding = str_replace("\n", " ", $string);
                  $result[$id]->encoding = $string;
                  break;
               case 'content-type':
                   $ctype_parts = explode(";", $string);
                  $ctype_parts = explode(";", $string);
                  $result[$id]->ctype = array_shift($ctype_parts);
                  foreach ($ctype_parts as $ctype_add)
                     if (preg_match('/charset="?([a-z0-9\-]+)"?/i', $ctype_add, $regs))
                  foreach ($ctype_parts as $ctype_add) {
                     if (preg_match('/charset="?([a-z0-9\-\.\_]+)"?/i',
                        $ctype_add, $regs)) {
                        $result[$id]->charset = $regs[1];
                     }
                  }
                  break;
               case 'in-reply-to':
                  $result[$id]->in_reply_to = ereg_replace("[\n<>]", '', $string);
@@ -1580,38 +1645,42 @@
               case 'references':
                  $result[$id]->references = $string;
                  break;
                  case 'return-receipt-to':
                  case 'disposition-notification-to':
                  case 'x-confirm-reading-to':
                     $result[$id]->mdn_to = str_replace("\n", " ", $string);
                     break;
               case 'return-receipt-to':
               case 'disposition-notification-to':
               case 'x-confirm-reading-to':
                  $result[$id]->mdn_to = $string;
                  break;
               case 'message-id':
                  $result[$id]->messageID = $string;
                  break;
                } // end switch ()
             } // end while ()
          } else {
             $a=explode(' ', $line);
          }
               case 'x-priority':
                  if (preg_match('/^(\d+)/', $string, $matches))
                     $result[$id]->priority = intval($matches[1]);
                  break;
               } // end switch ()
            } // end while ()
         } else {
            $a = explode(' ', $line);
         }
      }
   } while (strcmp($a[0], $key)!=0);
   } 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)\r\n";
   $request     = $command_key . $prefix;
   $request    .= " FETCH $message_set (UID RFC822.SIZE FLAGS INTERNALDATE)\r\n";
   if (!fputs($fp, $request)) {
       return false;
   }
    do {
      $line=chop(iil_ReadLine($fp, 200));
      $line = chop(iil_ReadLine($fp, 200));
      //$a = explode(' ', $line);
      //if (($line[0]=="*") && ($a[2]=="FETCH")) {
      if ($line[0]=="*") {
      if ($line[0] == '*') {
         //echo "<!-- $line //-->\n";
         //get outter most parens
         $open_pos = strpos($line, "(") + 1;
@@ -1642,8 +1711,12 @@
               // process flags
               $flags_str = eregi_replace('[\\\"]', '', $flags_str);
               $flags_a = explode(' ', $flags_str);
               //echo "<!-- ID: $id FLAGS: ".implode(",", $flags_a)." //-->\n";
               $flags_a   = explode(' ', $flags_str);
                    /*
                    trigger_error("<!-- ID: $id FLAGS: ".implode(",", $flags_a)." //-->\n",
                        E_USER_WARNING);
                    */
               
               if (is_array($flags_a)) {
                  reset($flags_a);
@@ -1674,18 +1747,19 @@
               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?
               if ($time_zone_str[0] == '-') {
                  $time_zone = $time_zone * -1.0; //minus?
               }
                    $result[$id]->internaldate = $time_str;
               $result[$id]->internaldate = $time_str;
               
               if ($IMAP_USE_INTERNAL_DATE) {
               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) {
@@ -1704,13 +1778,12 @@
   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);
    }
      return array_shift($a);
   }
   return false;
}
@@ -1731,7 +1804,7 @@
   $stripArr = ($field=='subject') ? array('Re: ','Fwd: ','Fw: ','"') : array('"');
   $c=count($a);
   if ($c>0) {
   if ($c > 0) {
      /*
         Strategy:
         First, we'll create an "index" array.
@@ -1759,7 +1832,7 @@
      }
      
      // sort index
      $i=0;
      $i = 0;
      if ($flag == 'ASC') {
          asort($index);
        } else {
@@ -1781,11 +1854,13 @@
function iil_C_Expunge(&$conn, $mailbox) {
   $fp = $conn->fp;
   if (iil_C_Select($conn, $mailbox)) {
      $c=0;
      $c = 0;
      fputs($fp, "exp1 EXPUNGE\r\n");
      do {
         $line=chop(iil_ReadLine($fp, 100));
         if ($line[0]=="*") $c++;
         if ($line[0] == '*') {
                $c++;
            }
      } while (!iil_StartsWith($line, 'exp1'));
      
      if (iil_ParseResult($line) == 0) {
@@ -1818,7 +1893,7 @@
   $flag = $flags[$flag];
    
   if (iil_C_Select($conn, $mailbox)) {
      $c=0;
      $c = 0;
      fputs($fp, "flg STORE $messages " . $mod . "FLAGS (" . $flag . ")\r\n");
      do {
         $line=chop(iil_ReadLine($fp, 100));
@@ -1881,12 +1956,6 @@
function iil_FormatSearchDate($month, $day, $year) {
   $month  = (int) $month;
    $months = $GLOBALS['IMAP_MONTHS'];
   /* $months=array(
         1=>"Jan", 2=>"Feb", 3=>"Mar", 4=>"Apr",
         5=>"May", 6=>"Jun", 7=>"Jul", 8=>"Aug",
         9=>"Sep", 10=>"Oct", 11=>"Nov", 12=>"Dec"
         );
    */
   return $day . '-' . $months[$month] . '-' . $year;
}
@@ -1939,7 +2008,7 @@
function iil_C_Search(&$conn, $folder, $criteria) {
   $fp = $conn->fp;
   if (iil_C_Select($conn, $folder)) {
      $c=0;
      $c = 0;
      
      $query = 'srch1 SEARCH ' . chop($criteria) . "\r\n";
      fputs($fp, $query);
@@ -1949,10 +2018,10 @@
            $str = trim(substr($line, 8));
            $messages = explode(' ', $str);
         }
      } while (!iil_StartsWith($line, "srch1"));
      } while (!iil_StartsWith($line, 'srch1'));
      
      $result_code=iil_ParseResult($line);
      if ($result_code==0) {
      $result_code = iil_ParseResult($line);
      if ($result_code == 0) {
          return $messages;
      }
      $conn->error = 'iil_C_Search: ' . $line . "\n";
@@ -1970,13 +2039,23 @@
       return -1;
   }
    
   $r=iil_C_Copy($conn, $messages, $from,$to);
   $r = iil_C_Copy($conn, $messages, $from,$to);
   if ($r==0) {
      return iil_C_Delete($conn, $from, $messages);
   }
    return $r;
}
/**
 * Gets the delimiter, for example:
 * INBOX.foo -> .
 * INBOX/foo -> /
 * INBOX\foo -> \
 *
 * @return mixed A delimiter (string), or false.
 * @param object $conn The current connection.
 * @see iil_Connect()
 */
function iil_C_GetHierarchyDelimiter(&$conn) {
   if ($conn->delimiter) {
        return $conn->delimiter;
@@ -1986,16 +2065,16 @@
   $delimiter = false;
   
   //try (LIST "" ""), should return delimiter (RFC2060 Sec 6.3.8)
   if (!fputs($fp, "ghd LIST \"\" \"\"\r\n")) {
   if (!fputs($fp, 'ghd LIST "" ""' . "\r\n")) {
       return false;
    }
    
   do {
      $line=iil_ReadLine($fp, 500);
      if ($line[0]=="*") {
      if ($line[0] == '*') {
         $line = rtrim($line);
         $a=iil_ExplodeQuotedString(' ', $line);
         if ($a[0]=="*") {
         if ($a[0] == '*') {
             $delimiter = str_replace('"', '', $a[count($a)-2]);
            }
      }
@@ -2010,11 +2089,11 @@
   fputs($conn->fp, "ns1 NAMESPACE\r\n");
   do {
      $line = iil_ReadLine($conn->fp, 1024);
      if (iil_StartsWith($line, "* NAMESPACE")) {
      if (iil_StartsWith($line, '* NAMESPACE')) {
         $i = 0;
         $data = iil_ParseNamespace2(substr($line,11), $i, 0, 0);
      }
   } while (!iil_StartsWith($line, "ns1"));
   } while (!iil_StartsWith($line, 'ns1'));
      
   if (!is_array($data)) {
       return false;
@@ -2081,7 +2160,7 @@
            // is it a container?
            $i++;
      }
   } while (!iil_StartsWith($line, "lmb"));
   } while (!iil_StartsWith($line, 'lmb'));
   if (is_array($folders)) {
        if (!empty($ref)) {
@@ -2149,7 +2228,7 @@
            // is it a container?
            $i++;
      }
   } while (!iil_StartsWith($line, "lsb"));
   } while (!iil_StartsWith($line, 'lsb'));
   if (is_array($folders)) {
        if (!empty($ref)) {
@@ -2174,6 +2253,7 @@
   $query = 'sub1 SUBSCRIBE "' . $folder. '"' . "\r\n";
   fputs($fp, $query);
   $line = trim(chop(iil_ReadLine($fp, 10000)));
   return iil_ParseResult($line);
}
@@ -2204,17 +2284,17 @@
      $request = $key . " FETCH $id (BODY.PEEK[$part])\r\n";
      if (!fputs($fp, $request)) return false;
      do {
         $line=chop(iil_ReadLine($fp, 200));
         $a=explode(' ', $line);
         $line = chop(iil_ReadLine($fp, 200));
         $a    = explode(' ', $line);
         if (($line[0] == '*') && ($a[2] == 'FETCH')
                && ($line[strlen($line)-1] != ')')) {
            $line=iil_ReadLine($fp, 300);
            while (chop($line)!=")") {
               $result.=$line;
            while (chop($line) != ')') {
               $result .= $line;
               $line=iil_ReadLine($fp, 300);
            }
         }
      } while (strcmp($a[0], $key)!=0);
      } while (strcmp($a[0], $key) != 0);
   }
   
   return $result;
@@ -2229,12 +2309,12 @@
    */
   $fp     = $conn->fp;
   $result = false;
   if (($part==0) || (empty($part))) {
   if (($part == 0) || empty($part)) {
       $part = 'TEXT';
   }
    
   if (iil_C_Select($conn, $mailbox)) {
        $reply_key='* ' . $id;
        $reply_key = '* ' . $id;
        
        // format request
      $key     = 'ftch' . ($c++) . ' ';
@@ -2248,21 +2328,26 @@
        do {
            $line = chop(iil_ReadLine($fp, 1000));
            $a    = explode(' ', $line);
        } while ($a[2]!="FETCH");
        } while ($a[2] != 'FETCH');
        $len = strlen($line);
        if ($line[$len-1] == ')') {
            //one line response, get everything between first and last quotes
            $from = strpos($line, '"') + 1;
            $to   = strrpos($line, '"');
            $len  = $to - $from;
            if ($mode == 1) {
                $result = substr($line, $from, $len);
            } else if ($mode == 2) {
                echo substr($line, $from, $len);
       if (substr($line, -4, 3) == 'NIL') {
      // NIL response
      $result = '';
       } else {
           $from = strpos($line, '"') + 1;
           $to   = strrpos($line, '"');
           $len  = $to - $from;
      $result = substr($line, $from, $len);
       }
            if ($mode == 2) {
                echo $result;
            } else if ($mode == 3) {
                echo base64_decode(substr($line, $from, $len));
                echo base64_decode($result);
            }
        }else if ($line[$len-1] == '}') {
        } else if ($line[$len-1] == '}') {
            //multi-line request, find sizes of content and receive that many bytes
            $from     = strpos($line, '{') + 1;
            $to       = strrpos($line, '}');
@@ -2279,11 +2364,11 @@
                    $line = substr($line, 0, $remaining);
                }
                $received += strlen($line);
                if ($mode==1) {
                    $result .= chop($line)."\n";
                } else if ($mode==2) {
                    echo chop($line)."\n"; flush();
                } else if ($mode==3) {
                if ($mode == 1) {
                    $result .= rtrim($line, "\t\r\n\0\x0B") . "\n";
                } else if ($mode == 2) {
                    echo rtrim($line, "\t\r\n\0\x0B") . "\n"; flush();
                } else if ($mode == 3) {
                    echo base64_decode($line); flush();
                }
            }
@@ -2294,12 +2379,12 @@
      } while (!iil_StartsWith($line, $key));
        
        if ($result) {
         $result = chop($result);
       $result = rtrim($result, "\t\r\n\0\x0B");
            return $result; // substr($result, 0, strlen($result)-1);
        }
        return false;
   } else {
      echo "Select failed.";
      echo 'Select failed.';
   }
    
    if ($mode==1) {
@@ -2322,10 +2407,10 @@
function iil_C_CreateFolder(&$conn, $folder) {
   $fp = $conn->fp;
   if (fputs($fp, "c CREATE \"".$folder."\"\r\n")) {
   if (fputs($fp, 'c CREATE "' . $folder . '"' . "\r\n")) {
      do {
         $line=iil_ReadLine($fp, 300);
      } while ($line[0]!="c");
      } while ($line[0] != 'c');
        $conn->error = $line;
      return (iil_ParseResult($line) == 0);
   }
@@ -2334,21 +2419,21 @@
function iil_C_RenameFolder(&$conn, $from, $to) {
   $fp = $conn->fp;
   if (fputs($fp, "r RENAME \"".$from."\" \"".$to."\"\r\n")) {
   if (fputs($fp, 'r RENAME "' . $from . '" "' . $to . '"' . "\r\n")) {
      do {
         $line=iil_ReadLine($fp, 300);
      } while ($line[0]!="r");
         $line = iil_ReadLine($fp, 300);
      } while ($line[0] != 'r');
      return (iil_ParseResult($line) == 0);
   }
    return false;
    return false;
}
function iil_C_DeleteFolder(&$conn, $folder) {
   $fp = $conn->fp;
   if (fputs($fp, "d DELETE \"".$folder."\"\r\n")) {
   if (fputs($fp, 'd DELETE "' . $folder. '"' . "\r\n")) {
      do {
         $line=iil_ReadLine($fp, 300);
      } while ($line[0]!="d");
      } while ($line[0] != 'd');
      return (iil_ParseResult($line) == 0);
   }
    $conn->error = "Couldn't send command\n";
@@ -2356,28 +2441,30 @@
}
function iil_C_Append(&$conn, $folder, &$message) {
   if (!$folder) return false;
   if (!$folder) {
        return false;
    }
   $fp = $conn->fp;
   $message = str_replace("\r", '', $message);
   $message = str_replace("\n", "\r\n", $message);      
   $len = strlen($message);
   if (!$len) return false;
   if (!$len) {
        return false;
   }
   $request = 'A APPEND "' . $folder .'" (\\Seen) {' . $len . "}\r\n";
    
   if (fputs($fp, $request)) {
      $line=iil_ReadLine($fp, 100);      
      $sent = fwrite($fp, $message."\r\n");
      flush();
      do {
         $line=iil_ReadLine($fp, 1000);
      } while ($line[0] != 'A');
   
      $result = (iil_ParseResult($line) == 0);
      if (!$result) {
          $conn->error .= $line."\n";
          $conn->error .= $line . "\n";
        }
      return $result;
   
@@ -2395,7 +2482,7 @@
   //open message file
   $in_fp = false;            
   if (file_exists(realpath($path))) {
       $in_fp = fopen($path, "r");
       $in_fp = fopen($path, 'r');
    }
   if (!$in_fp) { 
      $conn->error .= "Couldn't open $path for reading\n";
@@ -2412,11 +2499,11 @@
   $request    = 'A APPEND "' . $folder . '" (\\Seen) {' . $len . "}\r\n";
   $bytes_sent = 0;
   if (fputs($fp, $request)) {
      $line=iil_ReadLine($fp, 100);
      $line = iil_ReadLine($fp, 100);
            
      //send file
      while (!feof($in_fp)) {
         $buffer = fgets($in_fp, 4096);
         $buffer      = fgets($in_fp, 4096);
         $bytes_sent += strlen($buffer);
         fputs($fp, $buffer);
      }
@@ -2426,12 +2513,12 @@
      //read response
      do {
         $line=iil_ReadLine($fp, 1000);
         $line = iil_ReadLine($fp, 1000);
      } while ($line[0] != 'A');
         
      $result = (iil_ParseResult($line) == 0);
      if (!$result) {
          $conn->error .= $line."\n";
          $conn->error .= $line . "\n";
      }
        return $result;
   
@@ -2470,7 +2557,7 @@
               //truncate last ')' and return
               $result = substr($post, 0, strlen($post)-1);
            }
         } while (!preg_match("/^$key/",$line));
         } while (!preg_match("/^$key/", $line));
      }
   }
   return $result;
@@ -2495,21 +2582,21 @@
   $quota_line = '';
   
   //get line containing quota info
   if (fputs($fp, "QUOT1 GETQUOTAROOT \"INBOX\"\r\n")) {
   if (fputs($fp, 'QUOT1 GETQUOTAROOT "INBOX"' . "\r\n")) {
      do {
         $line=chop(iil_ReadLine($fp, 5000));
         if (iil_StartsWith($line, "* QUOTA ")) {
         if (iil_StartsWith($line, '* QUOTA ')) {
             $quota_line = $line;
            }
      } while (!iil_StartsWith($line, "QUOT1"));
      } while (!iil_StartsWith($line, 'QUOT1'));
   }
   
   //return false if not found, parse if found
   if (!empty($quota_line)) {
      $quota_line = eregi_replace("[()]", '', $quota_line);
      $parts = explode(' ', $quota_line);
      $storage_part = array_search("STORAGE", $parts);
      if ($storage_part>0) {
      $quota_line   = eregi_replace('[()]', '', $quota_line);
      $parts        = explode(' ', $quota_line);
      $storage_part = array_search('STORAGE', $parts);
      if ($storage_part > 0) {
         $result = array();
         $used   = $parts[$storage_part+1];
         $total  = $parts[$storage_part+2];
@@ -2520,7 +2607,6 @@
         $result['free']    = 100 - $result['percent'];
      }
   }
   return $result;
}
@@ -2532,5 +2618,4 @@
    }
   return (iil_C_Expunge($conn, $folder) >= 0);
}
?>