a8a42d7fe16d1e9fdf88f2d51765d37f96d8c1ec
[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 /** Favorites MIMEtypes to use, by order for reading multipart messages
20 */
21 var $body_mime = array('text/plain', 'text/html', 'text/richtext');
22 /** Indicate wether posting attachment is allowed
23 */
24 var $can_attach = true;
25 /** Maximum allowed file size for attachment
26 */
27 var $maxfilesize = 100000;
28
29 /** Regexp for selecting newsgroups to show (if empty, match all newsgroups)
30 * ex : '^xorg\..*' for xorg.*
31 */
32 var $grp_pattern;
33
34 var $tbefore = 5;
35 var $tafter = 5;
36 var $tmax = 50;
37
38 var $wrap = 74;
39 /** Match an url
40 * Should be included in a regexp delimited using ! (eg: "!$url_regexp!i")
41 * If it matches, return 3 main parts :
42 * \\1 and \\3 are delimiters
43 * \\2 is the url
44 *
45 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
46 * $matches[1] = "["
47 * $matches[2] = "http://www.polytechnique.org"
48 * $matches[3] = "]"
49 */
50 var $url_regexp = '(["\[])?((?:https?|ftp|news)://(?:&amp;|[a-z@0-9.~%$£µ&i#\-+=_/\?])*)(["\]])?';
51
52
53 /** Boundary for multipart messages
54 */
55 var $boundary = 'bananaBoundary42';
56 /** Global headers to use for messages
57 */
58 var $custom = "Mime-Version: 1.0\nUser-Agent: Banana @VERSION@\n";
59 /** Global headers to use from multipart messages
60 */
61 var $custom_mp = "Content-Type: multipart/mixed; boundary=\"bananaBoundary42\"\nContent-Transfer-Encoding: 7bit\n";
62 /** Body type when using plain text
63 */
64 var $custom_plain= "Content-Type: text/plain; charset=utf-8\nContent-Transfert-Encoding: 8bit\n";
65
66 /** News serveur to use
67 */
68 var $host = 'news://localhost:119/';
69
70 /** User profile
71 */
72 var $profile = Array( 'name' => 'Anonymous <anonymouse@example.com>', 'sig' => '', 'org' => '',
73 'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array());
74
75 var $state = Array('group' => null, 'artid' => null);
76 var $nntp;
77 var $groups;
78 var $newgroups;
79 var $post;
80 var $spool;
81
82 function Banana()
83 {
84 $this->_require('NetNNTP');
85 setlocale(LC_ALL, $this->profile['locale']);
86 $this->nntp = new nntp($this->host);
87 }
88
89 function run($class = 'Banana')
90 {
91 global $banana;
92
93 Banana::_require('misc');
94 $banana = new $class();
95
96 if (!$banana->nntp) {
97 return '<p class="error">'._b_('Impossible de contacter le serveur').'</p>';
98 }
99
100 $group = empty($_GET['group']) ? null : strtolower($_GET['group']);
101 $artid = empty($_GET['artid']) ? null : strtolower($_GET['artid']);
102 $partid = !isset($_GET['part']) ? -1 : $_GET['part'];
103 $banana->state = Array ('group' => $group, 'artid' => $artid);
104
105 if (is_null($group)) {
106 if (isset($_GET['subscribe'])) {
107 return $banana->action_listSubs();
108 } elseif (isset($_POST['subscribe'])) {
109 $banana->action_saveSubs();
110 }
111 return $banana->action_listGroups();
112
113 } elseif (is_null($artid)) {
114 if (isset($_POST['action']) && $_POST['action'] == 'new') {
115 return $banana->action_doFup($group, isset($_POST['artid']) ? intval($_POST['artid']) : -1);
116 } elseif (isset($_GET['action']) && $_GET['action'] == 'new') {
117 return $banana->action_newFup($group);
118 } else {
119 return $banana->action_showThread($group, isset($_GET['first']) ? intval($_GET['first']) : 1);
120 }
121
122 } else {
123 if (isset($_POST['action']) && $_POST['action']=='cancel') {
124 $res = $banana->action_cancelArticle($group, $artid);
125 } else {
126 $res = '';
127 }
128
129 if (isset($_GET['action'])) {
130 switch ($_GET['action']) {
131 case 'cancel':
132 $res .= $banana->action_showArticle($group, $artid, $partid);
133 if ($banana->post->checkcancel()) {
134 $form = '<p class="error">'._b_('Voulez-vous vraiment annuler ce message ?').'</p>'
135 . "<form action=\"?group=$group&amp;artid=$artid\" method='post'><p>"
136 . '<input type="hidden" name="action" value="cancel" />'
137 . '<input type="submit" value="Annuler !" />'
138 . '</p></form>';
139 return $form.$res;
140 }
141 return $res;
142
143 case 'new':
144 return $banana->action_newFup($group, $artid);
145 }
146 }
147
148 if (isset($_GET['pj'])) {
149 $action = false;
150 if (isset($_GET['action']) && $_GET['action'] == 'view') {
151 $action = true;
152 }
153 $att = $banana->action_getAttachment($group, $artid, $_GET['pj'], $action);
154 if ($att != "") {
155 return $res.$att;
156 }
157 return "";
158 }
159
160 return $res . $banana->action_showArticle($group, $artid, $partid);
161 }
162 }
163
164 /**************************************************************************/
165 /* actions */
166 /**************************************************************************/
167
168 function action_saveSubs()
169 {
170 return;
171 }
172
173 function action_listGroups()
174 {
175 $this->_newGroup();
176
177 $cuts = displayshortcuts();
178 $res = '<h1>'._b_('Les forums de Banana').'</h1>'.$cuts.$this->groups->to_html();
179 if (count($this->newgroups->overview)) {
180 $res .= '<p>'._b_('Les forums suivants ont été créés depuis ton dernier passage :').'</p>';
181 $res .= $this->newgroups->to_html();
182 }
183
184 $this->nntp->quit();
185 return $res.$cuts;
186 }
187
188 function action_listSubs()
189 {
190 $this->_require('groups');
191 $this->groups = new BananaGroups(BANANA_GROUP_ALL);
192
193 $cuts = displayshortcuts();
194 $res = '<h1>'._b_('Abonnements').'</h1>'.$cuts.$this->groups->to_html(true).$cuts;
195
196 $this->nntp->quit();
197 return $res;
198 }
199
200 function action_showThread($group, $first)
201 {
202 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
203
204 if ($first > count($this->spool->overview)) {
205 $first = count($this->spool->overview);
206 }
207
208 $first = $first - ($first % $this->tmax) + 1;
209
210 $cuts = displayshortcuts($first);
211
212 $res = '<h1>'.$group.'</h1>'.$cuts;
213 $res .= $this->spool->to_html($first, $first+$this->tmax);
214
215 $this->nntp->quit();
216
217 return $res.$cuts;
218 }
219
220 function action_showArticle($group, $id, $part)
221 {
222 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
223 $this->_newPost($id);
224 if (!$this->post) {
225 if ($this->nntp->lasterrorcode == "423") {
226 $this->spool->delid($id);
227 }
228 $this->nntp->quit();
229 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
230 }
231
232 $cuts = displayshortcuts();
233 $res = '<h1>'._b_('Message').'</h1>'.$cuts;
234 $res .= $this->post->to_html($part);
235
236 $this->nntp->quit();
237
238 return $res.$cuts;
239 }
240
241 function action_getAttachment($group, $id, $pjid, $action)
242 {
243 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
244 $this->_newPost($id);
245 if (!$this->post) {
246 if ($this->nntp->lasterrorcode == "423") {
247 $this->spool->delid($id);
248 }
249 $this->nntp->quit();
250 return displayshortcuts().'<p class="error">'._b_('Impossible d\'accéder au message. Le message a peut-être été annulé').'</p>';
251 }
252
253 $this->nntp->quit();
254 if ($this->post->get_attachment($pjid, $action)) {
255 return "";
256 } else {
257 return displayshortcuts().'<p calss="error">'._b_('Impossible d\'accéder à la pièce jointe.').'</p>';
258 }
259 }
260
261 function action_cancelArticle($group, $id)
262 {
263 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
264 $this->_newPost($id);
265 $mid = array_search($id, $this->spool->ids);
266
267 if (!$this->post->checkcancel()) {
268 return '<p class="error">'._b_('Vous n\'avez pas les permissions pour annuler ce message').'</p>';
269 }
270 $msg = 'From: '.$this->profile['name']."\n"
271 . "Newsgroups: $group\n"
272 . "Subject: cmsg $mid\n"
273 . $this->custom
274 . "Control: cancel $mid\n"
275 . "\n"
276 . "Message canceled with Banana";
277 if ($this->nntp->post($msg)) {
278 $this->spool->delid($id);
279 $this->nntp->quit();
280 header("Location: ?group=$group&amp;first=$id");
281 } else {
282 return '<p class="error">'._b_('Impossible d\'annuler le message').'</p>';
283 }
284 }
285
286 function action_newFup($group, $id = -1)
287 {
288 $subject = $body = '';
289 $target = $group;
290
291 if ($id > 0) {
292 $this->nntp->group($group);
293 $this->_newPost($id);
294 if ($this->post) {
295 $subject = preg_replace("/^re\s*:\s*/i", '', 'Re: '.$this->post->headers['subject']);
296 $body = utf8_encode($this->post->name." "._b_("a écrit"))." :\n".wrap($this->post->get_body(), "> ");
297 $target = isset($this->post->headers['followup-to']) ? $this->post->headers['followup-to'] : $this->post->headers['newsgroups'];
298 }
299 }
300
301 $this->nntp->quit();
302
303 $cuts = displayshortcuts();
304 $html = '<h1>'._b_('Nouveau message').'</h1>'.$cuts;
305 $html .= '<form enctype="multipart/form-data" action="?group='.$group.'" method="post" accept-charset="utf-8">';
306 $html .= '<table class="bicol" cellpadding="0" cellspacing="0">';
307 $html .= '<tr><th colspan="2">'._b_('En-têtes').'</th></tr>';
308 $html .= '<tr><td>'._b_('Nom').'</td><td>'.htmlentities($this->profile['name']).'</td></tr>';
309 $html .= '<tr><td>'._b_('Sujet').'</td><td><input type="text" name="subject" value="'.htmlentities($subject).'" size="60" /></td></tr>';
310 $html .= '<tr><td>'._b_('Forums').'</td><td><input type="text" name="newsgroups" value="'.htmlentities($target).'" size="60" /></td></tr>';
311 $html .= '<tr><td>'._b_('Suivi à').'</td><td><input type="text" name="followup" value="" size="60" /></td></tr>';
312 $html .= '<tr><td>'._b_('Organisation').'</td><td>'.$this->profile['org'].'</td></tr>';
313 $html .= '<tr><th colspan="2">'._b_('Corps').'</th></tr>';
314 $html .= '<tr><td colspan="2"><textarea name="body" cols="74" rows="16">'
315 . to_entities($body).($this->profile['sig'] ? "\n\n-- \n".htmlentities($this->profile['sig']) : '').'</textarea></td></tr>';
316 if ($this->can_attach) {
317 $html .= '<tr><th colspan="2">'._b_('Pièce jointe').'</th></tr>';
318 $html .= '<tr><td colspan="2"><input type="hidden" name="MAX_FILE_SIZE" value="'.$this->maxfilesize.'" />';
319 $html .= '<input type="file" name="newpj" size="40"/></td></tr>';
320 }
321 $html .= '<tr><th colspan="2">';
322 if ($id > 0) {
323 $html .= '<input type="hidden" name="artid" value="'.$id.'" />';
324 }
325 $html .= '<input type="hidden" name="action" value="new" />';
326 $html .= '<input type="submit" /></th></tr>';
327 $html .= '</table></form>';
328
329 return $html.$cuts;
330 }
331
332 function action_doFup($group, $artid = -1)
333 {
334 if ( ! ( is_utf8($_POST['subject']) && is_utf8($_POST['name'])
335 && is_utf8($_POST['org']) && is_utf8($_POST['body']) )
336 ) {
337 foreach(array('subject', 'name', 'org', 'body') as $key) {
338 $_POST[$key] = utf8_encode($_POST[$key]);
339 }
340 }
341
342 $this->_newSpool($group, $this->profile['display'], $this->profile['lastnews']);
343 $body = preg_replace("/\n\.[ \t\r]*\n/m", "\n..\n", $_POST['body']);
344 $msg = 'From: '.$this->profile['name']."\n"
345 . "Newsgroups: ".$_POST['newsgroups']."\n"
346 . "Subject: ".headerEncode($_POST['subject'], 128)."\n"
347 . (empty($this->profile['org']) ? '' : "Organization: {$this->profile['org']}\n")
348 . (empty($_POST['followup']) ? '' : 'Followup-To: '.$_POST['followup']."\n");
349
350 if ($artid != -1) {
351 $this->_require('post');
352 $post = new BananaPost($artid);
353 $refs = ( isset($post->headers['references']) ? $post->headers['references']." " : "" );
354 $msg .= "References: $refs{$post->headers['message-id']}\n";
355 }
356
357 $body_headers = $this->custom_plain;
358 $body = wrap($body, "");
359
360 // include attachment in the body
361 $uploaded = $this->_upload('newpj');
362 switch ($uploaded['error']) {
363 case UPLOAD_ERR_OK:
364 $this->custom = $this->custom_mp.$this->custom;
365 $body = $this->_make_part($body_headers, $body);
366 $file_head = 'Content-Type: '.$uploaded['type'].'; name="'.$uploaded['name']."\"\n"
367 . 'Content-Transfer-Encoding: '.$uploaded['encoding']."\n"
368 . 'Content-Disposition: attachment; filename="'.$uploaded['name']."\"\n";
369 $body .= $this->_make_part($file_head, $uploaded['data']);
370 $body .= "\n--".$this->boundary.'--';
371 break;
372
373 case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE:
374 return '<p class="error">'._b_('Fichier trop gros pour être envoyé : ')
375 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
376
377 case UPLOAD_ERR_PARTIAL:
378 return '<p class="error">'._b_('Erreur lors de l\'upload de ')
379 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
380
381 case UPLOAD_ERR_NO_FILE:
382 return '<p class="error">'._b_('Le fichier spécifié n\'existe pas : ')
383 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
384
385 case UPLOAD_ERR_NO_TMP_DIR:
386 return '<p class="error">'._b_('Une erreur est survenue sur le serveur lors de l\'upload de ')
387 .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
388
389 default:
390 $this->custom = $body_headers.$this->custom;
391 }
392
393 // finalise and post the message
394 $msg .= $this->custom.$this->profile['customhdr']."\n".$body;
395
396 if ($this->nntp->post($msg)) {
397 header("Location: ?group=$group".($artid==-1 ? '' : "&first=$artid"));
398 } else {
399 return "<p class=\"error\">"._b_('Impossible de poster le message')."</p>".$this->action_showThread($group, $artid);
400 }
401 }
402
403 /**************************************************************************/
404 /* Private functions */
405 /**************************************************************************/
406
407 function _newSpool($group, $disp=0, $since='') {
408 $this->_require('spool');
409 if (!$this->spool || $this->spool->group != $group) {
410 $this->spool = new BananaSpool($group, $disp, $since);
411 }
412 }
413
414 function _newPost($id)
415 {
416 $this->_require('post');
417 $this->post = new BananaPost($id);
418 }
419
420 function _newGroup()
421 {
422 $this->_require('groups');
423 $this->groups = new BananaGroups(BANANA_GROUP_SUB);
424 if ($this->groups->type == BANANA_GROUP_SUB) {
425 $this->newgroups = new BananaGroups(BANANA_GROUP_NEW);
426 }
427 }
428
429 function _require($file)
430 {
431 require_once (dirname(__FILE__).'/'.$file.'.inc.php');
432 }
433
434 function _upload($file)
435 {
436 if ($_FILES[$file]['name'] == "") {
437 return Array( 'error' => -1 );
438 }
439
440 // upload
441 $_FILES[$file]['tmp_name'];
442
443 // test if upload is ok
444 $file = $_FILES[$file];
445 if ($file['size'] == 0 || $file['error'] != 0) {
446 if ($file['error'] == 0) {
447 $file['error'] = -1;
448 }
449 return $file;
450 }
451
452 // adding custum data
453 $mime = rtrim(shell_exec('file -bi '.$file['tmp_name'])); //Because mime_content_type don't work :(
454 $encod = 'base64';
455 if (preg_match("@([^ ]+/[^ ]+); (.*)@", $mime, $format)) {
456 $mime = $format[1];
457 $encod = $format[2];
458 }
459 $data = fread(fopen($file['tmp_name'], 'r'), $file['size']);
460 if ($encod == 'base64') {
461 $data = chunk_split(base64_encode($data));
462 }
463 $file['name'] = basename($file['name']);
464 $file['type'] = $mime;
465 $file['encoding'] = $encod;
466 $file['data'] = $data;
467
468 return $file;
469 }
470
471 function _make_part($headers, $body)
472 {
473 return "\n--".$this->boundary."\n".$headers."\n".$body;
474 }
475 }
476
477 ?>