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_server_address = sqimap_get_user_server($imap_server_address, $username);
160 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
161 if ( !$imap_stream ) {
164 $server_info = fgets ($imap_stream, 1024);
166 /* Decrypt the password */
167 $password = OneTimePadDecrypt($password, $onetimepad);
169 /* Do some error correction */
172 set_up_language($squirrelmail_language, true);
173 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
174 echo "$error_number : $error_string<br>\r\n";
179 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
180 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
182 /* If the connection was not successful, lets see why */
183 if ($response != 'OK') {
185 if ($response != 'NO') {
186 /* "BAD" and anything else gets reported here. */
187 set_up_language($squirrelmail_language, true);
188 if ($response == 'BAD') {
189 printf (_("Bad request: %s")."<br>\r\n", $message);
191 printf (_("Unknown error: %s") . "<br>\n", $message);
193 echo '<br>' . _("Read data:") . "<br>\n";
194 if (is_array($read)) {
195 foreach ($read as $line) {
196 echo htmlspecialchars($line) . "<br>\n";
202 * If the user does not log in with the correct
203 * username and password it is not possible to get the
204 * correct locale from the user's preferences.
205 * Therefore, apply the same hack as on the login
208 * $squirrelmail_language is set by a cookie when
209 * the user selects language and logs out
212 set_up_language($squirrelmail_language, true);
213 include_once( '../functions/display_messages.php' );
214 logout_error( _("Unknown user or password incorrect.") );
225 /* Simply logs out the IMAP session */
226 function sqimap_logout ($imap_stream) {
227 /* Logout is not valid until the server returns 'BYE' */
228 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
231 function sqimap_capability($imap_stream, $capability='') {
232 global $sqimap_capabilities;
233 if (!is_array($sqimap_capabilities)) {
234 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
236 $c = explode(' ', $read[0]);
237 for ($i=2; $i < count($c); $i++
) {
238 $cap_list = explode('=', $c[$i]);
239 if (isset($cap_list[1])) {
240 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
242 $sqimap_capabilities[$cap_list[0]] = TRUE;
247 if (isset($sqimap_capabilities[$capability])) {
248 return $sqimap_capabilities[$capability];
253 return $sqimap_capabilities;
256 /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
257 function sqimap_get_delimiter ($imap_stream = false) {
258 global $sqimap_delimiter, $optional_delimiter;
260 /* Use configured delimiter if set */
261 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
262 return $optional_delimiter;
265 /* Do some caching here */
266 if (!$sqimap_delimiter) {
267 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
269 * According to something that I can't find, this is supposed to work on all systems
270 * OS: This won't work in Courier IMAP.
271 * OS: According to rfc2342 response from NAMESPACE command is:
272 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
273 * OS: We want to lookup all personal NAMESPACES...
275 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
276 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
277 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
280 $pna = explode(')(', $pn);
281 while (list($k, $v) = each($pna)) {
282 $lst = explode('"', $v);
283 if (isset($lst[3])) {
284 $pn[$lst[1]] = $lst[3];
290 $sqimap_delimiter = $pn[0];
292 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
293 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
294 $quote_position = strpos ($read[0], '"');
295 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
298 return $sqimap_delimiter;
302 /* Gets the number of messages in the current mailbox. */
303 function sqimap_get_num_messages ($imap_stream, $mailbox) {
304 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
305 for ($i = 0; $i < count($read_ary); $i++
) {
306 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
310 return "BUG! Couldn't get number of messages in $mailbox!";
314 /* Returns a displayable email address.
315 * Luke Ehresman <lehresma@css.tayloru.edu>
316 * "Luke Ehresman" <lehresma@css.tayloru.edu>
317 * <lehresma@css.tayloru.edu>
318 * lehresma@css.tayloru.edu (Luke Ehresman)
319 * lehresma@css.tayloru.edu
320 * becomes: lehresma@css.tayloru.edu
322 function sqimap_find_email ($string) {
323 if (ereg("<([^>]+)>", $string, $regs)) {
325 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
328 return trim($string);
333 * Takes the From: field and creates a displayable name.
334 * Luke Ehresman <lkehresman@yahoo.com>
335 * "Luke Ehresman" <lkehresman@yahoo.com>
336 * lkehresman@yahoo.com (Luke Ehresman)
337 * becomes: Luke Ehresman
338 * <lkehresman@yahoo.com>
339 * becomes: lkehresman@yahoo.com
341 function sqimap_find_displayable_name ($string) {
342 $string = trim($string);
344 if ( ereg('^(.+)<.*>', $string, $regs) ) {
345 $orig_string = $string;
346 $string = str_replace ('"', '', $regs[1] );
347 if (trim($string) == '') {
348 $string = sqimap_find_email($orig_string);
350 if( $string == '' ||
$string == ' ' ){
354 elseif ( ereg('\((.*)\)', $string, $regs) ) {
355 if( ( $regs[1] == '' ) ||
( $regs[1] == ' ' ) ){
356 if ( ereg('^(.+) \(', $string, $regs) ) {
357 $string = ereg_replace( ' \(\)$', '', $string );
366 $string = str_replace ('"', '', sqimap_find_email($string));
369 return trim($string);
373 * Returns the number of unseen messages in this folder
375 function sqimap_unseen_messages ($imap_stream, $mailbox) {
376 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
378 while (isset($read_ary[$i])) {
379 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
389 * Saves a message to a given folder -- used for saving sent messages
391 function sqimap_append ($imap_stream, $sent_folder, $length) {
392 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
393 $tmp = fgets ($imap_stream, 1024);
396 function sqimap_append_done ($imap_stream) {
397 fputs ($imap_stream, "\r\n");
398 $tmp = fgets ($imap_stream, 1024);
401 function sqimap_get_user_server ($imap_server, $username) {
403 if (substr($imap_server, 0, 4) != "map:") {
407 $function = substr($imap_server, 4);
408 return $function($username);
411 /* This is an example that gets imapservers from yellowpages (NIS).
412 * you can simple put map:map_yp_alias in your $imap_server_address
413 * in config.php use your own function instead map_yp_alias to map your
414 * LDAP whatever way to find the users imapserver. */
416 function map_yp_alias($username) {
417 $yp = `ypmatch
$username aliases`
;
418 return chop(substr($yp, strlen($username)+
1));