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) { |
1b187352 |
70 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE; |
71 | |
052e0c26 |
72 | $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string); |
73 | $server_info = fgets ($imap_stream, 1024); |
74 | |
1b187352 |
75 | // This function can sometimes be called before the check for |
76 | // gettext is done. |
77 | if (!function_exists("_")) { |
78 | function _($string) { |
79 | return $string; |
80 | } |
81 | } |
82 | |
052e0c26 |
83 | /** Do some error correction **/ |
84 | if (!$imap_stream) { |
85 | if (!$hide) { |
1b187352 |
86 | printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address); |
052e0c26 |
87 | echo "$error_number : $error_string<br>\r\n"; |
88 | } |
89 | exit; |
90 | } |
91 | |
92 | fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n"); |
93 | $read = fgets ($imap_stream, 1024); |
94 | |
95 | /** If the connection was not successful, lets see why **/ |
96 | if (substr($read, 0, 7) != "a001 OK") { |
97 | if (!$hide) { |
98 | if (substr($read, 0, 8) == "a001 BAD") { |
1b187352 |
99 | printf (_("Bad request: %s")."<br>\r\n", $read); |
052e0c26 |
100 | exit; |
101 | } else if (substr($read, 0, 7) == "a001 NO") { |
1b187352 |
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 | // Use HTTP content language negotiation if cookie |
112 | // not set |
113 | if (!isset($squirrelmail_language) && isset($HTTP_ACCEPT_LANGUAGE)) { |
114 | $squirrelmail_language = substr($HTTP_ACCEPT_LANGUAGE, 0, 2); |
115 | } |
116 | |
25c078d3 |
117 | if (isset($squirrelmail_language) && function_exists("bindtextdomain")) { |
1b187352 |
118 | if ($squirrelmail_language != "en" && $squirrelmail_language != "") { |
119 | putenv("LC_ALL=".$squirrelmail_language); |
120 | bindtextdomain("squirrelmail", "../locale/"); |
121 | textdomain("squirrelmail"); |
122 | header ("Content-Type: text/html; charset=".$languages[$squirrelmail_language]["CHARSET"]); |
123 | } |
124 | } |
125 | |
052e0c26 |
126 | ?> |
127 | <html> |
128 | <body bgcolor=ffffff> |
129 | <br> |
130 | <center> |
131 | <table width=70% noborder bgcolor=ffffff align=center> |
132 | <tr> |
133 | <td bgcolor=dcdcdc> |
134 | <font color=cc0000> |
135 | <center> |
59177427 |
136 | <?php echo _("ERROR") ?> |
052e0c26 |
137 | </center> |
138 | </font> |
139 | </td> |
140 | </tr> |
141 | <tr> |
142 | <td> |
143 | <center> |
59177427 |
144 | <?php echo _("Unknown user or password incorrect.") ?><br> |
145 | <a href="login.php"><?php echo _("Click here to try again") ?></a> |
052e0c26 |
146 | </center> |
147 | </td> |
148 | </tr> |
149 | </table> |
150 | </center> |
151 | </body> |
152 | </html> |
59177427 |
153 | <?php |
052e0c26 |
154 | session_destroy(); |
155 | exit; |
156 | } else { |
1b187352 |
157 | printf (_("Unknown error: %s")."<br>", $read); |
052e0c26 |
158 | exit; |
159 | } |
160 | } else { |
161 | exit; |
162 | } |
163 | } |
164 | |
165 | return $imap_stream; |
166 | } |
167 | |
168 | |
169 | |
170 | |
171 | /****************************************************************************** |
172 | ** Simply logs out the imap session |
173 | ******************************************************************************/ |
174 | function sqimap_logout ($imap_stream) { |
175 | fputs ($imap_stream, "a001 LOGOUT\r\n"); |
176 | } |
177 | |
178 | |
179 | |
180 | /****************************************************************************** |
181 | ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test... |
182 | ******************************************************************************/ |
183 | function sqimap_get_delimiter ($imap_stream) { |
8e9e8afa |
184 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
052e0c26 |
185 | $read = sqimap_read_data($imap_stream, ".", true, $a, $b); |
186 | $quote_position = strpos ($read[0], "\""); |
187 | $delim = substr ($read[0], $quote_position+1, 1); |
188 | |
189 | return $delim; |
190 | } |
191 | |
192 | |
193 | |
194 | |
195 | /****************************************************************************** |
196 | ** Gets the number of messages in the current mailbox. |
197 | ******************************************************************************/ |
198 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
199 | fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n"); |
200 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message); |
201 | for ($i = 0; $i < count($read_ary); $i++) { |
202 | if (substr(trim($read_ary[$i]), -6) == EXISTS) { |
203 | $array = explode (" ", $read_ary[$i]); |
204 | $num = $array[1]; |
205 | } |
206 | } |
207 | return $num; |
208 | } |
209 | |
210 | |
211 | /****************************************************************************** |
212 | ** Returns a displayable email address |
213 | ******************************************************************************/ |
214 | function sqimap_find_email ($string) { |
215 | /** Luke Ehresman <lehresma@css.tayloru.edu> |
216 | ** <lehresma@css.tayloru.edu> |
217 | ** lehresma@css.tayloru.edu |
218 | **/ |
219 | |
220 | if (strpos($string, "<") && strpos($string, ">")) { |
221 | $string = substr($string, strpos($string, "<")+1); |
222 | $string = substr($string, 0, strpos($string, ">")); |
223 | } |
224 | return trim($string); |
225 | } |
226 | |
227 | |
228 | /****************************************************************************** |
229 | ** Takes the From: field, and creates a displayable name. |
230 | ** Luke Ehresman <lkehresman@yahoo.com> |
231 | ** becomes: Luke Ehresman |
232 | ** <lkehresman@yahoo.com> |
233 | ** becomes: lkehresman@yahoo.com |
234 | ******************************************************************************/ |
235 | function sqimap_find_displayable_name ($string) { |
236 | $string = " ".trim($string); |
237 | if (strpos($string, "<") && strpos($string, ">")) { |
238 | if (strpos($string, "<") == 1) { |
239 | $string = sqimap_find_email($string); |
240 | } else { |
241 | $string = trim($string); |
242 | $string = substr($string, 0, strpos($string, "<")); |
243 | $string = ereg_replace ("\"", "", $string); |
244 | } |
245 | } |
246 | return $string; |
247 | } |
248 | |
249 | |
052e0c26 |
250 | /****************************************************************************** |
251 | ** Returns the number of unseen messages in this folder |
252 | ******************************************************************************/ |
c5eb2c03 |
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"); |
052e0c26 |
256 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message); |
257 | $unseen = false; |
258 | |
c5eb2c03 |
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)); |
052e0c26 |
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 | ?> |