d23f0c375944ce4e66fc1c28fa392af5b65079a3
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 $read = fgets($imap_stream, 9096);
51 } else if ($total_size > $size) {
52 $difference = $total_size - $size;
53 $total_size = $total_size - strlen($read);
54 $read = substr ($read, 0, strlen($read)-$difference);
56 $junk = fgets($imap_stream, 9096);
60 $read = fgets($imap_stream, 9096);
62 $total_size +
= strlen($read);
64 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
68 $read = fgets ($imap_stream, 9096);
74 $message = trim($regs[2]);
76 if ($imap_general_debug) echo "--<br>";
78 if ($handle_errors == false)
81 if ($response == "NO") {
82 // ignore this error from m$ exchange, it is not fatal (aka bug)
83 if (!ereg("command resulted in",$message)) {
84 set_up_language($squirrelmail_language);
85 echo "<br><b><font color=$color[2]>\n";
86 echo _("ERROR : Could not complete request.");
88 echo _("Reason Given: ");
89 echo $message . "</font><br>\n";
92 } else if ($response == "BAD") {
93 set_up_language($squirrelmail_language);
94 echo "<br><b><font color=$color[2]>\n";
95 echo _("ERROR : Bad or malformed request.");
97 echo _("Server responded: ");
98 echo $message . "</font><br>\n";
105 /******************************************************************************
106 ** Logs the user into the imap server. If $hide is set, no error messages
107 ** will be displayed. This function returns the imap connection handle.
108 ******************************************************************************/
109 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
110 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
112 $imap_stream = fsockopen ($imap_server_address, $imap_port,
113 $error_number, $error_string, 15);
114 $server_info = fgets ($imap_stream, 1024);
116 // Decrypt the password
117 $password = OneTimePadDecrypt($password, $onetimepad);
119 /** Do some error correction **/
122 set_up_language($squirrelmail_language, true);
123 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
124 echo "$error_number : $error_string<br>\r\n";
129 fputs ($imap_stream, "a001 LOGIN \"" . quotemeta($username) .
130 "\" \"" . quotemeta($password) . "\"\r\n");
131 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
133 /** If the connection was not successful, lets see why **/
134 if ($response != "OK") {
136 if ($response != "NO") {
137 // "BAD" and anything else gets reported here.
138 set_up_language($squirrelmail_language, true);
139 if ($response == "BAD")
140 printf (_("Bad request: %s")."<br>\r\n", $message);
142 printf (_("Unknown error: %s") . "<br>\n", $message);
144 echo _("Read data:") . "<br>\n";
147 foreach ($read as $line)
149 echo htmlspecialchars($line) . "<br>\n";
154 // If the user does not log in with the correct
155 // username and password it is not possible to get the
156 // correct locale from the user's preferences.
157 // Therefore, apply the same hack as on the login
160 // $squirrelmail_language is set by a cookie when
161 // the user selects language and logs out
163 set_up_language($squirrelmail_language, true);
167 <body bgcolor
=ffffff
>
170 <table width
=70% noborder bgcolor
=ffffff align
=center
>
175 <?php
echo _("ERROR") ?
>
183 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
184 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
207 /******************************************************************************
208 ** Simply logs out the imap session
209 ******************************************************************************/
210 function sqimap_logout ($imap_stream) {
211 fputs ($imap_stream, "a001 LOGOUT\r\n");
214 function sqimap_capability($imap_stream, $capability) {
215 global $sqimap_capabilities;
216 global $imap_general_debug;
218 if (!is_array($sqimap_capabilities)) {
219 fputs ($imap_stream, "a001 CAPABILITY\r\n");
220 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
222 $c = explode(' ', $read[0]);
223 for ($i=2; $i < count($c); $i++
) {
224 $cap_list = explode('=', $c[$i]);
225 if (isset($cap_list[1]))
226 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
228 $sqimap_capabilities[$cap_list[0]] = TRUE;
231 return $sqimap_capabilities[$capability];
234 /******************************************************************************
235 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
236 ******************************************************************************/
237 function sqimap_get_delimiter ($imap_stream = false) {
238 global $imap_general_debug;
239 global $sqimap_delimiter;
240 global $optional_delimiter;
242 /* Use configured delimiter if set */
243 if((!empty($optional_delimiter)) && $optional_delimiter != "detect") {
244 return $optional_delimiter;
247 /* Do some caching here */
248 if (!$sqimap_delimiter) {
249 if (sqimap_capability($imap_stream, "NAMESPACE")) {
250 /* According to something that I can't find, this is supposed to work on all systems
251 OS: This won't work in Courier IMAP.
252 OS: According to rfc2342 response from NAMESPACE command is:
253 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
254 OS: We want to lookup all personal NAMESPACES...
256 fputs ($imap_stream, "a001 NAMESPACE\r\n");
257 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
258 if (eregi('\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)', $read[0], $data)) {
259 if (eregi('^\( *\((.*)\) *\)', $data[1], $data2))
261 $pna = explode(')(', $pn);
262 while (list($k, $v) = each($pna))
264 list($_, $n, $_, $d) = explode('"', $v);
267 /* OS: We don't need this code right now, it is for other_users and shared folders
268 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
270 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
273 $ona = explode(')(', $on);
274 while (list($k, $v) = each($ona))
276 list($_, $n, $_, $d) = explode('"', $v);
279 $sna = explode(')(', $sn);
280 while (list($k, $v) = each($sna))
282 list($_, $n, $_, $d) = explode('"', $v);
287 $sqimap_delimiter = $pn[0];
289 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
290 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
291 $quote_position = strpos ($read[0], "\"");
292 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
295 return $sqimap_delimiter;
299 /******************************************************************************
300 ** Gets the number of messages in the current mailbox.
301 ******************************************************************************/
302 function sqimap_get_num_messages ($imap_stream, $mailbox) {
303 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
304 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
305 for ($i = 0; $i < count($read_ary); $i++
) {
306 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
310 return "BUG! Couldn't get number of messages in $mailbox!";
314 /******************************************************************************
315 ** Returns a displayable email address
316 ******************************************************************************/
317 function sqimap_find_email ($string) {
318 /** Luke Ehresman <lehresma@css.tayloru.edu>
319 ** <lehresma@css.tayloru.edu>
320 ** lehresma@css.tayloru.edu
323 ** lehresma@css.tayloru.edu (Luke Ehresman)
326 if (ereg("<([^>]+)>", $string, $regs)) {
329 return trim($string);
333 /******************************************************************************
334 ** Takes the From: field, and creates a displayable name.
335 ** Luke Ehresman <lkehresman@yahoo.com>
336 ** becomes: Luke Ehresman
337 ** <lkehresman@yahoo.com>
338 ** becomes: lkehresman@yahoo.com
339 ******************************************************************************/
340 function sqimap_find_displayable_name ($string) {
341 $string = " ".trim($string);
342 $orig_string = $string;
343 if (strpos($string, "<") && strpos($string, ">")) {
344 if (strpos($string, "<") == 1) {
345 $string = sqimap_find_email($string);
347 $string = trim($string);
348 $string = substr($string, 0, strpos($string, "<"));
349 $string = ereg_replace ("\"", "", $string);
352 if (trim($string) == "") {
353 $string = sqimap_find_email($orig_string);
360 /******************************************************************************
361 ** Returns the number of unseen messages in this folder
362 ******************************************************************************/
363 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
364 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
365 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
366 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
367 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
372 /******************************************************************************
373 ** Saves a message to a given folder -- used for saving sent messages
374 ******************************************************************************/
375 function sqimap_append ($imap_stream, $sent_folder, $length) {
376 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
377 $tmp = fgets ($imap_stream, 1024);
380 function sqimap_append_done ($imap_stream) {
381 fputs ($imap_stream, "\r\n");
382 $tmp = fgets ($imap_stream, 1024);