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)) {
32 // Continue if needed for this single line
33 while (strpos($read, "\n") === false) {
34 $read .= fgets($imap_stream, 9096);
36 if ($imap_general_debug) {
37 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
41 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
44 $dt[0] = $dt[count($dt)-1] = "";
45 $d = implode ("", $dt);
46 if (strlen($d) >= $size) {
50 $read = fgets ($imap_stream, 9096);
57 $read = fgets ($imap_stream, 9096);
62 $message = trim($regs[2]);
64 if ($imap_general_debug) echo "--<br>";
66 if ($handle_errors == false)
69 if ($response == "NO") {
70 // ignore this error from m$ exchange, it is not fatal (aka bug)
71 if (!ereg("command resulted in",$message)) {
72 set_up_language($squirrelmail_language);
73 echo "<br><b><font color=$color[2]>\n";
74 echo _("ERROR : Could not complete request.");
76 echo _("Reason Given: ");
77 echo $message . "</font><br>\n";
80 } else if ($response == "BAD") {
81 set_up_language($squirrelmail_language);
82 echo "<br><b><font color=$color[2]>\n";
83 echo _("ERROR : Bad or malformed request.");
85 echo _("Server responded: ");
86 echo $message . "</font><br>\n";
93 /******************************************************************************
94 ** Logs the user into the imap server. If $hide is set, no error messages
95 ** will be displayed. This function returns the imap connection handle.
96 ******************************************************************************/
97 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
98 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
100 $imap_stream = fsockopen ($imap_server_address, $imap_port,
101 &$error_number, &$error_string, 15);
102 $server_info = fgets ($imap_stream, 1024);
104 // Decrypt the password
105 $password = OneTimePadDecrypt($password, $onetimepad);
107 /** Do some error correction **/
110 set_up_language($squirrelmail_language, true);
111 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
112 echo "$error_number : $error_string<br>\r\n";
117 fputs ($imap_stream, "a001 LOGIN \"" . quotemeta($username) .
118 "\" \"" . quotemeta($password) . "\"\r\n");
119 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
121 /** If the connection was not successful, lets see why **/
122 if ($response != "OK") {
124 if ($response != "NO") {
125 // "BAD" and anything else gets reported here.
126 set_up_language($squirrelmail_language, true);
127 if ($response == "BAD")
128 printf (_("Bad request: %s")."<br>\r\n", $message);
130 printf (_("Unknown error: %s") . "<br>\n", $message);
132 echo _("Read data:") . "<br>\n";
135 foreach ($read as $line)
137 echo htmlspecialchars($line) . "<br>\n";
142 // If the user does not log in with the correct
143 // username and password it is not possible to get the
144 // correct locale from the user's preferences.
145 // Therefore, apply the same hack as on the login
148 // $squirrelmail_language is set by a cookie when
149 // the user selects language and logs out
151 set_up_language($squirrelmail_language, true);
155 <body bgcolor
=ffffff
>
158 <table width
=70% noborder bgcolor
=ffffff align
=center
>
163 <?php
echo _("ERROR") ?
>
171 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
172 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
195 /******************************************************************************
196 ** Simply logs out the imap session
197 ******************************************************************************/
198 function sqimap_logout ($imap_stream) {
199 fputs ($imap_stream, "a001 LOGOUT\r\n");
202 function sqimap_capability($imap_stream, $capability) {
203 global $sqimap_capabilities;
204 global $imap_general_debug;
206 if (!is_array($sqimap_capabilities)) {
207 fputs ($imap_stream, "a001 CAPABILITY\r\n");
208 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
210 $c = explode(' ', $read[0]);
211 for ($i=2; $i < count($c); $i++
) {
212 $cap_list = explode('=', $c[$i]);
213 if (isset($cap_list[1]))
214 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
216 $sqimap_capabilities[$cap_list[0]] = TRUE;
219 return $sqimap_capabilities[$capability];
222 /******************************************************************************
223 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
224 ******************************************************************************/
225 function sqimap_get_delimiter ($imap_stream = false) {
226 global $imap_general_debug;
227 global $sqimap_delimiter;
228 global $optional_delimiter;
230 /* Use configured delimiter if set */
231 if((!empty($optional_delimiter)) && $optional_delimiter != "detect") {
232 return $optional_delimiter;
235 /* Do some caching here */
236 if (!$sqimap_delimiter) {
237 if (sqimap_capability($imap_stream, "NAMESPACE")) {
238 /* According to something that I can't find, this is supposed to work on all systems
239 OS: This won't work in Courier IMAP.
240 OS: According to rfc2342 response from NAMESPACE command is:
241 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
242 OS: We want to lookup all personal NAMESPACES...
244 fputs ($imap_stream, "a001 NAMESPACE\r\n");
245 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
246 if (eregi('\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)', $read[0], $data)) {
247 if (eregi('^\( *\((.*)\) *\)', $data[1], $data2))
249 $pna = explode(')(', $pn);
250 while (list($k, $v) = each($pna))
252 list($_, $n, $_, $d) = explode('"', $v);
255 /* OS: We don't need this code right now, it is for other_users and shared folders
256 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
258 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
261 $ona = explode(')(', $on);
262 while (list($k, $v) = each($ona))
264 list($_, $n, $_, $d) = explode('"', $v);
267 $sna = explode(')(', $sn);
268 while (list($k, $v) = each($sna))
270 list($_, $n, $_, $d) = explode('"', $v);
275 $sqimap_delimiter = $pn[0];
277 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
278 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
279 $quote_position = strpos ($read[0], "\"");
280 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
283 return $sqimap_delimiter;
287 /******************************************************************************
288 ** Gets the number of messages in the current mailbox.
289 ******************************************************************************/
290 function sqimap_get_num_messages ($imap_stream, $mailbox) {
291 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
292 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
293 for ($i = 0; $i < count($read_ary); $i++
) {
294 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
298 return "BUG! Couldn't get number of messages in $mailbox!";
302 /******************************************************************************
303 ** Returns a displayable email address
304 ******************************************************************************/
305 function sqimap_find_email ($string) {
306 /** Luke Ehresman <lehresma@css.tayloru.edu>
307 ** <lehresma@css.tayloru.edu>
308 ** lehresma@css.tayloru.edu
311 ** lehresma@css.tayloru.edu (Luke Ehresman)
314 if (ereg("<([^>]+)>", $string, $regs)) {
317 return trim($string);
321 /******************************************************************************
322 ** Takes the From: field, and creates a displayable name.
323 ** Luke Ehresman <lkehresman@yahoo.com>
324 ** becomes: Luke Ehresman
325 ** <lkehresman@yahoo.com>
326 ** becomes: lkehresman@yahoo.com
327 ******************************************************************************/
328 function sqimap_find_displayable_name ($string) {
329 $string = " ".trim($string);
330 $orig_string = $string;
331 if (strpos($string, "<") && strpos($string, ">")) {
332 if (strpos($string, "<") == 1) {
333 $string = sqimap_find_email($string);
335 $string = trim($string);
336 $string = substr($string, 0, strpos($string, "<"));
337 $string = ereg_replace ("\"", "", $string);
340 if (trim($string) == "") {
341 $string = sqimap_find_email($orig_string);
348 /******************************************************************************
349 ** Returns the number of unseen messages in this folder
350 ******************************************************************************/
351 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
352 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
353 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
354 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
355 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
360 /******************************************************************************
361 ** Saves a message to a given folder -- used for saving sent messages
362 ******************************************************************************/
363 function sqimap_append ($imap_stream, $sent_folder, $length) {
364 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
365 $tmp = fgets ($imap_stream, 1024);
368 function sqimap_append_done ($imap_stream) {
369 fputs ($imap_stream, "\r\n");
370 $tmp = fgets ($imap_stream, 1024);