imap_mailbox: Fixed minor bug in sqimap_mailbox_list
[squirrelmail.git] / functions / imap_general.php
1 <?php
2 /**
3 ** imap.php
4 **
5 ** This implements all functions that do general imap functions.
6 **/
7
8 $imap_general_debug = false;
9 //$imap_general_debug = false;
10
11 /******************************************************************************
12 ** Reads the output from the IMAP stream. If handle_errors is set to true,
13 ** this will also handle all errors that are received. If it is not set,
14 ** the errors will be sent back through $response and $message
15 ******************************************************************************/
16 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
17 global $color, $squirrelmail_language, $imap_general_debug;
18
19 $counter = 0;
20 do {
21 $data[$counter] = $read = fgets ($imap_stream, 4096);
22 if ($imap_general_debug) { echo "<small><tt><font color=cc0000>$read</font></tt></small><br>"; flush(); }
23 $counter++;
24 } while (! ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs));
25
26 $response = $regs[1];
27 $message = trim($regs[2]);
28
29 if ($imap_general_debug) echo "--<br>";
30
31 if ($handle_errors == true) {
32 if ($response == "NO") {
33 set_up_language($squirrelmail_language);
34 echo "<br><b><font color=$color[2]>\n";
35 echo _("ERROR : Could not complete request.");
36 echo "</b><br>\n";
37 echo _("Reason Given: ");
38 echo $message . "</font><br>\n";
39 exit;
40 } else if ($response == "BAD") {
41 set_up_language($squirrelmail_language);
42 echo "<br><b><font color=$color[2]>\n";
43 echo _("ERROR : Bad or malformed request.");
44 echo "</b><br>\n";
45 echo _("Server responded: ");
46 echo $message . "</font><br>\n";
47 exit;
48 }
49 }
50
51 return $data;
52 }
53
54 /******************************************************************************
55 ** Logs the user into the imap server. If $hide is set, no error messages
56 ** will be displayed. This function returns the imap connection handle.
57 ******************************************************************************/
58 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
59 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
60
61 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
62 $server_info = fgets ($imap_stream, 1024);
63
64 // Decrypt the password
65 $password = OneTimePadDecrypt($password, $onetimepad);
66
67 /** Do some error correction **/
68 if (!$imap_stream) {
69 if (!$hide) {
70 set_up_language($squirrelmail_language, true);
71 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
72 echo "$error_number : $error_string<br>\r\n";
73 }
74 exit;
75 }
76
77 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
78 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
79
80 /** If the connection was not successful, lets see why **/
81 if ($response != "OK") {
82 if (!$hide) {
83 if ($response != "NO") {
84 // "BAD" and anything else gets reported here.
85 set_up_language($squirrelmail_language, true);
86 if ($response == "BAD")
87 printf (_("Bad request: %s")."<br>\r\n", $message);
88 else
89 printf (_("Unknown error: %s") . "<br>\n", $message);
90 echo "<br>";
91 echo _("Read data:") . "<br>\n";
92 foreach ($read as $line)
93 {
94 echo htmlspecialchars($line) . "<br>\n";
95 }
96 exit;
97 } else {
98 // If the user does not log in with the correct
99 // username and password it is not possible to get the
100 // correct locale from the user's preferences.
101 // Therefore, apply the same hack as on the login
102 // screen.
103
104 // $squirrelmail_language is set by a cookie when
105 // the user selects language and logs out
106
107 set_up_language($squirrelmail_language, true);
108
109 ?>
110 <html>
111 <body bgcolor=ffffff>
112 <br>
113 <center>
114 <table width=70% noborder bgcolor=ffffff align=center>
115 <tr>
116 <td bgcolor=dcdcdc>
117 <font color=cc0000>
118 <center>
119 <?php echo _("ERROR") ?>
120 </center>
121 </font>
122 </td>
123 </tr>
124 <tr>
125 <td>
126 <center>
127 <?php echo _("Unknown user or password incorrect.") ?><br>
128 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
129 </center>
130 </td>
131 </tr>
132 </table>
133 </center>
134 </body>
135 </html>
136 <?php
137 session_destroy();
138 exit;
139 }
140 } else {
141 exit;
142 }
143 }
144
145 return $imap_stream;
146 }
147
148
149
150
151 /******************************************************************************
152 ** Simply logs out the imap session
153 ******************************************************************************/
154 function sqimap_logout ($imap_stream) {
155 fputs ($imap_stream, "a001 LOGOUT\r\n");
156 }
157
158 function sqimap_capability($imap_stream, $capability) {
159 global $sqimap_capabilities;
160 global $imap_general_debug;
161
162 if (!is_array($sqimap_capabilities)) {
163 fputs ($imap_stream, "a001 CAPABILITY\r\n");
164 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
165
166 $c = explode(' ', $read[0]);
167 for ($i=2; $i < count($c); $i++) {
168 list($k, $v) = explode('=', $c[$i]);
169 $sqimap_capabilities[$k] = ($v)?$v:TRUE;
170 }
171 }
172 return $sqimap_capabilities[$capability];
173 }
174
175 /******************************************************************************
176 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
177 ******************************************************************************/
178 function sqimap_get_delimiter ($imap_stream = false) {
179 global $imap_general_debug;
180 global $sqimap_delimiter;
181
182 /* Do some caching here */
183 if (!$sqimap_delimiter) {
184 if (sqimap_capability($imap_stream, "NAMESPACE")) {
185 /* According to something that I can't find, this is supposed to work on all systems
186 OS: This won't work in Courier IMAP.
187 OS: According to rfc2342 response from NAMESPACE command is:
188 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
189 OS: We want to lookup all personal NAMESPACES...
190 */
191 fputs ($imap_stream, "a001 NAMESPACE\r\n");
192 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
193 if (eregi('\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)', $read[0], $data)) {
194 if (eregi('^\( *\((.*)\) *\)', $data[1], $data2))
195 $pn = $data2[1];
196 $pna = explode(')(', $pn);
197 while (list($k, $v) = each($pna))
198 {
199 list($_, $n, $_, $d) = explode('"', $v);
200 $pn[$n] = $d;
201 }
202 /* OS: We don't need this code right now, it is for other_users and shared folders
203 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
204 $on = $data2[1];
205 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
206 $sn = $data2[1];
207 unset($data);
208 $ona = explode(')(', $on);
209 while (list($k, $v) = each($ona))
210 {
211 list($_, $n, $_, $d) = explode('"', $v);
212 $on[$n] = $d;
213 }
214 $sna = explode(')(', $sn);
215 while (list($k, $v) = each($sna))
216 {
217 list($_, $n, $_, $d) = explode('"', $v);
218 $sn[$n] = $d;
219 }
220 */
221 }
222 $sqimap_delimiter = $pn[0];
223 } else {
224 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
225 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
226 $quote_position = strpos ($read[0], "\"");
227 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
228 }
229 }
230 return $sqimap_delimiter;
231 }
232
233
234 /******************************************************************************
235 ** Gets the number of messages in the current mailbox.
236 ******************************************************************************/
237 function sqimap_get_num_messages ($imap_stream, $mailbox) {
238 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
239 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
240 for ($i = 0; $i < count($read_ary); $i++) {
241 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
242 return $regs[1];
243 }
244 }
245 return "BUG! Couldn't get number of messages in $mailbox!";
246 }
247
248
249 /******************************************************************************
250 ** Returns a displayable email address
251 ******************************************************************************/
252 function sqimap_find_email ($string) {
253 /** Luke Ehresman <lehresma@css.tayloru.edu>
254 ** <lehresma@css.tayloru.edu>
255 ** lehresma@css.tayloru.edu
256 **
257 ** What about
258 ** lehresma@css.tayloru.edu (Luke Ehresman)
259 **/
260
261 if (ereg("<([^>]+)>", $string, $regs)) {
262 $string = $regs[1];
263 }
264 return trim($string);
265 }
266
267
268 /******************************************************************************
269 ** Takes the From: field, and creates a displayable name.
270 ** Luke Ehresman <lkehresman@yahoo.com>
271 ** becomes: Luke Ehresman
272 ** <lkehresman@yahoo.com>
273 ** becomes: lkehresman@yahoo.com
274 ******************************************************************************/
275 function sqimap_find_displayable_name ($string) {
276 $string = " ".trim($string);
277 $orig_string = $string;
278 if (strpos($string, "<") && strpos($string, ">")) {
279 if (strpos($string, "<") == 1) {
280 $string = sqimap_find_email($string);
281 } else {
282 $string = trim($string);
283 $string = substr($string, 0, strpos($string, "<"));
284 $string = ereg_replace ("\"", "", $string);
285 }
286
287 if (trim($string) == "") {
288 $string = sqimap_find_email($orig_string);
289 }
290 }
291 return $string;
292 }
293
294
295 /******************************************************************************
296 ** Returns the number of unseen messages in this folder
297 ******************************************************************************/
298 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
299 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
300 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
301 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
302 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
303 return $regs[1];
304 }
305
306
307 /******************************************************************************
308 ** Saves a message to a given folder -- used for saving sent messages
309 ******************************************************************************/
310 function sqimap_append ($imap_stream, $sent_folder, $length) {
311 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
312 $tmp = fgets ($imap_stream, 1024);
313 }
314
315 function sqimap_append_done ($imap_stream) {
316 fputs ($imap_stream, "\r\n");
317 $tmp = fgets ($imap_stream, 1024);
318 }
319 ?>