-<?
- /**
- ** imap_mailbox.php
- **
- ** This impliments all functions that manipulate mailboxes
- **/
-
- /******************************************************************************
- ** Expunges a mailbox
- ******************************************************************************/
- function sqimap_mailbox_expunge ($imap_stream, $mailbox) {
- sqimap_mailbox_select ($imap_stream, $mailbox);
- fputs ($imap_stream, "a001 EXPUNGE\r\n");
- $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
- }
-
-
- /******************************************************************************
- ** Checks whether or not the specified mailbox exists
- ******************************************************************************/
- function sqimap_mailbox_exists ($imap_stream, $mailbox) {
- $boxes = sqimap_mailbox_list ($imap_stream);
- $found = false;
- for ($i = 0; $i < count ($boxes); $i++) {
- if ($boxes[$i]["unformatted"] == $mailbox)
- $found = true;
- }
- return $found;
- }
-
-
-
- /******************************************************************************
- ** Selects a mailbox
- ******************************************************************************/
- function sqimap_mailbox_select ($imap_stream, $mailbox) {
- fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
- $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
- }
-
-
-
- /******************************************************************************
- ** Creates a folder
- ******************************************************************************/
- function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
- if (strtolower($type) == "noselect") {
- $dm = sqimap_get_delimiter($imap_stream);
- $mailbox = $mailbox.$dm;
- }
- fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
- $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
-
- sqimap_subscribe ($imap_stream, $mailbox);
- }
-
-
-
- /******************************************************************************
- ** Subscribes to an existing folder
- ******************************************************************************/
- function sqimap_subscribe ($imap_stream, $mailbox) {
- fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
- $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
- }
-
-
-
-
- /******************************************************************************
- ** Unsubscribes to an existing folder
- ******************************************************************************/
- function sqimap_unsubscribe ($imap_stream, $mailbox) {
- fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
- $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
- }
-
-
-
-
- /******************************************************************************
- ** This function simply deletes the given folder
- ******************************************************************************/
- function sqimap_mailbox_delete ($imap_stream, $mailbox) {
- fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
- $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
- sqimap_unsubscribe ($imap_stream, $mailbox);
- }
-
-
-
- /******************************************************************************
- ** Returns sorted mailbox lists in several different ways.
- ** The array returned looks like this:
- ******************************************************************************/
- function sqimap_mailbox_list ($imap_stream) {
- global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username;
- if (!isset($load_prefs_php))
- include "../src/load_prefs.php";
- else
- global $folder_prefix;
- global $special_folders, $list_special_folders_first, $default_folder_prefix;
-
- if (!function_exists ("ary_sort"))
- include ("../functions/array.php");
-
- $dm = sqimap_get_delimiter ($imap_stream);
-
- fputs ($imap_stream, "a001 LIST \"\" INBOX\r\n");
- $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
- $g = 0;
- $phase = "inbox";
- for ($i = 0; $i < count($read_ary); $i++) {
- if (substr ($read_ary[$i], 0, 4) != "a001") {
- $boxes[$g]["raw"] = $read_ary[$i];
-
- $mailbox = find_mailbox_name($read_ary[$i]);
- $dm_count = countCharInString($mailbox, $dm);
- if (substr($mailbox, -1) == $dm)
- $dm_count--;
-
- for ($j = 0; $j < $dm_count; $j++)
- $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
- $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
-
- $boxes[$g]["unformatted-dm"] = $mailbox;
- if (substr($mailbox, -1) == $dm)
- $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
- $boxes[$g]["unformatted"] = $mailbox;
- $boxes[$g]["id"] = $g;
-
- /** Now lets get the flags for this mailbox **/
- fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
- $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
-
- $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
- $flags = substr($flags, 0, strpos($flags, ")"));
- $flags = str_replace("\\", "", $flags);
- $flags = trim(strtolower($flags));
+<?php
+
+/**
+ * imap_mailbox.php
+ *
+ * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Licensed under the GNU GPL. For full terms see the file COPYING.
+ *
+ * This impliments all functions that manipulate mailboxes
+ *
+ * $Id$
+ */
+
+/*************************
+ ** Expunges a mailbox **
+ *************************/
+function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true)
+{
+ fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
+ $read = sqimap_read_data($imap_stream, sqimap_session_id(),
+ $handle_errors, $response, $message);
+}
+
+
+/******************************************************************************
+ ** Checks whether or not the specified mailbox exists
+ ******************************************************************************/
+function sqimap_mailbox_exists ($imap_stream, $mailbox)
+{
+ if (! isset($mailbox)) {
+ return false;
+ }
+ fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
+ $mbx = sqimap_read_data($imap_stream, sqimap_session_id(),
+ true, $response, $message);
+ return isset($mbx[0]);
+}
+
+/******************************************************************************
+ ** Selects a mailbox
+ ******************************************************************************/
+function sqimap_mailbox_select ($imap_stream, $mailbox,
+ $hide=true, $recent=false)
+{
+ global $auto_expunge;
+
+ if ( $mailbox == 'None' ) {
+ return;
+ }
+
+ fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
+ $read = sqimap_read_data($imap_stream, sqimap_session_id(),
+ true, $response, $message);
+ if ($recent) {
+ for ($i=0; $i<count($read); $i++) {
+ if (strpos(strtolower($read[$i]), 'recent')) {
+ $r = explode(' ', $read[$i]);
+ }
+ }
+ return $r[1];
+ }
+ if ($auto_expunge) {
+ fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
+ $tmp = sqimap_read_data($imap_stream, sqimap_session_id(),
+ false, $a, $b);
+ }
+}
+
+
+
+/******************************************************************************
+ ** Creates a folder
+ ******************************************************************************/
+function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
+{
+ global $delimiter;
+ if (strtolower($type) == 'noselect') {
+ $mailbox = $mailbox.$delimiter;
+ }
+ fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
+ $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
+ true, $response, $message);
+
+ sqimap_subscribe ($imap_stream, $mailbox);
+}
+
+
+
+/******************************************************************************
+ ** Subscribes to an existing folder
+ ******************************************************************************/
+function sqimap_subscribe ($imap_stream, $mailbox)
+{
+ fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
+ $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
+ true, $response, $message);
+}
+
+
+
+/******************************************************************************
+ ** Unsubscribes to an existing folder
+ ******************************************************************************/
+function sqimap_unsubscribe ($imap_stream, $mailbox)
+{
+ global $imap_server_type;
+
+ fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
+ $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
+ true, $response, $message);
+}
+
+
+
+/******************************************************************************
+ ** This function simply deletes the given folder
+ ******************************************************************************/
+function sqimap_mailbox_delete ($imap_stream, $mailbox)
+{
+ fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
+ $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
+ true, $response, $message);
+ sqimap_unsubscribe ($imap_stream, $mailbox);
+}
+
+/***********************************************************************
+ ** Determines if the user is subscribed to the folder or not
+ **********************************************************************/
+function sqimap_mailbox_is_subscribed($imap_stream, $folder)
+{
+ $boxes = sqimap_mailbox_list ($imap_stream);
+ foreach ($boxes as $ref) {
+ if ($ref['unformatted'] == $folder) {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+/******************************************************************************
+ ** Formats a mailbox into 4 parts for the $boxes array
+ **
+ ** The four parts are:
+ **
+ ** raw - Raw LIST/LSUB response from the IMAP server
+ ** formatted - nicely formatted folder name
+ ** unformatted - unformatted, but with delimiter at end removed
+ ** unformatted-dm - folder name as it appears in raw response
+ ** unformatted-disp - unformatted without $folder_prefix
+ **
+ ******************************************************************************/
+function sqimap_mailbox_parse ($line, $line_lsub)
+{
+ global $folder_prefix, $delimiter;
+
+ /* Process each folder line */
+ for ($g=0; $g < count($line); $g++) {
+
+ /* Store the raw IMAP reply */
+ if (isset($line[$g])) {
+ $boxes[$g]["raw"] = $line[$g];
+ }
+ else {
+ $boxes[$g]["raw"] = "";
+ }
+
+
+ /* Count number of delimiters ($delimiter) in folder name */
+ $mailbox = trim($line_lsub[$g]);
+ $dm_count = countCharInString($mailbox, $delimiter);
+ if (substr($mailbox, -1) == $delimiter) {
+ /* If name ends in delimiter - decrement count by one */
+ $dm_count--;
+ }
+
+ /* Format folder name, but only if it's a INBOX.* or have */
+ /* a parent. */
+ $boxesbyname[$mailbox] = $g;
+ $parentfolder = readMailboxParent($mailbox, $delimiter);
+ if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
+ (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
+ ( isset($boxesbyname[$parentfolder]) &&
+ (strlen($parentfolder) > 0) ) ) {
+ $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
+ if ($indent > 0) {
+ $boxes[$g]["formatted"] = str_repeat(" ", $indent);
+ }
+ else {
+ $boxes[$g]["formatted"] = '';
+ }
+ $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
+ }
+ else {
+ $boxes[$g]["formatted"] = $mailbox;
+ }
+
+ $boxes[$g]['unformatted-dm'] = $mailbox;
+ if (substr($mailbox, -1) == $delimiter) {
+ $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
+ }
+ $boxes[$g]['unformatted'] = $mailbox;
+ if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
+ $mailbox = substr($mailbox, strlen($folder_prefix));
+ }
+ $boxes[$g]['unformatted-disp'] = $mailbox;
+ $boxes[$g]['id'] = $g;
+
+ $boxes[$g]['flags'] = array();
+ if (isset($line[$g])) {
+ ereg("\(([^)]*)\)",$line[$g],$regs);
+ $flags = trim(strtolower(str_replace('\\', '',$regs[1])));