d29aac0e |
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) { |
4ca45d7b |
110 | global $special_folders, $list_special_folders_first; |
111 | |
d29aac0e |
112 | if (!function_exists ("ary_sort")) |
113 | include ("../functions/array.php"); |
114 | |
115 | $dm = sqimap_get_delimiter ($imap_stream); |
116 | |
117 | fputs ($imap_stream, "a001 LIST \"\" INBOX\n"); |
118 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
119 | $g = 0; |
120 | $phase = "inbox"; |
121 | for ($i = 0; $i < count($read_ary); $i++) { |
122 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
123 | $boxes[$g]["raw"] = $read_ary[$i]; |
124 | |
125 | $mailbox = find_mailbox_name($read_ary[$i]); |
126 | $dm_count = countCharInString($mailbox, $dm); |
127 | if (substr($mailbox, -1) == $dm) |
128 | $dm_count--; |
129 | |
130 | for ($j = 0; $j < $dm_count; $j++) |
131 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
132 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
133 | |
134 | if (substr($mailbox, -1) == $dm) |
135 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
136 | $boxes[$g]["unformatted"] = $mailbox; |
137 | $boxes[$g]["id"] = $g; |
138 | |
139 | /** Now lets get the flags for this mailbox **/ |
140 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\n"); |
141 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
142 | |
143 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
144 | $flags = substr($flags, 0, strpos($flags, ")")); |
145 | $flags = str_replace("\\", "", $flags); |
146 | $flags = trim(strtolower($flags)); |
147 | if ($flags) { |
148 | $boxes[$g]["flags"] = explode(" ", $flags); |
149 | } |
150 | } |
151 | $g++; |
152 | |
153 | if (!$read_ary[$i+1]) { |
154 | if ($phase == "inbox") { |
155 | fputs ($imap_stream, "a001 LSUB \"\" *\n"); |
156 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
157 | $phase = "lsub"; |
158 | $i--; |
159 | } |
160 | } |
161 | } |
162 | |
163 | $original = $boxes; |
4ca45d7b |
164 | |
165 | /** Get the folders into lower case so sorting is not case sensative */ |
166 | for ($i = 0; $i < count($original); $i++) { |
167 | $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]); |
168 | } |
169 | |
170 | /** Sort them **/ |
d29aac0e |
171 | $boxes = ary_sort($boxes, "unformatted", 1); |
4ca45d7b |
172 | |
173 | /** Get them back from the original array, still sorted by the id **/ |
d29aac0e |
174 | for ($i = 0; $i < count($boxes); $i++) { |
175 | for ($j = 0; $j < count($original); $j++) { |
176 | if ($boxes[$i]["id"] == $original[$j]["id"]) { |
177 | $boxes[$i] = $original[$j]; |
178 | } |
179 | } |
180 | } |
181 | |
4ca45d7b |
182 | |
d29aac0e |
183 | for ($i = 0; $i < count($boxes); $i++) { |
184 | if ($boxes[$i]["unformatted"] == $special_folders[0]) { |
185 | $boxesnew[0] = $boxes[$i]; |
4ca45d7b |
186 | $boxes[$i]["used"] = true; |
d29aac0e |
187 | } |
188 | } |
4ca45d7b |
189 | |
d29aac0e |
190 | if ($list_special_folders_first == true) { |
191 | for ($i = 0; $i < count($boxes); $i++) { |
192 | for ($j = 1; $j < count($special_folders); $j++) { |
193 | if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) { |
194 | $pos = count($boxesnew); |
195 | $boxesnew[$pos] = $boxes[$i]; |
196 | $boxes[$i]["used"] = true; |
197 | } |
198 | } |
199 | } |
200 | } |
4ca45d7b |
201 | |
d29aac0e |
202 | for ($i = 0; $i < count($boxes); $i++) { |
203 | if (($boxes[$i]["unformatted"] != $special_folders[0]) && |
204 | ($boxes[$i]["used"] == false)) { |
205 | $pos = count($boxesnew); |
206 | $boxesnew[$pos] = $boxes[$i]; |
207 | $boxes[$i]["used"] = true; |
208 | } |
209 | } |
210 | |
4ca45d7b |
211 | return $boxesnew; |
d29aac0e |
212 | } |
213 | |
4ca45d7b |
214 | ?> |