- /******************************************************************************
- ** 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 ($imap_stream, $pre, $handle_errors, $response, $message) {
- global $color;
-
- $read = fgets ($imap_stream, 1024);
- $counter = 0;
- while ((substr($read, 0, strlen("$pre OK")) != "$pre OK") &&
- (substr($read, 0, strlen("$pre BAD")) != "$pre BAD") &&
- (substr($read, 0, strlen("$pre NO")) != "$pre NO")) {
- $data[$counter] = $read;
- $read = fgets ($imap_stream, 1024);
- $counter++;
- }
- if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
- $response = "OK";
- $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
- }
- else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
- $response = "BAD";
- $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
- }
- else {
- $response = "NO";
- $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
- }
-
- if ($handle_errors == true) {
- if ($response == "NO") {
- echo "<br><b><font color=$color[2]>";
- echo _("ERROR : Could not complete request.");
- echo "</b><br>";
- echo _("Reason Given: ");
- echo "$message</font><br>";
- exit;
- } else if ($response == "BAD") {
- echo "<br><b><font color=$color[2]>";
- echo _("ERROR : Bad or malformed request.");
- echo "</b><br>";
- echo _("Server responded: ");
- echo "$message</font><br>";
+/**
+ * imap.php
+ *
+ * Copyright (c) 1999-2002 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$
+ */
+
+require_once('../functions/page_header.php');
+require_once('../functions/display_messages.php');
+
+/**
+ * Unique SessionId
+ *
+ * Sets an unique session id in order to avoid simultanous sessions crash.
+ *
+ * @return string a 4 chars unique string
+ */
+
+global $sqimap_session_id;
+$sqimap_session_id = 1;
+function sqimap_session_id() {
+
+ global $data_dir, $username, $sqimap_session_id;
+
+ return( sprintf("A%03d", $sqimap_session_id++) );
+}
+
+/******************************************************************************
+** Both send a command and accept the result from the command. This is
+** to allow proper session number handling.
+******************************************************************************/
+
+function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) {
+ $sid = sqimap_session_id();
+ fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
+ $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message);
+ return $read;
+}
+
+function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) {
+ $sid = sqimap_session_id();
+ fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
+ $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message);
+ return $read;
+}
+
+
+/******************************************************************************
+** 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);
+ }
+ // 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);
+ } 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);
+ }
+ }
+ }
+
+ 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) {
+ set_up_language($squirrelmail_language);
+ echo "<br><b><font color=$color[2]>\n" .
+ _("ERROR : Could not complete request.") .
+ "</b><br>\n" .
+ _("Reason Given: ") .
+ $message . "</font><br>\n";