Added myself to the AUTHORS file.
[squirrelmail.git] / functions / imap_general.php
CommitLineData
59177427 1<?php
052e0c26 2 /**
3 ** imap.php
4 **
5 ** This implements all functions that do general imap functions.
245a6892 6 **
7 ** $Id$
052e0c26 8 **/
9
f435778e 10 if (defined ('imap_general_php'))
11 return;
12 define ('imap_general_php', true);
13
14 global $imap_general_debug;
a3db804c 15 $imap_general_debug = false;
a3db804c 16
052e0c26 17 /******************************************************************************
18 ** Reads the output from the IMAP stream. If handle_errors is set to true,
19 ** this will also handle all errors that are received. If it is not set,
20 ** the errors will be sent back through $response and $message
21 ******************************************************************************/
a3432f47 22
91f68e94 23 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
24 &$response, &$message) {
25 global $color, $squirrelmail_language, $imap_general_debug;
245a6892 26
91f68e94 27 $read = "";
28 $resultlist = array();
245a6892 29
91f68e94 30 $more_msgs = true;
31 while ($more_msgs) {
32 $data = array();
33 $total_size = 0;
d9ca5b40 34 while (strpos($read, "\n") === false) {
35 $read .= fgets($imap_stream, 9096);
36 }
4f5c1bcb 37
91f68e94 38 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
39 $size = $regs[1];
40 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
41 // Sizeless response, probably single-line
42 // For debugging purposes
43 if ($imap_general_debug) {
44 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
45 flush();
d9ca5b40 46 }
91f68e94 47 $size = 0;
48 $data[] = $read;
49 $read = fgets($imap_stream, 9096);
d9ca5b40 50 } else {
91f68e94 51 $size = 0;
52 }
53 while (1) {
54 while (strpos($read, "\n") === false) {
55 $read .= fgets($imap_stream, 9096);
56 }
57 // For debugging purposes
58 if ($imap_general_debug) {
59 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
60 flush();
61 }
62 // If we know the size, no need to look at the end parameters
63 if ($size > 0) {
64 if ($total_size == $size) {
65 // We've reached the end of this 'message', switch to the next one.
66 $data[] = $read;
37e24380 67 break;
91f68e94 68 } else if ($total_size > $size) {
69 $difference = $total_size - $size;
70 $total_size = $total_size - strlen($read);
71 $data[] = substr ($read, 0, strlen($read)-$difference);
72 $read = substr ($read, strlen($read)-$difference, strlen($read));
73 break;
74 } else {
75 $data[] = $read;
76 $read = fgets($imap_stream, 9096);
3fac2aa9 77 while (strpos($read, "\n") === false) {
78 $read .= fgets($imap_stream, 9096);
79 }
91f68e94 80 }
81 $total_size += strlen($read);
4f5c1bcb 82 } else {
91f68e94 83 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
84 ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
85 break;
86 } else {
87 $data[] = $read;
88 $read = fgets ($imap_stream, 9096);
89 }
4f5c1bcb 90 }
d9ca5b40 91 }
a3db804c 92
91f68e94 93 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
94 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
95 $read = fgets($imap_stream, 9096);
96 }
97 $resultlist[] = $data;
98 }
3d2504a1 99 $response = $regs[1];
100 $message = trim($regs[2]);
101
74424a43 102 if ($imap_general_debug) echo '--<br>';
052e0c26 103
f358f099 104 if ($handle_errors == false)
91f68e94 105 return $resultlist;
d9ca5b40 106
74424a43 107 if ($response == 'NO') {
f358f099 108 // ignore this error from m$ exchange, it is not fatal (aka bug)
8e8ebd27 109 if (strstr($message, 'command resulted in') === false) {
441f2d33 110 set_up_language($squirrelmail_language);
04632dbc 111 echo "<br><b><font color=$color[2]>\n";
f358f099 112 echo _("ERROR : Could not complete request.");
04632dbc 113 echo "</b><br>\n";
f358f099 114 echo _("Reason Given: ");
3d2504a1 115 echo $message . "</font><br>\n";
052e0c26 116 exit;
117 }
74424a43 118 } else if ($response == 'BAD') {
f358f099 119 set_up_language($squirrelmail_language);
120 echo "<br><b><font color=$color[2]>\n";
121 echo _("ERROR : Bad or malformed request.");
122 echo "</b><br>\n";
123 echo _("Server responded: ");
124 echo $message . "</font><br>\n";
125 exit;
052e0c26 126 }
91f68e94 127 return $resultlist;
128 }
129
130 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
131 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
132 return $res[0];
052e0c26 133 }
134
052e0c26 135 /******************************************************************************
136 ** Logs the user into the imap server. If $hide is set, no error messages
137 ** will be displayed. This function returns the imap connection handle.
138 ******************************************************************************/
139 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
bc104ef3 140 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
1b187352 141
342c46bd 142 $imap_stream = fsockopen ($imap_server_address, $imap_port,
090595e1 143 $error_number, $error_string, 15);
052e0c26 144 $server_info = fgets ($imap_stream, 1024);
145
52eefafc 146 // Decrypt the password
bc104ef3 147 $password = OneTimePadDecrypt($password, $onetimepad);
52eefafc 148
052e0c26 149 /** Do some error correction **/
150 if (!$imap_stream) {
151 if (!$hide) {
441f2d33 152 set_up_language($squirrelmail_language, true);
1b187352 153 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
052e0c26 154 echo "$error_number : $error_string<br>\r\n";
155 }
156 exit;
157 }
158
704db5b2 159 fputs ($imap_stream, "a001 LOGIN \"" . quoteIMAP($username) .
160 '" "' . quoteIMAP($password) . "\"\r\n");
74424a43 161 $read = sqimap_read_data ($imap_stream, 'a001', false, $response, $message);
052e0c26 162
163 /** If the connection was not successful, lets see why **/
3d2504a1 164 if ($response != "OK") {
052e0c26 165 if (!$hide) {
74424a43 166 if ($response != 'NO') {
3d2504a1 167 // "BAD" and anything else gets reported here.
441f2d33 168 set_up_language($squirrelmail_language, true);
74424a43 169 if ($response == 'BAD')
3d2504a1 170 printf (_("Bad request: %s")."<br>\r\n", $message);
171 else
172 printf (_("Unknown error: %s") . "<br>\n", $message);
74424a43 173 echo '<br>';
3d2504a1 174 echo _("Read data:") . "<br>\n";
ec0b7dbd 175 if (is_array($read))
176 {
177 foreach ($read as $line)
178 {
179 echo htmlspecialchars($line) . "<br>\n";
180 }
3d2504a1 181 }
052e0c26 182 exit;
165e24a7 183 } else {
1b187352 184 // If the user does not log in with the correct
185 // username and password it is not possible to get the
186 // correct locale from the user's preferences.
187 // Therefore, apply the same hack as on the login
188 // screen.
189
190 // $squirrelmail_language is set by a cookie when
191 // the user selects language and logs out
192
441f2d33 193 set_up_language($squirrelmail_language, true);
1b187352 194
052e0c26 195 ?>
196 <html>
74424a43 197 <body bgcolor="ffffff">
052e0c26 198 <br>
199 <center>
74424a43 200 <table width="70%" noborder bgcolor="ffffff" align="center">
052e0c26 201 <tr>
74424a43 202 <td bgcolor="dcdcdc">
203 <font color="cc0000">
052e0c26 204 <center>
59177427 205 <?php echo _("ERROR") ?>
052e0c26 206 </center>
207 </font>
208 </td>
209 </tr>
210 <tr>
211 <td>
212 <center>
59177427 213 <?php echo _("Unknown user or password incorrect.") ?><br>
5630c7ec 214 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
052e0c26 215 </center>
216 </td>
217 </tr>
218 </table>
219 </center>
220 </body>
221 </html>
59177427 222 <?php
052e0c26 223 session_destroy();
224 exit;
052e0c26 225 }
226 } else {
227 exit;
228 }
229 }
230
231 return $imap_stream;
232 }
233
234
235
236
237 /******************************************************************************
238 ** Simply logs out the imap session
239 ******************************************************************************/
240 function sqimap_logout ($imap_stream) {
241 fputs ($imap_stream, "a001 LOGOUT\r\n");
242 }
243
f435778e 244 function sqimap_capability($imap_stream, $capability) {
a3db804c 245 global $sqimap_capabilities;
246 global $imap_general_debug;
052e0c26 247
a3db804c 248 if (!is_array($sqimap_capabilities)) {
249 fputs ($imap_stream, "a001 CAPABILITY\r\n");
74424a43 250 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
a3db804c 251
252 $c = explode(' ', $read[0]);
253 for ($i=2; $i < count($c); $i++) {
245a6892 254 $cap_list = explode('=', $c[$i]);
255 if (isset($cap_list[1]))
256 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
257 else
258 $sqimap_capabilities[$cap_list[0]] = TRUE;
a3db804c 259 }
260 }
bb7bf119 261 if (! isset($sqimap_capabilities[$capability]))
262 return false;
a3db804c 263 return $sqimap_capabilities[$capability];
264}
052e0c26 265
266 /******************************************************************************
267 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
268 ******************************************************************************/
f435778e 269 function sqimap_get_delimiter ($imap_stream = false) {
270 global $imap_general_debug;
271 global $sqimap_delimiter;
272 global $optional_delimiter;
f6941f00 273
f435778e 274 /* Use configured delimiter if set */
275 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
276 return $optional_delimiter;
a3db804c 277
f435778e 278 /* Do some caching here */
279 if (!$sqimap_delimiter) {
a3db804c 280 if (sqimap_capability($imap_stream, "NAMESPACE")) {
281 /* According to something that I can't find, this is supposed to work on all systems
282 OS: This won't work in Courier IMAP.
283 OS: According to rfc2342 response from NAMESPACE command is:
284 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
285 OS: We want to lookup all personal NAMESPACES...
286 */
287 fputs ($imap_stream, "a001 NAMESPACE\r\n");
74424a43 288 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
8e8ebd27 289 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
290 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
a3db804c 291 $pn = $data2[1];
292 $pna = explode(')(', $pn);
293 while (list($k, $v) = each($pna))
294 {
862ff2d3 295 $lst = explode('"', $v);
296 if (isset($lst[3])) {
297 $pn[$lst[1]] = $lst[3];
298 } else {
74424a43 299 $pn[$lst[1]] = '';
862ff2d3 300 }
a3db804c 301 }
a3db804c 302 }
303 $sqimap_delimiter = $pn[0];
40ba4d36 304 } else {
a3db804c 305 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
74424a43 306 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
307 $quote_position = strpos ($read[0], '"');
a3db804c 308 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
40ba4d36 309 }
a3db804c 310 }
311 return $sqimap_delimiter;
f435778e 312 }
052e0c26 313
314
315 /******************************************************************************
316 ** Gets the number of messages in the current mailbox.
317 ******************************************************************************/
318 function sqimap_get_num_messages ($imap_stream, $mailbox) {
319 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
74424a43 320 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
052e0c26 321 for ($i = 0; $i < count($read_ary); $i++) {
441f2d33 322 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
323 return $regs[1];
052e0c26 324 }
325 }
441f2d33 326 return "BUG! Couldn't get number of messages in $mailbox!";
052e0c26 327 }
328
329
330 /******************************************************************************
331 ** Returns a displayable email address
332 ******************************************************************************/
333 function sqimap_find_email ($string) {
334 /** Luke Ehresman <lehresma@css.tayloru.edu>
335 ** <lehresma@css.tayloru.edu>
336 ** lehresma@css.tayloru.edu
1d2ce1c3 337 **
338 ** What about
339 ** lehresma@css.tayloru.edu (Luke Ehresman)
052e0c26 340 **/
341
441f2d33 342 if (ereg("<([^>]+)>", $string, $regs)) {
343 $string = $regs[1];
052e0c26 344 }
345 return trim($string);
346 }
347
348
349 /******************************************************************************
350 ** Takes the From: field, and creates a displayable name.
351 ** Luke Ehresman <lkehresman@yahoo.com>
352 ** becomes: Luke Ehresman
353 ** <lkehresman@yahoo.com>
354 ** becomes: lkehresman@yahoo.com
355 ******************************************************************************/
356 function sqimap_find_displayable_name ($string) {
74424a43 357 $string = ' '.trim($string);
1d2ce1c3 358 $orig_string = $string;
74424a43 359 if (strpos($string, '<') && strpos($string, '>')) {
360 if (strpos($string, '<') == 1) {
1d2ce1c3 361 $string = sqimap_find_email($string);
362 } else {
363 $string = trim($string);
74424a43 364 $string = substr($string, 0, strpos($string, '<'));
365 $string = ereg_replace ('"', '', $string);
1d2ce1c3 366 }
367
74424a43 368 if (trim($string) == '') {
1d2ce1c3 369 $string = sqimap_find_email($orig_string);
370 }
371 }
372 return $string;
052e0c26 373 }
374
375
052e0c26 376 /******************************************************************************
377 ** Returns the number of unseen messages in this folder
378 ******************************************************************************/
4d2c01e3 379 function sqimap_unseen_messages ($imap_stream, $mailbox) {
c5eb2c03 380 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
381 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
74424a43 382 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
441f2d33 383 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
384 return $regs[1];
052e0c26 385 }
386
387
388 /******************************************************************************
389 ** Saves a message to a given folder -- used for saving sent messages
390 ******************************************************************************/
391 function sqimap_append ($imap_stream, $sent_folder, $length) {
392 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
393 $tmp = fgets ($imap_stream, 1024);
394 }
395
396 function sqimap_append_done ($imap_stream) {
397 fputs ($imap_stream, "\r\n");
398 $tmp = fgets ($imap_stream, 1024);
399 }
400?>