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