From 7ffc08ce87bdbc761eb9811f8be25a8c9c4e9976 Mon Sep 17 00:00:00 2001 From: thomascube <thomas@roundcube.net> Date: Tue, 27 May 2008 10:58:04 -0400 Subject: [PATCH] Remove cruft from upload response --- program/lib/imap.inc | 157 +++++++++++++++++++++++++++++---------------------- 1 files changed, 89 insertions(+), 68 deletions(-) diff --git a/program/lib/imap.inc b/program/lib/imap.inc index 3d20d9e..4456a22 100644 --- a/program/lib/imap.inc +++ b/program/lib/imap.inc @@ -51,6 +51,11 @@ - 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 ...) + File altered by Aleksander Machniak <alec@alec.pl> + - RFC3501 [7.1] don't call CAPABILITY if was returned in server + optional resposne in iil_Connect() + - trim(chop()) replaced by trim() + ********************************************************/ @@ -161,18 +166,19 @@ } function iil_ReadLine($fp, $size) { - $line = ''; - if ($fp) { - do { - // FIXME: hardcode size? - $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) { @@ -192,18 +198,21 @@ } 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) { do { - $line = chop(trim(iil_ReadLine($fp, 1024))); + $line = trim(iil_ReadLine($fp, 1024)); } while ($line[0] == '*'); return $line; @@ -469,7 +478,28 @@ } $iil_error .= "Socket connection established\r\n"; - $line = iil_ReadLine($conn->fp, 300); + $line = iil_ReadLine($conn->fp, 1024); + + // RFC3501 [7.1] optional CAPABILITY response + // commented out, because it's not working always as should +// if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) { +// $conn->capability = explode(' ', $matches[1]); +// } else { + fputs($conn->fp, "cp01 CAPABILITY\r\n"); + do { + $line = trim(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; + } + } + } while ($a[0] != 'cp01'); +// } if (strcasecmp($auth_method, "check") == 0) { //check for supported auth methods @@ -478,25 +508,12 @@ $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"; - - $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"; - } - } + foreach ($conn->capability as $c) + if (strcasecmp($c, 'AUTH=CRAM_MD5') == 0 || + strcasecmp($c, 'AUTH=CRAM-MD5') == 0) { + $auth_method = 'auth'; + break; } - } while ($a[0] != 'cp01'); } if (strcasecmp($auth_method, 'auth') == 0) { @@ -504,7 +521,7 @@ //do CRAM-MD5 authentication fputs($conn->fp, "a000 AUTHENTICATE CRAM-MD5\r\n"); - $line = trim(chop(iil_ReadLine($conn->fp, 1024))); + $line = trim(iil_ReadLine($conn->fp, 1024)); $conn->message .= "$line\n"; @@ -1561,10 +1578,10 @@ do { $line = chop(iil_ReadLine($fp, 300), "\r\n"); if (ord($line[0])<=32) { - $lines[$i] .= (empty($lines[$i])?'':"\n").trim(chop($line)); + $lines[$i] .= (empty($lines[$i])?'':"\n").trim($line); } else { $i++; - $lines[$i] = trim(chop($line)); + $lines[$i] = trim($line); } /* The preg_match below works around communigate imap, which outputs " UID <number>)". @@ -2009,7 +2026,7 @@ $query = 'srch1 SEARCH ' . chop($criteria) . "\r\n"; fputs($fp, $query); do { - $line=trim(chop(iil_ReadLine($fp, 10000))); + $line=trim(iil_ReadLine($fp, 10000)); if (eregi("^\* SEARCH", $line)) { $str = trim(substr($line, 8)); $messages = explode(' ', $str); @@ -2029,16 +2046,15 @@ } function iil_C_Move(&$conn, $messages, $from, $to) { - $fp = $conn->fp; - - if (!$from || !$to) { - return -1; - } - - $r = iil_C_Copy($conn, $messages, $from,$to); - if ($r==0) { - return iil_C_Delete($conn, $from, $messages); - } + $fp = $conn->fp; + + if (!$from || !$to) { + return -1; + } + $r = iil_C_Copy($conn, $messages, $from,$to); + if ($r==0) { + return iil_C_Delete($conn, $from, $messages); + } return $r; } @@ -2250,7 +2266,7 @@ $query = 'sub1 SUBSCRIBE "' . $folder. '"' . "\r\n"; fputs($fp, $query); - $line = trim(chop(iil_ReadLine($fp, 10000))); + $line = trim(iil_ReadLine($fp, 10000)); return iil_ParseResult($line); } @@ -2261,7 +2277,7 @@ $query = 'usub1 UNSUBSCRIBE "' . $folder . '"' . "\r\n"; fputs($fp, $query); - $line = trim(chop(iil_ReadLine($fp, 10000))); + $line = trim(iil_ReadLine($fp, 10000)); return iil_ParseResult($line); } @@ -2285,7 +2301,7 @@ if (($line[0] == '*') && ($a[2] == 'FETCH') && ($line[strlen($line)-1] != ')')) { $line=iil_ReadLine($fp, 300); - while (chop($line) != ')') { + while (trim($line) != ')') { $result .= $line; $line=iil_ReadLine($fp, 300); } @@ -2328,17 +2344,22 @@ $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, '}'); @@ -2356,9 +2377,9 @@ } $received += strlen($line); if ($mode == 1) { - $result .= chop($line) . "\n"; + $result .= rtrim($line, "\t\r\n\0\x0B") . "\n"; } else if ($mode == 2) { - echo chop($line) . "\n"; flush(); + echo rtrim($line, "\t\r\n\0\x0B") . "\n"; flush(); } else if ($mode == 3) { echo base64_decode($line); flush(); } @@ -2370,7 +2391,7 @@ } 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; -- Gitblit v1.9.1