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