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