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