Possibilité de choisir un ordre préférentiel des formats de texte à afficher...
[banana.git] / banana / post.inc.php
CommitLineData
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
13class 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]);
78cd27b3 55 } else {
cc43419f 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 }
78cd27b3 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);
cc43419f 79 }
a529ea7b 80 }
cc43419f 81 $this->set_body_to_part(0);
7a0e2710 82 }
83
84 /** extract new headers from the part
85 * @param $part STRING part of a multipart message
86 */
a529ea7b 87 function _get_part($part)
1f75b135 88 {
7a0e2710 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 }
7a0e2710 104 return Array('headers' => $local_headers, 'body' => join("\n", $lines));
105 }
106
1f75b135 107 /** add an attachment
108 */
a529ea7b 109 function _add_attachment($part)
1f75b135 110 {
7a0e2710 111 $local_header = $part['headers'];
112 $local_body = $part['body'];
113
114 if (!isset($local_header['content-transfer-encoding'])) {
1f75b135 115 return false;
7a0e2710 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)) {
1f75b135 134 return false;
7a0e2710 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));
1f75b135 141 return true;
7a0e2710 142 }
143
a529ea7b 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 $res = preg_replace("@<br[^>]>@", "@@@#@", $this->body);
153 $res = trim(html_entity_decode(strip_tags($res)));
154 $res = str_replace("@@@#@", "\n", $res);
155 if (!is_utf8($res)) {
156 $res = utf8_encode($res);
157 }
158 return $res;
159 }
160
7a0e2710 161 /** decode an attachment
162 * @param pjid INT id of the attachment to decode
163 * @param action BOOL action to execute : true=view, false=download
164 */
1f75b135 165 function get_attachment($pjid, $action = false)
166 {
7a0e2710 167 if ($pjid >= count($this->pj)) {
168 return false;
169 } else {
170 $file = $this->pj[$pjid];
a529ea7b 171 header('Content-Type: '.$file['MIME'].'; name="'.$file['filename'].'"');
7a0e2710 172 if (!$action) {
173 header('Content-Disposition: attachment; filename="'.$file['filename'].'"');
a529ea7b 174 } else {
175 header('Content-Disposition: inline; filename="'.$file['filename'].'"');
176 }
7a0e2710 177 if ($file['encoding'] == 'base64') {
178 echo base64_decode($file['data']);
179 } else {
180 header('Content-Transfer-Encoding: '.$file['encoding']);
181 echo $file['data'];
182 }
183 return true;
184 }
185 }
186
1f75b135 187 /** set body to represent the given part
188 * @param partid INT index of the part in messages
189 */
190 function set_body_to_part($partid)
191 {
192 global $banana;
193
194 if (count($this->messages) == 0) {
195 return false;
196 }
197
198 $local_header = $this->messages[$partid]['headers'];
199 $this->body = $this->messages[$partid]['body'];
200 foreach ($banana->parse_hdr as $hdr) {
201 if (isset($local_header[$hdr])) {
202 $this->headers[$hdr] = $local_header[$hdr];
203 }
204 }
cc43419f 205
206 if (preg_match('!charset=([^;]*)\s*(;|$)!', $this->headers['content-type'], $matches)) {
207 $this->body = iconv($matches[1], 'utf-8', $this->body);
208 } else {
209 $this->body = utf8_encode($this->body);
210 }
1f75b135 211 return true;
212 }
213
78cd27b3 214 function _header()
215 {
216 global $banana;
217 $hdrs = $banana->nntp->head($this->id);
218 if (!$hdrs) {
219 $this = null;
220 return false;
221 }
222
223 // parse headers
224 foreach ($hdrs as $line) {
225 if (preg_match("/^[\t\r ]+/", $line)) {
226 $line = ($hdr=="X-Face"?"":" ").ltrim($line);
227 if (in_array($hdr, $banana->parse_hdr)) {
228 $this->headers[$hdr] .= $line;
229 }
230 } else {
231 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
232 $hdr = strtolower($hdr);
233 if (in_array($hdr, $banana->parse_hdr)) {
234 $this->headers[$hdr] = $val;
235 }
236 }
237 }
238 // decode headers
239 foreach ($banana->hdecode as $hdr) {
240 if (isset($this->headers[$hdr])) {
241 $this->headers[$hdr] = headerDecode($this->headers[$hdr]);
242 }
243 }
244
245 $this->name = $this->headers['from'];
246 $this->name = preg_replace('/<[^ ]*>/', '', $this->name);
247 $this->name = trim($this->name);
248 }
249
250 function checkcancel()
251 {
252 if (function_exists('hook_checkcancel')) {
253 return hook_checkcancel($this->headers);
254 }
255 return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
256 }
257
1f75b135 258 /** convert message to html
259 * @param partid INT id of the multipart message that must be displaid
260 */
cc43419f 261 function to_html($partid = -1)
78cd27b3 262 {
263 global $banana;
264
cc43419f 265 if (count($this->messages) > 1) {
266 if ($partid != -1) {
267 $this->set_body_to_part($partid);
268 } else {
269 // Select prefered text-format
270 foreach ($banana->body_mime as $mime) {
271 for ($id = 0 ; $id < count($this->messages) ; $id++) {
272 if (preg_match("@$mime@", $this->messages[$id]['headers']['content-type'])) {
273 $partid = $id;
274 $this->set_body_to_part($partid);
275 break;
276 }
277 }
278 if ($partid != -1) {
279 break;
280 }
281 }
282 if ($partid == -1) {
283 $partid = 0;
284 }
285 }
286 } else {
287 $partid = 0;
1f75b135 288 }
289
78cd27b3 290 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
291 $res .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
292
293 foreach ($banana->show_hdr as $hdr) {
294 if (isset($this->headers[$hdr])) {
295 $res2 = formatdisplayheader($hdr, $this->headers[$hdr]);
296 if ($res2) {
297 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
298 }
299 }
300 }
301
1f75b135 302 $res .= '<tr><th colspan="2">'._b_('Corps');
303 if (count($this->messages) > 1) {
304 for ($i = 0 ; $i < count($this->messages) ; $i++) {
305 if ($i == 0) {
306 $res .= ' : ';
307 } else {
308 $res .= ' . ';
309 }
310 preg_match("@text/([^;]+);@", $this->messages[$i]['headers']['content-type'], $format);
311 $format = textFormat_translate($format[1]);
312 if ($i != $partid) {
313 $res .= '<a href="?group='.$banana->state['group'].'&artid='.$this->id.'&part='.$i.'">'.$format.'</a>';
314 } else {
315 $res .= $format;
316 }
317 }
318 }
319 $res .= '</th></tr>';
320
321 preg_match("@text/([^;]+);@", $this->headers['content-type'], $format);
322 $format = $format[1];
323 $res .= '<tr><td colspan="2">';
324 if ($format == 'html') {
325 $res .= formatbody($this->body, $format);
326 } else {
327 $res .= '<pre>'.formatbody($this->body).'</pre>';
328 }
329 $res .= '</td></tr>';
7a0e2710 330
331 if (count($this->pj) > 0) {
332 $res .= '<tr><th colspan="2">'._b_('Pièces jointes').'</th></tr>';
333 $res .= '<tr><td colspan="2">';
334 $i = 0;
335 foreach ($this->pj as $file) {
336 $res .= $file['filename'].' ('.$file['MIME'].') : ';
337 $res .= '<a href="pj.php?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'">télécharger</a>';
a79e9a4f 338 $res .= ' . <a href="pj.php?group='.$banana->state['group'].'&artid='.$this->id.'&pj='.$i.'&action=view" target="_blank">aperçu</a>';
7a0e2710 339 $res .= '<br/>';
340 $i++;
341 }
342 $res .= '</td></tr>';
343 }
78cd27b3 344
cc43419f 345 $res .= '<tr><th colspan="2">'._b_('Apercu').'</th></tr>';
78cd27b3 346 $ndx = $banana->spool->getndx($this->id);
347 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>';
348
349 return $res.'</table>';
350 }
351}
352
353?>