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');
20 * Sets an unique session id in order to avoid simultanous sessions crash.
22 * @return string a 4 chars unique string
25 global $sqimap_session_id;
26 $sqimap_session_id = 1;
27 function sqimap_session_id() {
29 global $data_dir, $username, $sqimap_session_id;
31 return( sprintf("A%03d", $sqimap_session_id++
) );
34 /******************************************************************************
35 ** Both send a command and accept the result from the command. This is
36 ** to allow proper session number handling.
37 ******************************************************************************/
39 function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) {
40 $sid = sqimap_session_id();
41 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
42 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message);
46 function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) {
47 $sid = sqimap_session_id();
48 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
49 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message);
54 /******************************************************************************
55 ** Reads the output from the IMAP stream. If handle_errors is set to true,
56 ** this will also handle all errors that are received. If it is not set,
57 ** the errors will be sent back through $response and $message
58 ******************************************************************************/
60 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
61 &$response, &$message) {
62 global $color, $squirrelmail_language;
65 $resultlist = array();
71 while (strpos($read, "\n") === false) {
72 if(!($read .= fgets($imap_stream, 9096))) {
77 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
79 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
80 // Sizeless response, probably single-line
83 $read = fgets($imap_stream, 9096);
88 while (strpos($read, "\n") === false) {
89 if(!($read .= fgets($imap_stream, 9096))) {
93 // If we know the size, no need to look at the end parameters
95 if ($total_size == $size) {
96 // We've reached the end of this 'message', switch to the next one.
99 } else if ($total_size > $size) {
100 $difference = $total_size - $size;
101 $total_size = $total_size - strlen($read);
102 $data[] = substr ($read, 0, strlen($read)-$difference);
103 $read = substr ($read, strlen($read)-$difference, strlen($read));
107 $read = fgets($imap_stream, 9096);
108 while (strpos($read, "\n") === false) {
109 $read .= fgets($imap_stream, 9096);
112 $total_size +
= strlen($read);
114 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
115 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
119 $read = fgets ($imap_stream, 9096);
124 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
125 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
126 $read = fgets($imap_stream, 9096);
128 $resultlist[] = $data;
130 $response = $regs[1];
131 $message = trim($regs[2]);
133 if ($handle_errors == false) { return $resultlist; }
135 if ($response == 'NO') {
136 // ignore this error from m$ exchange, it is not fatal (aka bug)
137 if (strstr($message, 'command resulted in') === false) {
138 set_up_language($squirrelmail_language);
139 echo "<br><b><font color=$color[2]>\n" .
140 _("ERROR : Could not complete request.") .
142 _("Reason Given: ") .
143 $message . "</font><br>\n";
146 } else if ($response == 'BAD') {
147 set_up_language($squirrelmail_language);
148 echo "<br><b><font color=$color[2]>\n" .
149 _("ERROR : Bad or malformed request.") .
151 _("Server responded: ") .
152 $message . "</font><br>\n";
158 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
160 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
165 /******************************************************************************
166 ** Logs the user into the imap server. If $hide is set, no error messages
167 ** will be displayed. This function returns the imap connection handle.
168 ******************************************************************************/
169 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
171 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
173 $imap_stream = fsockopen ( $imap_server_address, $imap_port,
174 $error_number, $error_string, 15);
175 if ( !$imap_stream ) {
178 $server_info = fgets ($imap_stream, 1024);
180 // Decrypt the password
181 $password = OneTimePadDecrypt($password, $onetimepad);
183 /** Do some error correction **/
186 set_up_language($squirrelmail_language, true);
187 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
188 echo "$error_number : $error_string<br>\r\n";
193 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
194 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
196 /** If the connection was not successful, lets see why **/
197 if ($response != 'OK') {
199 if ($response != 'NO') {
200 // "BAD" and anything else gets reported here.
201 set_up_language($squirrelmail_language, true);
202 if ($response == 'BAD') {
203 printf (_("Bad request: %s")."<br>\r\n", $message);
205 printf (_("Unknown error: %s") . "<br>\n", $message);
207 echo '<br>' . _("Read data:") . "<br>\n";
208 if (is_array($read)) {
209 foreach ($read as $line) {
210 echo htmlspecialchars($line) . "<br>\n";
215 /* If the user does not log in with the correct
216 * username and password it is not possible to get the
217 * correct locale from the user's preferences.
218 * Therefore, apply the same hack as on the login
222 /* $squirrelmail_language is set by a cookie when
223 * the user selects language and logs out
226 set_up_language($squirrelmail_language, true);
228 displayHtmlHeader( _("Unknown user or password incorrect.") );
229 echo "<body bgcolor=\"#ffffff\">\n";
230 error_username_password_incorrect();
243 * Simply logs out the imap session
245 function sqimap_logout ($imap_stream) {
246 /* Logout is not valid until the server returns 'BYE' */
247 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
250 function sqimap_capability($imap_stream, $capability) {
251 global $sqimap_capabilities;
253 if (!is_array($sqimap_capabilities)) {
254 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
256 $c = explode(' ', $read[0]);
257 for ($i=2; $i < count($c); $i++
) {
258 $cap_list = explode('=', $c[$i]);
259 if (isset($cap_list[1]))
260 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
262 $sqimap_capabilities[$cap_list[0]] = TRUE;
265 if (! isset($sqimap_capabilities[$capability])) {
268 return $sqimap_capabilities[$capability];
272 /******************************************************************************
273 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
274 ******************************************************************************/
275 function sqimap_get_delimiter ($imap_stream = false) {
277 global $sqimap_delimiter;
278 global $optional_delimiter;
280 /* Use configured delimiter if set */
281 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
282 return $optional_delimiter;
285 /* Do some caching here */
286 if (!$sqimap_delimiter) {
287 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
288 /* According to something that I can't find, this is supposed to work on all systems
289 OS: This won't work in Courier IMAP.
290 OS: According to rfc2342 response from NAMESPACE command is:
291 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
292 OS: We want to lookup all personal NAMESPACES...
294 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
295 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
296 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
299 $pna = explode(')(', $pn);
300 while (list($k, $v) = each($pna)) {
301 $lst = explode('"', $v);
302 if (isset($lst[3])) {
303 $pn[$lst[1]] = $lst[3];
309 $sqimap_delimiter = $pn[0];
311 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
312 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
313 $quote_position = strpos ($read[0], '"');
314 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
317 return $sqimap_delimiter;
322 * Gets the number of messages in the current mailbox.
324 function sqimap_get_num_messages ($imap_stream, $mailbox) {
326 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
327 for ($i = 0; $i < count($read_ary); $i++
) {
328 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
332 return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox );
338 * Returns a displayable email address
340 function sqimap_find_email ($string) {
341 /** Luke Ehresman <lehresma@css.tayloru.edu>
342 ** <lehresma@css.tayloru.edu>
343 ** lehresma@css.tayloru.edu
346 ** lehresma@css.tayloru.edu (Luke Ehresman)
349 if (ereg("<([^>]+)>", $string, $regs)) {
352 return trim($string);
357 * Takes the From: field, and creates a displayable name.
358 * Luke Ehresman <lkehresman@yahoo.com>
359 * "Luke Ehresman" <lkehresman@yahoo.com>
360 * lkehresman@yahoo.com (Luke Ehresman)
361 * becomes: Luke Ehresman
362 * <lkehresman@yahoo.com>
363 * becomes: lkehresman@yahoo.com
365 function sqimap_find_displayable_name ($string) {
366 $string = trim($string);
368 if ( ereg('^(.+)<.*>', $string, $regs) ) {
369 $orig_string = $string;
370 $string = str_replace ('"', '', $regs[1] );
371 if (trim($string) == '') {
372 $string = sqimap_find_email($orig_string);
374 if( $string == '' ||
$string == ' ' ){
378 elseif ( ereg('\((.*)\)', $string, $regs) ) {
379 if( ( $regs[1] == '' ) ||
( $regs[1] == ' ' ) ){
380 if ( ereg('^(.+) \(', $string, $regs) ) {
381 $string = ereg_replace( ' \(\)$', '', $string );
390 $string = str_replace ('"', '', sqimap_find_email($string));
393 return trim($string);
397 * Returns the number of unseen messages in this folder
399 function sqimap_unseen_messages ($imap_stream, $mailbox) {
400 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
401 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
402 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
408 * Saves a message to a given folder -- used for saving sent messages
410 function sqimap_append ($imap_stream, $sent_folder, $length) {
411 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
412 $tmp = fgets ($imap_stream, 1024);
415 function sqimap_append_done ($imap_stream) {
416 fputs ($imap_stream, "\r\n");
417 $tmp = fgets ($imap_stream, 1024);