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