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