16b50a2e1d524f7fb0ee9241dbe55f7904ce3033
[squirrelmail.git] / functions / imap_mailbox.php
1 <?
2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
6 **/
7
8
9 /******************************************************************************
10 ** Expunges a mailbox
11 ******************************************************************************/
12 function sqimap_mailbox_expunge ($imap_stream, $mailbox) {
13 sqimap_mailbox_select ($imap_stream, $mailbox);
14 fputs ($imap_stream, "a001 EXPUNGE\n");
15 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
16 }
17
18
19 /******************************************************************************
20 ** Checks whether or not the specified mailbox exists
21 ******************************************************************************/
22 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
23 $boxes = sqimap_mailbox_list ($imap_stream);
24 $found = false;
25 for ($i = 0; $i < count ($boxes); $i++) {
26 if ($boxes[$i]["unformatted"] == $mailbox)
27 $found = true;
28 }
29 return $found;
30 }
31
32
33
34 /******************************************************************************
35 ** Selects a mailbox
36 ******************************************************************************/
37 function sqimap_mailbox_select ($imap_stream, $mailbox) {
38 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\n");
39 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
40 }
41
42
43
44 /******************************************************************************
45 ** Creates a folder
46 ******************************************************************************/
47 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
48 if (strtolower($type) == "noselect") {
49 $dm = sqimap_get_delimiter($imap_stream);
50 $mailbox = $mailbox.$dm;
51 }
52 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\n");
53 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
54
55 sqimap_subscribe ($imap_stream, $mailbox);
56 }
57
58
59
60 /******************************************************************************
61 ** Subscribes to an existing folder
62 ******************************************************************************/
63 function sqimap_subscribe ($imap_stream, $mailbox) {
64 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\n");
65 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
66 }
67
68
69
70
71 /******************************************************************************
72 ** Unsubscribes to an existing folder
73 ******************************************************************************/
74 function sqimap_unsubscribe ($imap_stream, $mailbox) {
75 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\n");
76 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
77 }
78
79
80
81
82 /******************************************************************************
83 ** This is a recursive function that checks to see if the folder has any
84 ** subfolders, and if so it calls itself on the subfolders first, then
85 ** removes the parent folder.
86 ******************************************************************************/
87 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
88 global $boxes;
89
90 $dm = sqimap_get_delimiter($imap_stream);
91 for ($i = 0; $i < count($boxes); $i++) {
92 if (strstr($boxes[$i]["unformatted"], $mailbox . $dm)) {
93 $new_delete = $boxes[$i]["unformatted"];
94 $boxes = removeElement($boxes, $i);
95 // sqimap_mailbox_delete ($imap_stream, $new_delete);
96 }
97 }
98 sqimap_unsubscribe ($imap_stream, $mailbox);
99 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\n");
100 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
101 }
102
103
104
105 /******************************************************************************
106 ** Returns sorted mailbox lists in several different ways.
107 ** The array returned looks like this:
108 ******************************************************************************/
109 function sqimap_mailbox_list ($imap_stream) {
110 if (!function_exists ("ary_sort"))
111 include ("../functions/array.php");
112
113 $dm = sqimap_get_delimiter ($imap_stream);
114
115 fputs ($imap_stream, "a001 LIST \"\" INBOX\n");
116 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
117 $g = 0;
118 $phase = "inbox";
119 for ($i = 0; $i < count($read_ary); $i++) {
120 if (substr ($read_ary[$i], 0, 4) != "a001") {
121 $boxes[$g]["raw"] = $read_ary[$i];
122
123 $mailbox = find_mailbox_name($read_ary[$i]);
124 $dm_count = countCharInString($mailbox, $dm);
125 if (substr($mailbox, -1) == $dm)
126 $dm_count--;
127
128 for ($j = 0; $j < $dm_count; $j++)
129 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
130 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
131
132 if (substr($mailbox, -1) == $dm)
133 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
134 $boxes[$g]["unformatted"] = $mailbox;
135 $boxes[$g]["id"] = $g;
136
137 /** Now lets get the flags for this mailbox **/
138 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\n");
139 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
140
141 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
142 $flags = substr($flags, 0, strpos($flags, ")"));
143 $flags = str_replace("\\", "", $flags);
144 $flags = trim(strtolower($flags));
145 if ($flags) {
146 $boxes[$g]["flags"] = explode(" ", $flags);
147 }
148 }
149 $g++;
150
151 if (!$read_ary[$i+1]) {
152 if ($phase == "inbox") {
153 fputs ($imap_stream, "a001 LSUB \"\" *\n");
154 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
155 $phase = "lsub";
156 $i--;
157 }
158 }
159 }
160
161 $original = $boxes;
162 $boxes = ary_sort($boxes, "unformatted", 1);
163
164 for ($i = 0; $i < count($boxes); $i++) {
165 for ($j = 0; $j < count($original); $j++) {
166 if ($boxes[$i]["id"] == $original[$j]["id"]) {
167 $boxes[$i] = $original[$j];
168 }
169 }
170 }
171
172 for ($i = 0; $i < count($boxes); $i++) {
173 if ($boxes[$i]["unformatted"] == $special_folders[0]) {
174 $boxesnew[0] = $boxes[$i];
175 }
176 }
177 if ($list_special_folders_first == true) {
178 for ($i = 0; $i < count($boxes); $i++) {
179 for ($j = 1; $j < count($special_folders); $j++) {
180 if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
181 $pos = count($boxesnew);
182 $boxesnew[$pos] = $boxes[$i];
183 $boxes[$i]["used"] = true;
184 }
185 }
186 }
187 }
188 for ($i = 0; $i < count($boxes); $i++) {
189 if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
190 ($boxes[$i]["used"] == false)) {
191 $pos = count($boxesnew);
192 $boxesnew[$pos] = $boxes[$i];
193 $boxes[$i]["used"] = true;
194 }
195 }
196
197 return $boxes;
198 }
199
200 ?>