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