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 ******************************************************************************/
22 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
23 global $color, $squirrelmail_language, $imap_general_debug;
25 $read = fgets($imap_stream, 9096);
27 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
38 // Continue if needed for this single line
39 while (strpos($read, "\n") === false) {
40 $read .= fgets($imap_stream, 9096);
42 // For debugging purposes
43 if ($imap_general_debug) {
44 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
49 // If we know the size, no need to look at the end parameters
51 if ($total_size == $size) {
53 $read = fgets($imap_stream, 9096);
54 while (!ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
55 $read = fgets($imap_stream, 9096);
58 } else if ($total_size > $size) {
59 $difference = $total_size - $size;
60 $total_size = $total_size - strlen($read);
61 $read = substr ($read, 0, strlen($read)-$difference);
63 $junk = fgets($imap_stream, 9096);
67 $read = fgets($imap_stream, 9096);
69 $total_size +
= strlen($read);
71 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
75 $read = fgets ($imap_stream, 9096);
81 $message = trim($regs[2]);
83 if ($imap_general_debug) echo '--<br>';
85 if ($handle_errors == false)
88 if ($response == 'NO') {
89 // ignore this error from m$ exchange, it is not fatal (aka bug)
90 if (strstr($message, 'command resulted in') === false) {
91 set_up_language($squirrelmail_language);
92 echo "<br><b><font color=$color[2]>\n";
93 echo _("ERROR : Could not complete request.");
95 echo _("Reason Given: ");
96 echo $message . "</font><br>\n";
99 } else if ($response == 'BAD') {
100 set_up_language($squirrelmail_language);
101 echo "<br><b><font color=$color[2]>\n";
102 echo _("ERROR : Bad or malformed request.");
104 echo _("Server responded: ");
105 echo $message . "</font><br>\n";
112 /******************************************************************************
113 ** Logs the user into the imap server. If $hide is set, no error messages
114 ** will be displayed. This function returns the imap connection handle.
115 ******************************************************************************/
116 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
117 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
119 $imap_stream = fsockopen ($imap_server_address, $imap_port,
120 $error_number, $error_string, 15);
121 $server_info = fgets ($imap_stream, 1024);
123 // Decrypt the password
124 $password = OneTimePadDecrypt($password, $onetimepad);
126 /** Do some error correction **/
129 set_up_language($squirrelmail_language, true);
130 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
131 echo "$error_number : $error_string<br>\r\n";
136 fputs ($imap_stream, "a001 LOGIN \"" . quoteIMAP($username) .
137 '" "' . quoteIMAP($password) . "\"\r\n");
138 $read = sqimap_read_data ($imap_stream, 'a001', false, $response, $message);
140 /** If the connection was not successful, lets see why **/
141 if ($response != "OK") {
143 if ($response != 'NO') {
144 // "BAD" and anything else gets reported here.
145 set_up_language($squirrelmail_language, true);
146 if ($response == 'BAD')
147 printf (_("Bad request: %s")."<br>\r\n", $message);
149 printf (_("Unknown error: %s") . "<br>\n", $message);
151 echo _("Read data:") . "<br>\n";
154 foreach ($read as $line)
156 echo htmlspecialchars($line) . "<br>\n";
161 // If the user does not log in with the correct
162 // username and password it is not possible to get the
163 // correct locale from the user's preferences.
164 // Therefore, apply the same hack as on the login
167 // $squirrelmail_language is set by a cookie when
168 // the user selects language and logs out
170 set_up_language($squirrelmail_language, true);
174 <body bgcolor
="ffffff">
177 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
179 <td bgcolor
="dcdcdc">
180 <font color
="cc0000">
182 <?php
echo _("ERROR") ?
>
190 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
191 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
214 /******************************************************************************
215 ** Simply logs out the imap session
216 ******************************************************************************/
217 function sqimap_logout ($imap_stream) {
218 fputs ($imap_stream, "a001 LOGOUT\r\n");
221 function sqimap_capability($imap_stream, $capability) {
222 global $sqimap_capabilities;
223 global $imap_general_debug;
225 if (!is_array($sqimap_capabilities)) {
226 fputs ($imap_stream, "a001 CAPABILITY\r\n");
227 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
229 $c = explode(' ', $read[0]);
230 for ($i=2; $i < count($c); $i++
) {
231 $cap_list = explode('=', $c[$i]);
232 if (isset($cap_list[1]))
233 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
235 $sqimap_capabilities[$cap_list[0]] = TRUE;
238 return $sqimap_capabilities[$capability];
241 /******************************************************************************
242 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
243 ******************************************************************************/
244 function sqimap_get_delimiter ($imap_stream = false) {
245 global $imap_general_debug;
246 global $sqimap_delimiter;
247 global $optional_delimiter;
249 /* Use configured delimiter if set */
250 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
251 return $optional_delimiter;
253 /* Do some caching here */
254 if (!$sqimap_delimiter) {
255 if (sqimap_capability($imap_stream, "NAMESPACE")) {
256 /* According to something that I can't find, this is supposed to work on all systems
257 OS: This won't work in Courier IMAP.
258 OS: According to rfc2342 response from NAMESPACE command is:
259 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
260 OS: We want to lookup all personal NAMESPACES...
262 fputs ($imap_stream, "a001 NAMESPACE\r\n");
263 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
264 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
265 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
267 $pna = explode(')(', $pn);
268 while (list($k, $v) = each($pna))
270 $lst = explode('"', $v);
271 if (isset($lst[3])) {
272 $pn[$lst[1]] = $lst[3];
278 $sqimap_delimiter = $pn[0];
280 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
281 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
282 $quote_position = strpos ($read[0], '"');
283 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
286 return $sqimap_delimiter;
290 /******************************************************************************
291 ** Gets the number of messages in the current mailbox.
292 ******************************************************************************/
293 function sqimap_get_num_messages ($imap_stream, $mailbox) {
294 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
295 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
296 for ($i = 0; $i < count($read_ary); $i++
) {
297 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
301 return "BUG! Couldn't get number of messages in $mailbox!";
305 /******************************************************************************
306 ** Returns a displayable email address
307 ******************************************************************************/
308 function sqimap_find_email ($string) {
309 /** Luke Ehresman <lehresma@css.tayloru.edu>
310 ** <lehresma@css.tayloru.edu>
311 ** lehresma@css.tayloru.edu
314 ** lehresma@css.tayloru.edu (Luke Ehresman)
317 if (ereg("<([^>]+)>", $string, $regs)) {
320 return trim($string);
324 /******************************************************************************
325 ** Takes the From: field, and creates a displayable name.
326 ** Luke Ehresman <lkehresman@yahoo.com>
327 ** becomes: Luke Ehresman
328 ** <lkehresman@yahoo.com>
329 ** becomes: lkehresman@yahoo.com
330 ******************************************************************************/
331 function sqimap_find_displayable_name ($string) {
332 $string = ' '.trim($string);
333 $orig_string = $string;
334 if (strpos($string, '<') && strpos($string, '>')) {
335 if (strpos($string, '<') == 1) {
336 $string = sqimap_find_email($string);
338 $string = trim($string);
339 $string = substr($string, 0, strpos($string, '<'));
340 $string = ereg_replace ('"', '', $string);
343 if (trim($string) == '') {
344 $string = sqimap_find_email($orig_string);
351 /******************************************************************************
352 ** Returns the number of unseen messages in this folder
353 ******************************************************************************/
354 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
355 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
356 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
357 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
358 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
363 /******************************************************************************
364 ** Saves a message to a given folder -- used for saving sent messages
365 ******************************************************************************/
366 function sqimap_append ($imap_stream, $sent_folder, $length) {
367 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
368 $tmp = fgets ($imap_stream, 1024);
371 function sqimap_append_done ($imap_stream) {
372 fputs ($imap_stream, "\r\n");
373 $tmp = fgets ($imap_stream, 1024);