- $imap_general_debug = false;
-
- /******************************************************************************
- ** 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, $squirrelmail_language, $imap_general_debug;
-
- $read = fgets($imap_stream, 9096);
-
- if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
- $size = $regs[1];
- } else {
- $size = 0;
- }
-
- $data = Array();
- $total_size = 0;
-
- $continue = true;
- while ($continue) {
- // Continue if needed for this single line
- while (strpos($read, "\n") === false) {
- $read .= fgets($imap_stream, 9096);
- }
- // For debugging purposes
- if ($imap_general_debug) {
- echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
- flush();
- }
-
-
- // If we know the size, no need to look at the end parameters
- if ($size > 0) {
- if ($total_size == $size) {
- $data[] = $read;
- $read = fgets($imap_stream, 9096);
- $read = fgets($imap_stream, 9096);
- $continue = false;
- } else if ($total_size > $size) {
- $difference = $total_size - $size;
- $total_size = $total_size - strlen($read);
- $read = substr ($read, 0, strlen($read)-$difference);
- $data[] = $read;
- $junk = fgets($imap_stream, 9096);
- $continue = false;
- } else {
- $data[] = $read;
- $read = fgets($imap_stream, 9096);
+/**
+ * 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) {
+ if(!($read .= fgets($imap_stream, 9096))) {
+ break;