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