changed unclosed comment tags /* on lines 315 & 316 to //
[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 /******************************************************************************
9 ** Reads the output from the IMAP stream. If handle_errors is set to true,
10 ** this will also handle all errors that are received. If it is not set,
11 ** the errors will be sent back through $response and $message
12 ******************************************************************************/
13 function sqimap_read_data ($imap_stream, $pre, $handle_errors, $response, $message) {
14 global $color;
15
16 $read = fgets ($imap_stream, 1024);
17 // echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
18 $counter = 0;
19 while ((substr($read, 0, strlen("$pre OK")) != "$pre OK") &&
20 (substr($read, 0, strlen("$pre BAD")) != "$pre BAD") &&
21 (substr($read, 0, strlen("$pre NO")) != "$pre NO")) {
22 $data[$counter] = $read;
23 $read = fgets ($imap_stream, 1024);
24 // echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
25 $counter++;
26 }
27 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
28 $response = "OK";
29 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
30 }
31 else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
32 $response = "BAD";
33 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
34 }
35 else {
36 $response = "NO";
37 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
38 }
39
40 if ($handle_errors == true) {
41 if ($response == "NO") {
42 echo "<br><b><font color=$color[2]>";
43 echo _("ERROR : Could not complete request.");
44 echo "</b><br>";
45 echo _("Reason Given: ");
46 echo "$message</font><br>";
47 exit;
48 } else if ($response == "BAD") {
49 echo "<br><b><font color=$color[2]>";
50 echo _("ERROR : Bad or malformed request.");
51 echo "</b><br>";
52 echo _("Server responded: ");
53 echo "$message</font><br>";
54 exit;
55 }
56 }
57
58 return $data;
59 }
60
61
62
63
64 /******************************************************************************
65 ** Logs the user into the imap server. If $hide is set, no error messages
66 ** will be displayed. This function returns the imap connection handle.
67 ******************************************************************************/
68 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
69 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE;
70
71 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
72 $server_info = fgets ($imap_stream, 1024);
73
74 // This function can sometimes be called before the check for
75 // gettext is done.
76 if (!function_exists("_")) {
77 function _($string) {
78 return $string;
79 }
80 }
81
82 /** Do some error correction **/
83 if (!$imap_stream) {
84 if (!$hide) {
85 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
86 echo "$error_number : $error_string<br>\r\n";
87 }
88 exit;
89 }
90
91 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
92 $read = fgets ($imap_stream, 1024);
93
94 /** If the connection was not successful, lets see why **/
95 if (substr($read, 0, 7) != "a001 OK") {
96 if (!$hide) {
97 if (substr($read, 0, 8) == "a001 BAD") {
98 printf (_("Bad request: %s")."<br>\r\n", $read);
99 exit;
100 } else if (substr($read, 0, 7) == "a001 NO") {
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 // Use HTTP content language negotiation if cookie
111 // not set
112 if (!isset($squirrelmail_language) && isset($HTTP_ACCEPT_LANGUAGE)) {
113 $squirrelmail_language = substr($HTTP_ACCEPT_LANGUAGE, 0, 2);
114 }
115
116 if (isset($squirrelmail_language)) {
117 if ($squirrelmail_language != "en" && $squirrelmail_language != "") {
118 putenv("LC_ALL=".$squirrelmail_language);
119 bindtextdomain("squirrelmail", "../locale/");
120 textdomain("squirrelmail");
121 header ("Content-Type: text/html; charset=".$languages[$squirrelmail_language]["CHARSET"]);
122 }
123 }
124
125 ?>
126 <html>
127 <body bgcolor=ffffff>
128 <br>
129 <center>
130 <table width=70% noborder bgcolor=ffffff align=center>
131 <tr>
132 <td bgcolor=dcdcdc>
133 <font color=cc0000>
134 <center>
135 <?php echo _("ERROR") ?>
136 </center>
137 </font>
138 </td>
139 </tr>
140 <tr>
141 <td>
142 <center>
143 <?php echo _("Unknown user or password incorrect.") ?><br>
144 <a href="login.php"><?php echo _("Click here to try again") ?></a>
145 </center>
146 </td>
147 </tr>
148 </table>
149 </center>
150 </body>
151 </html>
152 <?php
153 session_destroy();
154 exit;
155 } else {
156 printf (_("Unknown error: %s")."<br>", $read);
157 exit;
158 }
159 } else {
160 exit;
161 }
162 }
163
164 return $imap_stream;
165 }
166
167
168
169
170 /******************************************************************************
171 ** Simply logs out the imap session
172 ******************************************************************************/
173 function sqimap_logout ($imap_stream) {
174 fputs ($imap_stream, "a001 LOGOUT\r\n");
175 }
176
177
178
179 /******************************************************************************
180 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
181 ******************************************************************************/
182 function sqimap_get_delimiter ($imap_stream) {
183 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
184 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
185 $quote_position = strpos ($read[0], "\"");
186 $delim = substr ($read[0], $quote_position+1, 1);
187
188 return $delim;
189 }
190
191
192
193
194 /******************************************************************************
195 ** Gets the number of messages in the current mailbox.
196 ******************************************************************************/
197 function sqimap_get_num_messages ($imap_stream, $mailbox) {
198 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
199 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
200 for ($i = 0; $i < count($read_ary); $i++) {
201 if (substr(trim($read_ary[$i]), -6) == EXISTS) {
202 $array = explode (" ", $read_ary[$i]);
203 $num = $array[1];
204 }
205 }
206 return $num;
207 }
208
209
210 /******************************************************************************
211 ** Returns a displayable email address
212 ******************************************************************************/
213 function sqimap_find_email ($string) {
214 /** Luke Ehresman <lehresma@css.tayloru.edu>
215 ** <lehresma@css.tayloru.edu>
216 ** lehresma@css.tayloru.edu
217 **/
218
219 if (strpos($string, "<") && strpos($string, ">")) {
220 $string = substr($string, strpos($string, "<")+1);
221 $string = substr($string, 0, strpos($string, ">"));
222 }
223 return trim($string);
224 }
225
226
227 /******************************************************************************
228 ** Takes the From: field, and creates a displayable name.
229 ** Luke Ehresman <lkehresman@yahoo.com>
230 ** becomes: Luke Ehresman
231 ** <lkehresman@yahoo.com>
232 ** becomes: lkehresman@yahoo.com
233 ******************************************************************************/
234 function sqimap_find_displayable_name ($string) {
235 $string = " ".trim($string);
236 if (strpos($string, "<") && strpos($string, ">")) {
237 if (strpos($string, "<") == 1) {
238 $string = sqimap_find_email($string);
239 } else {
240 $string = trim($string);
241 $string = substr($string, 0, strpos($string, "<"));
242 $string = ereg_replace ("\"", "", $string);
243 }
244 }
245 return $string;
246 }
247
248
249
250 /******************************************************************************
251 ** Returns the number of unseen messages in this folder
252 ******************************************************************************/
253 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
254 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
255 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
256 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
257 $unseen = false;
258
259 $read_ary[0] = trim($read_ary[0]);
260 return substr($read_ary[0], strrpos($read_ary[0], " ")+1, (strlen($read_ary[0]) - strrpos($read_ary[0], " ") - 2));
261 }
262
263
264 /******************************************************************************
265 ** Saves a message to a given folder -- used for saving sent messages
266 ******************************************************************************/
267 function sqimap_append ($imap_stream, $sent_folder, $length) {
268 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
269 $tmp = fgets ($imap_stream, 1024);
270 }
271
272 function sqimap_append_done ($imap_stream) {
273 fputs ($imap_stream, "\r\n");
274 $tmp = fgets ($imap_stream, 1024);
275 }
276 ?>