59177427 |
1 | <?php |
052e0c26 |
2 | /** |
3 | ** imap_messages.php |
4 | ** |
5 | ** This implements functions that manipulate messages |
6 | **/ |
7 | |
8 | /****************************************************************************** |
9 | ** Copies specified messages to specified folder |
10 | ******************************************************************************/ |
11 | function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) { |
12 | fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n"); |
13 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
14 | } |
15 | |
16 | /****************************************************************************** |
17 | ** Deletes specified messages and moves them to trash if possible |
18 | ******************************************************************************/ |
19 | function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) { |
20 | global $move_to_trash, $trash_folder, $auto_expunge; |
21 | |
22 | if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) { |
23 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
24 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted"); |
25 | } else { |
26 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted"); |
27 | } |
28 | } |
29 | |
30 | /****************************************************************************** |
31 | ** Sets the specified messages with specified flag |
32 | ******************************************************************************/ |
33 | function sqimap_messages_flag ($imap_stream, $start, $end, $flag) { |
34 | fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n"); |
35 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
052e0c26 |
36 | } |
37 | |
38 | /****************************************************************************** |
39 | ** Returns some general header information -- FROM, DATE, and SUBJECT |
40 | ******************************************************************************/ |
47ca0924 |
41 | function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) { |
55650753 |
42 | //fputs ($imap_stream, "a001 FETCH $id BODY[HEADER.FIELDS (DATE FROM SUBJECT)]\r\n"); |
47ca0924 |
43 | //fputs ($imap_stream, "a001 FETCH $id RFC822.HEADER\r\n"); |
44 | fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date From Subject)]\r\n"); |
052e0c26 |
45 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
46 | |
47 | $subject = _("(no subject)"); |
48 | $from = _("Unknown Sender"); |
49 | for ($i = 0; $i < count($read); $i++) { |
47ca0924 |
50 | if (eregi ("^from:", $read[$i])) { |
51 | $from = sqimap_find_displayable_name(substr($read[$i], 5)); |
52 | } else if (eregi ("^date:", $read[$i])) { |
052e0c26 |
53 | $date = substr($read[$i], 5); |
47ca0924 |
54 | } else if (eregi ("^subject:", $read[$i])) { |
55 | $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i])); |
56 | if (strlen($subject) == 0) |
052e0c26 |
57 | $subject = _("(no subject)"); |
58 | } |
59 | } |
60 | } |
61 | |
62 | /****************************************************************************** |
63 | ** Returns the flags for the specified messages |
64 | ******************************************************************************/ |
65 | function sqimap_get_flags ($imap_stream, $i) { |
66 | fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n"); |
67 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
68 | if (strpos($read[0], "FLAGS")) { |
69 | $tmp = ereg_replace("\(", "", $read[0]); |
70 | $tmp = ereg_replace("\)", "", $tmp); |
71 | $tmp = str_replace("\\", "", $tmp); |
72 | $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp)); |
73 | $tmp = trim($tmp); |
74 | $flags = explode(" ", $tmp); |
75 | } else { |
76 | $flags[0] = "None"; |
77 | } |
78 | return $flags; |
79 | } |
80 | |
81 | /****************************************************************************** |
82 | ** Returns a message array with all the information about a message. See |
83 | ** the documentation folder for more information about this array. |
84 | ******************************************************************************/ |
85 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
86 | $message["INFO"]["ID"] = $id; |
87 | $message["INFO"]["MAILBOX"] = $mailbox; |
88 | $message["HEADER"] = sqimap_get_message_header($imap_stream, $id); |
89 | $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]); |
90 | return $message; |
91 | } |
92 | |
93 | /****************************************************************************** |
94 | ** Wrapper function that reformats the header information. |
95 | ******************************************************************************/ |
96 | function sqimap_get_message_header ($imap_stream, $id) { |
97 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n"); |
98 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
99 | |
100 | return sqimap_get_header($imap_stream, $read); |
101 | } |
102 | |
103 | /****************************************************************************** |
104 | ** Wrapper function that returns entity headers for use by decodeMime |
105 | ******************************************************************************/ |
106 | function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) { |
107 | $header = sqimap_get_header($imap_stream, $read); |
108 | $type0 = $header["TYPE0"]; |
109 | $type1 = $header["TYPE1"]; |
110 | $bound = $header["BOUNDARY"]; |
111 | $encoding = $header["ENCODING"]; |
112 | $charset = $header["CHARSET"]; |
113 | $filename = $header["FILENAME"]; |
114 | } |
115 | |
116 | /****************************************************************************** |
117 | ** Queries the IMAP server and gets all header information. |
118 | ******************************************************************************/ |
119 | function sqimap_get_header ($imap_stream, $read) { |
120 | $i = 0; |
121 | // Set up some defaults |
122 | $header["TYPE0"] = "text"; |
123 | $header["TYPE1"] = "plain"; |
124 | $header["CHARSET"] = "us-ascii"; |
125 | |
126 | while ($i < count($read)) { |
127 | if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") { |
128 | $header["MIME"] = true; |
129 | $i++; |
130 | } |
131 | |
132 | /** ENCODING TYPE **/ |
133 | else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") { |
134 | $header["ENCODING"] = strtolower(trim(substr($read[$i], 26))); |
135 | $i++; |
136 | } |
137 | |
138 | /** CONTENT-TYPE **/ |
139 | else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") { |
140 | $cont = strtolower(trim(substr($read[$i], 13))); |
141 | if (strpos($cont, ";")) |
142 | $cont = substr($cont, 0, strpos($cont, ";")); |
143 | |
144 | |
145 | if (strpos($cont, "/")) { |
146 | $header["TYPE0"] = substr($cont, 0, strpos($cont, "/")); |
147 | $header["TYPE1"] = substr($cont, strpos($cont, "/")+1); |
148 | } else { |
149 | $header["TYPE0"] = $cont; |
150 | } |
151 | |
152 | |
153 | $line = $read[$i]; |
154 | $i++; |
155 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
156 | str_replace("\n", "", $line); |
157 | str_replace("\n", "", $read[$i]); |
158 | $line = "$line $read[$i]"; |
159 | $i++; |
160 | } |
161 | |
162 | /** Detect the boundary of a multipart message **/ |
163 | if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) { |
164 | $header["BOUNDARY"] = $regs[1]; |
165 | } |
166 | |
167 | /** Detect the charset **/ |
168 | if (strpos(strtolower(trim($line)), "charset=")) { |
169 | $pos = strpos($line, "charset=") + 8; |
170 | $charset = trim($line); |
171 | if (strpos($line, " ", $pos) > 0) { |
172 | $charset = substr($charset, $pos, strpos($line, " ", $pos)); |
173 | } else { |
174 | $charset = substr($charset, $pos); |
175 | } |
176 | $charset = str_replace("\"", "", $charset); |
177 | $header["CHARSET"] = $charset; |
178 | } else { |
179 | $header["CHARSET"] = "us-ascii"; |
180 | } |
181 | |
182 | } |
183 | |
184 | else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") { |
185 | /** Add better dontent-disposition support **/ |
186 | |
187 | $line = $read[$i]; |
188 | $i++; |
189 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
190 | str_replace("\n", "", $line); |
191 | str_replace("\n", "", $read[$i]); |
192 | $line = "$line $read[$i]"; |
193 | $i++; |
194 | } |
195 | |
196 | /** Detects filename if any **/ |
197 | if (strpos(strtolower(trim($line)), "filename=")) { |
198 | $pos = strpos($line, "filename=") + 9; |
199 | $name = trim($line); |
200 | if (strpos($line, " ", $pos) > 0) { |
201 | $name = substr($name, $pos, strpos($line, " ", $pos)); |
202 | } else { |
203 | $name = substr($name, $pos); |
204 | } |
205 | $name = str_replace("\"", "", $name); |
206 | $header["FILENAME"] = $name; |
207 | } |
208 | } |
209 | |
210 | /** REPLY-TO **/ |
211 | else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") { |
212 | $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i]))); |
213 | $i++; |
214 | } |
215 | |
216 | /** FROM **/ |
217 | else if (strtolower(substr($read[$i], 0, 5)) == "from:") { |
218 | $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6)); |
219 | if ($header["REPLYTO"] == "") |
220 | $header["REPLYTO"] = $header["FROM"]; |
221 | $i++; |
222 | } |
223 | /** DATE **/ |
224 | else if (strtolower(substr($read[$i], 0, 5)) == "date:") { |
225 | $d = substr($read[$i], 5); |
226 | $d = trim($d); |
227 | $d = ereg_replace(" ", " ", $d); |
228 | $d = explode(" ", $d); |
229 | $header["DATE"] = getTimeStamp($d); |
230 | $i++; |
231 | } |
232 | /** SUBJECT **/ |
233 | else if (strtolower(substr($read[$i], 0, 8)) == "subject:") { |
234 | $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9)); |
235 | if (strlen(Chop($header["SUBJECT"])) == 0) |
236 | $header["SUBJECT"] = _("(no subject)"); |
237 | $i++; |
238 | } |
239 | /** CC **/ |
240 | else if (strtolower(substr($read[$i], 0, 3)) == "cc:") { |
241 | $pos = 0; |
242 | $header["CC"][$pos] = trim(substr($read[$i], 4)); |
243 | $i++; |
47ca0924 |
244 | while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) { |
052e0c26 |
245 | $pos++; |
246 | $header["CC"][$pos] = trim($read[$i]); |
247 | $i++; |
248 | } |
249 | } |
250 | /** TO **/ |
251 | else if (strtolower(substr($read[$i], 0, 3)) == "to:") { |
252 | $pos = 0; |
253 | $header["TO"][$pos] = trim(substr($read[$i], 4)); |
254 | $i++; |
47ca0924 |
255 | while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){ |
052e0c26 |
256 | $pos++; |
257 | $header["TO"][$pos] = trim($read[$i]); |
258 | $i++; |
259 | } |
260 | } |
261 | /** MESSAGE ID **/ |
262 | else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") { |
263 | $header["MESSAGE-ID"] = trim(substr($read[$i], 11)); |
264 | $i++; |
265 | } |
266 | |
267 | |
268 | /** ERROR CORRECTION **/ |
269 | else if (substr($read[$i], 0, 1) == ")") { |
270 | if ($header["SUBJECT"] == "") |
271 | $header["SUBJECT"] = _("(no subject)"); |
272 | |
273 | if ($header["FROM"] == "") |
274 | $header["FROM"] = _("(unknown sender)"); |
275 | |
276 | if ($header["DATE"] == "") |
277 | $header["DATE"] = time(); |
278 | $i++; |
279 | } |
280 | else { |
281 | $i++; |
282 | } |
283 | } |
284 | return $header; |
285 | } |
286 | |
287 | |
288 | /****************************************************************************** |
289 | ** Returns the body of a message. |
290 | ******************************************************************************/ |
291 | function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) { |
292 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n"); |
293 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
294 | |
295 | $i = 0; |
296 | $j = 0; |
297 | while ($i < count($read)-1) { |
298 | if ( ($i != 0) ) { |
299 | $bodytmp[$j] = $read[$i]; |
300 | $j++; |
301 | } |
302 | $i++; |
303 | } |
304 | $body = $bodytmp; |
305 | |
306 | return decodeMime($body, $bound, $type0, $type1, $encoding, $charset); |
307 | } |
308 | ?> |