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