X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fimap_general.php;h=dbc42968526dbab1b0b0bb096d7648ac5a23da2b;hb=1916271776071b472e751cbee660dd1f85c11a2d;hp=4909543926f948ceb319a3bb67adb24efe963b81;hpb=355861846a51d35efe50848869d680fd3b4a5316;p=squirrelmail.git
diff --git a/functions/imap_general.php b/functions/imap_general.php
index 49095439..dbc42968 100755
--- a/functions/imap_general.php
+++ b/functions/imap_general.php
@@ -1,317 +1,559 @@
\n" .
+ _("ERROR : No available imapstream.") .
+ "\n";
+ error_box($string,$color);
+ return false;
+ }
+
+}
+
+function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = 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;
+ } else {
+ global $squirrelmail_language, $color;
+ set_up_language($squirrelmail_language);
+ require_once(SM_PATH . 'functions/display_messages.php');
+ $string = "\n" .
+ _("ERROR : No available imapstream.") .
+ "\n";
+ error_box($string,$color);
+ return false;
+ }
+
+}
- global $data_dir, $username;
- $IMAPSessionID = substr(session_id(), -4);
- if( $IMAPSessionID == '' ) {
- $IMAPSessionID = 'A001';
- }
+/*
+ * custom fgets function. gets a line from IMAP
+ * no matter how big it may be
+ */
- return( $IMAPSessionID );
- }
+function sqimap_fgets($imap_stream) {
+ $read = '';
+ $buffer = 4096;
+ $results = '';
+ $offset = 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 != '' ) {
+ $offset = strlen($results) - 1;
+ }
+ $results .= $read;
+ }
+ return $results;
+}
+/*
+ * 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
+ */
- /******************************************************************************
- ** 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
- ******************************************************************************/
-
- function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
- &$response, &$message) {
- global $color, $squirrelmail_language;
-
- $read = '';
- $resultlist = array();
-
- $more_msgs = true;
- while ($more_msgs) {
- $data = array();
- $total_size = 0;
- while (strpos($read, "\n") === false) {
- $read .= fgets($imap_stream, 9096);
- }
-
- if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
- $size = $regs[1];
- } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
- // Sizeless response, probably single-line
- $size = -1;
- $data[] = $read;
- $read = fgets($imap_stream, 9096);
- } else {
- $size = -1;
- }
- while (1) {
- while (strpos($read, "\n") === false) {
- $read .= fgets($imap_stream, 9096);
+function sqimap_read_data_list ($imap_stream, $tag_uid, $handle_errors, &$response, &$message, $query = '') {
+ global $color, $squirrelmail_language;
+ $read = '';
+ $tag_uid_a = explode(' ',trim($tag_uid));
+ $tag = $tag_uid_a[0];
+ $resultlist = array();
+ $data = array();
+ $read = sqimap_fgets($imap_stream);
+ $i = 0;
+ while ($read) {
+ $char = $read{0};
+ switch ($char)
+ {
+ case '+':
+ default:
+ $read = sqimap_fgets($imap_stream);
+ break;
+
+ case $tag{0}:
+ {
+ /* get the command */
+ $arg = '';
+ $i = strlen($tag)+1;
+ $s = substr($read,$i);
+ if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) {
+ $arg = substr($s,0,$j);
}
- // If we know the size, no need to look at the end parameters
- if ($size > 0) {
- if ($total_size == $size) {
- // We've reached the end of this 'message', switch to the next one.
- $data[] = $read;
- break;
- } else if ($total_size > $size) {
- $difference = $total_size - $size;
- $total_size = $total_size - strlen($read);
- $data[] = substr ($read, 0, strlen($read)-$difference);
- $read = substr ($read, strlen($read)-$difference, strlen($read));
- break;
- } else {
- $data[] = $read;
- $read = fgets($imap_stream, 9096);
- while (strpos($read, "\n") === false) {
- $read .= fgets($imap_stream, 9096);
- }
- }
- $total_size += strlen($read);
+ $found_tag = substr($read,0,$i-1);
+ if ($arg && $found_tag==$tag) {
+ switch ($arg)
+ {
+ case 'OK':
+ case 'BAD':
+ case 'NO':
+ case 'BYE':
+ case 'PREAUTH':
+ $response = $arg;
+ $message = trim(substr($read,$i+strlen($arg)));
+ break 3; /* switch switch while */
+ default:
+ /* this shouldn't happen */
+ $response = $arg;
+ $message = trim(substr($read,$i+strlen($arg)));
+ break 3; /* switch switch while */
+ }
+ } elseif($found_tag !== $tag) {
+ /* reset data array because we do not need this reponse */
+ $data = array();
+ $read = sqimap_fgets($imap_stream);
+ break;
+ }
+ } // end case $tag{0}
+
+ case '*':
+ {
+ if (preg_match('/^\*\s\d+\sFETCH/',$read)) {
+ /* check for literal */
+ $s = substr($read,-3);
+ $fetch_data = array();
+ do { /* outer loop, continue until next untagged fetch
+ or tagged reponse */
+ do { /* innerloop for fetching literals. with this loop
+ we prohibid that literal responses appear in the
+ outer loop so we can trust the untagged and
+ tagged info provided by $read */
+ if ($s === "}\r\n") {
+ $j = strrpos($read,'{');
+ $iLit = substr($read,$j+1,-3);
+ $fetch_data[] = $read;
+ $sLiteral = fread($imap_stream,$iLit);
+ if ($sLiteral === false) { /* error */
+ break 4; /* while while switch while */
+ }
+ /* backwards compattibility */
+ $aLiteral = explode("\n", $sLiteral);
+ /* release not neaded data */
+ unset($sLiteral);
+ foreach ($aLiteral as $line) {
+ $fetch_data[] = $line ."\n";
+ }
+ /* release not neaded data */
+ unset($aLiteral);
+ /* next fgets belongs to this fetch because
+ we just got the exact literalsize and there
+ must follow data to complete the response */
+ $read = sqimap_fgets($imap_stream);
+ if ($read === false) { /* error */
+ break 4; /* while while switch while */
+ }
+ $fetch_data[] = $read;
+ } else {
+ $fetch_data[] = $read;
+ }
+ /* retrieve next line and check in the while
+ statements if it belongs to this fetch response */
+ $read = sqimap_fgets($imap_stream);
+ if ($read === false) { /* error */
+ break 4; /* while while switch while */
+ }
+ /* check for next untagged reponse and break */
+ if ($read{0} == '*') break 2;
+ $s = substr($read,-3);
+ } while ($s === "}\r\n");
+ $s = substr($read,-3);
+ } while ($read{0} !== '*' &&
+ substr($read,0,strlen($tag)) !== $tag);
+ $resultlist[] = $fetch_data;
+ /* release not neaded data */
+ unset ($fetch_data);
} else {
- if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
- (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
- break;
- } else {
- $data[] = $read;
- $read = fgets ($imap_stream, 9096);
- }
+ $s = substr($read,-3);
+ do {
+ if ($s === "}\r\n") {
+ $j = strrpos($read,'{');
+ $iLit = substr($read,$j+1,-3);
+ $data[] = $read;
+ $sLiteral = fread($imap_stream,$iLit);
+ if ($sLiteral === false) { /* error */
+ $read = false;
+ break 3; /* while switch while */
+ }
+ $data[] = $sLiteral;
+ $fetch_data[] = sqimap_fgets($imap_stream);
+ } else {
+ $data[] = $read;
+ }
+ $read = sqimap_fgets($imap_stream);
+ if ($read === false) {
+ break 3; /* while switch while */
+ } else if ($read{0} == '*') {
+ break;
+ }
+ $s = substr($read,-3);
+ } while ($s === "}\r\n");
+ break 1;
}
- }
+ break;
+ } // end case '*'
+ } // end switch
+ } // end while
+
+ /* error processing in case $read is false */
+ if ($read === false) {
+ unset($data);
+ set_up_language($squirrelmail_language);
+ require_once(SM_PATH . 'functions/display_messages.php');
+ $string = "\n" .
+ _("ERROR : Connection dropped by imap-server.") .
+ "
\n" .
+ _("Query:") . ' '.
+ htmlspecialchars($query) . '
' . "
\n";
+ error_box($string,$color);
+ exit;
+ }
+
+ /* Set $resultlist array */
+ if (!empty($data)) {
+ $resultlist[] = $data;
+ }
+ elseif (empty($resultlist)) {
+ $resultlist[] = array();
+ }
- while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
- !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
- $read = fgets($imap_stream, 9096);
- }
- $resultlist[] = $data;
- }
- $response = $regs[1];
- $message = trim($regs[2]);
-
- if ($handle_errors == false) { return $resultlist; }
-
- if ($response == 'NO') {
- // ignore this error from m$ exchange, it is not fatal (aka bug)
- if (strstr($message, 'command resulted in') === false) {
+ /* Return result or handle errors */
+ if ($handle_errors == false) {
+ return( $resultlist );
+ }
+ switch ($response)
+ {
+ case 'OK':
+ return $resultlist;
+ break;
+ case 'NO':
+ /* ignore this error from M$ exchange, it is not fatal (aka bug) */
+ if (strstr($message, 'command resulted in') === false) {
set_up_language($squirrelmail_language);
- echo "
\n";
- echo _("ERROR : Could not complete request.");
- echo "
\n";
- echo _("Reason Given: ");
- echo $message . "
\n";
+ require_once(SM_PATH . 'functions/display_messages.php');
+ $string = "\n" .
+ _("ERROR : Could not complete request.") .
+ "
\n" .
+ _("Query:") . ' ' .
+ htmlspecialchars($query) . '
' .
+ _("Reason Given: ") .
+ htmlspecialchars($message) . "
\n";
+ error_box($string,$color);
+ echo '