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