-<?
- /**
- ** imap_messages.php
- **
- ** This implements functions that manipulate messages
- **/
-
- /******************************************************************************
- ** Copies specified messages to specified folder
- ******************************************************************************/
- function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
- fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
- $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
- }
-
- /******************************************************************************
- ** Deletes specified messages and moves them to trash if possible
- ******************************************************************************/
- function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
- global $move_to_trash, $trash_folder, $auto_expunge;
-
- if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) {
- sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
- sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
- } else {
- sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
- }
- }
-
- /******************************************************************************
- ** Sets the specified messages with specified flag
- ******************************************************************************/
- function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
- fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
- $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
-
- }
-
- /******************************************************************************
- ** Returns some general header information -- FROM, DATE, and SUBJECT
- ******************************************************************************/
- function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) {
- fputs ($imap_stream, "a001 FETCH $id:$id RFC822.HEADER.LINES (From Subject Date)\r\n");
- $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
-
- $subject = _("(no subject)");
- $from = _("Unknown Sender");
- for ($i = 0; $i < count($read); $i++) {
- if (strtolower(substr($read[$i], 0, 5)) == "from:") {
- $from = sqimap_find_displayable_name(substr($read[$i], 5));
- } else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
- $date = substr($read[$i], 5);
- } else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
- $subject = htmlspecialchars(substr($read[$i], 8));
- if (strlen(trim($subject)) == 0)
- $subject = _("(no subject)");
- }
- }
- }
-
- /******************************************************************************
- ** Returns the flags for the specified messages
- ******************************************************************************/
- function sqimap_get_flags ($imap_stream, $i) {
- fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
- $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
- if (strpos($read[0], "FLAGS")) {
- $tmp = ereg_replace("\(", "", $read[0]);
- $tmp = ereg_replace("\)", "", $tmp);
- $tmp = str_replace("\\", "", $tmp);
- $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp));
- $tmp = trim($tmp);
- $flags = explode(" ", $tmp);
- } else {
- $flags[0] = "None";
- }
- return $flags;
- }
-
- /******************************************************************************
- ** Returns a message array with all the information about a message. See
- ** the documentation folder for more information about this array.
- ******************************************************************************/
- function sqimap_get_message ($imap_stream, $id, $mailbox) {
- $message["INFO"]["ID"] = $id;
- $message["INFO"]["MAILBOX"] = $mailbox;
- $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
- $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]);
- return $message;
- }
-
- /******************************************************************************
- ** Wrapper function that reformats the header information.
- ******************************************************************************/
- function sqimap_get_message_header ($imap_stream, $id) {
- fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
- $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
-
- return sqimap_get_header($imap_stream, $read);
- }
-
- /******************************************************************************
- ** Wrapper function that returns entity headers for use by decodeMime
- ******************************************************************************/
- function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
- $header = sqimap_get_header($imap_stream, $read);
- $type0 = $header["TYPE0"];
- $type1 = $header["TYPE1"];
- $bound = $header["BOUNDARY"];
- $encoding = $header["ENCODING"];
- $charset = $header["CHARSET"];
- $filename = $header["FILENAME"];
- }
-
- /******************************************************************************
- ** Queries the IMAP server and gets all header information.
- ******************************************************************************/
- function sqimap_get_header ($imap_stream, $read) {
- $i = 0;
- // Set up some defaults
- $header["TYPE0"] = "text";
- $header["TYPE1"] = "plain";
- $header["CHARSET"] = "us-ascii";
-
- while ($i < count($read)) {
- if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
- $header["MIME"] = true;
- $i++;
- }
-
- /** ENCODING TYPE **/
- else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
- $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
- $i++;
- }
-
- /** CONTENT-TYPE **/
- else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
- $cont = strtolower(trim(substr($read[$i], 13)));
- if (strpos($cont, ";"))
- $cont = substr($cont, 0, strpos($cont, ";"));
-
-
- if (strpos($cont, "/")) {
- $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
- $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
- } else {
- $header["TYPE0"] = $cont;
+<?php
+
+/**
+ * imap_messages.php
+ *
+ * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Licensed under the GNU GPL. For full terms see the file COPYING.
+ *
+ * This implements functions that manipulate messages
+ *
+ * $Id$
+ */
+
+/* Copies specified messages to specified folder */
+function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
+ global $uid_support;
+ $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message, $uid_support);
+}
+
+/* Deletes specified messages and moves them to trash if possible */
+function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
+ global $move_to_trash, $trash_folder, $auto_expunge, $uid_support;
+
+ if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
+ sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
+ }
+ sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true);
+}
+
+/* Sets the specified messages with specified flag */
+function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
+ global $uid_support;
+ $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
+}
+
+/* Remove specified flag from specified messages */
+function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
+ global $uid_support;
+ $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
+}
+
+/* Returns some general header information -- FROM, DATE, and SUBJECT */
+class small_header {
+ var $from = '', $subject = '', $date = '', $to = '',
+ $priority = 0, $message_id = 0, $cc = '', $uid = '';
+}
+
+function sqimap_get_small_header ($imap_stream, $id, $sent) {
+ $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
+ return $res[0];
+}
+
+/*
+ * Sort the message list and crunch to be as small as possible
+ * (overflow could happen, so make it small if possible)
+ */
+function sqimap_message_list_squisher($messages_array) {
+ if( !is_array( $messages_array ) ) {
+ return;
+ }
+
+ sort($messages_array, SORT_NUMERIC);
+ $msgs_str = '';
+ while ($messages_array) {
+ $start = array_shift($messages_array);
+ $end = $start;
+ while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
+ $end = array_shift($messages_array);
+ }
+ if ($msgs_str != '') {
+ $msgs_str .= ',';
+ }
+ $msgs_str .= $start;
+ if ($start != $end) {
+ $msgs_str .= ':' . $end;
+ }
+ }
+ return $msgs_str;
+}
+
+/* returns the references header lines */
+function get_reference_header ($imap_stream, $message) {
+ global $uid_support;
+ $responses = array ();
+ $sid = sqimap_session_id($uid_support);
+ $results = array();
+ $references = "";
+ $query = "$sid FETCH $message BODY[HEADER.FIELDS (References)]\r\n";
+ fputs ($imap_stream, $query);
+ $responses = sqimap_read_data_list($imap_stream, $sid, true, $responses, $message);
+ if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) {
+ $responses = array ();
+ }
+ return $responses;
+}
+
+
+/* get sort order from server and
+ * return it as the $id array for
+ * mailbox_display
+ */
+
+function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) {
+ global $default_charset, $thread_sort_messages,
+ $internal_date_sort, $server_sort_array,
+ $sent_folder, $mailbox, $uid_support;
+
+ if (session_is_registered('server_sort_array')) {
+ session_unregister('server_sort_array');
+ }
+
+ $sid = sqimap_session_id($uid_support);
+ $sort_on = array();
+ $reverse = 0;
+ $server_sort_array = array();
+ $sort_test = array();
+ $sort_query = '';
+
+ if ($sort == 6) {
+ if ($uid_support) {
+ $uidnext = $mbxresponse['UIDNEXT']-1;
+ $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
+ fputs($imap_stream, $uid_query);
+ $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
+ if (isset($uids[0])) {
+ if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
+ $server_sort_array = preg_split("/ /", trim($regs[1]));
+ }