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