6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This implements all functions that do general imap functions.
14 require_once('../functions/page_header.php');
15 require_once('../functions/display_messages.php');
17 global $sqimap_session_id;
18 $sqimap_session_id = 1;
20 /* Sets an unique session id in order to avoid simultanous sessions crash. */
21 function sqimap_session_id() {
22 global $data_dir, $username, $sqimap_session_id;
23 return( sprintf("A%03d", $sqimap_session_id++
) );
27 * Both send a command and accept the result from the command.
28 * This is to allow proper session number handling.
30 function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) {
31 $sid = sqimap_session_id();
32 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
33 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
37 function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) {
38 $sid = sqimap_session_id();
39 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
40 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
46 * Reads the output from the IMAP stream. If handle_errors is set to true,
47 * this will also handle all errors that are received. If it is not set,
48 * the errors will be sent back through $response and $message
50 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
51 global $color, $squirrelmail_language;
55 $resultlist = array();
62 while (strpos($read, "\n") === false) {
63 if(!($read .= fgets($imap_stream, $bufsize))) {
68 /* if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { */
69 if (preg_match('/^\* [0-9]+ FETCH.*\{([0-9]+)\}/', $read, $regs)) {
71 /* } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) { */
72 } else if (preg_match('/^\* [0-9]+ FETCH/', $read, $regs)) {
73 /* Sizeless response, probably single-line */
76 $read = fgets($imap_stream, $bufsize);
81 while (strpos($read, "\n") === false) {
82 if(!($read .= fgets($imap_stream, $bufsize))) {
86 /* If we know the size, no need to look at the end parameters */
88 if ($total_size == $size) {
89 /* We've reached the end of this 'message', switch to the next one. */
92 } else if ($total_size > $size) {
93 $difference = $total_size - $size;
94 $total_size = $total_size - strlen($read);
95 $data[] = substr ($read, 0, strlen($read)-$difference);
96 $read = substr ($read, strlen($read)-$difference, strlen($read));
100 $read = fgets($imap_stream, $bufsize);
101 while (strpos($read, "\n") === false) {
102 $read .= fgets($imap_stream, $bufsize);
105 $total_size +
= strlen($read);
107 /* if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) || */
108 if (preg_match("/^$pre (OK|BAD|NO)(.*)/", $read, $regs) ||
109 /* (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) { */
110 (($size == -1) && preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs))) {
112 } else if ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
114 * This block has been added in order to avoid the problem
115 * caused by the * OK [PARSE] Missing parameter answer
116 * Please, replace it with a better parsing if you know how.
117 * This block has been updated by
118 * Seth E. Randall <sethr@missoulafcu.org>. Once we see
119 * one OK [PARSE line, we just go through and keep
120 * tossing them out until we get something different.
122 while ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
123 $read = fgets($imap_stream, $bufsize);
126 $read = fgets ($imap_stream, $bufsize);
127 } else if (preg_match('/^\* BYE \[ALERT\](.*)/', $read, $regs)) {
129 * It seems that the IMAP server has coughed up a lung
130 * and hung up the connection. Print any info we have
133 echo _("Please contact your system administrator and report the following error:") . "<br>\n";
138 $read = fgets ($imap_stream, $bufsize);
144 * while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
145 * !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
147 while (($more_msgs = !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs)) &&
148 !preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs)) {
149 $read = fgets($imap_stream, $bufsize);
151 $resultlist[] = $data;
154 $response = $regs[1];
155 $message = trim($regs[2]);
157 if ($handle_errors == false) {
158 return( $resultlist );
159 } else if ($response == 'NO') {
160 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
161 if (strstr($message, 'command resulted in') === false) {
162 set_up_language($squirrelmail_language);
163 echo "<br><b><font color=$color[2]>\n" .
164 _("ERROR : Could not complete request.") .
168 _("Reason Given: ") .
169 $message . "</font><br>\n";
172 } else if ($response == 'BAD') {
173 set_up_language($squirrelmail_language);
174 echo "<br><b><font color=$color[2]>\n" .
175 _("ERROR : Bad or malformed request.") .
179 _("Server responded: ") .
180 $message . "</font><br>\n";
187 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
188 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
193 * Logs the user into the imap server. If $hide is set, no error messages
194 * will be displayed. This function returns the imap connection handle.
196 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
197 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
199 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
200 if ( !$imap_stream ) {
203 $server_info = fgets ($imap_stream, 1024);
205 /* Decrypt the password */
206 $password = OneTimePadDecrypt($password, $onetimepad);
208 /* Do some error correction */
211 set_up_language($squirrelmail_language, true);
212 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
213 echo "$error_number : $error_string<br>\r\n";
218 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
219 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
221 /* If the connection was not successful, lets see why */
222 if ($response != 'OK') {
224 if ($response != 'NO') {
225 /* "BAD" and anything else gets reported here. */
226 set_up_language($squirrelmail_language, true);
227 if ($response == 'BAD') {
228 printf (_("Bad request: %s")."<br>\r\n", $message);
230 printf (_("Unknown error: %s") . "<br>\n", $message);
232 echo '<br>' . _("Read data:") . "<br>\n";
233 if (is_array($read)) {
234 foreach ($read as $line) {
235 echo htmlspecialchars($line) . "<br>\n";
241 * If the user does not log in with the correct
242 * username and password it is not possible to get the
243 * correct locale from the user's preferences.
244 * Therefore, apply the same hack as on the login
247 * $squirrelmail_language is set by a cookie when
248 * the user selects language and logs out
251 set_up_language($squirrelmail_language, true);
253 displayHtmlHeader( _("Unknown user or password incorrect.") );
254 echo "<body bgcolor=\"#ffffff\">\n";
255 error_username_password_incorrect();
266 /* Simply logs out the IMAP session */
267 function sqimap_logout ($imap_stream) {
268 /* Logout is not valid until the server returns 'BYE' */
269 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
272 function sqimap_capability($imap_stream, $capability) {
273 global $sqimap_capabilities;
275 if (!is_array($sqimap_capabilities)) {
276 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
278 $c = explode(' ', $read[0]);
279 for ($i=2; $i < count($c); $i++
) {
280 $cap_list = explode('=', $c[$i]);
281 if (isset($cap_list[1])) {
282 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
284 $sqimap_capabilities[$cap_list[0]] = TRUE;
288 if (isset($sqimap_capabilities[$capability])) {
289 return $sqimap_capabilities[$capability];
295 /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
296 function sqimap_get_delimiter ($imap_stream = false) {
297 global $sqimap_delimiter, $optional_delimiter;
299 /* Use configured delimiter if set */
300 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
301 return $optional_delimiter;
304 /* Do some caching here */
305 if (!$sqimap_delimiter) {
306 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
308 * According to something that I can't find, this is supposed to work on all systems
309 * OS: This won't work in Courier IMAP.
310 * OS: According to rfc2342 response from NAMESPACE command is:
311 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
312 * OS: We want to lookup all personal NAMESPACES...
314 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
315 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
316 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
319 $pna = explode(')(', $pn);
320 while (list($k, $v) = each($pna)) {
321 $lst = explode('"', $v);
322 if (isset($lst[3])) {
323 $pn[$lst[1]] = $lst[3];
329 $sqimap_delimiter = $pn[0];
331 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
332 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
333 $quote_position = strpos ($read[0], '"');
334 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
337 return $sqimap_delimiter;
341 /* Gets the number of messages in the current mailbox. */
342 function sqimap_get_num_messages ($imap_stream, $mailbox) {
343 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
344 for ($i = 0; $i < count($read_ary); $i++
) {
345 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
349 return "BUG! Couldn't get number of messages in $mailbox!";
353 /* Returns a displayable email address.
354 * Luke Ehresman <lehresma@css.tayloru.edu>
355 * "Luke Ehresman" <lehresma@css.tayloru.edu>
356 * <lehresma@css.tayloru.edu>
357 * lehresma@css.tayloru.edu (Luke Ehresman)
358 * lehresma@css.tayloru.edu
359 * becomes: lehresma@css.tayloru.edu
361 function sqimap_find_email ($string) {
362 if (ereg("<([^>]+)>", $string, $regs)) {
364 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
367 return trim($string);
372 * Takes the From: field and creates a displayable name.
373 * Luke Ehresman <lkehresman@yahoo.com>
374 * "Luke Ehresman" <lkehresman@yahoo.com>
375 * lkehresman@yahoo.com (Luke Ehresman)
376 * becomes: Luke Ehresman
377 * <lkehresman@yahoo.com>
378 * becomes: lkehresman@yahoo.com
380 function sqimap_find_displayable_name ($string) {
381 $string = trim($string);
383 if ( ereg('^(.+)<.*>', $string, $regs) ) {
384 $orig_string = $string;
385 $string = str_replace ('"', '', $regs[1] );
386 if (trim($string) == '') {
387 $string = sqimap_find_email($orig_string);
389 if( $string == '' ||
$string == ' ' ){
393 elseif ( ereg('\((.*)\)', $string, $regs) ) {
394 if( ( $regs[1] == '' ) ||
( $regs[1] == ' ' ) ){
395 if ( ereg('^(.+) \(', $string, $regs) ) {
396 $string = ereg_replace( ' \(\)$', '', $string );
405 $string = str_replace ('"', '', sqimap_find_email($string));
408 return trim($string);
412 * Returns the number of unseen messages in this folder
414 function sqimap_unseen_messages ($imap_stream, $mailbox) {
415 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
417 while (isset($read_ary[$i])) {
418 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
428 * Saves a message to a given folder -- used for saving sent messages
430 function sqimap_append ($imap_stream, $sent_folder, $length) {
431 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
432 $tmp = fgets ($imap_stream, 1024);
435 function sqimap_append_done ($imap_stream) {
436 fputs ($imap_stream, "\r\n");
437 $tmp = fgets ($imap_stream, 1024);