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