alecpl
2009-07-11 5f571eb1dba01a5f257c52978f30cd53d773d15c
program/lib/imap.inc
@@ -101,15 +101,6 @@
    $IMAP_USE_INTERNAL_DATE = true;
}
/**
 * @todo Maybe use date() to generate this.
 */
$GLOBALS['IMAP_MONTHS'] = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4,
    "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10,
    "Nov" => 11, "Dec" => 12);
$GLOBALS['IMAP_SERVER_TZ'] = date('Z');
$GLOBALS['IMAP_FLAGS'] = array(
    'SEEN'     => '\\Seen',
    'DELETED'  => '\\Deleted',
@@ -206,7 +197,11 @@
}
function iil_PutLine($fp, $string, $endln=true) {
//      console('C: '. rtrim($string));
   global $my_prefs;
   if(!empty($my_prefs['debug_mode']))
          write_log('imap', 'C: '. rtrim($string));
        return fputs($fp, $string . ($endln ? "\r\n" : ''));
}
@@ -234,6 +229,8 @@
}
function iil_ReadLine($fp, $size=1024) {
   global $my_prefs;
   $line = '';
   if (!$fp) {
@@ -249,7 +246,8 @@
          if ($buffer === false) {
              break;
          }
//      console('S: '. chop($buffer));
      if(!empty($my_prefs['debug_mode']))
         write_log('imap', 'S: '. chop($buffer));
          $line .= $buffer;
   } while ($buffer[strlen($buffer)-1] != "\n");
   
@@ -269,19 +267,22 @@
      }
      $line = $a[1][0] . '"' . ($escape ? iil_Escape($out) : $out) . '"';
//      console('[...] '. $out);
   }
   return $line;
}
function iil_ReadBytes($fp, $bytes) {
   global $my_prefs;
   $data = '';
   $len  = 0;
   do {
          $data .= fread($fp, $bytes-$len);
      $d = fread($fp, $bytes-$len);
                $data .= $d;
      if ($len == strlen($data)) {
                  break; //nothing was read -> exit to avoid apache lockups
          }
      if(!empty($my_prefs['debug_mode']))
         write_log('imap', 'S: '. $d);
          $len = strlen($data);
   } while ($len < $bytes);
   
@@ -561,6 +562,8 @@
                $my_prefs['rootdir'] = $optval;
         } else if ($optkey == 'delimiter') {
                $my_prefs['delimiter'] = $optval;
         } else if ($optkey == 'debug_mode') {
                $my_prefs['debug_mode'] = $optval;
         }
      }
   }
@@ -841,46 +844,23 @@
   return $string;
}
function iil_StrToTime($str) {
   $IMAP_MONTHS    = $GLOBALS['IMAP_MONTHS'];
   $IMAP_SERVER_TZ = $GLOBALS['IMAP_SERVER_TZ'];
   if ($str) {
           $time1 = strtotime($str);
   }
   if ($time1 && $time1 != -1) {
       return $time1-$IMAP_SERVER_TZ;
   }
   //echo '<!--'.$str.'//-->';
   //replace double spaces with single space
   $str = trim($str);
   $str = str_replace('  ', ' ', $str);
   //strip off day of week
   $pos = strpos($str, ' ');
   if (!is_numeric(substr($str, 0, $pos))) {
       $str = substr($str, $pos+1);
   }
   //explode, take good parts
   $a = explode(' ', $str);
function iil_StrToTime($date) {
   $month_str = $a[1];
   $month     = $IMAP_MONTHS[$month_str];
   $day       = (int)$a[0];
   $year      = (int)$a[2];
   $time      = $a[3];
   $tz_str    = $a[4];
   $tz        = substr($tz_str, 0, 3);
   $ta        = explode(':', $time);
   $hour      = (int)$ta[0]-(int)$tz;
   $minute    = (int)$ta[1];
   $second    = (int)$ta[2];
   //make UNIX timestamp
   $time2 = mktime($hour, $minute, $second, $month, $day, $year);
   //echo '<!--'.$time1.' '.$time2.' //-->'."\n";
   return $time2;
   // support non-standard "GMTXXXX" literal
   $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date);
        // if date parsing fails, we have a date in non-rfc format.
   // remove token from the end and try again
   while ((($ts = @strtotime($date))===false) || ($ts < 0))
   {
           $d = explode(' ', $date);
      array_pop($d);
      if (!$d) break;
      $date = implode(' ', $d);
   }
   $ts = (int) $ts;
   return $ts < 0 ? 0 : $ts;
}
function iil_C_Sort(&$conn, $mailbox, $field, $add='', $is_uid=FALSE,
@@ -1033,7 +1013,7 @@
            $result[$id] = in_array('\\'.$index_field, $flags) ? 1 : 0;
         } else if ($mode == 4) {
            if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) {
               $result[$id] = strtotime($matches[1]);
               $result[$id] = iil_StrToTime($matches[1]);
            } else {
               $result[$id] = 0;
            }
@@ -1513,25 +1493,8 @@
               // 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, 0, -5); // remove timezone
               $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]->timestamp = iil_StrToTime($time_str);
               $result[$id]->date = $time_str;
            }
@@ -1750,8 +1713,8 @@
      while (list($key, $val)=each($a)) {
         if ($field == 'timestamp') {
            $data = @strtotime($val->date);
            if ($data == false) {
            $data = iil_StrToTime($val->date);
            if (!$data) {
               $data = $val->timestamp;
                     }
         } else {
@@ -1875,12 +1838,6 @@
   } else {
      return -1;
   }
}
function iil_FormatSearchDate($month, $day, $year) {
   $month  = (int) $month;
   $months = $GLOBALS['IMAP_MONTHS'];
   return $day . '-' . $months[$month] . '-' . $year;
}
function iil_C_CountUnseen(&$conn, $folder) {
@@ -2233,14 +2190,14 @@
   return $result;
}
function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $part) {
function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $is_uid=false, $part=NULL) {
   $part = empty($part) ? 'HEADER' : $part.'.MIME';
        return iil_C_HandlePartBody($conn, $mailbox, $id, $part);
        return iil_C_HandlePartBody($conn, $mailbox, $id, $is_uid, $part);
}
function iil_C_HandlePartBody(&$conn, $mailbox, $id, $part='', $encoding=NULL, $print=NULL, $file=NULL) {
function iil_C_HandlePartBody(&$conn, $mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL) {
   
   $fp     = $conn->fp;
   $result = false;
@@ -2267,7 +2224,7 @@
          // format request
      $key     = 'ftch0';
      $request = $key . " FETCH $id (BODY.PEEK[$part])";
      $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])";
          // send request
      if (!iil_PutLine($fp, $request)) {
          return false;
@@ -2292,7 +2249,7 @@
                 $len  = $to - $from;
            $result = substr($line, $from, $len);
         }
              if ($mode == 1)
            $result = base64_decode($result);
         else if ($mode == 2)
@@ -2308,7 +2265,7 @@
                   $sizeStr  = substr($line, $from, $len);
              $bytes    = (int)$sizeStr;
         $prev     = '';
              while ($bytes > 0) {
                          $line      = iil_ReadLine($fp, 1024);
                     $len       = strlen($line);
@@ -2372,7 +2329,7 @@
         do {
                 $line = iil_ReadLine($fp, 1024);
         } while (!iil_StartsWith($line, $key, true));
          if ($result) {
             $result = rtrim($result, "\t\r\n\0\x0B");
         if ($file) {
@@ -2381,20 +2338,12 @@
            echo $result;
         } else
            return $result; // substr($result, 0, strlen($result)-1);
         return true;
          }
      return true;
   }
    
   return false;
}
function iil_C_FetchPartBody(&$conn, $mailbox, $id, $part, $file=NULL) {
   return iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, NULL, $file);
}
function iil_C_PrintPartBody(&$conn, $mailbox, $id, $part) {
   iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, true, NULL);
}
function iil_C_CreateFolder(&$conn, $folder) {
@@ -2520,23 +2469,22 @@
   return false;
}
function iil_C_FetchStructureString(&$conn, $folder, $id) {
function iil_C_FetchStructureString(&$conn, $folder, $id, $is_uid=false) {
   $fp     = $conn->fp;
   $result = false;
   
   if (iil_C_Select($conn, $folder)) {
      $key = 'F1247';
      if (iil_PutLine($fp, "$key FETCH $id (BODYSTRUCTURE)")) {
      if (iil_PutLine($fp, $key . ($is_uid ? ' UID' : '') ." FETCH $id (BODYSTRUCTURE)")) {
         do {
            $line = iil_ReadLine($fp, 5000);
            $line = iil_MultLine($fp, $line);
            list(, $index, $cmd, $rest) = explode(' ', $line);
            if ($cmd != 'FETCH' || $index == $id || preg_match("/^$key/", $line))
            if (!preg_match("/^$key/", $line))
               $result .= $line;
         } while (!preg_match("/^$key/", $line));
         $result = trim(substr($result, strpos($result, 'BODYSTRUCTURE')+13, -(strlen($result)-strrpos($result, $key)+1)));
         $result = trim(substr($result, strpos($result, 'BODYSTRUCTURE')+13, -1));
      }
   }
   return $result;