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 | } |
d29aac0e |
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)\n"); |
35 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
36 | } |
37 | |
38 | /****************************************************************************** |
39 | ** Returns some general header information -- FROM, DATE, and SUBJECT |
40 | ******************************************************************************/ |
41 | function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) { |
8b673ac1 |
42 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER.FIELDS (From Subject Date)]\n"); |
d29aac0e |
43 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
44 | |
45 | for ($i = 0; $i < count($read); $i++) { |
8b673ac1 |
46 | if (strtolower(substr($read[$i], 0, 5)) == "from:") { |
d29aac0e |
47 | $from = sqimap_find_displayable_name(substr($read[$i], 5)); |
8b673ac1 |
48 | } else if (strtolower(substr($read[$i], 0, 5)) == "date:") { |
d29aac0e |
49 | $date = substr($read[$i], 5); |
8b673ac1 |
50 | } else if (strtolower(substr($read[$i], 0, 8)) == "subject:") { |
d29aac0e |
51 | $subject = htmlspecialchars(substr($read[$i], 8)); |
52 | if (strlen(trim($subject)) == 0) |
7b9f6e3a |
53 | $subject = _("(no subject)"); |
d29aac0e |
54 | } |
55 | } |
56 | } |
57 | |
58 | /****************************************************************************** |
59 | ** Returns the flags for the specified messages |
60 | ******************************************************************************/ |
d41c12e1 |
61 | function sqimap_get_flags ($imap_stream, $start, $end) { |
62 | fputs ($imap_stream, "a001 FETCH $start:$end FLAGS\n"); |
63 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
64 | $i = 0; |
65 | while ($i < count($read)) { |
66 | if (strpos($read[$i], "FLAGS")) { |
67 | $tmp = ereg_replace("\(", "", $read[$i]); |
68 | $tmp = ereg_replace("\)", "", $tmp); |
69 | $tmp = str_replace("\\", "", $tmp); |
70 | $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp)); |
71 | $tmp = trim($tmp); |
72 | $flags[$i] = explode(" ", $tmp); |
73 | } else { |
74 | $flags[$i][0] = "None"; |
75 | } |
76 | $i++; |
77 | } |
78 | return $flags; |
d29aac0e |
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); |
4ccd80f8 |
89 | $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"]); |
d29aac0e |
90 | return $message; |
91 | } |
92 | |
93 | /****************************************************************************** |
94 | ** Wrapper function that reformats the header information. |
95 | ******************************************************************************/ |
96 | function sqimap_get_message_header ($imap_stream, $id) { |
8b673ac1 |
97 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\n"); |
d29aac0e |
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 | while ($i < count($read)) { |
c9f297ba |
122 | // Set up some defaults |
123 | $header["TYPE0"] = "text"; |
124 | $header["TYPE1"] = "plain"; |
125 | $header["CHARSET"] = "us-ascii"; |
126 | |
d29aac0e |
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 (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 | |
c9f297ba |
144 | |
d29aac0e |
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 | $line = $read[$i]; |
153 | $i++; |
154 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
155 | str_replace("\n", "", $line); |
156 | str_replace("\n", "", $read[$i]); |
157 | $line = "$line $read[$i]"; |
158 | $i++; |
159 | } |
160 | |
161 | /** Detect the boundary of a multipart message **/ |
162 | if (strpos(strtolower(trim($line)), "boundary=")) { |
163 | $pos = strpos($line, "boundary=") + 9; |
164 | $bound = trim($line); |
165 | if (strpos($line, " ", $pos) > 0) { |
166 | $bound = substr($bound, $pos, strpos($line, " ", $pos)); |
167 | } else { |
168 | $bound = substr($bound, $pos); |
169 | } |
170 | $bound = str_replace("\"", "", $bound); |
171 | $header["BOUNDARY"] = $bound; |
172 | } |
173 | |
174 | /** Detect the charset **/ |
175 | if (strpos(strtolower(trim($line)), "charset=")) { |
176 | $pos = strpos($line, "charset=") + 8; |
177 | $charset = trim($line); |
178 | if (strpos($line, " ", $pos) > 0) { |
179 | $charset = substr($charset, $pos, strpos($line, " ", $pos)); |
180 | } else { |
181 | $charset = substr($charset, $pos); |
182 | } |
183 | $charset = str_replace("\"", "", $charset); |
184 | $header["CHARSET"] = $charset; |
185 | } else { |
186 | $header["CHARSET"] = "us-ascii"; |
187 | } |
188 | |
189 | /** Detects filename if any **/ |
190 | if (strpos(strtolower(trim($line)), "name=")) { |
191 | $pos = strpos($line, "name=") + 5; |
192 | $name = trim($line); |
193 | if (strpos($line, " ", $pos) > 0) { |
194 | $name = substr($name, $pos, strpos($line, " ", $pos)); |
195 | } else { |
196 | $name = substr($name, $pos); |
197 | } |
198 | $name = str_replace("\"", "", $name); |
199 | $header["FILENAME"] = $name; |
200 | } |
201 | } |
202 | |
203 | /** REPLY-TO **/ |
204 | else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") { |
205 | $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i]))); |
206 | $i++; |
207 | } |
208 | |
209 | /** FROM **/ |
210 | else if (strtolower(substr($read[$i], 0, 5)) == "from:") { |
211 | $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6)); |
212 | if ($header["REPLYTO"] == "") |
213 | $header["REPLYTO"] = $header["FROM"]; |
214 | $i++; |
215 | } |
216 | /** DATE **/ |
217 | else if (strtolower(substr($read[$i], 0, 5)) == "date:") { |
218 | $d = substr($read[$i], 5); |
219 | $d = trim($d); |
220 | $d = ereg_replace(" ", " ", $d); |
221 | $d = explode(" ", $d); |
222 | $header["DATE"] = getTimeStamp($d); |
223 | $i++; |
224 | } |
225 | /** SUBJECT **/ |
226 | else if (strtolower(substr($read[$i], 0, 8)) == "subject:") { |
227 | $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9)); |
228 | if (strlen(Chop($header["SUBJECT"])) == 0) |
7b9f6e3a |
229 | $header["SUBJECT"] = _("(no subject)"); |
d29aac0e |
230 | $i++; |
231 | } |
232 | /** CC **/ |
233 | else if (strtolower(substr($read[$i], 0, 3)) == "cc:") { |
234 | $pos = 0; |
235 | $header["CC"][$pos] = trim(substr($read[$i], 4)); |
236 | $i++; |
237 | while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) { |
238 | $pos++; |
239 | $header["CC"][$pos] = trim($read[$i]); |
240 | $i++; |
241 | } |
242 | } |
243 | /** TO **/ |
244 | else if (strtolower(substr($read[$i], 0, 3)) == "to:") { |
245 | $pos = 0; |
246 | $header["TO"][$pos] = trim(substr($read[$i], 4)); |
247 | $i++; |
248 | while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){ |
249 | $pos++; |
250 | $header["TO"][$pos] = trim($read[$i]); |
251 | $i++; |
252 | } |
253 | } |
254 | |
255 | /** ERROR CORRECTION **/ |
256 | else if (substr($read[$i], 0, 1) == ")") { |
257 | if ($header["SUBJECT"] == "") |
7b9f6e3a |
258 | $header["SUBJECT"] = _("(no subject)"); |
d29aac0e |
259 | |
260 | if ($header["FROM"] == "") |
7b9f6e3a |
261 | $header["FROM"] = _("(unknown sender)"); |
d29aac0e |
262 | |
263 | if ($header["DATE"] == "") |
264 | $header["DATE"] = time(); |
265 | $i++; |
266 | } |
267 | else { |
268 | $i++; |
269 | } |
270 | } |
271 | return $header; |
272 | } |
273 | |
dc4446ba |
274 | |
d29aac0e |
275 | /****************************************************************************** |
276 | ** Returns the body of a message. |
277 | ******************************************************************************/ |
4ccd80f8 |
278 | function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding) { |
d29aac0e |
279 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\n"); |
280 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
281 | |
282 | $i = 0; |
283 | $j = 0; |
dc4446ba |
284 | while ($i < count($read)-1) { |
285 | if ( ($i != 0) ) { |
d29aac0e |
286 | $bodytmp[$j] = $read[$i]; |
287 | $j++; |
288 | } |
289 | $i++; |
290 | } |
291 | $body = $bodytmp; |
4ccd80f8 |
292 | |
293 | return decodeMime($body, $bound, $type0, $type1, $encoding); |
d29aac0e |
294 | } |
4ca45d7b |
295 | ?> |