6 * Copyright (c) 1999-2001 The SquirrelMail Development 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 /*****************************************************************/
15 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17 /*** + Base level indent should begin at left margin, as ***/
18 /*** the function definition and comments start below. ***/
19 /*** + All identation should consist of four space blocks ***/
20 /*** + Tab characters are evil. ***/
21 /*** + all comments should use "slash-star ... star-slash" ***/
22 /*** style -- no pound characters, no slash-slash style ***/
23 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
25 /*** + Please use ' instead of ", when possible. Note " ***/
26 /*** should always be used in _( ) function calls. ***/
27 /*** Thank you for your help making the SM code more readable. ***/
28 /*****************************************************************/
33 * Sets an unique session id in order to avoid simultanous sessions crash.
35 * @return string a 4 chars unique string
37 function sqimap_session_id() {
39 global $data_dir, $username;
41 $IMAPSessionID = substr(session_id(), -4);
42 if( $IMAPSessionID == '' ) {
43 $IMAPSessionID = 'A001';
46 return( $IMAPSessionID );
50 /******************************************************************************
51 ** Reads the output from the IMAP stream. If handle_errors is set to true,
52 ** this will also handle all errors that are received. If it is not set,
53 ** the errors will be sent back through $response and $message
54 ******************************************************************************/
56 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
57 &$response, &$message) {
58 global $color, $squirrelmail_language;
61 $resultlist = array();
67 while (strpos($read, "\n") === false) {
68 $read .= fgets($imap_stream, 9096);
71 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
73 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
74 // Sizeless response, probably single-line
77 $read = fgets($imap_stream, 9096);
82 while (strpos($read, "\n") === false) {
83 $read .= fgets($imap_stream, 9096);
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, 9096);
100 while (strpos($read, "\n") === false) {
101 $read .= fgets($imap_stream, 9096);
104 $total_size +
= strlen($read);
106 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
107 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
111 $read = fgets ($imap_stream, 9096);
116 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
117 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
118 $read = fgets($imap_stream, 9096);
120 $resultlist[] = $data;
122 $response = $regs[1];
123 $message = trim($regs[2]);
125 if ($handle_errors == false) { return $resultlist; }
127 if ($response == 'NO') {
128 // ignore this error from m$ exchange, it is not fatal (aka bug)
129 if (strstr($message, 'command resulted in') === false) {
130 set_up_language($squirrelmail_language);
131 echo "<br><b><font color=$color[2]>\n";
132 echo _("ERROR : Could not complete request.");
134 echo _("Reason Given: ");
135 echo $message . "</font><br>\n";
138 } else if ($response == 'BAD') {
139 set_up_language($squirrelmail_language);
140 echo "<br><b><font color=$color[2]>\n";
141 echo _("ERROR : Bad or malformed request.");
143 echo _("Server responded: ");
144 echo $message . "</font><br>\n";
150 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
151 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
155 /******************************************************************************
156 ** Logs the user into the imap server. If $hide is set, no error messages
157 ** will be displayed. This function returns the imap connection handle.
158 ******************************************************************************/
159 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
161 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
163 $imap_stream = fsockopen ( $imap_server_address, $imap_port,
164 $error_number, $error_string, 15);
165 $server_info = fgets ($imap_stream, 1024);
167 // Decrypt the password
168 $password = OneTimePadDecrypt($password, $onetimepad);
170 /** Do some error correction **/
173 set_up_language($squirrelmail_language, true);
174 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
175 echo "$error_number : $error_string<br>\r\n";
180 fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
181 '" "' . quoteIMAP($password) . "\"\r\n");
182 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
184 /** If the connection was not successful, lets see why **/
185 if ($response != "OK") {
187 if ($response != 'NO') {
188 // "BAD" and anything else gets reported here.
189 set_up_language($squirrelmail_language, true);
190 if ($response == 'BAD')
191 printf (_("Bad request: %s")."<br>\r\n", $message);
193 printf (_("Unknown error: %s") . "<br>\n", $message);
195 echo _("Read data:") . "<br>\n";
198 foreach ($read as $line)
200 echo htmlspecialchars($line) . "<br>\n";
205 // If the user does not log in with the correct
206 // username and password it is not possible to get the
207 // correct locale from the user's preferences.
208 // Therefore, apply the same hack as on the login
211 // $squirrelmail_language is set by a cookie when
212 // the user selects language and logs out
214 set_up_language($squirrelmail_language, true);
218 <body bgcolor
="ffffff">
221 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
223 <td bgcolor
="dcdcdc">
224 <font color
="cc0000">
226 <?php
echo _("ERROR") ?
>
234 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
235 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
258 /******************************************************************************
259 ** Simply logs out the imap session
260 ******************************************************************************/
261 function sqimap_logout ($imap_stream) {
262 fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
265 function sqimap_capability($imap_stream, $capability) {
266 global $sqimap_capabilities;
268 if (!is_array($sqimap_capabilities)) {
269 fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
270 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
272 $c = explode(' ', $read[0]);
273 for ($i=2; $i < count($c); $i++
) {
274 $cap_list = explode('=', $c[$i]);
275 if (isset($cap_list[1]))
276 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
278 $sqimap_capabilities[$cap_list[0]] = TRUE;
281 if (! isset($sqimap_capabilities[$capability]))
283 return $sqimap_capabilities[$capability];
286 /******************************************************************************
287 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
288 ******************************************************************************/
289 function sqimap_get_delimiter ($imap_stream = false) {
291 global $sqimap_delimiter;
292 global $optional_delimiter;
294 /* Use configured delimiter if set */
295 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
296 return $optional_delimiter;
298 /* Do some caching here */
299 if (!$sqimap_delimiter) {
300 if (sqimap_capability($imap_stream, "NAMESPACE")) {
301 /* According to something that I can't find, this is supposed to work on all systems
302 OS: This won't work in Courier IMAP.
303 OS: According to rfc2342 response from NAMESPACE command is:
304 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
305 OS: We want to lookup all personal NAMESPACES...
307 fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
308 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
309 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
310 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
312 $pna = explode(')(', $pn);
313 while (list($k, $v) = each($pna))
315 $lst = explode('"', $v);
316 if (isset($lst[3])) {
317 $pn[$lst[1]] = $lst[3];
323 $sqimap_delimiter = $pn[0];
325 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
326 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
327 $quote_position = strpos ($read[0], '"');
328 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
331 return $sqimap_delimiter;
335 /******************************************************************************
336 ** Gets the number of messages in the current mailbox.
337 ******************************************************************************/
338 function sqimap_get_num_messages ($imap_stream, $mailbox) {
339 fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
340 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), 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 /******************************************************************************
351 ** Returns a displayable email address
352 ******************************************************************************/
353 function sqimap_find_email ($string) {
354 /** Luke Ehresman <lehresma@css.tayloru.edu>
355 ** <lehresma@css.tayloru.edu>
356 ** lehresma@css.tayloru.edu
359 ** lehresma@css.tayloru.edu (Luke Ehresman)
362 if (ereg("<([^>]+)>", $string, $regs)) {
365 return trim($string);
369 /******************************************************************************
370 ** Takes the From: field, and creates a displayable name.
371 ** Luke Ehresman <lkehresman@yahoo.com>
372 ** becomes: Luke Ehresman
373 ** <lkehresman@yahoo.com>
374 ** becomes: lkehresman@yahoo.com
375 ******************************************************************************/
376 function sqimap_find_displayable_name ($string) {
377 $string = ' '.trim($string);
378 $orig_string = $string;
379 if (strpos($string, '<') && strpos($string, '>')) {
380 if (strpos($string, '<') == 1) {
381 $string = sqimap_find_email($string);
383 $string = trim($string);
384 $string = substr($string, 0, strpos($string, '<'));
385 $string = ereg_replace ('"', '', $string);
388 if (trim($string) == '') {
389 $string = sqimap_find_email($orig_string);
396 /******************************************************************************
397 ** Returns the number of unseen messages in this folder
398 ******************************************************************************/
399 function sqimap_unseen_messages ($imap_stream, $mailbox) {
400 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
401 fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
402 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
403 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
408 /******************************************************************************
409 ** Saves a message to a given folder -- used for saving sent messages
410 ******************************************************************************/
411 function sqimap_append ($imap_stream, $sent_folder, $length) {
412 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
413 $tmp = fgets ($imap_stream, 1024);
416 function sqimap_append_done ($imap_stream) {
417 fputs ($imap_stream, "\r\n");
418 $tmp = fgets ($imap_stream, 1024);