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