7e482e2846e2d0222e13c7a05f78d40e1de2930c
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() {
21 global $data_dir, $username, $sqimap_session_id;
22 return( sprintf("A%03d", $sqimap_session_id++
) );
26 * Both send a command and accept the result from the command.
27 * This is to allow proper session number handling.
29 function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) {
30 $sid = sqimap_session_id();
31 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
32 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
36 function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) {
37 $sid = sqimap_session_id();
38 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
39 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
45 * Reads the output from the IMAP stream. If handle_errors is set to true,
46 * this will also handle all errors that are received. If it is not set,
47 * the errors will be sent back through $response and $message
49 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
50 global $color, $squirrelmail_language;
54 $resultlist = array();
61 while (strpos($read, "\n") === false) {
62 if(!($read .= fgets($imap_stream, $bufsize))) {
67 /* if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { */
68 if (preg_match('/^\* [0-9]+ FETCH.*\{([0-9]+)\}/', $read, $regs)) {
70 /* } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) { */
71 } else if (preg_match('/^\* [0-9]+ FETCH/', $read, $regs)) {
72 /* Sizeless response, probably single-line */
75 $read = fgets($imap_stream, $bufsize);
80 while (strpos($read, "\n") === false) {
81 if(!($read .= fgets($imap_stream, $bufsize))) {
85 /* If we know the size, no need to look at the end parameters */
87 if ($total_size == $size) {
88 /* We've reached the end of this 'message', switch to the next one. */
91 } else if ($total_size > $size) {
92 $difference = $total_size - $size;
93 $total_size = $total_size - strlen($read);
94 $data[] = substr ($read, 0, strlen($read)-$difference);
95 $read = substr ($read, strlen($read)-$difference, strlen($read));
99 $read = fgets($imap_stream, $bufsize);
100 while (strpos($read, "\n") === false) {
101 $read .= fgets($imap_stream, $bufsize);
104 $total_size +
= strlen($read);
106 /* if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) || */
107 if (preg_match("/^$pre (OK|BAD|NO)(.*)/", $read, $regs) ||
108 /* (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) { */
109 (($size == -1) && preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs))) {
111 } else if ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
113 * This block has been added in order to avoid the problem
114 * caused by the * OK [PARSE] Missing parameter answer
115 * Please, replace it with a better parsing if you know how.
116 * This block has been updated by
117 * Seth E. Randall <sethr@missoulafcu.org>. Once we see
118 * one OK [PARSE line, we just go through and keep
119 * tossing them out until we get something different.
121 while ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
122 $read = fgets($imap_stream, $bufsize);
125 $read = fgets ($imap_stream, $bufsize);
126 } else if (preg_match('/^\* BYE \[ALERT\](.*)/', $read, $regs)) {
128 * It seems that the IMAP server has coughed up a lung
129 * and hung up the connection. Print any info we have
132 echo _("Please contact your system administrator and report the following error:") . "<br>\n";
137 $read = fgets ($imap_stream, $bufsize);
143 * while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
144 * !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
146 while (($more_msgs = !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs)) &&
147 !preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs)) {
148 $read = fgets($imap_stream, $bufsize);
150 $resultlist[] = $data;
153 $response = $regs[1];
154 $message = trim($regs[2]);
156 if ($handle_errors == false) {
157 return( $resultlist );
158 } else if ($response == 'NO') {
159 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
160 if (strstr($message, 'command resulted in') === false) {
161 set_up_language($squirrelmail_language);
162 echo "<br><b><font color=$color[2]>\n" .
163 _("ERROR : Could not complete request.") .
167 _("Reason Given: ") .
168 $message . "</font><br>\n";
171 } else if ($response == 'BAD') {
172 set_up_language($squirrelmail_language);
173 echo "<br><b><font color=$color[2]>\n" .
174 _("ERROR : Bad or malformed request.") .
178 _("Server responded: ") .
179 $message . "</font><br>\n";
186 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
187 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
192 * Logs the user into the imap server. If $hide is set, no error messages
193 * will be displayed. This function returns the imap connection handle.
195 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
196 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
198 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
199 if ( !$imap_stream ) {
202 $server_info = fgets ($imap_stream, 1024);
204 /* Decrypt the password */
205 $password = OneTimePadDecrypt($password, $onetimepad);
207 /* Do some error correction */
210 set_up_language($squirrelmail_language, true);
211 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
212 echo "$error_number : $error_string<br>\r\n";
217 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
218 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
220 /* If the connection was not successful, lets see why */
221 if ($response != 'OK') {
223 if ($response != 'NO') {
224 /* "BAD" and anything else gets reported here. */
225 set_up_language($squirrelmail_language, true);
226 if ($response == 'BAD') {
227 printf (_("Bad request: %s")."<br>\r\n", $message);
229 printf (_("Unknown error: %s") . "<br>\n", $message);
231 echo '<br>' . _("Read data:") . "<br>\n";
232 if (is_array($read)) {
233 foreach ($read as $line) {
234 echo htmlspecialchars($line) . "<br>\n";
240 * If the user does not log in with the correct
241 * username and password it is not possible to get the
242 * correct locale from the user's preferences.
243 * Therefore, apply the same hack as on the login
246 * $squirrelmail_language is set by a cookie when
247 * the user selects language and logs out
250 set_up_language($squirrelmail_language, true);
251 include_once( '../functions/display_messages.php' );
252 logout_error( _("Unknown user or password incorrect.") );
263 /* Simply logs out the IMAP session */
264 function sqimap_logout ($imap_stream) {
265 /* Logout is not valid until the server returns 'BYE' */
266 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
269 function sqimap_capability($imap_stream, $capability) {
270 global $sqimap_capabilities;
272 if (!is_array($sqimap_capabilities)) {
273 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
275 $c = explode(' ', $read[0]);
276 for ($i=2; $i < count($c); $i++
) {
277 $cap_list = explode('=', $c[$i]);
278 if (isset($cap_list[1])) {
279 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
281 $sqimap_capabilities[$cap_list[0]] = TRUE;
285 if (isset($sqimap_capabilities[$capability])) {
286 return $sqimap_capabilities[$capability];
292 /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
293 function sqimap_get_delimiter ($imap_stream = false) {
294 global $sqimap_delimiter, $optional_delimiter;
296 /* Use configured delimiter if set */
297 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
298 return $optional_delimiter;
301 /* Do some caching here */
302 if (!$sqimap_delimiter) {
303 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
305 * According to something that I can't find, this is supposed to work on all systems
306 * OS: This won't work in Courier IMAP.
307 * OS: According to rfc2342 response from NAMESPACE command is:
308 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
309 * OS: We want to lookup all personal NAMESPACES...
311 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
312 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
313 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
316 $pna = explode(')(', $pn);
317 while (list($k, $v) = each($pna)) {
318 $lst = explode('"', $v);
319 if (isset($lst[3])) {
320 $pn[$lst[1]] = $lst[3];
326 $sqimap_delimiter = $pn[0];
328 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
329 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
330 $quote_position = strpos ($read[0], '"');
331 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
334 return $sqimap_delimiter;
338 /* Gets the number of messages in the current mailbox. */
339 function sqimap_get_num_messages ($imap_stream, $mailbox) {
340 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
341 for ($i = 0; $i < count($read_ary); $i++
) {
342 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
346 return "BUG! Couldn't get number of messages in $mailbox!";
350 /* Returns a displayable email address.
351 * Luke Ehresman <lehresma@css.tayloru.edu>
352 * "Luke Ehresman" <lehresma@css.tayloru.edu>
353 * <lehresma@css.tayloru.edu>
354 * lehresma@css.tayloru.edu (Luke Ehresman)
355 * lehresma@css.tayloru.edu
356 * becomes: lehresma@css.tayloru.edu
358 function sqimap_find_email ($string) {
359 if (ereg("<([^>]+)>", $string, $regs)) {
361 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
364 return trim($string);
369 * Takes the From: field and creates a displayable name.
370 * Luke Ehresman <lkehresman@yahoo.com>
371 * "Luke Ehresman" <lkehresman@yahoo.com>
372 * lkehresman@yahoo.com (Luke Ehresman)
373 * becomes: Luke Ehresman
374 * <lkehresman@yahoo.com>
375 * becomes: lkehresman@yahoo.com
377 function sqimap_find_displayable_name ($string) {
378 $string = trim($string);
380 if ( ereg('^(.+)<.*>', $string, $regs) ) {
381 $orig_string = $string;
382 $string = str_replace ('"', '', $regs[1] );
383 if (trim($string) == '') {
384 $string = sqimap_find_email($orig_string);
386 if( $string == '' ||
$string == ' ' ){
390 elseif ( ereg('\((.*)\)', $string, $regs) ) {
391 if( ( $regs[1] == '' ) ||
( $regs[1] == ' ' ) ){
392 if ( ereg('^(.+) \(', $string, $regs) ) {
393 $string = ereg_replace( ' \(\)$', '', $string );
402 $string = str_replace ('"', '', sqimap_find_email($string));
405 return trim($string);
409 * Returns the number of unseen messages in this folder
411 function sqimap_unseen_messages ($imap_stream, $mailbox) {
412 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
414 while (isset($read_ary[$i])) {
415 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
425 * Saves a message to a given folder -- used for saving sent messages
427 function sqimap_append ($imap_stream, $sent_folder, $length) {
428 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
429 $tmp = fgets ($imap_stream, 1024);
432 function sqimap_append_done ($imap_stream) {
433 fputs ($imap_stream, "\r\n");
434 $tmp = fgets ($imap_stream, 1024);