X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=blobdiff_plain;f=functions%2Fimap_general.php;h=0e11590c37f27238a3178778cdeca6af48d05abe;hp=19ff2788bed5e14ad74152cc9d65c440d0fd367f;hb=3fedd15bc50274624eb61e63e0ec707ce09a0c22;hpb=811318c51f2e27e815c205ff80d42dd88cfd1d49
diff --git a/functions/imap_general.php b/functions/imap_general.php
index 19ff2788..0e11590c 100755
--- a/functions/imap_general.php
+++ b/functions/imap_general.php
@@ -3,24 +3,30 @@
/**
* imap_general.php
*
- * Copyright (c) 1999-2003 The SquirrelMail Project Team
+ * Copyright (c) 1999-2004 The SquirrelMail Project Team
* Licensed under the GNU GPL. For full terms see the file COPYING.
*
* This implements all functions that do general imap functions.
*
- * $Id$
+ * @version $Id$
+ * @package squirrelmail
+ * @subpackage imap
*/
+/** Includes.. */
require_once(SM_PATH . 'functions/page_header.php');
require_once(SM_PATH . 'functions/auth.php');
-global $sqimap_session_id;
-$sqimap_session_id = 1;
+/**
+ * Generates a new session ID by incrementing the last one used;
+ * this ensures that each command has a unique ID.
+ * @param bool unique_id
+ * @return string IMAP session id of the form 'A000'.
+ */
+function sqimap_session_id($unique_id = FALSE) {
+ static $sqimap_session_id = 1;
-/* Sets an unique session id in order to avoid simultanous sessions crash. */
-function sqimap_session_id($unique_id = false) {
- global $data_dir, $username, $sqimap_session_id;
if (!$unique_id) {
return( sprintf("A%03d", $sqimap_session_id++) );
} else {
@@ -28,7 +34,7 @@ function sqimap_session_id($unique_id = false) {
}
}
-/*
+/**
* Both send a command and accept the result from the command.
* This is to allow proper session number handling.
*/
@@ -66,10 +72,15 @@ function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response,
$read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response,
$message, $query,$filter,$outputstream,$no_return);
+ if (empty($read)) { //Imap server dropped its connection
+ $response = '';
+ $message = '';
+ return false;
+ }
/* retrieve the response and the message */
$response = $response[$tag];
$message = $message[$tag];
-
+
if (!empty($read[$tag])) {
return $read[$tag][0];
} else {
@@ -86,6 +97,7 @@ function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response,
return false;
}
}
+
function sqimap_prepare_pipelined_query($new_query,&$tag,&$aQuery,$unique_id) {
$sid = sqimap_session_id($unique_id);
$tag_uid_a = explode(' ',trim($sid));
@@ -157,11 +169,12 @@ function sqimap_run_pipelined_command ($imap_stream, $aQueryList, $handle_errors
return $aResults;
}
-/*
- * custom fgets function. gets a line from IMAP
- * no matter how big it may be
+/**
+ * Custom fgets function: gets a line from the IMAP-server,
+ * no matter how big it may be.
+ * @param stream imap_stream the stream to read from
+ * @return string a line
*/
-
function sqimap_fgets($imap_stream) {
$read = '';
$buffer = 4096;
@@ -188,62 +201,38 @@ function sqimap_fread($imap_stream,$iSize,$filter=false,
$iBufferSize = $iSize;
} else {
// see php bug 24033. They changed fread behaviour %$^&$%
- $iBufferSize = 780; // multiple of 78 in case of base64 decoding.
+ $iBufferSize = 7800; // multiple of 78 in case of base64 decoding.
}
- $iRet = $iSize - $iBufferSize;
+ if ($iSize < $iBufferSize) {
+ $iBufferSize = $iSize;
+ }
+
$iRetrieved = 0;
- $i = 0;
- $results = $sReadRem = '';
- $bFinished = $bBufferSizeAdapted = $bBufferIsOk = false;
- while (($iRetrieved < ($iSize - $iBufferSize))) {
+ $results = '';
+ $sRead = $sReadRem = '';
+ // NB: fread can also stop at end of a packet on sockets.
+ while ($iRetrieved < $iSize) {
$sRead = fread($imap_stream,$iBufferSize);
+ $iLength = strlen($sRead);
+ $iRetrieved += $iLength ;
+ $iRemaining = $iSize - $iRetrieved;
+ if ($iRemaining < $iBufferSize) {
+ $iBufferSize = $iRemaining;
+ }
if (!$sRead) {
$results = false;
break;
}
- $iRetrieved += $iBufferSize;
- if ($filter) {
- // in case line-endings do not appear at position 78 we adapt the buffersize so we can base64 decode on the fly
- if (!$bBufferSizeAdapted) {
- $i = strpos($sRead,"\n");
- if ($i) {
- ++$i;
- $iFragments = floor($iBufferSize / $i);
- $iNewBufferSize = $iFragments * $i;
- $iRemainder = $iNewBufferSize + $i - $iBufferSize;
- if ($iNewBufferSize == $iBufferSize) {
- $bBufferIsOk = true;
- $iRemainder = 0;
- $iNewBufferSize = $iBufferSize;
- $bBufferSizeAdapted = true;
- }
- if (!$bBufferIsOk && ($iRemainder + $iBufferSize) < $iSize) {
- $sReadRem = fread($imap_stream,$iRemainder);
- } else if (!$bBufferIsOk) {
- $sReadRem = fread($imap_stream,$iSize - $iBufferSize);
- $bFinished = true;
- }
- if (!$sReadRem && $sReadRem !== '') {
- $results = false;
- break;
- }
- $iBufferSize = $iNewBufferSize;
- $bBufferSizeAdapted = true;
- } else {
- $sReadRem = fread($imap_stream,$iSize - $iBufferSize);
- $bFinished = true;
- if (!$sReadRem) {
- $results = false;
- break;
- }
- }
- $sRead .= $sReadRem;
- $iRetrieved += $iRemainder;
- unset($sReadRem);
- }
- $filter($sRead);
+ if ($sReadRem) {
+ $sRead = $sReadRem . $sRead;
+ $sReadRem = '';
+ }
+
+ if ($filter && $sRead) {
+ // in case the filter is base64 decoding we return a remainder
+ $sReadRem = $filter($sRead);
}
- if ($outputstream) {
+ if ($outputstream && $sRead) {
if (is_resource($outputstream)) {
fwrite($outputstream,$sRead);
} else if ($outputstream == 'php://stdout') {
@@ -252,29 +241,18 @@ function sqimap_fread($imap_stream,$iSize,$filter=false,
}
if ($no_return) {
$sRead = '';
- }
- $results .= $sRead;
- }
- if (!$results && !$bFinished) {
- $sRead = fread($imap_stream,($iSize - ($iRetrieved)));
- if ($filter) {
- $filter($sRead);
+ } else {
+ $results .= $sRead;
}
- if ($outputstream) {
- if (is_resource($outputstream)) {
- fwrite($outputstream,$sRead);
- } else if ($outputstream == 'php://stdout') { // FIXME
- echo $sRead;
- }
- }
- if ($no_return) {
- $sRead = '';
- }
- $results .= $sRead;
}
return $results;
}
-/* obsolete function, inform plugins that use it */
+
+
+/**
+ * Obsolete function, inform plugins that use it
+ * @deprecated use sqimap_run_command or sqimap_run_command_list instead
+ */
function sqimap_read_data_list($imap_stream, $tag, $handle_errors,
&$response, &$message, $query = '') {
global $color, $squirrelmail_language;
@@ -295,29 +273,42 @@ function sqimap_read_data_list($imap_stream, $tag, $handle_errors,
exit;
}
-function sqimap_error_box($title, $query = '', $message_title = '', $message = '')
+/**
+ * Function to display an error related to an IMAP-query.
+ * @param string title the caption of the error box
+ * @param string query the query that went wrong
+ * @param string message_title optional message title
+ * @param string message optional error message
+ * @param string $link an optional link to try again
+ * @return void
+ */
+function sqimap_error_box($title, $query = '', $message_title = '', $message = '', $link = '')
{
global $color, $squirrelmail_language;
set_up_language($squirrelmail_language);
require_once(SM_PATH . 'functions/display_messages.php');
$string = "\n" . $title . "
\n";
- if ($query != '')
+ $cmd = explode(' ',$query);
+ $cmd= strtolower($cmd[0]);
+
+ if ($query != '' && $cmd != 'login')
$string .= _("Query:") . ' ' . htmlspecialchars($query) . '
';
if ($message_title != '')
$string .= $message_title;
if ($message != '')
$string .= htmlspecialchars($message);
$string .= "
\n";
+ if ($link != '')
+ $string .= $link;
error_box($string,$color);
}
-/*
+/**
* Reads the output from the IMAP stream. If handle_errors is set to true,
* this will also handle all errors that are received. If it is not set,
- * the errors will be sent back through $response and $message
+ * the errors will be sent back through $response and $message.
*/
-
function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
&$response, &$message, $query = '',
$filter = false, $outputstream = false, $no_return = false) {
@@ -325,6 +316,7 @@ function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
$read = '';
if (!is_array($message)) $message = array();
if (!is_array($response)) $response = array();
+ $aResponse = '';
$resultlist = array();
$data = array();
$read = sqimap_fgets($imap_stream);
@@ -478,9 +470,20 @@ function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
/* error processing in case $read is false */
if ($read === false) {
- unset($data);
- sqimap_error_box(_("ERROR : Connection dropped by imap-server."), $query);
- exit;
+ // try to retrieve an untagged bye respons from the results
+ $sResponse = array_pop($data);
+ if ($sResponse !== NULL && strpos($sResponse,'* BYE') !== false) {
+ if (!$handle_errors) {
+ $query = '';
+ }
+ sqimap_error_box(_("ERROR : Imap server closed the connection."), $query, _("Server responded:"),$sResponse);
+ echo '