prepare banana to be a library
[banana.git] / include / 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 /** poster name */
21 var $name;
22
23 /** constructor
24 * @param $_id STRING MSGNUM or MSGID (a group should be selected in this case)
25 */
26 function BananaPost($_id)
27 {
28 global $banana;
29 $this->id = $_id;
30 $this->_header();
31
32 $this->body = join("\n", $banana->nntp->body($_id));
33
34 if (isset($this->headers['content-transfer-encoding'])) {
35 if (preg_match("/base64/", $this->headers['content-transfer-encoding'])) {
36 $this->body = base64_decode($this->body);
37 } elseif (preg_match("/quoted-printable/", $this->headers['content-transfer-encoding'])) {
38 $this->body = quoted_printable_decode($this->body);
39 }
40 }
41
42 if (preg_match('!charset=([^;]*);!', $this->headers['content-type'], $matches)) {
43 $this->body = iconv($matches[1], 'iso-8859-1', $this->body);
44 }
45 }
46
47 function _header()
48 {
49 global $banana;
50 $hdrs = $banana->nntp->head($this->id);
51 if (!$hdrs) {
52 $this = null;
53 return false;
54 }
55
56 // parse headers
57 foreach ($hdrs as $line) {
58 if (preg_match("/^[\t\r ]+/", $line)) {
59 $line = ($hdr=="X-Face"?"":" ").ltrim($line);
60 if (in_array($hdr, $banana->parse_hdr)) {
61 $this->headers[$hdr] .= $line;
62 }
63 } else {
64 list($hdr, $val) = split(":[ \t\r]*", $line, 2);
65 $hdr = strtolower($hdr);
66 if (in_array($hdr, $banana->parse_hdr)) {
67 $this->headers[$hdr] = $val;
68 }
69 }
70 }
71 // decode headers
72 foreach ($banana->hdecode as $hdr) {
73 if (isset($this->headers[$hdr])) {
74 $this->headers[$hdr] = headerDecode($this->headers[$hdr]);
75 }
76 }
77
78 $this->name = $this->headers['from'];
79 $this->name = preg_replace('/<[^ ]*>/', '', $this->name);
80 $this->name = trim($this->name);
81 }
82
83 function checkcancel()
84 {
85 if (function_exists('hook_checkcancel')) {
86 return hook_checkcancel($this->headers);
87 }
88 return ($this->headers['from'] == $_SESSION['name']." <".$_SESSION['mail'].">");
89 }
90
91 function to_html()
92 {
93 global $banana;
94
95 $res = '<table class="bicol banana_msg" cellpadding="0" cellspacing="0">';
96 $res .= '<tr><th colspan="2">'._b_('En-tĂȘtes').'</th></tr>';
97
98 foreach ($banana->show_hdr as $hdr) {
99 if (isset($this->headers[$hdr])) {
100 $res2 = formatdisplayheader($hdr, $this->headers[$hdr]);
101 if ($res2) {
102 $res .= '<tr><td class="hdr">'.header_translate($hdr)."</td><td class='val'>$res2</td></tr>\n";
103 }
104 }
105 }
106
107 $res .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
108 $res .= '<tr><td colspan="2"><pre>'.formatbody($this->body).'</pre></td></tr>';
109
110 $res .= '<tr><th colspan="2">'._b_('apercu').'</th></tr>';
111 $ndx = $banana->spool->getndx($this->id);
112 $res .= '<tr><td class="thrd" colspan="2">'.$banana->spool->to_html($ndx-$banana->tbefore, $ndx+$banana->tafter, $ndx).'</td></tr>';
113
114 return $res.'</table>';
115 }
116 }
117
118 ?>