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