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