Ajoute le support du 'richtext'... avec un jeu de balise relativement réduit
[banana.git] / banana / post.inc.php
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;
20 /** formating */
21 var $messages;
22 /** attachment */
23 var $pj;
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;
33 $this->id = $_id;
34 $this->pj = array();
35 $this->messages = array();
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
52 if (preg_match("@multipart/([^;]+);@", $this->headers['content-type'], $mpart_type)) {
53 preg_match("/boundary=\"?([^ \"]+)\"?/", $this->headers['content-type'], $mpart_boundary);
54 $this->_split_multipart($mpart_type[1], $mpart_boundary[1]);
55 } else {
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 }
61 }
62 }
63
64 /** split multipart messages
65 * @param $type STRING multipart type description
66 * @param $boundary STRING multipart boundary identification string
67 */
68 function _split_multipart($type, $boundary)
69 {
70 $parts = preg_split("/\n--$boundary(--|\n)/", $this->body);
71 foreach ($parts as $part) {
72 $part = $this->_get_part($part);
73 $local_header = $part['headers'];
74 $local_body = $part['body'];
75 if (isset($local_header['content-disposition']) && preg_match("/attachment/", $local_header['content-disposition'])) {
76 $this->_add_attachment($part);
77 } else if (isset($local_header['content-type']) && preg_match("@text/([^;]+);@", $local_header['content-type'], $format)) {
78 array_push($this->messages, $part);
79 }
80 }
81 $this->set_body_to_part(0);
82 }
83
84 /** extract new headers from the part
85 * @param $part STRING part of a multipart message
86 */
87 function _get_part($part)
88 {
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 }
104 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
105 }
106
107 /** add an attachment
108 */
109 function _add_attachment($part)
110 {
111 $local_header = $part['headers'];
112 $local_body = $part['body'];
113
114 if (!isset($local_header['content-transfer-encoding'])) {
115 return false;
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)) {
134 return false;
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));
141 return true;
142 }
143
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 }
152 if ($format[1] == 'richtext') {
153 return htmlToPlainText(richtextToHtml($this->body));
154 } else {
155 return htmlToPlainText($this->body);
156 }
157 }
158
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 */
163 function get_attachment($pjid, $action = false)
164 {
165 if ($pjid >= count($this->pj)) {
166 return false;
167 } else {
168 $file = $this->pj[$pjid];
169 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
170 if (!$action) {
171 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
172 } else {
173 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
174 }
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
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 }
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 }
209 return true;
210 }
211
212 function _header()
213 {
214 global $banana;
215 $hdrs = $banana->nntp->head($this->id);
216 if (!$hdrs) {
217 $this = null;
218 return false;
219 }
220
221 // parse headers
222 foreach ($hdrs as $line) {
223 if (preg_match("/^[\t\r ]+/", $line)) {
224 $line = ($hdr=="X-Face"?"":" ").ltrim($line);
225 if (in_array($hdr, $banana->parse_hdr)) {
226 $this->headers[$hdr] .= $line;
227 }
228 } else {
229 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
230 $hdr = strtolower($hdr);
231 if (in_array($hdr, $banana->parse_hdr)) {
232 $this->headers[$hdr] = $val;
233 }
234 }
235 }
236 // decode headers
237 foreach ($banana->hdecode as $hdr) {
238 if (isset($this->headers[$hdr])) {
239 $this->headers[$hdr] = headerDecode($this->headers[$hdr]);
240 }
241 }
242
243 $this->name = $this->headers['from'];
244 $this->name = preg_replace('/<[^ ]*>/', '', $this->name);
245 $this->name = trim($this->name);
246 }
247
248 function checkcancel()
249 {
250 if (function_exists('hook_checkcancel')) {
251 return hook_checkcancel($this->headers);
252 }
253 return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
254 }
255
256 /** convert message to html
257 * @param partid INT id of the multipart message that must be displaid
258 */
259 function to_html($partid = -1)
260 {
261 global $banana;
262
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;
286 }
287
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
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>';
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>';
336 $res .= ' . <a href="pj.php?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'&action=view" target="_blank">aperçu</a>';
337 $res .= '<br/>';
338 $i++;
339 }
340 $res .= '</td></tr>';
341 }
342
343 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
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 }
349 }
350
351 ?>