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