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