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