5 ** This implements all functions that do general imap functions.
8 /******************************************************************************
9 ** Reads the output from the IMAP stream. If handle_errors is set to true,
10 ** this will also handle all errors that are received. If it is not set,
11 ** the errors will be sent back through $response and $message
12 ******************************************************************************/
13 function sqimap_read_data ($imap_stream, $pre, $handle_errors, $response, $message) {
16 //$imap_general_debug = true;
17 $imap_general_debug = false;
19 $read = fgets ($imap_stream, 1024);
20 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
22 while ((substr($read, 0, strlen("$pre OK")) != "$pre OK") &&
23 (substr($read, 0, strlen("$pre BAD")) != "$pre BAD") &&
24 (substr($read, 0, strlen("$pre NO")) != "$pre NO")) {
25 $data[$counter] = $read;
26 $read = fgets ($imap_stream, 1024);
27 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
30 if ($imap_general_debug) echo "--<br>";
31 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
33 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
35 else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
37 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
41 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
44 if ($handle_errors == true) {
45 if ($response == "NO") {
46 echo "<br><b><font color=$color[2]>\n";
47 echo _("ERROR : Could not complete request.");
49 echo _("Reason Given: ");
50 echo "$message</font><br>\n";
52 } else if ($response == "BAD") {
53 echo "<br><b><font color=$color[2]>\n";
54 echo _("ERROR : Bad or malformed request.");
56 echo _("Server responded: ");
57 echo "$message</font><br>\n";
68 /******************************************************************************
69 ** Logs the user into the imap server. If $hide is set, no error messages
70 ** will be displayed. This function returns the imap connection handle.
71 ******************************************************************************/
72 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
73 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
75 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
76 $server_info = fgets ($imap_stream, 1024);
78 // Decrypt the password
79 $password = OneTimePadDecrypt($password, $onetimepad);
81 // This function can sometimes be called before the check for
83 if (!function_exists("_")) {
89 /** Do some error correction **/
92 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
93 echo "$error_number : $error_string<br>\r\n";
98 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
99 $read = fgets ($imap_stream, 1024);
101 /** If the connection was not successful, lets see why **/
102 if (substr($read, 0, 7) != "a001 OK") {
104 if (substr($read, 0, 8) == "a001 BAD") {
105 printf (_("Bad request: %s")."<br>\r\n", $read);
107 } else if (substr($read, 0, 7) == "a001 NO") {
108 // If the user does not log in with the correct
109 // username and password it is not possible to get the
110 // correct locale from the user's preferences.
111 // Therefore, apply the same hack as on the login
114 // $squirrelmail_language is set by a cookie when
115 // the user selects language and logs out
117 // Use HTTP content language negotiation if cookie
119 if (!isset($squirrelmail_language) && isset($HTTP_ACCEPT_LANGUAGE)) {
120 $squirrelmail_language = substr($HTTP_ACCEPT_LANGUAGE, 0, 2);
123 if (isset($squirrelmail_language) && function_exists("bindtextdomain")) {
124 if ($squirrelmail_language != "en" && $squirrelmail_language != "") {
125 putenv("LC_ALL=".$squirrelmail_language);
126 bindtextdomain("squirrelmail", "../locale/");
127 textdomain("squirrelmail");
128 header ("Content-Type: text/html; charset=".$languages[$squirrelmail_language]["CHARSET"]);
134 <body bgcolor
=ffffff
>
137 <table width
=70% noborder bgcolor
=ffffff align
=center
>
142 <?php
echo _("ERROR") ?
>
150 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
151 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
163 printf (_("Unknown error: %s")."<br>", $read);
177 /******************************************************************************
178 ** Simply logs out the imap session
179 ******************************************************************************/
180 function sqimap_logout ($imap_stream) {
181 fputs ($imap_stream, "a001 LOGOUT\r\n");
186 /******************************************************************************
187 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
188 ******************************************************************************/
189 function sqimap_get_delimiter ($imap_stream) {
190 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
191 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
192 $quote_position = strpos ($read[0], "\"");
193 $delim = substr ($read[0], $quote_position+
1, 1);
201 /******************************************************************************
202 ** Gets the number of messages in the current mailbox.
203 ******************************************************************************/
204 function sqimap_get_num_messages ($imap_stream, $mailbox) {
205 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
206 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
207 for ($i = 0; $i < count($read_ary); $i++
) {
208 if (substr(trim($read_ary[$i]), -6) == EXISTS
) {
209 $array = explode (" ", $read_ary[$i]);
217 /******************************************************************************
218 ** Returns a displayable email address
219 ******************************************************************************/
220 function sqimap_find_email ($string) {
221 /** Luke Ehresman <lehresma@css.tayloru.edu>
222 ** <lehresma@css.tayloru.edu>
223 ** lehresma@css.tayloru.edu
226 if (strpos($string, "<") && strpos($string, ">")) {
227 $string = substr($string, strpos($string, "<")+
1);
228 $string = substr($string, 0, strpos($string, ">"));
230 return trim($string);
234 /******************************************************************************
235 ** Takes the From: field, and creates a displayable name.
236 ** Luke Ehresman <lkehresman@yahoo.com>
237 ** becomes: Luke Ehresman
238 ** <lkehresman@yahoo.com>
239 ** becomes: lkehresman@yahoo.com
240 ******************************************************************************/
241 function sqimap_find_displayable_name ($string) {
242 $string = " ".trim($string);
243 $orig_string = $string;
244 if (strpos($string, "<") && strpos($string, ">")) {
245 if (strpos($string, "<") == 1) {
246 $string = sqimap_find_email($string);
248 $string = trim($string);
249 $string = substr($string, 0, strpos($string, "<"));
250 $string = ereg_replace ("\"", "", $string);
253 if (trim($string) == "") {
254 $string = sqimap_find_email($orig_string);
261 /******************************************************************************
262 ** Returns the number of unseen messages in this folder
263 ******************************************************************************/
264 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
265 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
266 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
267 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
270 $read_ary[0] = trim($read_ary[0]);
271 return substr($read_ary[0], strrpos($read_ary[0], " ")+
1, (strlen($read_ary[0]) - strrpos($read_ary[0], " ") - 2));
275 /******************************************************************************
276 ** Saves a message to a given folder -- used for saving sent messages
277 ******************************************************************************/
278 function sqimap_append ($imap_stream, $sent_folder, $length) {
279 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
280 $tmp = fgets ($imap_stream, 1024);
283 function sqimap_append_done ($imap_stream) {
284 fputs ($imap_stream, "\r\n");
285 $tmp = fgets ($imap_stream, 1024);