- /**
- ** imap.php
- **
- ** This implements all functions that do general imap functions.
- **/
-
- /******************************************************************************
- ** 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);
-// echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
- $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);
-// echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
- $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_general.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');
+
+global $sqimap_session_id;
+$sqimap_session_id = 1;
+
+/* Sets an unique session id in order to avoid simultanous sessions crash. */
+function sqimap_session_id($unique_id = false) {
+ global $data_dir, $username, $sqimap_session_id;
+ if (!$unique_id) {
+ return( sprintf("A%03d", $sqimap_session_id++) );
+ } else {
+ return( sprintf("A%03d", $sqimap_session_id++) . ' UID' );
+ }
+}
+
+/*
+ * 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, $unique_id = false) {
+ $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;
+}
+
+function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
+ $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;
+}
+
+
+/*
+ * custom fgets function. gets a line from IMAP
+ * no matter how big it may be
+ */
+
+function sqimap_fgets($imap_stream) {
+ $read = '';
+ $buffer = 4096;
+ $results = '';
+ while (strpos($read, "\n") === false) {
+ if (!($read = fgets($imap_stream, $buffer))) {
+ break;
+ }
+ $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
+ */
+
+function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
+ global $color, $squirrelmail_language;
+ $read = '';
+ $pre_a = explode(' ',trim($pre));
+ $pre = $pre_a[0];
+ $resultlist = array();
+ $data = array();
+ $read = sqimap_fgets($imap_stream);
+ while (1) {
+ switch (true) {
+ case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs):
+ case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs):
+ $response = $regs[1];
+ $message = trim($regs[2]);
+ break 2;
+ case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read):
+ $read = sqimap_fgets($imap_stream);
+ break 1;
+ case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs):
+ $fetch_data = array();
+ $fetch_data[] = $read;
+ $read = sqimap_fgets($imap_stream);
+ while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) &&
+ !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) {
+ $fetch_data[] = $read;
+ $last = $read;
+ $read = sqimap_fgets($imap_stream);
+ }
+ if (isset($last) && preg_match('/^\)/', $last)) {
+ array_pop($fetch_data);
+ }
+ $resultlist[] = $fetch_data;
+ break 1;
+ default:
+ $data[] = $read;
+ $read = sqimap_fgets($imap_stream);
+ break 1;
+ }
+ }
+ if (!empty($data)) {
+ $resultlist[] = $data;
+ }
+ elseif (empty($resultlist)) {
+ $resultlist[] = array();
+ }
+ if ($handle_errors == false) {
+ return( $resultlist );
+ }
+ elseif ($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" .
+ _("Query:") .
+ $query . '<br>' .
+ _("Reason Given: ") .
+ $message . "</font><br>\n";