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');
16 global $sqimap_session_id;
17 $sqimap_session_id = 1;
19 /* Sets an unique session id in order to avoid simultanous sessions crash. */
20 function sqimap_session_id($unique_id = false) {
21 global $data_dir, $username, $sqimap_session_id;
23 return( sprintf("A%03d", $sqimap_session_id++
) );
25 return( sprintf("A%03d", $sqimap_session_id++
) . ' UID' );
30 * Both send a command and accept the result from the command.
31 * This is to allow proper session number handling.
33 function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
34 $sid = sqimap_session_id($unique_id);
35 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
36 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
40 function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
41 $sid = sqimap_session_id($unique_id);
42 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
43 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
49 * custom fgets function. gets a line from IMAP
50 * no matter how big it may be
53 function sqimap_fgets($imap_stream) {
57 while (strpos($read, "\n") === false) {
58 if (!($read = fgets($imap_stream, $buffer))) {
67 * Reads the output from the IMAP stream. If handle_errors is set to true,
68 * this will also handle all errors that are received. If it is not set,
69 * the errors will be sent back through $response and $message
72 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
73 global $color, $squirrelmail_language;
75 $pre_a = explode(' ',trim($pre));
77 $resultlist = array();
79 $read = sqimap_fgets($imap_stream);
82 case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs):
83 case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs):
85 $message = trim($regs[2]);
87 case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read):
88 $read = sqimap_fgets($imap_stream);
90 case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs):
91 $fetch_data = array();
92 $fetch_data[] = $read;
93 $read = sqimap_fgets($imap_stream);
95 while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) &&
96 !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) {
97 $fetch_data[] = $read;
98 $read = sqimap_fgets($imap_stream);
100 $resultlist[] = $fetch_data;
104 $read = sqimap_fgets($imap_stream);
109 $resultlist[] = $data;
111 elseif (empty($resultlist)) {
112 $resultlist[] = array();
114 if ($handle_errors == false) {
115 return( $resultlist );
117 elseif ($response == 'NO') {
118 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
119 if (strstr($message, 'command resulted in') === false) {
120 set_up_language($squirrelmail_language);
121 echo "<br><b><font color=$color[2]>\n" .
122 _("ERROR : Could not complete request.") .
126 _("Reason Given: ") .
127 $message . "</font><br>\n";
131 elseif ($response == 'BAD') {
132 set_up_language($squirrelmail_language);
133 echo "<br><b><font color=$color[2]>\n" .
134 _("ERROR : Bad or malformed request.") .
138 _("Server responded: ") .
139 $message . "</font><br>\n";
147 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
148 global $color, $squirrelmail_language;
150 $pre_a = explode(' ',trim($pre));
154 $read = sqimap_fgets($imap_stream);
157 case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs):
158 case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs):
159 $response = $regs[1];
160 $message = trim($regs[2]);
162 case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read):
163 $read = sqimap_fgets($imap_stream);
165 case preg_match('/^\* [0-9]+ FETCH.*/', $read):
166 $fetch_data = array();
167 $fetch_data[] = $read;
168 $read = sqimap_fgets($imap_stream);
169 while (!preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) {
170 $fetch_data[] = $read;
171 $read = sqimap_fgets($imap_stream);
173 $result = $fetch_data;
177 $read = sqimap_fgets($imap_stream);
184 if ($handle_errors == false) {
187 elseif ($response == 'NO') {
188 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
189 if (strstr($message, 'command resulted in') === false) {
190 set_up_language($squirrelmail_language);
191 echo "<br><b><font color=$color[2]>\n" .
192 _("ERROR : Could not complete request.") .
196 _("Reason Given: ") .
197 $message . "</font><br>\n";
201 elseif ($response == 'BAD') {
202 set_up_language($squirrelmail_language);
203 echo "<br><b><font color=$color[2]>\n" .
204 _("ERROR : Bad or malformed request.") .
208 _("Server responded: ") .
209 $message . "</font><br>\n";
219 * Logs the user into the imap server. If $hide is set, no error messages
220 * will be displayed. This function returns the imap connection handle.
222 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
223 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
225 $imap_server_address = sqimap_get_user_server($imap_server_address, $username);
227 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
228 if ( !$imap_stream ) {
231 $server_info = fgets ($imap_stream, 1024);
233 /* Decrypt the password */
234 $password = OneTimePadDecrypt($password, $onetimepad);
236 /* Do some error correction */
239 set_up_language($squirrelmail_language, true);
240 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
241 echo "$error_number : $error_string<br>\r\n";
246 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
247 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
249 /* If the connection was not successful, lets see why */
250 if ($response != 'OK') {
252 if ($response != 'NO') {
253 /* "BAD" and anything else gets reported here. */
254 set_up_language($squirrelmail_language, true);
255 if ($response == 'BAD') {
256 printf (_("Bad request: %s")."<br>\r\n", $message);
258 printf (_("Unknown error: %s") . "<br>\n", $message);
260 echo '<br>' . _("Read data:") . "<br>\n";
261 if (is_array($read)) {
262 foreach ($read as $line) {
263 echo htmlspecialchars($line) . "<br>\n";
269 * If the user does not log in with the correct
270 * username and password it is not possible to get the
271 * correct locale from the user's preferences.
272 * Therefore, apply the same hack as on the login
275 * $squirrelmail_language is set by a cookie when
276 * the user selects language and logs out
279 set_up_language($squirrelmail_language, true);
280 include_once( '../functions/display_messages.php' );
281 logout_error( _("Unknown user or password incorrect.") );
292 /* Simply logs out the IMAP session */
293 function sqimap_logout ($imap_stream) {
294 /* Logout is not valid until the server returns 'BYE' */
295 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
298 function sqimap_capability($imap_stream, $capability='') {
299 global $sqimap_capabilities;
300 if (!is_array($sqimap_capabilities)) {
301 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
303 $c = explode(' ', $read[0]);
304 for ($i=2; $i < count($c); $i++
) {
305 $cap_list = explode('=', $c[$i]);
306 if (isset($cap_list[1])) {
307 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
309 $sqimap_capabilities[$cap_list[0]] = TRUE;
314 if (isset($sqimap_capabilities[$capability])) {
315 return $sqimap_capabilities[$capability];
320 return $sqimap_capabilities;
323 /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
324 function sqimap_get_delimiter ($imap_stream = false) {
325 global $sqimap_delimiter, $optional_delimiter;
327 /* Use configured delimiter if set */
328 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
329 return $optional_delimiter;
332 /* Do some caching here */
333 if (!$sqimap_delimiter) {
334 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
336 * According to something that I can't find, this is supposed to work on all systems
337 * OS: This won't work in Courier IMAP.
338 * OS: According to rfc2342 response from NAMESPACE command is:
339 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
340 * OS: We want to lookup all personal NAMESPACES...
342 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
343 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
344 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
347 $pna = explode(')(', $pn);
348 while (list($k, $v) = each($pna)) {
349 $lst = explode('"', $v);
350 if (isset($lst[3])) {
351 $pn[$lst[1]] = $lst[3];
357 $sqimap_delimiter = $pn[0];
359 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
360 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
361 $quote_position = strpos ($read[0], '"');
362 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
365 return $sqimap_delimiter;
369 /* Gets the number of messages in the current mailbox. */
370 function sqimap_get_num_messages ($imap_stream, $mailbox) {
371 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
372 for ($i = 0; $i < count($read_ary); $i++
) {
373 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
377 return "BUG! Couldn't get number of messages in $mailbox!";
381 /* Returns a displayable email address.
382 * Luke Ehresman <lehresma@css.tayloru.edu>
383 * "Luke Ehresman" <lehresma@css.tayloru.edu>
384 * <lehresma@css.tayloru.edu>
385 * lehresma@css.tayloru.edu (Luke Ehresman)
386 * lehresma@css.tayloru.edu
387 * becomes: lehresma@css.tayloru.edu
389 function sqimap_find_email ($string) {
390 if (ereg("<([^>]+)>", $string, $regs)) {
392 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
395 return trim($string);
400 * Takes the From: field and creates a displayable name.
401 * Luke Ehresman <lkehresman@yahoo.com>
402 * "Luke Ehresman" <lkehresman@yahoo.com>
403 * lkehresman@yahoo.com (Luke Ehresman)
404 * becomes: Luke Ehresman
405 * <lkehresman@yahoo.com>
406 * becomes: lkehresman@yahoo.com
408 function sqimap_find_displayable_name ($string) {
409 $string = trim($string);
411 if ( ereg('^(.+)<.*>', $string, $regs) ) {
412 $orig_string = $string;
413 $string = str_replace ('"', '', $regs[1] );
414 if (trim($string) == '') {
415 $string = sqimap_find_email($orig_string);
417 if( $string == '' ||
$string == ' ' ){
421 elseif ( ereg('\((.*)\)', $string, $regs) ) {
422 if( ( $regs[1] == '' ) ||
( $regs[1] == ' ' ) ){
423 if ( ereg('^(.+) \(', $string, $regs) ) {
424 $string = ereg_replace( ' \(\)$', '', $string );
433 $string = str_replace ('"', '', sqimap_find_email($string));
436 return trim($string);
440 * Returns the number of unseen messages in this folder
442 function sqimap_unseen_messages ($imap_stream, $mailbox) {
443 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
445 while (isset($read_ary[$i])) {
446 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
456 * Saves a message to a given folder -- used for saving sent messages
458 function sqimap_append ($imap_stream, $sent_folder, $length) {
459 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
460 $tmp = fgets ($imap_stream, 1024);
463 function sqimap_append_done ($imap_stream) {
464 fputs ($imap_stream, "\r\n");
465 $tmp = fgets ($imap_stream, 1024);
468 function sqimap_get_user_server ($imap_server, $username) {
470 if (substr($imap_server, 0, 4) != "map:") {
474 $function = substr($imap_server, 4);
475 return $function($username);
478 /* This is an example that gets imapservers from yellowpages (NIS).
479 * you can simple put map:map_yp_alias in your $imap_server_address
480 * in config.php use your own function instead map_yp_alias to map your
481 * LDAP whatever way to find the users imapserver. */
483 function map_yp_alias($username) {
484 $yp = `ypmatch
$username aliases`
;
485 return chop(substr($yp, strlen($username)+
1));