5 ** This implements all functions that do general imap functions.
10 if (defined ('imap_general_php'))
12 define ('imap_general_php', true);
14 global $imap_general_debug;
15 $imap_general_debug = false;
17 /******************************************************************************
18 ** Reads the output from the IMAP stream. If handle_errors is set to true,
19 ** this will also handle all errors that are received. If it is not set,
20 ** the errors will be sent back through $response and $message
21 ******************************************************************************/
23 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
24 &$response, &$message) {
25 global $color, $squirrelmail_language, $imap_general_debug;
28 $resultlist = array();
34 while (strpos($read, "\n") === false) {
35 $read .= fgets($imap_stream, 9096);
38 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
40 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
41 // Sizeless response, probably single-line
42 // For debugging purposes
43 if ($imap_general_debug) {
44 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
49 $read = fgets($imap_stream, 9096);
54 while (strpos($read, "\n") === false) {
55 $read .= fgets($imap_stream, 9096);
57 // For debugging purposes
58 if ($imap_general_debug) {
59 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
62 // If we know the size, no need to look at the end parameters
64 if ($total_size == $size) {
65 // We've reached the end of this 'message', switch to the next one.
68 } else if ($total_size > $size) {
69 $difference = $total_size - $size;
70 $total_size = $total_size - strlen($read);
71 $data[] = substr ($read, 0, strlen($read)-$difference);
72 $read = substr ($read, strlen($read)-$difference, strlen($read));
76 $read = fgets($imap_stream, 9096);
78 $total_size +
= strlen($read);
80 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
81 ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
85 $read = fgets ($imap_stream, 9096);
90 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
91 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
92 $read = fgets($imap_stream, 9096);
94 $resultlist[] = $data;
97 $message = trim($regs[2]);
99 if ($imap_general_debug) echo '--<br>';
101 if ($handle_errors == false)
104 if ($response == 'NO') {
105 // ignore this error from m$ exchange, it is not fatal (aka bug)
106 if (strstr($message, 'command resulted in') === false) {
107 set_up_language($squirrelmail_language);
108 echo "<br><b><font color=$color[2]>\n";
109 echo _("ERROR : Could not complete request.");
111 echo _("Reason Given: ");
112 echo $message . "</font><br>\n";
115 } else if ($response == 'BAD') {
116 set_up_language($squirrelmail_language);
117 echo "<br><b><font color=$color[2]>\n";
118 echo _("ERROR : Bad or malformed request.");
120 echo _("Server responded: ");
121 echo $message . "</font><br>\n";
127 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
128 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
132 /******************************************************************************
133 ** Logs the user into the imap server. If $hide is set, no error messages
134 ** will be displayed. This function returns the imap connection handle.
135 ******************************************************************************/
136 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
137 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
139 $imap_stream = fsockopen ($imap_server_address, $imap_port,
140 $error_number, $error_string, 15);
141 $server_info = fgets ($imap_stream, 1024);
143 // Decrypt the password
144 $password = OneTimePadDecrypt($password, $onetimepad);
146 /** Do some error correction **/
149 set_up_language($squirrelmail_language, true);
150 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
151 echo "$error_number : $error_string<br>\r\n";
156 fputs ($imap_stream, "a001 LOGIN \"" . quoteIMAP($username) .
157 '" "' . quoteIMAP($password) . "\"\r\n");
158 $read = sqimap_read_data ($imap_stream, 'a001', false, $response, $message);
160 /** If the connection was not successful, lets see why **/
161 if ($response != "OK") {
163 if ($response != 'NO') {
164 // "BAD" and anything else gets reported here.
165 set_up_language($squirrelmail_language, true);
166 if ($response == 'BAD')
167 printf (_("Bad request: %s")."<br>\r\n", $message);
169 printf (_("Unknown error: %s") . "<br>\n", $message);
171 echo _("Read data:") . "<br>\n";
174 foreach ($read as $line)
176 echo htmlspecialchars($line) . "<br>\n";
181 // If the user does not log in with the correct
182 // username and password it is not possible to get the
183 // correct locale from the user's preferences.
184 // Therefore, apply the same hack as on the login
187 // $squirrelmail_language is set by a cookie when
188 // the user selects language and logs out
190 set_up_language($squirrelmail_language, true);
194 <body bgcolor
="ffffff">
197 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
199 <td bgcolor
="dcdcdc">
200 <font color
="cc0000">
202 <?php
echo _("ERROR") ?
>
210 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
211 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
234 /******************************************************************************
235 ** Simply logs out the imap session
236 ******************************************************************************/
237 function sqimap_logout ($imap_stream) {
238 fputs ($imap_stream, "a001 LOGOUT\r\n");
241 function sqimap_capability($imap_stream, $capability) {
242 global $sqimap_capabilities;
243 global $imap_general_debug;
245 if (!is_array($sqimap_capabilities)) {
246 fputs ($imap_stream, "a001 CAPABILITY\r\n");
247 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
249 $c = explode(' ', $read[0]);
250 for ($i=2; $i < count($c); $i++
) {
251 $cap_list = explode('=', $c[$i]);
252 if (isset($cap_list[1]))
253 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
255 $sqimap_capabilities[$cap_list[0]] = TRUE;
258 return $sqimap_capabilities[$capability];
261 /******************************************************************************
262 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
263 ******************************************************************************/
264 function sqimap_get_delimiter ($imap_stream = false) {
265 global $imap_general_debug;
266 global $sqimap_delimiter;
267 global $optional_delimiter;
269 /* Use configured delimiter if set */
270 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
271 return $optional_delimiter;
273 /* Do some caching here */
274 if (!$sqimap_delimiter) {
275 if (sqimap_capability($imap_stream, "NAMESPACE")) {
276 /* According to something that I can't find, this is supposed to work on all systems
277 OS: This won't work in Courier IMAP.
278 OS: According to rfc2342 response from NAMESPACE command is:
279 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
280 OS: We want to lookup all personal NAMESPACES...
282 fputs ($imap_stream, "a001 NAMESPACE\r\n");
283 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
284 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
285 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
287 $pna = explode(')(', $pn);
288 while (list($k, $v) = each($pna))
290 $lst = explode('"', $v);
291 if (isset($lst[3])) {
292 $pn[$lst[1]] = $lst[3];
298 $sqimap_delimiter = $pn[0];
300 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
301 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
302 $quote_position = strpos ($read[0], '"');
303 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
306 return $sqimap_delimiter;
310 /******************************************************************************
311 ** Gets the number of messages in the current mailbox.
312 ******************************************************************************/
313 function sqimap_get_num_messages ($imap_stream, $mailbox) {
314 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
315 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
316 for ($i = 0; $i < count($read_ary); $i++
) {
317 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
321 return "BUG! Couldn't get number of messages in $mailbox!";
325 /******************************************************************************
326 ** Returns a displayable email address
327 ******************************************************************************/
328 function sqimap_find_email ($string) {
329 /** Luke Ehresman <lehresma@css.tayloru.edu>
330 ** <lehresma@css.tayloru.edu>
331 ** lehresma@css.tayloru.edu
334 ** lehresma@css.tayloru.edu (Luke Ehresman)
337 if (ereg("<([^>]+)>", $string, $regs)) {
340 return trim($string);
344 /******************************************************************************
345 ** Takes the From: field, and creates a displayable name.
346 ** Luke Ehresman <lkehresman@yahoo.com>
347 ** becomes: Luke Ehresman
348 ** <lkehresman@yahoo.com>
349 ** becomes: lkehresman@yahoo.com
350 ******************************************************************************/
351 function sqimap_find_displayable_name ($string) {
352 $string = ' '.trim($string);
353 $orig_string = $string;
354 if (strpos($string, '<') && strpos($string, '>')) {
355 if (strpos($string, '<') == 1) {
356 $string = sqimap_find_email($string);
358 $string = trim($string);
359 $string = substr($string, 0, strpos($string, '<'));
360 $string = ereg_replace ('"', '', $string);
363 if (trim($string) == '') {
364 $string = sqimap_find_email($orig_string);
371 /******************************************************************************
372 ** Returns the number of unseen messages in this folder
373 ******************************************************************************/
374 function sqimap_unseen_messages ($imap_stream, $mailbox) {
375 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
376 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
377 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
378 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
383 /******************************************************************************
384 ** Saves a message to a given folder -- used for saving sent messages
385 ******************************************************************************/
386 function sqimap_append ($imap_stream, $sent_folder, $length) {
387 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
388 $tmp = fgets ($imap_stream, 1024);
391 function sqimap_append_done ($imap_stream) {
392 fputs ($imap_stream, "\r\n");
393 $tmp = fgets ($imap_stream, 1024);