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):
91 $fetch_data = array();
92 $fetch_data[] = $read;
93 $read = sqimap_fgets($imap_stream);
94 while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) &&
95 !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) {
96 $fetch_data[] = $read;
97 $read = sqimap_fgets($imap_stream);
99 $resultlist[] = $fetch_data;
103 $read = sqimap_fgets($imap_stream);
108 $resultlist[] = $data;
110 elseif (empty($resultlist)) {
111 $resultlist[] = array();
113 if ($handle_errors == false) {
114 return( $resultlist );
116 elseif ($response == 'NO') {
117 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
118 if (strstr($message, 'command resulted in') === false) {
119 set_up_language($squirrelmail_language);
120 echo "<br><b><font color=$color[2]>\n" .
121 _("ERROR : Could not complete request.") .
125 _("Reason Given: ") .
126 $message . "</font><br>\n";
130 elseif ($response == 'BAD') {
131 set_up_language($squirrelmail_language);
132 echo "<br><b><font color=$color[2]>\n" .
133 _("ERROR : Bad or malformed request.") .
137 _("Server responded: ") .
138 $message . "</font><br>\n";
146 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
147 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
152 * Logs the user into the imap server. If $hide is set, no error messages
153 * will be displayed. This function returns the imap connection handle.
155 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
156 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
158 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
159 if ( !$imap_stream ) {
162 $server_info = fgets ($imap_stream, 1024);
164 /* Decrypt the password */
165 $password = OneTimePadDecrypt($password, $onetimepad);
167 /* Do some error correction */
170 set_up_language($squirrelmail_language, true);
171 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
172 echo "$error_number : $error_string<br>\r\n";
177 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
178 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
180 /* If the connection was not successful, lets see why */
181 if ($response != 'OK') {
183 if ($response != 'NO') {
184 /* "BAD" and anything else gets reported here. */
185 set_up_language($squirrelmail_language, true);
186 if ($response == 'BAD') {
187 printf (_("Bad request: %s")."<br>\r\n", $message);
189 printf (_("Unknown error: %s") . "<br>\n", $message);
191 echo '<br>' . _("Read data:") . "<br>\n";
192 if (is_array($read)) {
193 foreach ($read as $line) {
194 echo htmlspecialchars($line) . "<br>\n";
200 * If the user does not log in with the correct
201 * username and password it is not possible to get the
202 * correct locale from the user's preferences.
203 * Therefore, apply the same hack as on the login
206 * $squirrelmail_language is set by a cookie when
207 * the user selects language and logs out
210 set_up_language($squirrelmail_language, true);
211 include_once( '../functions/display_messages.php' );
212 logout_error( _("Unknown user or password incorrect.") );
223 /* Simply logs out the IMAP session */
224 function sqimap_logout ($imap_stream) {
225 /* Logout is not valid until the server returns 'BYE' */
226 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
229 function sqimap_capability($imap_stream, $capability='') {
230 global $sqimap_capabilities;
231 if (!is_array($sqimap_capabilities)) {
232 $read = sqimap_run_command($imap_stream, 'CAPABILITY', 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;
245 if (isset($sqimap_capabilities[$capability])) {
246 return $sqimap_capabilities[$capability];
251 return $sqimap_capabilities;
254 /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
255 function sqimap_get_delimiter ($imap_stream = false) {
256 global $sqimap_delimiter, $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')) {
267 * According to something that I can't find, this is supposed to work on all systems
268 * OS: This won't work in Courier IMAP.
269 * OS: According to rfc2342 response from NAMESPACE command is:
270 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
271 * OS: We want to lookup all personal NAMESPACES...
273 $read = sqimap_run_command($imap_stream, 'NAMESPACE', 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;
300 /* Gets the number of messages in the current mailbox. */
301 function sqimap_get_num_messages ($imap_stream, $mailbox) {
302 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
303 for ($i = 0; $i < count($read_ary); $i++
) {
304 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
308 return "BUG! Couldn't get number of messages in $mailbox!";
312 /* Returns a displayable email address.
313 * Luke Ehresman <lehresma@css.tayloru.edu>
314 * "Luke Ehresman" <lehresma@css.tayloru.edu>
315 * <lehresma@css.tayloru.edu>
316 * lehresma@css.tayloru.edu (Luke Ehresman)
317 * lehresma@css.tayloru.edu
318 * becomes: lehresma@css.tayloru.edu
320 function sqimap_find_email ($string) {
321 if (ereg("<([^>]+)>", $string, $regs)) {
323 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
326 return trim($string);
331 * Takes the From: field and creates a displayable name.
332 * Luke Ehresman <lkehresman@yahoo.com>
333 * "Luke Ehresman" <lkehresman@yahoo.com>
334 * lkehresman@yahoo.com (Luke Ehresman)
335 * becomes: Luke Ehresman
336 * <lkehresman@yahoo.com>
337 * becomes: lkehresman@yahoo.com
339 function sqimap_find_displayable_name ($string) {
340 $string = trim($string);
342 if ( ereg('^(.+)<.*>', $string, $regs) ) {
343 $orig_string = $string;
344 $string = str_replace ('"', '', $regs[1] );
345 if (trim($string) == '') {
346 $string = sqimap_find_email($orig_string);
348 if( $string == '' ||
$string == ' ' ){
352 elseif ( ereg('\((.*)\)', $string, $regs) ) {
353 if( ( $regs[1] == '' ) ||
( $regs[1] == ' ' ) ){
354 if ( ereg('^(.+) \(', $string, $regs) ) {
355 $string = ereg_replace( ' \(\)$', '', $string );
364 $string = str_replace ('"', '', sqimap_find_email($string));
367 return trim($string);
371 * Returns the number of unseen messages in this folder
373 function sqimap_unseen_messages ($imap_stream, $mailbox) {
374 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
376 while (isset($read_ary[$i])) {
377 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
387 * Saves a message to a given folder -- used for saving sent messages
389 function sqimap_append ($imap_stream, $sent_folder, $length) {
390 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
391 $tmp = fgets ($imap_stream, 1024);
394 function sqimap_append_done ($imap_stream) {
395 fputs ($imap_stream, "\r\n");
396 $tmp = fgets ($imap_stream, 1024);