alecpl
2009-12-23 aa16b4af8f67d91b1bfd2c584a665a53c403f47c
program/lib/imap.inc
@@ -234,7 +234,7 @@
   $line = '';
   if (!$fp) {
          return $line;
          return NULL;
   }
    
   if (!$size) {
@@ -242,6 +242,10 @@
   }
    
   do {
      if (feof($fp)) {
         return $line ? $line : NULL;
      }
          $buffer = fgets($fp, $size);
          if ($buffer === false) {
@@ -264,6 +268,8 @@
      $bytes = $a[2][0];
      while (strlen($out) < $bytes) {
         $line = iil_ReadBytes($fp, $bytes); 
         if ($line === NULL)
            break;
         $out .= $line;
      }
@@ -276,7 +282,8 @@
   global $my_prefs;
   $data = '';
   $len  = 0;
   do {
   while ($len < $bytes && !feof($fp))
   {
      $d = fread($fp, $bytes-$len);
      if (!empty($my_prefs['debug_mode']))
         write_log('imap', 'S: '. $d);
@@ -286,7 +293,7 @@
                  break; //nothing was read -> exit to avoid apache lockups
          }
          $len = $data_len;
   } while ($len < $bytes);
   };
   
   return $data;
}
@@ -303,13 +310,14 @@
function iil_ParseResult($string) {
   $a = explode(' ', trim($string));
   if (count($a) >= 2) {
      if (strcasecmp($a[1], 'OK') == 0) {
      $res = strtoupper($a[1]);
      if ($res == 'OK') {
         return 0;
      } else if (strcasecmp($a[1], 'NO') == 0) {
      } else if ($res == 'NO') {
         return -1;
      } else if (strcasecmp($a[1], 'BAD') == 0) {
      } else if ($res == 'BAD') {
         return -2;
      } else if (strcasecmp($a[1], 'BYE') == 0) {
      } else if ($res == 'BYE') {
         return -3;
      }
   }
@@ -341,7 +349,6 @@
   }
   if ($error && preg_match('/^\* (BYE|BAD) /i', $string)) {
      return true;
   }
   return false;
}
@@ -554,8 +561,8 @@
   // set some imap options
   if (is_array($options)) {
      foreach($options as $optkey => $optval) {
         if ($optkey == 'imap') {
            $auth_method = $optval;
         if ($optkey == 'auth_method') {
            $auth_method = strtoupper($optval);
         } else if ($optkey == 'rootdir') {
                $my_prefs['rootdir'] = $optval;
         } else if ($optkey == 'delimiter') {
@@ -567,7 +574,7 @@
   }
   if (empty($auth_method))
          $auth_method = 'check';
          $auth_method = 'CHECK';
      
   $message = "INITIAL: $auth_method\n";
      
@@ -612,7 +619,7 @@
      $host = $ICL_SSL . '://' . $host;
   }
   $conn->fp = fsockopen($host, $ICL_PORT, $errno, $errstr, 10);
   $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 = -2;
@@ -620,7 +627,7 @@
   }
   stream_set_timeout($conn->fp, 10);
   $line = stream_get_line($conn->fp, 8192, "\r\n");
   $line = trim(fgets($conn->fp, 8192));
   if ($my_prefs['debug_mode'] && $line)
      write_log('imap', 'S: '. $line);
@@ -640,7 +647,7 @@
      $conn->capability = explode(' ', strtoupper($matches[1]));
   }
   $conn->message .= $line;
   $conn->message .= $line . "\n";
   // TLS connection
   if ($ICL_SSL == 'tls' && iil_C_GetCapability($conn, 'STARTTLS')) {
@@ -665,29 +672,23 @@
           }
   }
   if (strcasecmp($auth_method, "check") == 0) {
   if ($auth_method == 'CHECK') {
      //check for supported auth methods
      if (iil_C_GetCapability($conn, 'AUTH=CRAM-MD5') || iil_C_GetCapability($conn, 'AUTH=CRAM_MD5')) {
         $auth_method = 'auth';
         $auth_method = 'AUTH';
      }
      else {
         //default to plain text auth
         $auth_method = 'plain';
         $auth_method = 'PLAIN';
      }
   }
   if (strcasecmp($auth_method, 'auth') == 0) {
      $conn->message .= "Trying CRAM-MD5\n";
   if ($auth_method == 'AUTH') {
      //do CRAM-MD5 authentication
      iil_PutLine($conn->fp, "a000 AUTHENTICATE CRAM-MD5");
      $line = trim(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
         $result = iil_C_Authenticate($conn, $user, $password, substr($line,2));
              
@@ -697,21 +698,19 @@
                     $iil_errornum = $conn->errorNum;
            return false;
         }
         $conn->message .= "Tried CRAM-MD5: $result \n";
         $conn->message .= "AUTH CRAM-MD5: $result\n";
      } else {
         $conn->message .='No challenge ('.htmlspecialchars($line)."), try plain\n";
         $auth = 'plain';
         $conn->message .= "AUTH CRAM-MD5: failed\n";
         $auth_method = 'PLAIN';
      }
   }
      
   if ((!$result)||(strcasecmp($auth, "plain") == 0)) {
   if (!$result || $auth_method == 'PLAIN') {
      //do plain text auth
      $result = iil_C_Login($conn, $user, $password);
      $conn->message .= "Tried PLAIN: $result \n";
      $conn->message .= "AUTH PLAIN: $result\n";
   }
      
   $conn->message .= $auth;
   if (!is_int($result)) {
      iil_C_Namespace($conn);
      return $conn;
@@ -724,7 +723,8 @@
function iil_Close(&$conn) {
   if (iil_PutLine($conn->fp, "I LOGOUT")) {
      fgets($conn->fp, 1024);
      if (!feof($conn->fp))
         fgets($conn->fp, 1024);
      fclose($conn->fp);
      $conn->fp = false;
   }
@@ -779,7 +779,7 @@
   if (empty($mailbox)) {
      return false;
   }
   if (strcmp($conn->selected, $mailbox) == 0) {
   if ($conn->selected == $mailbox) {
      return true;
   }
    
@@ -788,10 +788,11 @@
         $line = chop(iil_ReadLine($conn->fp, 300));
         $a = explode(' ', $line);
         if (count($a) == 3) {
            if (strcasecmp($a[2], 'EXISTS') == 0) {
            $token = strtoupper($a[2]);
            if ($token == 'EXISTS') {
               $conn->exists = (int) $a[1];
            }
            else if (strcasecmp($a[2], 'RECENT') == 0) {
            else if ($token == 'RECENT') {
               $conn->recent = (int) $a[1];
            }
         }
@@ -799,8 +800,6 @@
            $conn->permanentflags = explode(' ', $match[1]);
         }
      } while (!iil_StartsWith($line, 'sel1', true));
      $a = explode(' ', $line);
      if (strcasecmp($a[1], 'OK') == 0) {
         $conn->selected = $mailbox;
@@ -1477,13 +1476,13 @@
            $parts_count = count($a);
            if ($parts_count>=6) {
               for ($i=0; $i<$parts_count; $i=$i+2) {
                  if (strcasecmp($a[$i],'UID') == 0)
                  if ($a[$i] == 'UID')
                     $result[$id]->uid = $a[$i+1];
                  else if (strcasecmp($a[$i],'RFC822.SIZE') == 0)
                  else if ($a[$i] == 'RFC822.SIZE')
                     $result[$id]->size = $a[$i+1];
                  else if (strcasecmp($a[$i],'INTERNALDATE') == 0)
                  else if ($a[$i] == 'INTERNALDATE')
                     $time_str = $a[$i+1];
                  else if (strcasecmp($a[$i],'FLAGS') == 0)
                  else if ($a[$i] == 'FLAGS')
                     $flags_str = $a[$i+1];
               }
@@ -1501,7 +1500,7 @@
            if($bodystr) {
               while (!preg_match('/ BODYSTRUCTURE (.*) BODY\[HEADER.FIELDS/s', $line, $m)) {
                  $line2 = iil_ReadLine($fp, 1024);
                  $line .= iil_MultLine($fp, $line2);
                  $line .= iil_MultLine($fp, $line2, true);
               }
               $result[$id]->body_structure = $m[1];
            }
@@ -1510,11 +1509,11 @@
            preg_match('/ BODY\[HEADER.FIELDS \(.*?\)\]\s*(.*)$/s', $line, $m);
            $reslines = explode("\n", trim($m[1], '"'));
            // re-parse (see below)
            foreach ($reslines as $line) {
               if (ord($line[0])<=32) {
                  $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($line);
            foreach ($reslines as $resln) {
               if (ord($resln[0])<=32) {
                  $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($resln);
               } else {
                  $lines[++$ln] = trim($line);
                  $lines[++$ln] = trim($resln);
               }
            }
         }
@@ -1553,7 +1552,7 @@
               $lines[++$ln] = trim($line);
            }
         // patch from "Maksim Rubis" <siburny@hotmail.com>
         } while (trim($line[0]) != ')' && strncmp($line, $key, strlen($key)));
         } while ($line[0] != ')' && !iil_StartsWith($line, $key, true));
         if (strncmp($line, $key, strlen($key))) { 
            // process header, fill iilBasicHeader obj.
@@ -1601,13 +1600,10 @@
                  $result[$id]->encoding = $string;
                  break;
               case 'content-type':
                  $ctype_parts = explode(";", $string);
                  $ctype_parts = preg_split('/[; ]/', $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)) {
                        $result[$id]->charset = $regs[1];
                     }
                  if (preg_match('/charset\s*=\s*"?([a-z0-9\-\.\_]+)"?/i', $string, $regs)) {
                     $result[$id]->charset = $regs[1];
                  }
                  break;
               case 'in-reply-to':
@@ -1644,23 +1640,24 @@
            $flags_a   = explode(' ', $flags_str);
               
            if (is_array($flags_a)) {
               reset($flags_a);
               while (list(,$val)=each($flags_a)) {
                  if (strcasecmp($val,'Seen') == 0) {
            //   reset($flags_a);
               foreach($flags_a as $flag) {
                  $flag = strtoupper($flag);
                  if ($flag == 'SEEN') {
                      $result[$id]->seen = true;
                  } else if (strcasecmp($val, 'Deleted') == 0) {
                      $result[$id]->deleted=true;
                  } else if (strcasecmp($val, 'Recent') == 0) {
                  } else if ($flag == 'DELETED') {
                      $result[$id]->deleted = true;
                  } else if ($flag == 'RECENT') {
                      $result[$id]->recent = true;
                  } else if (strcasecmp($val, 'Answered') == 0) {
                  } else if ($flag == 'ANSWERED') {
                     $result[$id]->answered = true;
                  } else if (strcasecmp($val, '$Forwarded') == 0) {
                  } else if ($flag == '$FORWARDED') {
                     $result[$id]->forwarded = true;
                  } else if (strcasecmp($val, 'Draft') == 0) {
                  } else if ($flag == 'DRAFT') {
                     $result[$id]->is_draft = true;
                  } else if (strcasecmp($val, '$MDNSent') == 0) {
                  } else if ($flag == '$MDNSENT') {
                     $result[$id]->mdn_sent = true;
                  } else if (strcasecmp($val, 'Flagged') == 0) {
                  } else if ($flag == 'FLAGGED') {
                           $result[$id]->flagged = true;
                  }
               }
@@ -1668,7 +1665,7 @@
            }
         }
      }
   } while (strcmp($a[0], $key) != 0);
   } while (!iil_StartsWith($line, $key, true));
   return $result;
}
@@ -2139,7 +2136,7 @@
   return (iil_ParseResult($line) == 0);
}
function iil_C_FetchMIMEHeaders(&$conn, $mailbox, $id, $parts) {
function iil_C_FetchMIMEHeaders(&$conn, $mailbox, $id, $parts, $mime=true) {
   
   $fp     = $conn->fp;
@@ -2152,10 +2149,11 @@
   $key = 'fmh0';
   $peeks = '';
   $idx = 0;
        $type = $mime ? 'MIME' : 'HEADER';
   // format request
   foreach($parts as $part)
      $peeks[] = "BODY.PEEK[$part.MIME]";
      $peeks[] = "BODY.PEEK[$part.$type]";
   
   $request = "$key FETCH $id (" . implode(' ', $peeks) . ')';
@@ -2168,9 +2166,9 @@
           $line = iil_ReadLine($fp, 1000);
           $line = iil_MultLine($fp, $line);
      if (preg_match('/BODY\[([0-9\.]+)\.MIME\]/', $line, $matches)) {
      if (preg_match('/BODY\[([0-9\.]+)\.'.$type.'\]/', $line, $matches)) {
         $idx = $matches[1];
         $result[$idx] = preg_replace('/^(\* '.$id.' FETCH \()?\s*BODY\['.$idx.'\.MIME\]\s+/', '', $line);
         $result[$idx] = preg_replace('/^(\* '.$id.' FETCH \()?\s*BODY\['.$idx.'\.'.$type.'\]\s+/', '', $line);
         $result[$idx] = trim($result[$idx], '"');
             $result[$idx] = rtrim($result[$idx], "\t\r\n\0\x0B");
          }
@@ -2277,7 +2275,7 @@
               }
               else
                  $prev = '';
               if ($file)
                  fwrite($file, base64_decode($line));
                        else if ($print)
@@ -2313,7 +2311,6 @@
            }
              }
          }
           // read in anything up until last line
      if (!$end)
         do {
@@ -2321,18 +2318,17 @@
         } while (!iil_StartsWith($line, $key, true));
          if ($result) {
             $result = rtrim($result, "\t\r\n\0\x0B");
         if ($file) {
            fwrite($file, $result);
         } else if ($print) {
            echo $result;
         } else
            return $result; // substr($result, 0, strlen($result)-1);
            return $result;
         return true;
          }
   }
   return false;
}
@@ -2388,8 +2384,8 @@
   if (iil_PutLine($fp, $request)) {
      $line = iil_ReadLine($fp, 512);
      $result = (iil_ParseResult($line) == 0);
      if (!$result) {
      if ($line[0] != '+') {
         // $errornum = iil_ParseResult($line);
         $conn->error .= "Cannot write to folder: $line\n";
         return false;
      }
@@ -2437,8 +2433,8 @@
   if (iil_PutLine($fp, $request)) {
      $line = iil_ReadLine($fp, 512);
      $result = (iil_ParseResult($line) == 0);
      if (!$result) {
      if ($line[0] != '+') {
         //$errornum = iil_ParseResult($line);
         $conn->error .= "Cannot write to folder: $line\n";
         return false;
      }
@@ -2479,7 +2475,7 @@
      if (iil_PutLine($fp, $key . ($is_uid ? ' UID' : '') ." FETCH $id (BODYSTRUCTURE)")) {
         do {
            $line = iil_ReadLine($fp, 5000);
            $line = iil_MultLine($fp, $line);
            $line = iil_MultLine($fp, $line, true);
            if (!preg_match("/^$key/", $line))
               $result .= $line;
         } while (!iil_StartsWith($line, $key, true));