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