=?utf-8?q?Ajout=20de=20:
[banana.git] / banana / 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-disposition', '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=utf-8\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 $this->_require('NetNNTP');
43 setlocale(LC_ALL, $this->profile['locale']);
44 $this->nntp = new nntp($this->host);
45 }
46
47 function run($class = 'Banana')
48 {
49 global $banana;
50 Banana::_require('misc');
51 $banana = new $class();
52
53 if (!$banana->nntp) {
54 return '<p class="error">'._b_('Impossible de contacter le serveur').'</p>';
55 }
56
57 $group = empty($_GET['group']) ? null : strtolower($_GET['group']);
58 $artid = empty($_GET['artid']) ? null : strtolower($_GET['artid']);
59 $banana->state = Array ('group' => $group, 'artid' => $artid);
60
61 if (is_null($group)) {
62
63 if (isset($_GET['subscribe'])) {
64 return $banana->action_listSubs();
65 } elseif (isset($_POST['subscribe'])) {
66 $banana->action_saveSubs();
67 }
68 return $banana->action_listGroups();
69
70 } elseif (is_null($artid)) {
71
72 if (isset($_POST['action']) && $_POST['action'] == 'new') {
73 return $banana->action_doFup($group, isset($_POST['artid']) ? intval($_POST['artid']) : -1);
74 } elseif (isset($_GET['action']) && $_GET['action'] == 'new') {
75 return $banana->action_newFup($group);
76 } else {
77 return $banana->action_showThread($group, isset($_GET['first']) ? intval($_GET['first']) : 1);
78 }
79
80 } else {
81
82 if (isset($_POST['action']) && $_POST['action']=='cancel') {
83 $res = $banana->action_cancelArticle($group, $artid);
84 } else {
85 $res = '';
86 }
87
88 if (isset($_GET['action'])) {
89 switch ($_GET['action']) {
90 case 'cancel':
91 $res .= $banana->action_showArticle($group, $artid);
92 if ($banana->post->checkcancel()) {
93 $form = '<p class="error">'._b_('Voulez-vous vraiment annuler ce message ?').'</p>'
94 . "<form action=\"?group=$group&amp;artid=$artid\" method='post'><p>"
95 . '<input type="hidden" name="action" value="cancel" />'
96 . '<input type="submit" value="Annuler !" />'
97 . '</p></form>';
98 return $form.$res;
99 }
100 return $res;
101
102 case 'new':
103 return $banana->action_newFup($group, $artid);
104 }
105 }
106
107 if (isset($_GET['pj'])) {
108 $action = false;
109 if (isset($_GET['action']) && $_GET['action'] == 'view') {
110 $action = true;
111 }
112 return $banana->action_getAttachment($group, $artid, $_GET['pj'], $action);
113 }
114
115 return $res . $banana->action_showArticle($group, $artid);
116 }
117 }
118
119 /**************************************************************************/
120 /* actions */
121 /**************************************************************************/
122
123 function action_saveSubs()
124 {
125 return;
126 }
127
128 function action_listGroups()
129 {
130 $this->_newGroup();
131
132 $cuts = displayshortcuts();
133 $res = '<h1>'._b_('Les forums de Banana').'</h1>'.$cuts.$this->groups->to_html();
134 if (count($this->newgroups->overview)) {
135 $res .= '<p>'._b_('Les forums suivants ont été créés depuis ton dernier passage :').'</p>';
136 $res .= $this->newgroups->to_html();
137 }
138
139 $this->nntp->quit();
140 return $res.$cuts;
141 }
142
143 function action_listSubs()
144 {
145 $this->_require('groups');
146 $this->groups = new BananaGroups(BANANA_GROUP_ALL);
147
148 $cuts = displayshortcuts();
149 $res = '<h1>'._b_('Abonnements').'</h1>'.$cuts.$this->groups->to_html(true).$cuts;
150
151 $this->nntp->quit();
152 return $res;
153 }
154
155 function action_showThread($group, $first)
156 {
157 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
158
159 if ($first > count($this->spool->overview)) {
160 $first = count($this->spool->overview);
161 }
162
163 $first = $first - ($first % $this->tmax) + 1;
164
165 $cuts = displayshortcuts($first);
166
167 $res = '<h1>'.$group.'</h1>'.$cuts;
168 $res .= $this->spool->to_html($first, $first+$this->tmax);
169
170 $this->nntp->quit();
171
172 return $res.$cuts;
173 }
174
175 function action_showArticle($group, $id)
176 {
177 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
178 $this->_newPost($id);
179 if (!$this->post) {
180 if ($this->nntp->lasterrorcode == "423") {
181 $this->spool->delid($id);
182 }
183 $this->nntp->quit();
184 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
185 }
186
187 $cuts = displayshortcuts();
188 $res = '<h1>'._b_('Message').'</h1>'.$cuts;
189 $res .= $this->post->to_html();
190
191 $this->nntp->quit();
192
193 return $res.$cuts;
194 }
195
196 function action_getAttachment($group, $id, $pjid, $action)
197 {
198 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
199 $this->_newPost($id);
200 if (!$this->post) {
201 if ($this->nntp->lasterrorcode == "423") {
202 $this->spool->delid($id);
203 }
204 $this->nntp->quit();
205 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
206 }
207
208 $this->nntp->quit();
209 if ($this->post->get_attachment($pjid, $action)) {
210 return "";
211 } else {
212 return displayshortcuts().'<p calss="error">'._b_('Impossible d\'accéder à la pièce jointe.').'</p>';
213 }
214 }
215
216 function action_cancelArticle($group, $id)
217 {
218 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
219 $this->_newPost($id);
220 $mid = array_search($id, $this->spool->ids);
221
222 if (!$this->post->checkcancel()) {
223 return '<p class="error">'._b_('Vous n\'avez pas les permissions pour annuler ce message').'</p>';
224 }
225 $msg = 'From: '.$this->profile['name']."\n"
226 . "Newsgroups: $group\n"
227 . "Subject: cmsg $mid\n"
228 . $this->custom
229 . "Control: cancel $mid\n"
230 . "\n"
231 . "Message canceled with Banana";
232 if ($this->nntp->post($msg)) {
233 $this->spool->delid($id);
234 $this->nntp->quit();
235 header("Location: ?group=$group&amp;first=$id");
236 } else {
237 return '<p class="error">'._b_('Impossible d\'annuler le message').'</p>';
238 }
239 }
240
241 function action_newFup($group, $id = -1)
242 {
243 $subject = $body = '';
244 $target = $group;
245
246 if ($id > 0) {
247 $this->nntp->group($group);
248 $this->_newPost($id);
249 if ($this->post) {
250 $subject = preg_replace("/^re\s*:\s*/i", '', 'Re: '.$this->post->headers['subject']);
251 $body = utf8_encode($this->post->name." "._b_("a écrit"))." :\n".wrap($this->post->body, "> ");
252 $target = isset($this->post->headers['followup-to']) ? $this->post->headers['followup-to'] : $this->post->headers['newsgroups'];
253 }
254 }
255
256 $this->nntp->quit();
257
258 $cuts = displayshortcuts();
259 $html = '<h1>'._b_('Nouveau message').'</h1>'.$cuts;
260 $html .= '<form action="?group='.$group.'" method="post" accept-charset="utf-8">';
261 $html .= '<table class="bicol" cellpadding="0" cellspacing="0">';
262 $html .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
263 $html .= '<tr><td>'._b_('Nom').'</td><td>'.htmlentities($this->profile['name']).'</td></tr>';
264 $html .= '<tr><td>'._b_('Sujet').'</td><td><input type="text" name="subject" value="'.htmlentities($subject).'" size="60" /></td></tr>';
265 $html .= '<tr><td>'._b_('Forums').'</td><td><input type="text" name="newsgroups" value="'.htmlentities($target).'" size="60" /></td></tr>';
266 $html .= '<tr><td>'._b_('Suivi à').'</td><td><input type="text" name="followup" value="" size="60" /></td></tr>';
267 $html .= '<tr><td>'._b_('Organisation').'</td><td>'.$this->profile['org'].'</td></tr>';
268 $html .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
269 $html .= '<tr><td colspan="2"><textarea name="body" cols="74" rows="16">'
270 .to_entities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></th>';
271 $html .= '<tr><td colspan="2">';
272 if ($id > 0) {
273 $html .= '<input type="hidden" name="artid" value="'.$id.'" />';
274 }
275 $html .= '<input type="hidden" name="action" value="new" />';
276 $html .= '<input type="submit" /></td></tr>';
277 $html .= '</table></form>';
278
279 return $html.$cuts;
280 }
281
282 function action_doFup($group, $artid = -1)
283 {
284 if ( ! ( is_utf8($_POST['subject']) && is_utf8($_POST['name'])
285 && is_utf8($_POST['org']) && is_utf8($_POST['body']) )
286 ) {
287 foreach(array('subject', 'name', 'org', 'body') as $key) {
288 $_POST[$key] = utf8_encode($_POST[$key]);
289 }
290 }
291
292 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
293 $body = preg_replace("/\n\.[ \t\r]*\n/m", "\n..\n", $_POST['body']);
294 $msg = 'From: '.$this->profile['name']."\n"
295 . "Newsgroups: ".$_POST['newsgroups']."\n"
296 . "Subject: ".headerEncode($_POST['subject'], 128)."\n"
297 . (empty($this->profile['org']) ? '' : "Organization: {$this->profile['org']}\n")
298 . (empty($_POST['followup']) ? '' : 'Followup-To: '.$_POST['followup']."\n");
299
300 if ($artid != -1) {
301 $this->_require('post');
302 $post = new BananaPost($artid);
303 $refs = ( isset($post->headers['references']) ? $post->headers['references']." " : "" );
304 $msg .= "References: $refs{$post->headers['message-id']}\n";
305 }
306
307 $msg .= $this->custom.$this->profile['customhdr']."\n".wrap($body, "", $this->wrap);
308
309 if ($this->nntp->post($msg)) {
310 header("Location: ?group=$group".($artid==-1 ? '' : "&first=$artid"));
311 } else {
312 return "<p class=\"error\">"._b_('Impossible de poster le message')."</p>".$this->action_showThread($group, $artid);
313 }
314 }
315
316 /**************************************************************************/
317 /* Private functions */
318 /**************************************************************************/
319
320 function _newSpool($group, $disp=0, $since='') {
321 $this->_require('spool');
322 if (!$this->spool || $this->spool->group != $group) {
323 $this->spool = new BananaSpool($group, $disp, $since);
324 }
325 }
326
327 function _newPost($id)
328 {
329 $this->_require('post');
330 $this->post = new BananaPost($id);
331 }
332
333 function _newGroup()
334 {
335 $this->_require('groups');
336 $this->groups = new BananaGroups(BANANA_GROUP_SUB);
337 if ($this->groups->type == BANANA_GROUP_SUB) {
338 $this->newgroups = new BananaGroups(BANANA_GROUP_NEW);
339 }
340 }
341
342 function _require($file)
343 {
344 require_once (dirname(__FILE__).'/'.$file.'.inc.php');
345 }
346 }
347
348 ?>