initial import of german help (from "Ilja Gerhardt" <ilja@cryptix.de>)
[squirrelmail.git] / functions / imap_general.php
1 <?php
2
3 /**
4 * imap_general.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project 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 require_once('../functions/page_header.php');
15
16 global $sqimap_session_id;
17 $sqimap_session_id = 1;
18
19 /* Sets an unique session id in order to avoid simultanous sessions crash. */
20 function sqimap_session_id() {
21 global $data_dir, $username, $sqimap_session_id;
22 return( sprintf("A%03d", $sqimap_session_id++) );
23 }
24
25 /*
26 * Both send a command and accept the result from the command.
27 * This is to allow proper session number handling.
28 */
29 function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) {
30 $sid = sqimap_session_id();
31 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
32 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
33 return $read;
34 }
35
36 function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) {
37 $sid = sqimap_session_id();
38 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
39 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
40 return $read;
41 }
42
43
44 /*
45 * Reads the output from the IMAP stream. If handle_errors is set to true,
46 * this will also handle all errors that are received. If it is not set,
47 * the errors will be sent back through $response and $message
48 */
49 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
50 global $color, $squirrelmail_language;
51
52 $read = '';
53 $bufsize = 9096;
54 $resultlist = array();
55
56 $more_msgs = true;
57 while ($more_msgs) {
58 $data = array();
59 $total_size = 0;
60
61 while (strpos($read, "\n") === false) {
62 if(!($read .= fgets($imap_stream, $bufsize))) {
63 break;
64 }
65 }
66
67 /* if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { */
68 if (preg_match('/^\* [0-9]+ FETCH.*\{([0-9]+)\}/', $read, $regs)) {
69 $size = $regs[1];
70 /* } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) { */
71 } else if (preg_match('/^\* [0-9]+ FETCH/', $read, $regs)) {
72 /* Sizeless response, probably single-line */
73 $size = -1;
74 $data[] = $read;
75 $read = fgets($imap_stream, $bufsize);
76 } else {
77 $size = -1;
78 }
79 while (1) {
80 while (strpos($read, "\n") === false) {
81 if(!($read .= fgets($imap_stream, $bufsize))) {
82 break;
83 }
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, $bufsize);
100 while (strpos($read, "\n") === false) {
101 $read .= fgets($imap_stream, $bufsize);
102 }
103 }
104 $total_size += strlen($read);
105 } else {
106 /* if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) || */
107 if (preg_match("/^$pre (OK|BAD|NO)(.*)/", $read, $regs) ||
108 /* (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) { */
109 (($size == -1) && preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs))) {
110 break;
111 } else if ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
112 /*
113 * This block has been added in order to avoid the problem
114 * caused by the * OK [PARSE] Missing parameter answer
115 * Please, replace it with a better parsing if you know how.
116 * This block has been updated by
117 * Seth E. Randall <sethr@missoulafcu.org>. Once we see
118 * one OK [PARSE line, we just go through and keep
119 * tossing them out until we get something different.
120 */
121 while ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
122 $read = fgets($imap_stream, $bufsize);
123 }
124 $data[] = $read;
125 $read = fgets ($imap_stream, $bufsize);
126 } else if (preg_match('/^\* BYE \[ALERT\](.*)/', $read, $regs)) {
127 /*
128 * It seems that the IMAP server has coughed up a lung
129 * and hung up the connection. Print any info we have
130 * and abort.
131 */
132 echo _("Please contact your system administrator and report the following error:") . "<br>\n";
133 echo $regs[1];
134 exit;
135 } else {
136 $data[] = $read;
137 $read = fgets ($imap_stream, $bufsize);
138 }
139 }
140 }
141
142 /*
143 * while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
144 * !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
145 */
146 while (($more_msgs = !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs)) &&
147 !preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs)) {
148 $read = fgets($imap_stream, $bufsize);
149 }
150 $resultlist[] = $data;
151 }
152
153 $response = $regs[1];
154 $message = trim($regs[2]);
155
156 if ($handle_errors == false) {
157 return( $resultlist );
158 } else if ($response == 'NO') {
159 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
160 if (strstr($message, 'command resulted in') === false) {
161 set_up_language($squirrelmail_language);
162 echo "<br><b><font color=$color[2]>\n" .
163 _("ERROR : Could not complete request.") .
164 "</b><br>\n" .
165 _("Query:") .
166 $query . '<br>' .
167 _("Reason Given: ") .
168 $message . "</font><br>\n";
169 exit;
170 }
171 } else if ($response == 'BAD') {
172 set_up_language($squirrelmail_language);
173 echo "<br><b><font color=$color[2]>\n" .
174 _("ERROR : Bad or malformed request.") .
175 "</b><br>\n" .
176 _("Query:") .
177 $query . '<br>' .
178 _("Server responded: ") .
179 $message . "</font><br>\n";
180 exit;
181 } else {
182 return $resultlist;
183 }
184 }
185
186 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
187 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
188 return $res[0];
189 }
190
191 /*
192 * Logs the user into the imap server. If $hide is set, no error messages
193 * will be displayed. This function returns the imap connection handle.
194 */
195 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
196 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
197
198 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
199 if ( !$imap_stream ) {
200 return false;
201 }
202 $server_info = fgets ($imap_stream, 1024);
203
204 /* Decrypt the password */
205 $password = OneTimePadDecrypt($password, $onetimepad);
206
207 /* Do some error correction */
208 if (!$imap_stream) {
209 if (!$hide) {
210 set_up_language($squirrelmail_language, true);
211 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
212 echo "$error_number : $error_string<br>\r\n";
213 }
214 exit;
215 }
216
217 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
218 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
219
220 /* If the connection was not successful, lets see why */
221 if ($response != 'OK') {
222 if (!$hide) {
223 if ($response != 'NO') {
224 /* "BAD" and anything else gets reported here. */
225 set_up_language($squirrelmail_language, true);
226 if ($response == 'BAD') {
227 printf (_("Bad request: %s")."<br>\r\n", $message);
228 } else {
229 printf (_("Unknown error: %s") . "<br>\n", $message);
230 }
231 echo '<br>' . _("Read data:") . "<br>\n";
232 if (is_array($read)) {
233 foreach ($read as $line) {
234 echo htmlspecialchars($line) . "<br>\n";
235 }
236 }
237 exit;
238 } else {
239 /*
240 * If the user does not log in with the correct
241 * username and password it is not possible to get the
242 * correct locale from the user's preferences.
243 * Therefore, apply the same hack as on the login
244 * screen.
245 *
246 * $squirrelmail_language is set by a cookie when
247 * the user selects language and logs out
248 */
249
250 set_up_language($squirrelmail_language, true);
251 include_once( '../functions/display_messages.php' );
252 logout_error( _("Unknown user or password incorrect.") );
253 session_destroy();
254 exit;
255 }
256 } else {
257 exit;
258 }
259 }
260 return $imap_stream;
261 }
262
263 /* Simply logs out the IMAP session */
264 function sqimap_logout ($imap_stream) {
265 /* Logout is not valid until the server returns 'BYE' */
266 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
267 }
268
269 function sqimap_capability($imap_stream, $capability) {
270 global $sqimap_capabilities;
271
272 if (!is_array($sqimap_capabilities)) {
273 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
274
275 $c = explode(' ', $read[0]);
276 for ($i=2; $i < count($c); $i++) {
277 $cap_list = explode('=', $c[$i]);
278 if (isset($cap_list[1])) {
279 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
280 } else {
281 $sqimap_capabilities[$cap_list[0]] = TRUE;
282 }
283 }
284 }
285 if (isset($sqimap_capabilities[$capability])) {
286 return $sqimap_capabilities[$capability];
287 } else {
288 return false;
289 }
290 }
291
292 /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
293 function sqimap_get_delimiter ($imap_stream = false) {
294 global $sqimap_delimiter, $optional_delimiter;
295
296 /* Use configured delimiter if set */
297 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
298 return $optional_delimiter;
299 }
300
301 /* Do some caching here */
302 if (!$sqimap_delimiter) {
303 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
304 /*
305 * According to something that I can't find, this is supposed to work on all systems
306 * OS: This won't work in Courier IMAP.
307 * OS: According to rfc2342 response from NAMESPACE command is:
308 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
309 * OS: We want to lookup all personal NAMESPACES...
310 */
311 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
312 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
313 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
314 $pn = $data2[1];
315 }
316 $pna = explode(')(', $pn);
317 while (list($k, $v) = each($pna)) {
318 $lst = explode('"', $v);
319 if (isset($lst[3])) {
320 $pn[$lst[1]] = $lst[3];
321 } else {
322 $pn[$lst[1]] = '';
323 }
324 }
325 }
326 $sqimap_delimiter = $pn[0];
327 } else {
328 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
329 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
330 $quote_position = strpos ($read[0], '"');
331 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
332 }
333 }
334 return $sqimap_delimiter;
335 }
336
337
338 /* Gets the number of messages in the current mailbox. */
339 function sqimap_get_num_messages ($imap_stream, $mailbox) {
340 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", 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 /* Returns a displayable email address.
351 * Luke Ehresman <lehresma@css.tayloru.edu>
352 * "Luke Ehresman" <lehresma@css.tayloru.edu>
353 * <lehresma@css.tayloru.edu>
354 * lehresma@css.tayloru.edu (Luke Ehresman)
355 * lehresma@css.tayloru.edu
356 * becomes: lehresma@css.tayloru.edu
357 */
358 function sqimap_find_email ($string) {
359 if (ereg("<([^>]+)>", $string, $regs)) {
360 $string = $regs[1];
361 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
362 $string = $regs[1];
363 }
364 return trim($string);
365 }
366
367
368 /*
369 * Takes the From: field and creates a displayable name.
370 * Luke Ehresman <lkehresman@yahoo.com>
371 * "Luke Ehresman" <lkehresman@yahoo.com>
372 * lkehresman@yahoo.com (Luke Ehresman)
373 * becomes: Luke Ehresman
374 * <lkehresman@yahoo.com>
375 * becomes: lkehresman@yahoo.com
376 */
377 function sqimap_find_displayable_name ($string) {
378 $string = trim($string);
379
380 if ( ereg('^(.+)<.*>', $string, $regs) ) {
381 $orig_string = $string;
382 $string = str_replace ('"', '', $regs[1] );
383 if (trim($string) == '') {
384 $string = sqimap_find_email($orig_string);
385 }
386 if( $string == '' || $string == ' ' ){
387 $string = '&nbsp';
388 }
389 }
390 elseif ( ereg('\((.*)\)', $string, $regs) ) {
391 if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){
392 if ( ereg('^(.+) \(', $string, $regs) ) {
393 $string = ereg_replace( ' \(\)$', '', $string );
394 } else {
395 $string = '&nbsp';
396 }
397 } else {
398 $string = $regs[1];
399 }
400 }
401 else {
402 $string = str_replace ('"', '', sqimap_find_email($string));
403 }
404
405 return trim($string);
406 }
407
408 /*
409 * Returns the number of unseen messages in this folder
410 */
411 function sqimap_unseen_messages ($imap_stream, $mailbox) {
412 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
413 $i = 0;
414 while (isset($read_ary[$i])) {
415 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
416 break;
417 }
418 $i++;
419 }
420 return $regs[1];
421 }
422
423
424 /*
425 * Saves a message to a given folder -- used for saving sent messages
426 */
427 function sqimap_append ($imap_stream, $sent_folder, $length) {
428 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
429 $tmp = fgets ($imap_stream, 1024);
430 }
431
432 function sqimap_append_done ($imap_stream) {
433 fputs ($imap_stream, "\r\n");
434 $tmp = fgets ($imap_stream, 1024);
435 }
436
437 ?>