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