ba24a0519c1453a426db2815077774929f33a4b8
[banana.git] / include / banana.inc.php.in
1 <?php
2 /********************************************************************************
3 * install.d/config.inc.php : configuration file
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 Banana
11 {
12 var $maxspool = 3000;
13
14 var $hdecode = array('from','name','organization','subject');
15 var $parse_hdr = array('content-transfer-encoding', 'content-type', 'date', 'followup-to', 'from',
16 'message-id', 'newsgroups', 'organization', 'references', 'subject', 'x-face');
17 var $show_hdr = array('from', 'subject', 'newsgroups', 'followup', 'date', 'organization', 'references', 'x-face');
18
19
20 var $tbefore = 5;
21 var $tafter = 5;
22 var $tmax = 50;
23
24 var $wrap = 74;
25
26 var $custom = "Content-Type: text/plain; charset=iso-8859-15\nMime-Version: 1.0\nContent-Transfer-Encoding: 8bit\nUser-Agent: Banana @VERSION@\n";
27
28 var $host = 'news://localhost:119/';
29
30 var $profile = Array( 'name' => 'Anonymous <anonymouse@example.com>', 'sig' => '', 'org' => '',
31 'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array());
32
33 var $state = Array('group' => null, 'artid' => null);
34 var $nntp;
35 var $groups;
36 var $newgroups;
37 var $post;
38 var $spool;
39
40 function Banana()
41 {
42 require_once('include/NetNNTP.inc.php');
43 $this->_setupProfile();
44 $this->nntp = new nntp($this->host);
45
46 $this->profile['subscribe'][] = 'xorg.general';
47 $this->profile['subscribe'][] = 'xorg.test';
48 $this->profile['subscribe'][] = 'xorg.promo.x1970';
49 $this->profile['lastnews'] = time() - 24*3600*7;
50
51 }
52
53 function run()
54 {
55 require_once("include/misc.inc.php");
56 global $banana;
57
58 $banana = new Banana();
59 $group = empty($_GET['group']) ? null : strtolower($_GET['group']);
60 $artid = empty($_GET['artid']) ? null : strtolower($_GET['artid']);
61 $banana->state = Array ('group' => $group, 'artid' => $artid);
62
63 if (is_null($group)) {
64
65 return $banana->action_listGroups();
66
67 } elseif (is_null($artid)) {
68
69 if (isset($_POST['action']) && $_POST['action'] == 'new') {
70 return $banana->action_doFup($group, isset($_POST['artid']) ? intval($_POST['artid']) : -1);
71 } elseif (isset($_GET['action']) && $_GET['action'] == 'new') {
72 return $banana->action_newFup($group);
73 } else {
74 return $banana->action_showThread($group, isset($_GET['first']) ? intval($_GET['first']) : 1);
75 }
76
77 } else {
78
79 if (isset($_POST['action']) && $_POST['action']=='cancel') {
80 $res = $banana->action_cancelArticle($group, $artid);
81 } else {
82 $res = '';
83 }
84
85 if (isset($_GET['action'])) {
86 switch ($_GET['action']) {
87 case 'cancel':
88 $res .= $banana->action_showArticle($group, $artid);
89 if ($banana->post->checkcancel()) {
90 $form = '<p class="error">'._b_('Voulez-vous vraiment annuler ce message ?').'</p>'
91 . "<form action=\"?group=$group&amp;artid=$artid\" method='post'><p>"
92 . '<input type="hidden" name="action" value="cancel" />'
93 . '<input type="submit" value="Annuler !" />'
94 . '</p></form>';
95 return $form.$res;
96 }
97 return $res;
98
99 case 'new':
100 return $banana->action_newFup($group, $artid);
101 }
102 }
103 return $res . $banana->action_showArticle($group, $artid);
104 }
105 }
106
107 /**************************************************************************/
108 /* actions */
109 /**************************************************************************/
110
111 function action_listGroups()
112 {
113 $this->_newGroup();
114
115 $cuts = displayshortcuts();
116 $res = '<h1>'._b_('Les forums de Banana').'</h1>'.$cuts.$this->groups->to_html();
117 if (count($this->newgroups->overview)) {
118 $res .= '<p>'._b_('Les forums suivants ont été créés depuis ton dernier passage :').'</p>';
119 $res .= $this->newgroups->to_html();
120 }
121
122 $this->nntp->quit();
123 return $res.$cuts;
124 }
125
126 function action_listSubs()
127 {
128 require_once("include/groups.inc.php");
129 $this->groups = new BananaGroups(BANANA_GROUP_ALL);
130
131 $cuts = displayshortcuts();
132 $res = '<h1>'._b_('Abonnements').'</h1>'.$cuts.$this->groups->to_html(true).$cuts;
133
134 $this->nntp->quit();
135 return $res;
136 }
137
138 function action_showThread($group, $first)
139 {
140 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
141
142 if ($first > count($this->spool->overview)) {
143 $first = count($this->spool->overview);
144 }
145
146 $first = $first - ($first % $this->tmax) + 1;
147
148 $cuts = displayshortcuts($first);
149
150 $res = '<h1>'.$group.'</h1>'.$cuts;
151 $res .= $this->spool->to_html($first, $first+$this->tmax);
152
153 $this->nntp->quit();
154
155 return $res.$cuts;
156 }
157
158 function action_showArticle($group, $id)
159 {
160 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
161 $this->_newPost($id);
162 if (!$this->post) {
163 if ($this->nntp->lasterrorcode == "423") {
164 $this->spool->delid($id);
165 }
166 $this->nntp->quit();
167 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
168 }
169
170 $cuts = displayshortcuts();
171 $res = '<h1>'._b_('Message').'</h1>'.$cuts;
172 $res .= $this->post->to_html();
173
174 $this->nntp->quit();
175
176 return $res.$cuts;
177 }
178
179 function action_cancelArticle($group, $id)
180 {
181 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
182 $this->_newPost($id);
183 $mid = array_search($id, $this->spool->ids);
184
185 if (!$this->post->checkcancel()) {
186 return '<p class="error">'._b_('Vous n\'avez pas les permissions pour annuler ce message').'</p>';
187 }
188 $msg = 'From: '.$this->profile['name']."\n"
189 . "Newsgroups: $group\n"
190 . "Subject: cmsg $mid\n"
191 . $this->custom
192 . "Control: cancel $mid\n"
193 . "\n"
194 . "Message canceled with Banana";
195 if ($this->nntp->post($msg)) {
196 $this->spool->delid($id);
197 $this->nntp->quit();
198 header("Location: ?group=$group&amp;first=$id");
199 } else {
200 return '<p class="error">'._b_('Impossible d\'annuler le message').'</p>';
201 }
202 }
203
204 function action_newFup($group, $id = -1)
205 {
206 $subject = $body = '';
207 $target = $group;
208
209 if ($id > 0) {
210 $this->nntp->group($group);
211 $this->_newPost($id);
212 if ($this->post) {
213 $subject = preg_replace("/^re\s*:\s*/i", 'Re: ', $this->post->headers['subject']);
214 $body = $this->post->name." "._b_("a écrit")." :\n".wrap($this->post->body, "> ");
215 $target = isset($this->post->headers['followup-to']) ? $this->post->headers['followup-to'] : $this->post->headers['newsgroups'];
216 }
217 }
218
219 $this->nntp->quit();
220
221 $cuts = displayshortcuts();
222 $html = '<h1>'._b_('Nouveau message').'</h1>'.$cuts;
223 $html .= '<form action="?group='.$group.'" method="post">';
224 $html .= '<table class="bicol" cellpadding="0" cellspacing="0">';
225 $html .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
226 $html .= '<tr><td>'._b_('Nom').'</td><td>'.htmlentities($this->profile['name']).'</td></tr>';
227 $html .= '<tr><td>'._b_('Sujet').'</td><td><input type="text" name="subject" value="'.htmlentities($subject).'" size="60" /></td></tr>';
228 $html .= '<tr><td>'._b_('Forums').'</td><td><input type="text" name="newsgroups" value="'.htmlentities($target).'" size="60" /></td></tr>';
229 $html .= '<tr><td>'._b_('Suivi à').'</td><td><input type="text" name="followup" value="" size="60" /></td></tr>';
230 $html .= '<tr><td>'._b_('Organisation').'</td><td>'.$this->profile['org'].'</td></tr>';
231 $html .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
232 $html .= '<tr><td colspan="2"><textarea name="body" cols="74" rows="16">'
233 .htmlentities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></th>';
234 $html .= '<tr><td colspan="2">';
235 if ($id > 0) {
236 $html .= '<input type="hidden" name="artid" value="'.$id.'" />';
237 }
238 $html .= '<input type="hidden" name="action" value="new" />';
239 $html .= '<input type="submit" /></td></tr>';
240 $html .= '</table></form>';
241
242 return $html.$cuts;
243 }
244
245 function action_doFup($group, $artid = -1)
246 {
247 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
248 $body = preg_replace("/\n\.[ \t\r]*\n/m", "\n..\n", $_POST['body']);
249 $msg = 'From: '.$this->profile['name']."\n"
250 . "Newsgroups: ".$_POST['newsgroups']."\n"
251 . "Subject: ".$_POST['subject']."\n"
252 . (empty($this->profile['org']) ? '' : "Organization: {$this->profile['org']}\n")
253 . (empty($_POST['followup']) ? '' : 'Followup-To: '.$_POST['followup']."\n");
254
255 if ($artid != -1) {
256 require_once("include/post.inc.php");
257 $post = new BananaPost($artid);
258 $refs = ( isset($post->headers['references']) ? $post->headers['references']." " : "" );
259 $msg .= "References: $refs{$post->headers['message-id']}\n";
260 }
261
262 $msg .= $this->custom.$this->profile['customhdr']."\n".wrap($body, "", $this->wrap);
263
264 if ($this->nntp->post($msg)) {
265 header("Location: ?group=$group".($artid==-1 ? '' : "&first=$artid"));
266 } else {
267 return "<p class=\"error\">"._b_('Impossible de poster le message')."</p>".$this->action_showThread($group, $artid);
268 }
269 }
270
271 /**************************************************************************/
272 /* Private functions */
273 /**************************************************************************/
274
275 function _newSpool($group, $disp=0, $since='') {
276 require_once('include/spool.inc.php');
277 if (!$this->spool || $this->spool->group != $group) {
278 $this->spool = new BananaSpool($group, $disp, $since);
279 }
280 }
281
282 function _newPost($id)
283 {
284 require_once("include/post.inc.php");
285 $this->post = new BananaPost($id);
286 }
287
288 function _newGroup()
289 {
290 require_once("include/groups.inc.php");
291 $this->groups = new BananaGroups(BANANA_GROUP_SUB);
292 if ($this->groups->type == BANANA_GROUP_SUB) {
293 $this->newgroups = new BananaGroups(BANANA_GROUP_NEW);
294 }
295 }
296
297 function _setupProfile()
298 {
299 if (function_exists('hook_getprofile')) {
300 $this->profile = hook_getprofile();
301 }
302
303 setlocale(LC_ALL, $this->profile['locale']);
304 }
305 }
306
307 ?>