78cd27b3 |
1 | <?php |
2 | /******************************************************************************** |
3 | * include/posts.inc.php : class for posts |
4 | * ----------------------- |
5 | * |
6 | * This file is part of the banana distribution |
7 | * Copyright: See COPYING files that comes with this distribution |
8 | ********************************************************************************/ |
9 | |
10 | /** class for posts |
11 | */ |
12 | |
13 | class BananaPost |
14 | { |
15 | var $id; |
16 | /** headers */ |
17 | var $headers; |
18 | /** body */ |
19 | var $body; |
1f75b135 |
20 | /** formating */ |
21 | var $messages; |
7a0e2710 |
22 | /** attachment */ |
23 | var $pj; |
78cd27b3 |
24 | /** poster name */ |
25 | var $name; |
26 | |
27 | /** constructor |
28 | * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case) |
29 | */ |
30 | function BananaPost($_id) |
31 | { |
32 | global $banana; |
1f75b135 |
33 | $this->id = $_id; |
34 | $this->pj = array(); |
35 | $this->messages = array(); |
78cd27b3 |
36 | $this->_header(); |
37 | |
38 | if ($body = $banana->nntp->body($_id)) { |
39 | $this->body = join("\n", $body); |
40 | } else { |
41 | return ($this = null); |
42 | } |
43 | |
44 | if (isset($this->headers['content-transfer-encoding'])) { |
45 | if (preg_match("/base64/", $this->headers['content-transfer-encoding'])) { |
46 | $this->body = base64_decode($this->body); |
47 | } elseif (preg_match("/quoted-printable/", $this->headers['content-transfer-encoding'])) { |
48 | $this->body = quoted_printable_decode($this->body); |
49 | } |
50 | } |
51 | |
7a0e2710 |
52 | if (preg_match("@multipart/([^;]+);@", $this->headers['content-type'], $mpart_type)) { |
53 | preg_match("/boundary=\"?([^ \"]+)\"?/", $this->headers['content-type'], $mpart_boundary); |
a529ea7b |
54 | $this->_split_multipart($mpart_type[1], $mpart_boundary[1]); |
7a0e2710 |
55 | } |
56 | |
78cd27b3 |
57 | if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) { |
58 | $this->body = iconv($matches[1], 'utf-8', $this->body); |
59 | } else { |
60 | $this->body = utf8_encode($this->body); |
61 | } |
62 | } |
63 | |
7a0e2710 |
64 | /** split multipart messages |
65 | * @param $type STRING multipart type description |
66 | * @param $boundary STRING multipart boundary identification string |
67 | */ |
a529ea7b |
68 | function _split_multipart($type, $boundary) |
7a0e2710 |
69 | { |
7a0e2710 |
70 | $parts = preg_split("/\n--$boundary(--|\n)/", $this->body); |
71 | foreach ($parts as $part) { |
a529ea7b |
72 | $part = $this->_get_part($part); |
7a0e2710 |
73 | $local_header = $part['headers']; |
74 | $local_body = $part['body']; |
75 | if (isset($local_header['content-disposition']) && preg_match("/attachment/", $local_header['content-disposition'])) { |
a529ea7b |
76 | $this->_add_attachment($part); |
1f75b135 |
77 | } else if (isset($local_header['content-type']) && preg_match("@text/([^;]+);@", $local_header['content-type'], $format)) { |
78 | array_push($this->messages, $part); |
a529ea7b |
79 | } |
1f75b135 |
80 | } |
81 | if (count($this->messages) > 0) { |
82 | $this->set_body_to_part(0); |
7a0e2710 |
83 | } |
84 | } |
85 | |
86 | /** extract new headers from the part |
87 | * @param $part STRING part of a multipart message |
88 | */ |
a529ea7b |
89 | function _get_part($part) |
1f75b135 |
90 | { |
7a0e2710 |
91 | global $banana; |
92 | |
93 | $lines = split("\n", $part); |
94 | while (count($lines)) { |
95 | $line = array_shift($lines); |
96 | if ($line != "") { |
97 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
98 | $hdr = strtolower($hdr); |
99 | if (in_array($hdr, $banana->parse_hdr)) { |
100 | $local_headers[$hdr] = $val; |
101 | } |
102 | } else { |
103 | break; |
104 | } |
105 | } |
7a0e2710 |
106 | return Array('headers' => $local_headers, 'body' => join("\n", $lines)); |
107 | } |
108 | |
1f75b135 |
109 | /** add an attachment |
110 | */ |
a529ea7b |
111 | function _add_attachment($part) |
1f75b135 |
112 | { |
7a0e2710 |
113 | $local_header = $part['headers']; |
114 | $local_body = $part['body']; |
115 | |
116 | if (!isset($local_header['content-transfer-encoding'])) { |
1f75b135 |
117 | return false; |
7a0e2710 |
118 | } |
119 | |
120 | if (isset($local_header['content-disposition'])) { |
121 | if (preg_match("/attachment/", $local_header['content-disposition'])) { |
122 | preg_match("/filename=\"?([^\"]+)\"?/", $local_header['content-disposition'], $filename); |
123 | $filename = $filename[1]; |
124 | } |
125 | } |
126 | if (!isset($filename)) { |
127 | $filename = "attachment".count($pj); |
128 | } |
129 | |
130 | if (isset($local_header['content-type'])) { |
131 | if (preg_match("/^\\s*([^ ;]+);/", $local_header['content-type'], $mimetype)) { |
132 | $mimetype = $mimetype[1]; |
133 | } |
134 | } |
135 | if (!isset($mimetype)) { |
1f75b135 |
136 | return false; |
7a0e2710 |
137 | } |
138 | |
139 | array_push($this->pj, Array('MIME' => $mimetype, |
140 | 'filename' => $filename, |
141 | 'encoding' => strtolower($local_header['content-transfer-encoding']), |
142 | 'data' => $local_body)); |
1f75b135 |
143 | return true; |
7a0e2710 |
144 | } |
145 | |
a529ea7b |
146 | /** return body in plain text (useful for messages without a text/plain part) |
147 | */ |
148 | function get_body() |
149 | { |
150 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
151 | if ($format[1] == 'plain') { |
152 | return $this->body; |
153 | } |
154 | $res = preg_replace("@<br[^>]>@", "@@@#@", $this->body); |
155 | $res = trim(html_entity_decode(strip_tags($res))); |
156 | $res = str_replace("@@@#@", "\n", $res); |
157 | if (!is_utf8($res)) { |
158 | $res = utf8_encode($res); |
159 | } |
160 | return $res; |
161 | } |
162 | |
7a0e2710 |
163 | /** decode an attachment |
164 | * @param pjid INT id of the attachment to decode |
165 | * @param action BOOL action to execute : true=view, false=download |
166 | */ |
1f75b135 |
167 | function get_attachment($pjid, $action = false) |
168 | { |
7a0e2710 |
169 | if ($pjid >= count($this->pj)) { |
170 | return false; |
171 | } else { |
172 | $file = $this->pj[$pjid]; |
a529ea7b |
173 | header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"'); |
7a0e2710 |
174 | if (!$action) { |
175 | header('Content-Disposition: attachment; filename="'.$file['filename'].'"'); |
a529ea7b |
176 | } else { |
177 | header('Content-Disposition: inline; filename="'.$file['filename'].'"'); |
178 | } |
7a0e2710 |
179 | if ($file['encoding'] == 'base64') { |
180 | echo base64_decode($file['data']); |
181 | } else { |
182 | header('Content-Transfer-Encoding: '.$file['encoding']); |
183 | echo $file['data']; |
184 | } |
185 | return true; |
186 | } |
187 | } |
188 | |
1f75b135 |
189 | /** set body to represent the given part |
190 | * @param partid INT index of the part in messages |
191 | */ |
192 | function set_body_to_part($partid) |
193 | { |
194 | global $banana; |
195 | |
196 | if (count($this->messages) == 0) { |
197 | return false; |
198 | } |
199 | |
200 | $local_header = $this->messages[$partid]['headers']; |
201 | $this->body = $this->messages[$partid]['body']; |
202 | foreach ($banana->parse_hdr as $hdr) { |
203 | if (isset($local_header[$hdr])) { |
204 | $this->headers[$hdr] = $local_header[$hdr]; |
205 | } |
206 | } |
207 | return true; |
208 | } |
209 | |
78cd27b3 |
210 | function _header() |
211 | { |
212 | global $banana; |
213 | $hdrs = $banana->nntp->head($this->id); |
214 | if (!$hdrs) { |
215 | $this = null; |
216 | return false; |
217 | } |
218 | |
219 | // parse headers |
220 | foreach ($hdrs as $line) { |
221 | if (preg_match("/^[\t\r ]+/", $line)) { |
222 | $line = ($hdr=="X-Face"?"":" ").ltrim($line); |
223 | if (in_array($hdr, $banana->parse_hdr)) { |
224 | $this->headers[$hdr] .= $line; |
225 | } |
226 | } else { |
227 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
228 | $hdr = strtolower($hdr); |
229 | if (in_array($hdr, $banana->parse_hdr)) { |
230 | $this->headers[$hdr] = $val; |
231 | } |
232 | } |
233 | } |
234 | // decode headers |
235 | foreach ($banana->hdecode as $hdr) { |
236 | if (isset($this->headers[$hdr])) { |
237 | $this->headers[$hdr] = headerDecode($this->headers[$hdr]); |
238 | } |
239 | } |
240 | |
241 | $this->name = $this->headers['from']; |
242 | $this->name = preg_replace('/<[^ ]*>/', '', $this->name); |
243 | $this->name = trim($this->name); |
244 | } |
245 | |
246 | function checkcancel() |
247 | { |
248 | if (function_exists('hook_checkcancel')) { |
249 | return hook_checkcancel($this->headers); |
250 | } |
251 | return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">"); |
252 | } |
253 | |
1f75b135 |
254 | /** convert message to html |
255 | * @param partid INT id of the multipart message that must be displaid |
256 | */ |
257 | function to_html($partid = 0) |
78cd27b3 |
258 | { |
259 | global $banana; |
260 | |
1f75b135 |
261 | if ($partid != 0) { |
262 | $this->set_body_to_part($partid); |
263 | } |
264 | |
78cd27b3 |
265 | $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">'; |
266 | $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>'; |
267 | |
268 | foreach ($banana->show_hdr as $hdr) { |
269 | if (isset($this->headers[$hdr])) { |
270 | $res2 = formatdisplayheader($hdr, $this->headers[$hdr]); |
271 | if ($res2) { |
272 | $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n"; |
273 | } |
274 | } |
275 | } |
276 | |
1f75b135 |
277 | $res .= '<tr><th colspan="2">'._b_('Corps'); |
278 | if (count($this->messages) > 1) { |
279 | for ($i = 0 ; $i < count($this->messages) ; $i++) { |
280 | if ($i == 0) { |
281 | $res .= ' : '; |
282 | } else { |
283 | $res .= ' . '; |
284 | } |
285 | preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format); |
286 | $format = textFormat_translate($format[1]); |
287 | if ($i != $partid) { |
288 | $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&part='.$i.'">'.$format.'</a>'; |
289 | } else { |
290 | $res .= $format; |
291 | } |
292 | } |
293 | } |
294 | $res .= '</th></tr>'; |
295 | |
296 | preg_match("@text/([^;]+);@", $this->headers['content-type'], $format); |
297 | $format = $format[1]; |
298 | $res .= '<tr><td colspan="2">'; |
299 | if ($format == 'html') { |
300 | $res .= formatbody($this->body, $format); |
301 | } else { |
302 | $res .= '<pre>'.formatbody($this->body).'</pre>'; |
303 | } |
304 | $res .= '</td></tr>'; |
7a0e2710 |
305 | |
306 | if (count($this->pj) > 0) { |
307 | $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>'; |
308 | $res .= '<tr><td colspan="2">'; |
309 | $i = 0; |
310 | foreach ($this->pj as $file) { |
311 | $res .= $file['filename'].' ('.$file['MIME'].') : '; |
312 | $res .= '<a href="pj.php?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'">télécharger</a>'; |
a79e9a4f |
313 | $res .= ' . <a href="pj.php?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'&action=view" target="_blank">aperçu</a>'; |
7a0e2710 |
314 | $res .= '<br/>'; |
315 | $i++; |
316 | } |
317 | $res .= '</td></tr>'; |
318 | } |
78cd27b3 |
319 | |
320 | $res .= '<tr><th colspan="2">'._b_('apercu').'</th></tr>'; |
321 | $ndx = $banana->spool->getndx($this->id); |
322 | $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>'; |
323 | |
324 | return $res.'</table>'; |
325 | } |
326 | } |
327 | |
328 | ?> |