5 ** This implements all functions that do general imap functions.
10 $imap_general_debug = false;
12 /******************************************************************************
13 ** Reads the output from the IMAP stream. If handle_errors is set to true,
14 ** this will also handle all errors that are received. If it is not set,
15 ** the errors will be sent back through $response and $message
16 ******************************************************************************/
17 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
18 global $color, $squirrelmail_language, $imap_general_debug;
20 $read = fgets($imap_stream, 9096);
22 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
33 // Continue if needed for this single line
34 while (strpos($read, "\n") === false) {
35 $read .= fgets($imap_stream, 9096);
37 // For debugging purposes
38 if ($imap_general_debug) {
39 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
44 // If we know the size, no need to look at the end parameters
46 if ($total_size == $size) {
48 $read = fgets($imap_stream, 9096);
49 while (!ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
50 $read = fgets($imap_stream, 9096);
53 } else if ($total_size > $size) {
54 $difference = $total_size - $size;
55 $total_size = $total_size - strlen($read);
56 $read = substr ($read, 0, strlen($read)-$difference);
58 $junk = fgets($imap_stream, 9096);
62 $read = fgets($imap_stream, 9096);
64 $total_size +
= strlen($read);
66 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
70 $read = fgets ($imap_stream, 9096);
76 $message = trim($regs[2]);
78 if ($imap_general_debug) echo '--<br>';
80 if ($handle_errors == false)
83 if ($response == 'NO') {
84 // ignore this error from m$ exchange, it is not fatal (aka bug)
85 if (strstr($message, 'command resulted in') === false) {
86 set_up_language($squirrelmail_language);
87 echo "<br><b><font color=$color[2]>\n";
88 echo _("ERROR : Could not complete request.");
90 echo _("Reason Given: ");
91 echo $message . "</font><br>\n";
94 } else if ($response == 'BAD') {
95 set_up_language($squirrelmail_language);
96 echo "<br><b><font color=$color[2]>\n";
97 echo _("ERROR : Bad or malformed request.");
99 echo _("Server responded: ");
100 echo $message . "</font><br>\n";
107 /******************************************************************************
108 ** Logs the user into the imap server. If $hide is set, no error messages
109 ** will be displayed. This function returns the imap connection handle.
110 ******************************************************************************/
111 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
112 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
114 $imap_stream = fsockopen ($imap_server_address, $imap_port,
115 $error_number, $error_string, 15);
116 $server_info = fgets ($imap_stream, 1024);
118 // Decrypt the password
119 $password = OneTimePadDecrypt($password, $onetimepad);
121 /** Do some error correction **/
124 set_up_language($squirrelmail_language, true);
125 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
126 echo "$error_number : $error_string<br>\r\n";
131 fputs ($imap_stream, "a001 LOGIN \"" . quoteIMAP($username) .
132 '" "' . quoteIMAP($password) . "\"\r\n");
133 $read = sqimap_read_data ($imap_stream, 'a001', false, $response, $message);
135 /** If the connection was not successful, lets see why **/
136 if ($response != "OK") {
138 if ($response != 'NO') {
139 // "BAD" and anything else gets reported here.
140 set_up_language($squirrelmail_language, true);
141 if ($response == 'BAD')
142 printf (_("Bad request: %s")."<br>\r\n", $message);
144 printf (_("Unknown error: %s") . "<br>\n", $message);
146 echo _("Read data:") . "<br>\n";
149 foreach ($read as $line)
151 echo htmlspecialchars($line) . "<br>\n";
156 // If the user does not log in with the correct
157 // username and password it is not possible to get the
158 // correct locale from the user's preferences.
159 // Therefore, apply the same hack as on the login
162 // $squirrelmail_language is set by a cookie when
163 // the user selects language and logs out
165 set_up_language($squirrelmail_language, true);
169 <body bgcolor
="ffffff">
172 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
174 <td bgcolor
="dcdcdc">
175 <font color
="cc0000">
177 <?php
echo _("ERROR") ?
>
185 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
186 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
209 /******************************************************************************
210 ** Simply logs out the imap session
211 ******************************************************************************/
212 function sqimap_logout ($imap_stream) {
213 fputs ($imap_stream, "a001 LOGOUT\r\n");
216 function sqimap_capability($imap_stream, $capability) {
217 global $sqimap_capabilities;
218 global $imap_general_debug;
220 if (!is_array($sqimap_capabilities)) {
221 fputs ($imap_stream, "a001 CAPABILITY\r\n");
222 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
224 $c = explode(' ', $read[0]);
225 for ($i=2; $i < count($c); $i++
) {
226 $cap_list = explode('=', $c[$i]);
227 if (isset($cap_list[1]))
228 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
230 $sqimap_capabilities[$cap_list[0]] = TRUE;
233 return $sqimap_capabilities[$capability];
236 /******************************************************************************
237 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
238 ******************************************************************************/
239 function sqimap_get_delimiter ($imap_stream = false) {
240 global $imap_general_debug;
241 global $sqimap_delimiter;
242 global $optional_delimiter;
244 /* Use configured delimiter if set */
245 if((!empty($optional_delimiter)) && $optional_delimiter != "detect") {
246 return $optional_delimiter;
249 /* Do some caching here */
250 if (!$sqimap_delimiter) {
251 if (sqimap_capability($imap_stream, "NAMESPACE")) {
252 /* According to something that I can't find, this is supposed to work on all systems
253 OS: This won't work in Courier IMAP.
254 OS: According to rfc2342 response from NAMESPACE command is:
255 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
256 OS: We want to lookup all personal NAMESPACES...
258 fputs ($imap_stream, "a001 NAMESPACE\r\n");
259 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
260 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
261 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
263 $pna = explode(')(', $pn);
264 while (list($k, $v) = each($pna))
266 $lst = explode('"', $v);
267 if (isset($lst[3])) {
268 $pn[$lst[1]] = $lst[3];
274 $sqimap_delimiter = $pn[0];
276 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
277 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
278 $quote_position = strpos ($read[0], '"');
279 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
282 return $sqimap_delimiter;
286 /******************************************************************************
287 ** Gets the number of messages in the current mailbox.
288 ******************************************************************************/
289 function sqimap_get_num_messages ($imap_stream, $mailbox) {
290 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
291 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
292 for ($i = 0; $i < count($read_ary); $i++
) {
293 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
297 return "BUG! Couldn't get number of messages in $mailbox!";
301 /******************************************************************************
302 ** Returns a displayable email address
303 ******************************************************************************/
304 function sqimap_find_email ($string) {
305 /** Luke Ehresman <lehresma@css.tayloru.edu>
306 ** <lehresma@css.tayloru.edu>
307 ** lehresma@css.tayloru.edu
310 ** lehresma@css.tayloru.edu (Luke Ehresman)
313 if (ereg("<([^>]+)>", $string, $regs)) {
316 return trim($string);
320 /******************************************************************************
321 ** Takes the From: field, and creates a displayable name.
322 ** Luke Ehresman <lkehresman@yahoo.com>
323 ** becomes: Luke Ehresman
324 ** <lkehresman@yahoo.com>
325 ** becomes: lkehresman@yahoo.com
326 ******************************************************************************/
327 function sqimap_find_displayable_name ($string) {
328 $string = ' '.trim($string);
329 $orig_string = $string;
330 if (strpos($string, '<') && strpos($string, '>')) {
331 if (strpos($string, '<') == 1) {
332 $string = sqimap_find_email($string);
334 $string = trim($string);
335 $string = substr($string, 0, strpos($string, '<'));
336 $string = ereg_replace ('"', '', $string);
339 if (trim($string) == '') {
340 $string = sqimap_find_email($orig_string);
347 /******************************************************************************
348 ** Returns the number of unseen messages in this folder
349 ******************************************************************************/
350 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
351 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
352 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
353 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
354 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
359 /******************************************************************************
360 ** Saves a message to a given folder -- used for saving sent messages
361 ******************************************************************************/
362 function sqimap_append ($imap_stream, $sent_folder, $length) {
363 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
364 $tmp = fgets ($imap_stream, 1024);
367 function sqimap_append_done ($imap_stream) {
368 fputs ($imap_stream, "\r\n");
369 $tmp = fgets ($imap_stream, 1024);