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
24 function sqimap_session_id() {
26 global $data_dir, $username;
28 $IMAPSessionID = substr(session_id(), -4);
29 if( $IMAPSessionID == '' ) {
30 $IMAPSessionID = 'A001';
33 return( $IMAPSessionID );
37 /******************************************************************************
38 ** Reads the output from the IMAP stream. If handle_errors is set to true,
39 ** this will also handle all errors that are received. If it is not set,
40 ** the errors will be sent back through $response and $message
41 ******************************************************************************/
43 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
44 &$response, &$message) {
45 global $color, $squirrelmail_language;
48 $resultlist = array();
54 while (strpos($read, "\n") === false) {
55 $read .= fgets($imap_stream, 9096);
58 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
60 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
61 // Sizeless response, probably single-line
64 $read = fgets($imap_stream, 9096);
69 while (strpos($read, "\n") === false) {
70 $read .= fgets($imap_stream, 9096);
72 // If we know the size, no need to look at the end parameters
74 if ($total_size == $size) {
75 // We've reached the end of this 'message', switch to the next one.
78 } else if ($total_size > $size) {
79 $difference = $total_size - $size;
80 $total_size = $total_size - strlen($read);
81 $data[] = substr ($read, 0, strlen($read)-$difference);
82 $read = substr ($read, strlen($read)-$difference, strlen($read));
86 $read = fgets($imap_stream, 9096);
87 while (strpos($read, "\n") === false) {
88 $read .= fgets($imap_stream, 9096);
91 $total_size +
= strlen($read);
93 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
94 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
98 $read = fgets ($imap_stream, 9096);
103 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
104 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
105 $read = fgets($imap_stream, 9096);
107 $resultlist[] = $data;
109 $response = $regs[1];
110 $message = trim($regs[2]);
112 if ($handle_errors == false) { return $resultlist; }
114 if ($response == 'NO') {
115 // ignore this error from m$ exchange, it is not fatal (aka bug)
116 if (strstr($message, 'command resulted in') === false) {
117 set_up_language($squirrelmail_language);
118 echo "<br><b><font color=$color[2]>\n" .
119 _("ERROR : Could not complete request.") .
121 _("Reason Given: ") .
122 $message . "</font><br>\n";
125 } else if ($response == 'BAD') {
126 set_up_language($squirrelmail_language);
127 echo "<br><b><font color=$color[2]>\n" .
128 _("ERROR : Bad or malformed request.") .
130 _("Server responded: ") .
131 $message . "</font><br>\n";
137 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
139 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
144 /******************************************************************************
145 ** Logs the user into the imap server. If $hide is set, no error messages
146 ** will be displayed. This function returns the imap connection handle.
147 ******************************************************************************/
148 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
150 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
152 $imap_stream = fsockopen ( $imap_server_address, $imap_port,
153 $error_number, $error_string, 15);
154 if ( !$imap_stream ) {
157 $server_info = fgets ($imap_stream, 1024);
159 // Decrypt the password
160 $password = OneTimePadDecrypt($password, $onetimepad);
162 /** Do some error correction **/
165 set_up_language($squirrelmail_language, true);
166 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
167 echo "$error_number : $error_string<br>\r\n";
172 fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
173 '" "' . quoteIMAP($password) . "\"\r\n");
174 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
176 /** If the connection was not successful, lets see why **/
177 if ($response != 'OK') {
179 if ($response != 'NO') {
180 // "BAD" and anything else gets reported here.
181 set_up_language($squirrelmail_language, true);
182 if ($response == 'BAD') {
183 printf (_("Bad request: %s")."<br>\r\n", $message);
185 printf (_("Unknown error: %s") . "<br>\n", $message);
187 echo '<br>' . _("Read data:") . "<br>\n";
188 if (is_array($read)) {
189 foreach ($read as $line) {
190 echo htmlspecialchars($line) . "<br>\n";
195 // If the user does not log in with the correct
196 // username and password it is not possible to get the
197 // correct locale from the user's preferences.
198 // Therefore, apply the same hack as on the login
201 // $squirrelmail_language is set by a cookie when
202 // the user selects language and logs out
204 set_up_language($squirrelmail_language, true);
206 displayHtmlHeader( _("Unknown user or password incorrect.") );
207 echo "<body bgcolor=\"#ffffff\">\n";
208 error_username_password_incorrect();
221 * Simply logs out the imap session
223 function sqimap_logout ($imap_stream) {
224 fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
227 function sqimap_capability($imap_stream, $capability) {
228 global $sqimap_capabilities;
230 if (!is_array($sqimap_capabilities)) {
231 fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
232 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
234 $c = explode(' ', $read[0]);
235 for ($i=2; $i < count($c); $i++
) {
236 $cap_list = explode('=', $c[$i]);
237 if (isset($cap_list[1]))
238 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
240 $sqimap_capabilities[$cap_list[0]] = TRUE;
243 if (! isset($sqimap_capabilities[$capability])) {
246 return $sqimap_capabilities[$capability];
250 /******************************************************************************
251 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
252 ******************************************************************************/
253 function sqimap_get_delimiter ($imap_stream = false) {
255 global $sqimap_delimiter;
256 global $optional_delimiter;
258 /* Use configured delimiter if set */
259 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
260 return $optional_delimiter;
263 /* Do some caching here */
264 if (!$sqimap_delimiter) {
265 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
266 /* According to something that I can't find, this is supposed to work on all systems
267 OS: This won't work in Courier IMAP.
268 OS: According to rfc2342 response from NAMESPACE command is:
269 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
270 OS: We want to lookup all personal NAMESPACES...
272 fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
273 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
274 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
275 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
278 $pna = explode(')(', $pn);
279 while (list($k, $v) = each($pna)) {
280 $lst = explode('"', $v);
281 if (isset($lst[3])) {
282 $pn[$lst[1]] = $lst[3];
288 $sqimap_delimiter = $pn[0];
290 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
291 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
292 $quote_position = strpos ($read[0], '"');
293 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
296 return $sqimap_delimiter;
301 * Gets the number of messages in the current mailbox.
303 function sqimap_get_num_messages ($imap_stream, $mailbox) {
305 fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
306 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
307 for ($i = 0; $i < count($read_ary); $i++
) {
308 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
312 return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox );
318 * Returns a displayable email address
320 function sqimap_find_email ($string) {
321 /** Luke Ehresman <lehresma@css.tayloru.edu>
322 ** <lehresma@css.tayloru.edu>
323 ** lehresma@css.tayloru.edu
326 ** lehresma@css.tayloru.edu (Luke Ehresman)
329 if (ereg("<([^>]+)>", $string, $regs)) {
332 return trim($string);
337 * Takes the From: field, and creates a displayable name.
338 * Luke Ehresman <lkehresman@yahoo.com>
339 * "Luke Ehresman" <lkehresman@yahoo.com>
340 * lkehresman@yahoo.com (Luke Ehresman)
341 * become: Luke Ehresman
342 * <lkehresman@yahoo.com>
343 * becomes: lkehresman@yahoo.com
345 function sqimap_find_displayable_name ($string) {
346 $string = trim($string);
348 if ( ereg('^(.+)<.*>', $string, $regs) ) {
349 $string = ereg_replace ('"', '', $regs[1] );
351 elseif ( ereg('\((.*)\)', $string, $regs) ) {
355 $string = sqimap_find_email($string);
358 return trim($string);
362 * Returns the number of unseen messages in this folder
364 function sqimap_unseen_messages ($imap_stream, $mailbox) {
365 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
366 fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
367 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
368 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
374 * Saves a message to a given folder -- used for saving sent messages
376 function sqimap_append ($imap_stream, $sent_folder, $length) {
377 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
378 $tmp = fgets ($imap_stream, 1024);
381 function sqimap_append_done ($imap_stream) {
382 fputs ($imap_stream, "\r\n");
383 $tmp = fgets ($imap_stream, 1024);