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