added CHARSET option to searching.. should fix the german character
[squirrelmail.git] / functions / imap_general.php
CommitLineData
59177427 1<?php
052e0c26 2 /**
3 ** imap.php
4 **
5 ** This implements all functions that do general imap functions.
6 **/
7
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) {
14 global $color;
15
a3432f47 16 //$imap_general_debug = true;
17 $imap_general_debug = false;
18
052e0c26 19 $read = fgets ($imap_stream, 1024);
a3432f47 20 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
052e0c26 21 $counter = 0;
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);
a3432f47 27 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
052e0c26 28 $counter++;
29 }
a3432f47 30 if ($imap_general_debug) echo "--<br>";
052e0c26 31 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
32 $response = "OK";
33 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
34 }
35 else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
36 $response = "BAD";
37 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
38 }
39 else {
40 $response = "NO";
41 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
42 }
43
44 if ($handle_errors == true) {
45 if ($response == "NO") {
04632dbc 46 echo "<br><b><font color=$color[2]>\n";
052e0c26 47 echo _("ERROR : Could not complete request.");
04632dbc 48 echo "</b><br>\n";
052e0c26 49 echo _("Reason Given: ");
04632dbc 50 echo "$message</font><br>\n";
052e0c26 51 exit;
52 } else if ($response == "BAD") {
04632dbc 53 echo "<br><b><font color=$color[2]>\n";
052e0c26 54 echo _("ERROR : Bad or malformed request.");
04632dbc 55 echo "</b><br>\n";
052e0c26 56 echo _("Server responded: ");
04632dbc 57 echo "$message</font><br>\n";
052e0c26 58 exit;
59 }
60 }
61
62 return $data;
63 }
64
65
66
67
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) {
52eefafc 73 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
1b187352 74
052e0c26 75 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
76 $server_info = fgets ($imap_stream, 1024);
77
52eefafc 78 // Decrypt the password
79 $password = OneTimePadDecrypt($password, $onetimepad);
80
1b187352 81 // This function can sometimes be called before the check for
82 // gettext is done.
83 if (!function_exists("_")) {
84 function _($string) {
85 return $string;
86 }
87 }
88
052e0c26 89 /** Do some error correction **/
90 if (!$imap_stream) {
91 if (!$hide) {
1b187352 92 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
052e0c26 93 echo "$error_number : $error_string<br>\r\n";
94 }
95 exit;
96 }
97
98 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
99 $read = fgets ($imap_stream, 1024);
100
101 /** If the connection was not successful, lets see why **/
102 if (substr($read, 0, 7) != "a001 OK") {
103 if (!$hide) {
104 if (substr($read, 0, 8) == "a001 BAD") {
1b187352 105 printf (_("Bad request: %s")."<br>\r\n", $read);
052e0c26 106 exit;
107 } else if (substr($read, 0, 7) == "a001 NO") {
1b187352 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
112 // screen.
113
114 // $squirrelmail_language is set by a cookie when
115 // the user selects language and logs out
116
117 // Use HTTP content language negotiation if cookie
118 // not set
119 if (!isset($squirrelmail_language) && isset($HTTP_ACCEPT_LANGUAGE)) {
120 $squirrelmail_language = substr($HTTP_ACCEPT_LANGUAGE, 0, 2);
121 }
122
25c078d3 123 if (isset($squirrelmail_language) && function_exists("bindtextdomain")) {
1b187352 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"]);
129 }
130 }
131
052e0c26 132 ?>
133 <html>
134 <body bgcolor=ffffff>
135 <br>
136 <center>
137 <table width=70% noborder bgcolor=ffffff align=center>
138 <tr>
139 <td bgcolor=dcdcdc>
140 <font color=cc0000>
141 <center>
59177427 142 <?php echo _("ERROR") ?>
052e0c26 143 </center>
144 </font>
145 </td>
146 </tr>
147 <tr>
148 <td>
149 <center>
59177427 150 <?php echo _("Unknown user or password incorrect.") ?><br>
151 <a href="login.php"><?php echo _("Click here to try again") ?></a>
052e0c26 152 </center>
153 </td>
154 </tr>
155 </table>
156 </center>
157 </body>
158 </html>
59177427 159 <?php
052e0c26 160 session_destroy();
161 exit;
162 } else {
1b187352 163 printf (_("Unknown error: %s")."<br>", $read);
052e0c26 164 exit;
165 }
166 } else {
167 exit;
168 }
169 }
170
171 return $imap_stream;
172 }
173
174
175
176
177 /******************************************************************************
178 ** Simply logs out the imap session
179 ******************************************************************************/
180 function sqimap_logout ($imap_stream) {
181 fputs ($imap_stream, "a001 LOGOUT\r\n");
182 }
183
184
185
186 /******************************************************************************
187 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
188 ******************************************************************************/
189 function sqimap_get_delimiter ($imap_stream) {
8e9e8afa 190 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
052e0c26 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);
194
195 return $delim;
196 }
197
198
199
200
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]);
210 $num = $array[1];
211 }
212 }
213 return $num;
214 }
215
216
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
224 **/
225
226 if (strpos($string, "<") && strpos($string, ">")) {
227 $string = substr($string, strpos($string, "<")+1);
228 $string = substr($string, 0, strpos($string, ">"));
229 }
230 return trim($string);
231 }
232
233
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);
5967e1b4 243 $orig_string = $string;
052e0c26 244 if (strpos($string, "<") && strpos($string, ">")) {
245 if (strpos($string, "<") == 1) {
246 $string = sqimap_find_email($string);
247 } else {
248 $string = trim($string);
249 $string = substr($string, 0, strpos($string, "<"));
250 $string = ereg_replace ("\"", "", $string);
251 }
5967e1b4 252
253 if (trim($string) == "") {
254 $string = sqimap_find_email($orig_string);
255 }
052e0c26 256 }
257 return $string;
258 }
259
260
052e0c26 261 /******************************************************************************
262 ** Returns the number of unseen messages in this folder
263 ******************************************************************************/
c5eb2c03 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");
052e0c26 267 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
268 $unseen = false;
269
c5eb2c03 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));
052e0c26 272 }
273
274
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);
281 }
282
283 function sqimap_append_done ($imap_stream) {
284 fputs ($imap_stream, "\r\n");
285 $tmp = fgets ($imap_stream, 1024);
286 }
287?>