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, $query );
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, $query);
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
60 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
61 &$response, &$message, $query = '') {
62 global $color, $squirrelmail_language;
66 $resultlist = array();
73 while (strpos($read, "\n") === false) {
74 if(!($read .= fgets($imap_stream, $bufsize))) {
79 // if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
80 if (preg_match('/^\* [0-9]+ FETCH.*\{([0-9]+)\}/', $read, $regs)) {
82 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
83 // Sizeless response, probably single-line
86 $read = fgets($imap_stream, $bufsize);
91 while (strpos($read, "\n") === false) {
92 if(!($read .= fgets($imap_stream, $bufsize))) {
96 // If we know the size, no need to look at the end parameters
98 if ($total_size == $size) {
99 // We've reached the end of this 'message', switch to the next one.
102 } else if ($total_size > $size) {
103 $difference = $total_size - $size;
104 $total_size = $total_size - strlen($read);
105 $data[] = substr ($read, 0, strlen($read)-$difference);
106 $read = substr ($read, strlen($read)-$difference, strlen($read));
110 $read = fgets($imap_stream, $bufsize);
111 while (strpos($read, "\n") === false) {
112 $read .= fgets($imap_stream, $bufsize);
115 $total_size +
= strlen($read);
117 if (preg_match("/^$pre (OK|BAD|NO)(.*)/", $read, $regs) ||
118 // if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
119 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
121 } else if ( preg_match("/^\* OK \[PARSE.*/", $read, $regs ) ) {
123 This block has been added in order to avoid the problem
124 caused by the * OK [PARSE] Missing parameter answer
125 Please, replace it with a better parsing if you know how.
127 $read = fgets ($imap_stream, $bufsize);
129 $read = fgets ($imap_stream, $bufsize);
132 $read = fgets ($imap_stream, $bufsize);
137 // while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
138 // !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
139 while (($more_msgs = !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs)) &&
140 !preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs)) {
141 $read = fgets($imap_stream, $bufsize);
143 $resultlist[] = $data;
146 $response = $regs[1];
147 $message = trim($regs[2]);
149 if ($handle_errors == false) {
150 return( $resultlist );
151 } else if ($response == 'NO') {
152 // ignore this error from m$ exchange, it is not fatal (aka bug)
153 if (strstr($message, 'command resulted in') === false) {
154 set_up_language($squirrelmail_language);
155 echo "<br><b><font color=$color[2]>\n" .
156 _("ERROR : Could not complete request.") .
160 _("Reason Given: ") .
161 $message . "</font><br>\n";
164 } else if ($response == 'BAD') {
165 set_up_language($squirrelmail_language);
166 echo "<br><b><font color=$color[2]>\n" .
167 _("ERROR : Bad or malformed request.") .
171 _("Server responded: ") .
172 $message . "</font><br>\n";
175 return( $resultlist );
179 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
181 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
186 /******************************************************************************
187 ** Logs the user into the imap server. If $hide is set, no error messages
188 ** will be displayed. This function returns the imap connection handle.
189 ******************************************************************************/
190 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
192 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
194 $imap_stream = fsockopen ( $imap_server_address, $imap_port,
195 $error_number, $error_string, 15);
196 if ( !$imap_stream ) {
199 $server_info = fgets ($imap_stream, 1024);
201 // Decrypt the password
202 $password = OneTimePadDecrypt($password, $onetimepad);
204 /** Do some error correction **/
207 set_up_language($squirrelmail_language, true);
208 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
209 echo "$error_number : $error_string<br>\r\n";
214 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
215 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
217 /** If the connection was not successful, lets see why **/
218 if ($response != 'OK') {
220 if ($response != 'NO') {
221 // "BAD" and anything else gets reported here.
222 set_up_language($squirrelmail_language, true);
223 if ($response == 'BAD') {
224 printf (_("Bad request: %s")."<br>\r\n", $message);
226 printf (_("Unknown error: %s") . "<br>\n", $message);
228 echo '<br>' . _("Read data:") . "<br>\n";
229 if (is_array($read)) {
230 foreach ($read as $line) {
231 echo htmlspecialchars($line) . "<br>\n";
236 /* If the user does not log in with the correct
237 * username and password it is not possible to get the
238 * correct locale from the user's preferences.
239 * Therefore, apply the same hack as on the login
243 /* $squirrelmail_language is set by a cookie when
244 * the user selects language and logs out
247 set_up_language($squirrelmail_language, true);
249 displayHtmlHeader( _("Unknown user or password incorrect.") );
250 echo "<body bgcolor=\"#ffffff\">\n";
251 error_username_password_incorrect();
264 * Simply logs out the imap session
266 function sqimap_logout ($imap_stream) {
267 /* Logout is not valid until the server returns 'BYE' */
268 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
271 function sqimap_capability($imap_stream, $capability) {
272 global $sqimap_capabilities;
274 if (!is_array($sqimap_capabilities)) {
275 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
277 $c = explode(' ', $read[0]);
278 for ($i=2; $i < count($c); $i++
) {
279 $cap_list = explode('=', $c[$i]);
280 if (isset($cap_list[1]))
281 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
283 $sqimap_capabilities[$cap_list[0]] = TRUE;
286 if (! isset($sqimap_capabilities[$capability])) {
289 return $sqimap_capabilities[$capability];
293 /******************************************************************************
294 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
295 ******************************************************************************/
296 function sqimap_get_delimiter ($imap_stream = false) {
298 global $sqimap_delimiter;
299 global $optional_delimiter;
301 /* Use configured delimiter if set */
302 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
303 return $optional_delimiter;
306 /* Do some caching here */
307 if (!$sqimap_delimiter) {
308 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
309 /* According to something that I can't find, this is supposed to work on all systems
310 OS: This won't work in Courier IMAP.
311 OS: According to rfc2342 response from NAMESPACE command is:
312 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
313 OS: We want to lookup all personal NAMESPACES...
315 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
316 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
317 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
320 $pna = explode(')(', $pn);
321 while (list($k, $v) = each($pna)) {
322 $lst = explode('"', $v);
323 if (isset($lst[3])) {
324 $pn[$lst[1]] = $lst[3];
330 $sqimap_delimiter = $pn[0];
332 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
333 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
334 $quote_position = strpos ($read[0], '"');
335 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
338 return $sqimap_delimiter;
343 * Gets the number of messages in the current mailbox.
345 function sqimap_get_num_messages ($imap_stream, $mailbox) {
347 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
348 for ($i = 0; $i < count($read_ary); $i++
) {
349 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
353 return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox );
359 * Returns a displayable email address
361 function sqimap_find_email ($string) {
362 /** Luke Ehresman <lehresma@css.tayloru.edu>
363 ** <lehresma@css.tayloru.edu>
364 ** lehresma@css.tayloru.edu
367 ** lehresma@css.tayloru.edu (Luke Ehresman)
370 if (ereg("<([^>]+)>", $string, $regs)) {
373 return trim($string);
378 * Takes the From: field, and creates a displayable name.
379 * Luke Ehresman <lkehresman@yahoo.com>
380 * "Luke Ehresman" <lkehresman@yahoo.com>
381 * lkehresman@yahoo.com (Luke Ehresman)
382 * becomes: Luke Ehresman
383 * <lkehresman@yahoo.com>
384 * becomes: lkehresman@yahoo.com
386 function sqimap_find_displayable_name ($string) {
387 $string = trim($string);
389 if ( ereg('^(.+)<.*>', $string, $regs) ) {
390 $orig_string = $string;
391 $string = str_replace ('"', '', $regs[1] );
392 if (trim($string) == '') {
393 $string = sqimap_find_email($orig_string);
395 if( $string == '' ||
$string == ' ' ){
399 elseif ( ereg('\((.*)\)', $string, $regs) ) {
400 if( ( $regs[1] == '' ) ||
( $regs[1] == ' ' ) ){
401 if ( ereg('^(.+) \(', $string, $regs) ) {
402 $string = ereg_replace( ' \(\)$', '', $string );
411 $string = str_replace ('"', '', sqimap_find_email($string));
414 return trim($string);
418 * Returns the number of unseen messages in this folder
420 function sqimap_unseen_messages ($imap_stream, $mailbox) {
421 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
422 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
423 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
429 * Saves a message to a given folder -- used for saving sent messages
431 function sqimap_append ($imap_stream, $sent_folder, $length) {
432 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
433 $tmp = fgets ($imap_stream, 1024);
436 function sqimap_append_done ($imap_stream) {
437 fputs ($imap_stream, "\r\n");
438 $tmp = fgets ($imap_stream, 1024);