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