X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fimap_general.php;h=17671284c7173093e7adb175b4a1ad648cd398b8;hb=6c540963c77709619a0670cc733249c8a4162c30;hp=7d2f8ae84ecf53109ff47614a7053de3dfaf6155;hpb=15bc7f666ca2f73a6317b086e9f626885dc52263;p=squirrelmail.git
diff --git a/functions/imap_general.php b/functions/imap_general.php
index 7d2f8ae8..17671284 100755
--- a/functions/imap_general.php
+++ b/functions/imap_general.php
@@ -36,8 +36,13 @@ function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$respon
if ($imap_stream) {
$sid = sqimap_session_id($unique_id);
fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
- $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
- return $read;
+ $tag_uid_a = explode(' ',trim($sid));
+ $tag = $tag_uid_a[0];
+ $read = sqimap_retrieve_imap_response ($imap_stream, $tag, $handle_errors, $response, $message, $query );
+ /* get the response and the message */
+ $message = $message[$tag];
+ $response = $response[$tag];
+ return $read[$tag];
} else {
global $squirrelmail_language, $color;
set_up_language($squirrelmail_language);
@@ -48,15 +53,28 @@ function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$respon
error_box($string,$color);
return false;
}
-
}
-function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
+function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response,
+ &$message, $unique_id = false,$filter=false,
+ $outputstream=false,$no_return=false) {
if ($imap_stream) {
$sid = sqimap_session_id($unique_id);
- fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
- $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
- return $read;
+ fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
+ $tag_uid_a = explode(' ',trim($sid));
+ $tag = $tag_uid_a[0];
+
+ $read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response,
+ $message, $query,$filter,$outputstream,$no_return);
+ /* retrieve the response and the message */
+ $response = $response[$tag];
+ $message = $message[$tag];
+
+ if (!empty($read[$tag])) {
+ return $read[$tag][0];
+ } else {
+ return $read[$tag];
+ }
} else {
global $squirrelmail_language, $color;
set_up_language($squirrelmail_language);
@@ -66,11 +84,79 @@ function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &
"\n";
error_box($string,$color);
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));
+ $tag = $tag_uid_a[0];
+ $query = $sid . ' '.$new_query."\r\n";
+ $aQuery[$tag] = $query;
+}
+
+function sqimap_run_pipelined_command ($imap_stream, $aQueryList, $handle_errors,
+ &$aServerResponse, &$aServerMessage, $unique_id = false,
+ $filter=false,$outputstream=false,$no_return=false) {
+ $aResponse = false;
+
+ /*
+ Do not fire all calls at once to the imap-server but split the calls up
+ in portions of $iChunkSize. If we do not do that I think we misbehave as
+ IMAP client or should handle BYE calls if the IMAP-server drops the
+ connection because the number of queries is to large. This isn't tested
+ but a wild guess how it could work in the field.
+
+ After testing it on Exchange 2000 we discovered that a chunksize of 32
+ was quicker then when we raised it to 128.
+ */
+ $iQueryCount = count($aQueryList);
+ $iChunkSize = 32;
+ // array_chunk would also do the job but it's supported from php > 4.2
+ $aQueryChunks = array();
+ $iLoops = floor($iQueryCount / $iChunkSize);
+
+ if ($iLoops * $iChunkSize != $iQueryCount) ++$iLoops;
+
+ if (!function_exists('array_chunk')) { // arraychunk replacement
+ reset($aQueryList);
+ for($i=0;$i<$iLoops;++$i) {
+ for($j=0;$j<$iChunkSize;++$j) {
+ $key = key($aQueryList);
+ $aTmp[$key] = $aQueryList[$key];
+ if (next($aQueryList) === false) break;
+ }
+ $aQueryChunks[] = $aTmp;
+ }
+ } else {
+ $aQueryChunks = array_chunk($aQueryList,$iChunkSize,true);
}
+ for ($i=0;$i<$iLoops;++$i) {
+ $aQuery = $aQueryChunks[$i];
+ foreach($aQuery as $tag => $query) {
+ fputs($imap_stream,$query);
+ $aResults[$tag] = false;
+ }
+ foreach($aQuery as $tag => $query) {
+ if ($aResults[$tag] == false) {
+ $aReturnedResponse = sqimap_retrieve_imap_response ($imap_stream, $tag,
+ $handle_errors, $response, $message, $query,
+ $filter,$outputstream,$no_return);
+ foreach ($aReturnedResponse as $returned_tag => $aResponse) {
+ if (!empty($aResponse)) {
+ $aResults[$returned_tag] = $aResponse[0];
+ } else {
+ $aResults[$returned_tag] = $aResponse;
+ }
+ $aServerResponse[$returned_tag] = $response[$returned_tag];
+ $aServerMessage[$returned_tag] = $message[$returned_tag];
+ }
+ }
+ }
+ }
+ return $aResults;
}
-
/*
* custom fgets function. gets a line from IMAP
* no matter how big it may be
@@ -81,9 +167,11 @@ function sqimap_fgets($imap_stream) {
$buffer = 4096;
$results = '';
$offset = 0;
- $i=0;
while (strpos($results, "\r\n", $offset) === false) {
if (!($read = fgets($imap_stream, $buffer))) {
+ /* this happens in case of an error */
+ /* reset $results because it's useless */
+ $results = false;
break;
}
if ( $results != '' ) {
@@ -94,21 +182,156 @@ function sqimap_fgets($imap_stream) {
return $results;
}
+function sqimap_fread($imap_stream,$iSize,$filter=false,
+ $outputstream=false, $no_return=false) {
+ if (!$filter || !$outputstream) {
+ $iBufferSize = $iSize;
+ } else {
+ // see php bug 24033. They changed fread behaviour %$^&$%
+ $iBufferSize = 780; // multiple of 78 in case of base64 decoding.
+ }
+ $iRet = $iSize - $iBufferSize;
+ $iRetrieved = 0;
+ $i = 0;
+ $results = $sReadRem = '';
+ $bFinished = $bBufferSizeAdapted = $bBufferIsOk = false;
+ while (($iRetrieved < ($iSize - $iBufferSize))) {
+ $sRead = fread($imap_stream,$iBufferSize);
+ 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 ($outputstream) {
+ if (is_resource($outputstream)) {
+ fwrite($outputstream,$sRead);
+ } else if ($outputstream == 'php://stdout') {
+ echo $sRead;
+ }
+ }
+ if ($no_return) {
+ $sRead = '';
+ }
+ $results .= $sRead;
+ }
+ if (!$results && !$bFinished) {
+ $sRead = fread($imap_stream,($iSize - ($iRetrieved)));
+ if ($filter) {
+ $filter($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 */
+function sqimap_read_data_list($imap_stream, $tag, $handle_errors,
+ &$response, &$message, $query = '') {
+ global $color, $squirrelmail_language;
+ set_up_language($squirrelmail_language);
+ require_once(SM_PATH . 'functions/display_messages.php');
+ $string = "\n" .
+ _("ERROR : Bad function call.") .
+ "
\n" .
+ _("Reason:") . ' '.
+ 'There is a plugin installed which make use of the
' .
+ 'SquirrelMail internal function sqimap_read_data_list.
'.
+ 'Please adapt the installed plugin and let it use
'.
+ 'sqimap_run_command or sqimap_run_command_list instead
'.
+ 'The following query was issued:
'.
+ htmlspecialchars($query) . '
' . "
\n";
+ error_box($string,$color);
+ echo '