Minor change of wording...
[squirrelmail.git] / functions / imap_general.php
1 <?php
2 /**
3 ** imap.php
4 **
5 ** This implements all functions that do general imap functions.
6 **
7 ** $Id$
8 **/
9
10 if (defined ('imap_general_php'))
11 return;
12 define ('imap_general_php', true);
13
14 global $imap_general_debug;
15 $imap_general_debug = false;
16
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
23 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
24 &$response, &$message) {
25 global $color, $squirrelmail_language, $imap_general_debug;
26
27 $read = "";
28 $resultlist = array();
29
30 $more_msgs = true;
31 while ($more_msgs) {
32 $data = array();
33 $total_size = 0;
34 while (strpos($read, "\n") === false) {
35 $read .= fgets($imap_stream, 9096);
36 }
37
38 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
39 $size = $regs[1];
40 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
41 // Sizeless response, probably single-line
42 // For debugging purposes
43 if ($imap_general_debug) {
44 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
45 flush();
46 }
47 $size = 0;
48 $data[] = $read;
49 $read = fgets($imap_stream, 9096);
50 } else {
51 $size = 0;
52 }
53 while (1) {
54 while (strpos($read, "\n") === false) {
55 $read .= fgets($imap_stream, 9096);
56 }
57 // For debugging purposes
58 if ($imap_general_debug) {
59 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
60 flush();
61 }
62 // If we know the size, no need to look at the end parameters
63 if ($size > 0) {
64 if ($total_size == $size) {
65 // We've reached the end of this 'message', switch to the next one.
66 $data[] = $read;
67 break;
68 } else if ($total_size > $size) {
69 $difference = $total_size - $size;
70 $total_size = $total_size - strlen($read);
71 $data[] = substr ($read, 0, strlen($read)-$difference);
72 $read = substr ($read, strlen($read)-$difference, strlen($read));
73 break;
74 } else {
75 $data[] = $read;
76 $read = fgets($imap_stream, 9096);
77 while (strpos($read, "\n") === false) {
78 $read .= fgets($imap_stream, 9096);
79 }
80 }
81 $total_size += strlen($read);
82 } else {
83 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
84 ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
85 break;
86 } else {
87 $data[] = $read;
88 $read = fgets ($imap_stream, 9096);
89 }
90 }
91 }
92
93 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
94 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
95 $read = fgets($imap_stream, 9096);
96 }
97 $resultlist[] = $data;
98 }
99 $response = $regs[1];
100 $message = trim($regs[2]);
101
102 if ($imap_general_debug) echo '--<br>';
103
104 if ($handle_errors == false)
105 return $resultlist;
106
107 if ($response == 'NO') {
108 // ignore this error from m$ exchange, it is not fatal (aka bug)
109 if (strstr($message, 'command resulted in') === false) {
110 set_up_language($squirrelmail_language);
111 echo "<br><b><font color=$color[2]>\n";
112 echo _("ERROR : Could not complete request.");
113 echo "</b><br>\n";
114 echo _("Reason Given: ");
115 echo $message . "</font><br>\n";
116 exit;
117 }
118 } else if ($response == 'BAD') {
119 set_up_language($squirrelmail_language);
120 echo "<br><b><font color=$color[2]>\n";
121 echo _("ERROR : Bad or malformed request.");
122 echo "</b><br>\n";
123 echo _("Server responded: ");
124 echo $message . "</font><br>\n";
125 exit;
126 }
127 return $resultlist;
128 }
129
130 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
131 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
132 return $res[0];
133 }
134
135 /******************************************************************************
136 ** Logs the user into the imap server. If $hide is set, no error messages
137 ** will be displayed. This function returns the imap connection handle.
138 ******************************************************************************/
139 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
140 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
141
142 $imap_stream = fsockopen ($imap_server_address, $imap_port,
143 $error_number, $error_string, 15);
144 $server_info = fgets ($imap_stream, 1024);
145
146 // Decrypt the password
147 $password = OneTimePadDecrypt($password, $onetimepad);
148
149 /** Do some error correction **/
150 if (!$imap_stream) {
151 if (!$hide) {
152 set_up_language($squirrelmail_language, true);
153 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
154 echo "$error_number : $error_string<br>\r\n";
155 }
156 exit;
157 }
158
159 fputs ($imap_stream, "a001 LOGIN \"" . quoteIMAP($username) .
160 '" "' . quoteIMAP($password) . "\"\r\n");
161 $read = sqimap_read_data ($imap_stream, 'a001', false, $response, $message);
162
163 /** If the connection was not successful, lets see why **/
164 if ($response != "OK") {
165 if (!$hide) {
166 if ($response != 'NO') {
167 // "BAD" and anything else gets reported here.
168 set_up_language($squirrelmail_language, true);
169 if ($response == 'BAD')
170 printf (_("Bad request: %s")."<br>\r\n", $message);
171 else
172 printf (_("Unknown error: %s") . "<br>\n", $message);
173 echo '<br>';
174 echo _("Read data:") . "<br>\n";
175 if (is_array($read))
176 {
177 foreach ($read as $line)
178 {
179 echo htmlspecialchars($line) . "<br>\n";
180 }
181 }
182 exit;
183 } else {
184 // If the user does not log in with the correct
185 // username and password it is not possible to get the
186 // correct locale from the user's preferences.
187 // Therefore, apply the same hack as on the login
188 // screen.
189
190 // $squirrelmail_language is set by a cookie when
191 // the user selects language and logs out
192
193 set_up_language($squirrelmail_language, true);
194
195 ?>
196 <html>
197 <body bgcolor="ffffff">
198 <br>
199 <center>
200 <table width="70%" noborder bgcolor="ffffff" align="center">
201 <tr>
202 <td bgcolor="dcdcdc">
203 <font color="cc0000">
204 <center>
205 <?php echo _("ERROR") ?>
206 </center>
207 </font>
208 </td>
209 </tr>
210 <tr>
211 <td>
212 <center>
213 <?php echo _("Unknown user or password incorrect.") ?><br>
214 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
215 </center>
216 </td>
217 </tr>
218 </table>
219 </center>
220 </body>
221 </html>
222 <?php
223 session_destroy();
224 exit;
225 }
226 } else {
227 exit;
228 }
229 }
230
231 return $imap_stream;
232 }
233
234
235
236
237 /******************************************************************************
238 ** Simply logs out the imap session
239 ******************************************************************************/
240 function sqimap_logout ($imap_stream) {
241 fputs ($imap_stream, "a001 LOGOUT\r\n");
242 }
243
244 function sqimap_capability($imap_stream, $capability) {
245 global $sqimap_capabilities;
246 global $imap_general_debug;
247
248 if (!is_array($sqimap_capabilities)) {
249 fputs ($imap_stream, "a001 CAPABILITY\r\n");
250 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
251
252 $c = explode(' ', $read[0]);
253 for ($i=2; $i < count($c); $i++) {
254 $cap_list = explode('=', $c[$i]);
255 if (isset($cap_list[1]))
256 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
257 else
258 $sqimap_capabilities[$cap_list[0]] = TRUE;
259 }
260 }
261 if (! isset($sqimap_capabilities[$capability]))
262 return false;
263 return $sqimap_capabilities[$capability];
264 }
265
266 /******************************************************************************
267 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
268 ******************************************************************************/
269 function sqimap_get_delimiter ($imap_stream = false) {
270 global $imap_general_debug;
271 global $sqimap_delimiter;
272 global $optional_delimiter;
273
274 /* Use configured delimiter if set */
275 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
276 return $optional_delimiter;
277
278 /* Do some caching here */
279 if (!$sqimap_delimiter) {
280 if (sqimap_capability($imap_stream, "NAMESPACE")) {
281 /* According to something that I can't find, this is supposed to work on all systems
282 OS: This won't work in Courier IMAP.
283 OS: According to rfc2342 response from NAMESPACE command is:
284 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
285 OS: We want to lookup all personal NAMESPACES...
286 */
287 fputs ($imap_stream, "a001 NAMESPACE\r\n");
288 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
289 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
290 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
291 $pn = $data2[1];
292 $pna = explode(')(', $pn);
293 while (list($k, $v) = each($pna))
294 {
295 $lst = explode('"', $v);
296 if (isset($lst[3])) {
297 $pn[$lst[1]] = $lst[3];
298 } else {
299 $pn[$lst[1]] = '';
300 }
301 }
302 }
303 $sqimap_delimiter = $pn[0];
304 } else {
305 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
306 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
307 $quote_position = strpos ($read[0], '"');
308 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
309 }
310 }
311 return $sqimap_delimiter;
312 }
313
314
315 /******************************************************************************
316 ** Gets the number of messages in the current mailbox.
317 ******************************************************************************/
318 function sqimap_get_num_messages ($imap_stream, $mailbox) {
319 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
320 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
321 for ($i = 0; $i < count($read_ary); $i++) {
322 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
323 return $regs[1];
324 }
325 }
326 return "BUG! Couldn't get number of messages in $mailbox!";
327 }
328
329
330 /******************************************************************************
331 ** Returns a displayable email address
332 ******************************************************************************/
333 function sqimap_find_email ($string) {
334 /** Luke Ehresman <lehresma@css.tayloru.edu>
335 ** <lehresma@css.tayloru.edu>
336 ** lehresma@css.tayloru.edu
337 **
338 ** What about
339 ** lehresma@css.tayloru.edu (Luke Ehresman)
340 **/
341
342 if (ereg("<([^>]+)>", $string, $regs)) {
343 $string = $regs[1];
344 }
345 return trim($string);
346 }
347
348
349 /******************************************************************************
350 ** Takes the From: field, and creates a displayable name.
351 ** Luke Ehresman <lkehresman@yahoo.com>
352 ** becomes: Luke Ehresman
353 ** <lkehresman@yahoo.com>
354 ** becomes: lkehresman@yahoo.com
355 ******************************************************************************/
356 function sqimap_find_displayable_name ($string) {
357 $string = ' '.trim($string);
358 $orig_string = $string;
359 if (strpos($string, '<') && strpos($string, '>')) {
360 if (strpos($string, '<') == 1) {
361 $string = sqimap_find_email($string);
362 } else {
363 $string = trim($string);
364 $string = substr($string, 0, strpos($string, '<'));
365 $string = ereg_replace ('"', '', $string);
366 }
367
368 if (trim($string) == '') {
369 $string = sqimap_find_email($orig_string);
370 }
371 }
372 return $string;
373 }
374
375
376 /******************************************************************************
377 ** Returns the number of unseen messages in this folder
378 ******************************************************************************/
379 function sqimap_unseen_messages ($imap_stream, $mailbox) {
380 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
381 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
382 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
383 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
384 return $regs[1];
385 }
386
387
388 /******************************************************************************
389 ** Saves a message to a given folder -- used for saving sent messages
390 ******************************************************************************/
391 function sqimap_append ($imap_stream, $sent_folder, $length) {
392 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
393 $tmp = fgets ($imap_stream, 1024);
394 }
395
396 function sqimap_append_done ($imap_stream) {
397 fputs ($imap_stream, "\r\n");
398 $tmp = fgets ($imap_stream, 1024);
399 }
400 ?>