+function sqimap_fread($imap_stream,$iSize,$filter=false,
+ $outputstream=false, $no_return=false) {
+ if (!$filter || !$outputstream) {
+ $iBufferSize = $iSize;
+ } else {
+ $iBufferSize = 62400; // 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 the returned lines are split, do not end with \n
+ // then we have a problem and need to adjust (happened with uw)
+ if ($bBufferSizeAdapted && substr($sRead,-1) !== "\n") {
+ // use fgets because it stops at \n.
+ // we can do it because it's for correction
+ $sRead .= fgets($imap_stream,$iBufferSize);
+ $iRetrieved += strlen($sRead);
+ if ($iRetrieved == $iSize) {
+ $bFinished = true;
+ }
+ }
+ 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 = "<b><font color=$color[2]>\n" .
+ _("ERROR : Bad function call.") .
+ "</b><br>\n" .
+ _("Reason:") . ' '.
+ 'There is a plugin installed which make use of the <br>' .
+ 'SquirrelMail internal function sqimap_read_data_list.<br>'.
+ 'Please adapt the installed plugin and let it use<br>'.
+ 'sqimap_run_command or sqimap_run_command_list instead<br><br>'.
+ 'The following query was issued:<br>'.
+ htmlspecialchars($query) . '<br>' . "</font><br>\n";
+ error_box($string,$color);
+ echo '</body></html>';
+ exit;
+}
+
+function sqimap_error_box($title, $query = '', $message_title = '', $message = '')
+{
+ global $color, $squirrelmail_language;
+
+ set_up_language($squirrelmail_language);
+ require_once(SM_PATH . 'functions/display_messages.php');
+ $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
+ if ($query != '')
+ $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
+ if ($message_title != '')
+ $string .= $message_title;
+ if ($message != '')
+ $string .= htmlspecialchars($message);
+ $string .= "</font><br>\n";
+ error_box($string,$color);
+}
+