+/**
+ * @param stream $imap_stream
+ * @param integer $iSize
+ * @param boolean $filter
+ * @param mixed $outputstream stream or 'php://stdout' string
+ * @param boolean $no_return controls data returned by function
+ * @return string
+ * @since 1.4.1
+ */
+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 = 7800; // multiple of 78 in case of base64 decoding.
+ }
+ if ($iSize < $iBufferSize) {
+ $iBufferSize = $iSize;
+ }
+
+ $iRetrieved = 0;
+ $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;
+ }
+ 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 && $sRead != '') {
+ if (is_resource($outputstream)) {
+ fwrite($outputstream,$sRead);
+ } else if ($outputstream == 'php://stdout') {
+ echo $sRead;
+ }
+ }
+ if ($no_return) {
+ $sRead = '';
+ } else {
+ $results .= $sRead;
+ }
+ }
+ return $results;
+}
+
+
+/**
+ * Obsolete function, inform plugins that use it
+ * @param stream $imap_stream
+ * @param string $tag
+ * @param boolean $handle_errors
+ * @param array $response
+ * @param array $message
+ * @param string $query
+ * @since 1.1.3
+ * @deprecated (since 1.5.0) 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;
+ 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 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
+ * @since 1.5.0
+ */
+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 = "<font color=\"$color[2]\"><b>\n" . $title . "</b><br />\n";
+ $cmd = explode(' ',$query);
+ $cmd= strtolower($cmd[0]);
+
+ if ($query != '' && $cmd != 'login')
+ $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br />';
+ if ($message_title != '')
+ $string .= $message_title;
+ if ($message != '')
+ $string .= htmlspecialchars($message);
+ $string .= "</font><br />\n";
+ if ($link != '')
+ $string .= $link;
+ error_box($string,$color);
+}
+
+/**