From f66f5f02e30d9fc2d5c06eeefb4377b8ff36fce2 Mon Sep 17 00:00:00 2001 From: alecpl <alec@alec.pl> Date: Fri, 08 Apr 2011 02:15:37 -0400 Subject: [PATCH] - Removed dependency on rcube_* functions, small improvements --- program/include/rcube_imap_generic.php | 189 +++++++++++++++++++++++----------------------- 1 files changed, 95 insertions(+), 94 deletions(-) diff --git a/program/include/rcube_imap_generic.php b/program/include/rcube_imap_generic.php index cc590e0..b4479d6 100644 --- a/program/include/rcube_imap_generic.php +++ b/program/include/rcube_imap_generic.php @@ -56,13 +56,10 @@ public $priority; public $mdn_to; public $mdn_sent = false; - public $is_draft = false; public $seen = false; public $deleted = false; - public $recent = false; public $answered = false; public $forwarded = false; - public $junk = false; public $flagged = false; public $has_children = false; public $depth = 0; @@ -91,7 +88,6 @@ public $flags = array( 'SEEN' => '\\Seen', 'DELETED' => '\\Deleted', - 'RECENT' => '\\Recent', 'ANSWERED' => '\\Answered', 'DRAFT' => '\\Draft', 'FLAGGED' => '\\Flagged', @@ -213,31 +209,26 @@ { $line = ''; - if (!$this->fp) { - return NULL; - } - if (!$size) { $size = 1024; } do { - if (feof($this->fp)) { + if ($this->eof()) { return $line ? $line : NULL; } $buffer = fgets($this->fp, $size); if ($buffer === false) { - @fclose($this->fp); - $this->fp = null; + $this->closeSocket(); break; } if ($this->_debug) { $this->debug('S: '. rtrim($buffer)); } $line .= $buffer; - } while ($buffer[strlen($buffer)-1] != "\n"); + } while (substr($buffer, -1) != "\n"); return $line; } @@ -267,7 +258,7 @@ { $data = ''; $len = 0; - while ($len < $bytes && !feof($this->fp)) + while ($len < $bytes && !$this->eof()) { $d = fread($this->fp, $bytes-$len); if ($this->_debug) { @@ -312,8 +303,7 @@ } else if ($res == 'BAD') { $this->errornum = self::ERROR_BAD; } else if ($res == 'BYE') { - @fclose($this->fp); - $this->fp = null; + $this->closeSocket(); $this->errornum = self::ERROR_BYE; } @@ -339,6 +329,32 @@ return self::ERROR_UNKNOWN; } + private function eof() + { + if (!is_resource($this->fp)) { + return true; + } + + // If a connection opened by fsockopen() wasn't closed + // by the server, feof() will hang. + $start = microtime(true); + + if (feof($this->fp) || + ($this->prefs['timeout'] && (microtime(true) - $start > $this->prefs['timeout'])) + ) { + $this->closeSocket(); + return true; + } + + return false; + } + + private function closeSocket() + { + @fclose($this->fp); + $this->fp = null; + } + function setError($code, $msg='') { $this->errornum = $code; @@ -360,8 +376,7 @@ } if ($error && preg_match('/^\* (BYE|BAD) /i', $string, $m)) { if (strtoupper($m[1]) == 'BYE') { - @fclose($this->fp); - $this->fp = null; + $this->closeSocket(); } return true; } @@ -701,11 +716,12 @@ $host = $this->prefs['ssl_mode'] . '://' . $host; } + if ($this->prefs['timeout'] <= 0) { + $this->prefs['timeout'] = ini_get('default_socket_timeout'); + } + // Connect - if ($this->prefs['timeout'] > 0) - $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']); - else - $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr); + $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']); if (!$this->fp) { $this->setError(self::ERROR_BAD, sprintf("Could not connect to %s:%d: %s", $host, $this->prefs['port'], $errstr)); @@ -848,8 +864,7 @@ $this->readReply(); } - @fclose($this->fp); - $this->fp = false; + $this->closeSocket(); } /** @@ -1344,7 +1359,7 @@ $result[$id] = ''; } } else if ($mode == 2) { - if (preg_match('/\((UID|RFC822\.SIZE) ([0-9]+)/', $line, $matches)) { + if (preg_match('/(UID|RFC822\.SIZE) ([0-9]+)/', $line, $matches)) { $result[$id] = trim($matches[2]); } else { $result[$id] = 0; @@ -1543,36 +1558,21 @@ if (preg_match('/^\* [0-9]+ FETCH \((.*) BODY/sU', $line, $matches)) { $str = $matches[1]; - // swap parents with quotes, then explode - $str = preg_replace('/[()]/', '"', $str); - $a = rcube_explode_quoted_string(' ', $str); - - // did we get the right number of replies? - $parts_count = count($a); - if ($parts_count>=6) { - for ($i=0; $i<$parts_count; $i=$i+2) { - if ($a[$i] == 'UID') { - $result[$id]->uid = intval($a[$i+1]); - } - else if ($a[$i] == 'RFC822.SIZE') { - $result[$id]->size = intval($a[$i+1]); - } - else if ($a[$i] == 'INTERNALDATE') { - $time_str = $a[$i+1]; - } - else if ($a[$i] == 'FLAGS') { - $flags_str = $a[$i+1]; - } + while (list($name, $value) = $this->tokenizeResponse($str, 2)) { + if ($name == 'UID') { + $result[$id]->uid = intval($value); } - - $time_str = str_replace('"', '', $time_str); - - // if time is gmt... - $time_str = str_replace('GMT','+0000',$time_str); - - $result[$id]->internaldate = $time_str; - $result[$id]->timestamp = $this->StrToTime($time_str); - $result[$id]->date = $time_str; + else if ($name == 'RFC822.SIZE') { + $result[$id]->size = intval($value); + } + else if ($name == 'INTERNALDATE') { + $result[$id]->internaldate = $value; + $result[$id]->date = $value; + $result[$id]->timestamp = $this->StrToTime($value); + } + else if ($name == 'FLAGS') { + $flags_a = $value; + } } // BODYSTRUCTURE @@ -1620,7 +1620,7 @@ // handle FLAGS reply after headers (AOL, Zimbra?) if (preg_match('/\s+FLAGS \((.*)\)\)$/', $line, $matches)) { - $flags_str = $matches[1]; + $flags_a = $this->tokenizeResponse($matches[1]); break; } @@ -1644,10 +1644,10 @@ // create array with header field:data while (list($lines_key, $str) = each($lines)) { - list($field, $string) = $this->splitHeaderLine($str); + list($field, $string) = explode(':', $str, 2); $field = strtolower($field); - $string = preg_replace('/\n\s*/', ' ', $string); + $string = preg_replace('/\n[\t\s]*/', ' ', trim($string)); switch ($field) { case 'date'; @@ -1711,32 +1711,31 @@ } // process flags - if (!empty($flags_str)) { - $flags_str = preg_replace('/[\\\"]/', '', $flags_str); - $flags_a = explode(' ', $flags_str); + if (!empty($flags_a)) { + foreach ($flags_a as $flag) { + $flag = str_replace('\\', '', $flag); + $result[$id]->flags[] = $flag; - if (is_array($flags_a)) { - foreach($flags_a as $flag) { - $flag = strtoupper($flag); - if ($flag == 'SEEN') { - $result[$id]->seen = true; - } else if ($flag == 'DELETED') { - $result[$id]->deleted = true; - } else if ($flag == 'RECENT') { - $result[$id]->recent = true; - } else if ($flag == 'ANSWERED') { - $result[$id]->answered = true; - } else if ($flag == '$FORWARDED') { - $result[$id]->forwarded = true; - } else if ($flag == 'DRAFT') { - $result[$id]->is_draft = true; - } else if ($flag == '$MDNSENT') { - $result[$id]->mdn_sent = true; - } else if ($flag == 'FLAGGED') { - $result[$id]->flagged = true; - } + switch (strtoupper($flag)) { + case 'SEEN': + $result[$id]->seen = true; + break; + case 'DELETED': + $result[$id]->deleted = true; + break; + case 'ANSWERED': + $result[$id]->answered = true; + break; + case '$FORWARDED': + $result[$id]->forwarded = true; + break; + case '$MDNSENT': + $result[$id]->mdn_sent = true; + break; + case 'FLAGGED': + $result[$id]->flagged = true; + break; } - $result[$id]->flags = $flags_a; } } } @@ -3267,21 +3266,23 @@ * * @return int Unix timestamp */ - private function strToTime($date) + static function strToTime($date) { - $ts = (int) rcube_strtotime($date); - return $ts < 0 ? 0 : $ts; - } + // support non-standard "GMTXXXX" literal + $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date); - private function splitHeaderLine($string) - { - $pos = strpos($string, ':'); - if ($pos>0) { - $res[0] = substr($string, 0, $pos); - $res[1] = trim(substr($string, $pos+1)); - return $res; + // if date parsing fails, we have a date in non-rfc format + // remove token from the end and try again + while (($ts = intval(@strtotime($date))) <= 0) { + $d = explode(' ', $date); + array_pop($d); + if (empty($d)) { + break; + } + $date = implode(' ', $d); } - return $string; + + return $ts < 0 ? 0 : $ts; } private function parseCapability($str, $trusted=false) @@ -3321,7 +3322,7 @@ else if ($force_quotes || preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5B\x5C\x5D\x7F]+)/', $string) ) { - return '"' . strtr($string, array('"'=>'\\"', '\\' => '\\\\')) . '"'; + return '"' . addcslashes($string, '\\"') . '"'; } // atom @@ -3330,7 +3331,7 @@ static function unEscape($string) { - return strtr($string, array('\\"'=>'"', '\\\\' => '\\')); + return stripslashes($string); } /** -- Gitblit v1.9.1