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