Fix format catcher (ie // ** __ catcher)
[banana.git] / banana / banana.inc.php.in
CommitLineData
c4f176d8 1<?php
2/********************************************************************************
3* banana/banana.inc.php : banana main file
4* --------------------------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
10require_once dirname(__FILE__) . '/text.func.inc.php';
11
12class Banana
13{
14
15#######
16# Configuration variables
17#######
18
19### General ###
20 static public $profile = Array( 'signature' => '',
21 'headers' => array('From' => 'Anonymous <anonymouse@example.com>'),
22 'display' => 0,
23 'lastnews' => 0,
24 'locale' => 'fr_FR.UTF-8',
25 'subscribe' => array(),
26 'autoup' => 1);
27 static public $boxpattern;
28 static public $withtabs = true;
29 static public $mimeparts = array();
30
31### Spool ###
32 static public $spool_root = '/var/spool/banana';
33 static public $spool_max = 3000;
34 static public $spool_tbefore = 5;
35 static public $spool_tafter = 5;
36 static public $spool_tmax = 50;
37 static public $spool_boxlist = true;
38
39### Message processing ###
40 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
41 'content-type', 'content-id', 'date', 'followup-to',
42 'from', 'message-id', 'newsgroups', 'organization',
43 'references', 'subject', 'x-face', 'in-reply-to',
44 'to', 'cc', 'reply-to');
45
46### Message display ###
47 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
48 'organization', 'date', 'references', 'in-reply-to');
49 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed',
50 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
51 static public $msgshow_xface = true;
52 static public $msgshow_wrap = 78;
53 static public $msgshow_externalimages = false;
54 static public $msgshow_hasextimages = false;
55 static public $msgshow_withthread = true;
56
57 /** Match an url
58 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
59 * If it matches, return 3 main parts :
60 * \\1 and \\3 are delimiters
61 * \\2 is the url
62 *
63 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
64 * $matches[1] = "["
65 * $matches[2] = "http://www.polytechnique.org"
66 * $matches[3] = "]"
67 */
68 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
69
70### Message edition ###
71 static public $msgedit_canattach = true;
72 static public $msgedit_maxfilesize = 100000;
73 /** Global headers to use for messages
74 */
75 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
76 /** Mime type order for quoting
77 */
78 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
79
80### Protocole ###
81 /** News serveur to use
82 */
83 static public $nntp_host = 'news://localhost:119/';
84
85 static public $mbox_path = '/var/mail';
b10e2813 86 static public $mbox_helper = './mbox-helper';
c4f176d8 87
88### Debug ###
89 static public $debug_nntp = false;
90 static public $debug_mbox = false;
91 static public $debug_smarty = false;
92
93
94#######
95# Constants
96#######
97
98 // Actions
99 const ACTION_BOX_NEEDED = 1; // mask
100 const ACTION_BOX_LIST = 2;
101 const ACTION_BOX_SUBS = 4;
102 const ACTION_MSG_LIST = 3;
103 const ACTION_MSG_READ = 5;
104 const ACTION_MSG_NEW = 9;
105 const ACTION_MSG_CANCEL = 17;
106 const ACTION_MSG_IMAGES = 33;
107
108 // Box list view
109 const BOXES_ALL = 0;
110 const BOXES_SUB = 1;
111 const BOXES_NEW = 2;
112
113 // Spool view mode
114 const SPOOL_ALL = 0;
115 const SPOOL_UNREAD = 1;
116
117
118#######
119# Runtime variables
120#######
121
122 static public $protocole = null;
123 static public $spool = null;
124 static public $message = null;
125 static public $page = null;
126
127 static public $group = null;
128 static public $artid = null;
129 static public $action = null;
130 static public $part = null;
131 static public $first = null;
132
133 /** Class parameters storage
134 */
135 public $params;
136
137
138#######
139# Banana Implementation
140#######
141
142 /** Build the instance of Banana
143 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
144 * @param protocole Protocole to use
145 */
146 public function __construct($params = null, $protocole = 'NNTP', $pageclass = 'BananaPage')
147 {
148 if (is_null($params)) {
149 $this->params = $_GET;
150 } else {
151 $this->params = $params;
152 }
153 $this->loadParams();
154
155 // connect to protocole handler
156 $classname = 'Banana' . $protocole;
157 if (!class_exists($classname)) {
158 Banana::load($protocole);
159 }
160 Banana::$protocole = new $classname(Banana::$group);
161
162 // build the page
163 if ($pageclass == 'BananaPage') {
164 Banana::load('page');
165 }
166 Banana::$page = new $pageclass;
167 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
168 'multipart/mixed' => _b_('Composition'),
169 'text/html' => _b_('Texte formaté'),
170 'text/plain' => _b_('Texte brut'),
171 'text/enriched' => _b_('Texte enrichi'),
172 'text' => _b_('Texte'),
173 'message/rfc822' => _b_('Mail'),
174 'message' => _b_('Message'),
175 'source' => _b_('Source'));
176 Banana::$mimeparts = array_merge($types, Banana::$mimeparts);
177 }
178
179 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
180 */
181 protected function loadParams()
182 {
183 Banana::$group = isset($this->params['group']) ? $this->params['group'] : null;
184 Banana::$artid = isset($this->params['artid']) ? $this->params['artid'] : null;
185 Banana::$first = isset($this->params['first']) ? $this->params['first'] : null;
186 Banana::$part = isset($this->params['part']) ? $this->params['part'] : 'text';
187
188 // Look for the action to execute
189 if (is_null(Banana::$group)) {
190 if (isset($this->params['action']) && $this->params['action'] == 'subscribe') {
191 Banana::$action = Banana::ACTION_BOX_SUBS;
192 } else {
193 Banana::$action = Banana::ACTION_BOX_LIST;
194 }
195 return;
196 }
197 $action = isset($this->params['action']) ? $this->params['action'] : null;
198 if (is_null(Banana::$artid)) {
199 if ($action == 'new') {
200 Banana::$action = Banana::ACTION_MSG_NEW;
201 } else {
202 Banana::$action = Banana::ACTION_MSG_LIST;
203 }
204 return;
205 }
206 switch ($action) {
207 case 'new':
208 Banana::$action = Banana::ACTION_MSG_NEW;
209 return;
210 case 'cancel':
211 Banana::$action = Banana::ACTION_MSG_CANCEL;
212 return;
213 case 'showext':
214 Banana::$action = Banana::ACTION_MSG_IMAGES;
215 return;
216 default:
217 Banana::$action = Banana::ACTION_MSG_READ;
218 }
219 }
220
221 /** Run Banana
222 * This function need user profile to be initialised
223 */
224 public function run()
225 {
226 // Configure locales
227 setlocale(LC_ALL, Banana::$profile['locale']);
228
229 // Check if the state is valid
230 if (Banana::$protocole->lastErrNo()) {
231 return Banana::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
232 . Banana::$protocole->lastError());
233 }
234 if (!Banana::$protocole->isValid()) {
235 return Banana::$page->kill(_b_('Connexion non-valide'));
236 }
237 if (Banana::$action & Banana::ACTION_BOX_NEEDED) {
238 if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', $group)) {
239 Banana::$page->setPage('group');
240 return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
241 }
242 }
243
244 // Dispatch to the action handlers
245 switch (Banana::$action) {
246 case Banana::ACTION_BOX_SUBS:
247 $error = $this->action_subscribe();
248 break;
249 case Banana::ACTION_BOX_LIST:
250 $error = $this->action_listBoxes();
251 break;
252 case Banana::ACTION_MSG_LIST:
253 $error = $this->action_showThread(Banana::$group, Banana::$first);
254 break;
255 case Banana::ACTION_MSG_IMAGES:
256 Banana::$msgshow_externalimages = true;
257 case Banana::ACTION_MSG_READ:
258 $error = $this->action_showMessage(Banana::$group, Banana::$artid, Banana::$part);
259 break;
260 case Banana::ACTION_MSG_NEW:
261 $error = $this->action_newMessage(Banana::$group, Banana::$artid);
262 break;
263 case Banana::ACTION_MSG_CANCEL:
264 $error = $this->action_cancelMessage(Banana::$group, Banana::$artid);
265 break;
266 default:
267 $error = _b_("L'action demandée n'est pas supportée par Banana");
268 }
269
270 // Generate the page
271 if (is_string($error)) {
272 return Banana::$page->kill($error);
273 }
274 return Banana::$page->run();
275 }
276
277 /** Return the CSS code to include in the headers
278 */
279 public function css()
280 {
281 return Banana::$page->css;
282 }
283
6a684b9b 284 /** Return the execution backtrace of the current BananaProtocole
285 */
286 public function backtrace()
287 {
288 if (Banana::$protocole) {
289 return Banana::$protocole->backtrace();
290 }
291 return null;
292 }
293
c4f176d8 294 /**************************************************************************/
295 /* actions */
296 /**************************************************************************/
297 protected function action_saveSubs($groups)
298 {
299 Banana::$profile['subscribe'] = $groups;
300 return true;
301 }
302
303 protected function action_subscribe()
304 {
305 Banana::$page->setPage('subscribe');
306 if (isset($_POST['validsubs'])) {
307 $this->action_saveSubs(array_keys($_POST['subscribe']));
308 Banana::$page->redirect();
309 }
310 $groups = Banana::$protocole->getBoxList(Banana::BOXES_ALL);
311 Banana::$page->assign('groups', $groups);
312 return true;
313 }
314
315 protected function action_listBoxes()
316 {
317 Banana::$page->setPage('forums');
318 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
319 Banana::$page->assign('groups', $groups);
320 if (empty(Banana::$profile['subscribe']) || Banana::$profile['lastnews']) {
321 $newgroups = Banana::$protocole->getBoxList(Banana::BOXES_NEW, Banana::$profile['lastnews'], true);
322 Banana::$page->assign('newgroups', $newgroups);
323 }
324 return true;
325 }
326
327 protected function action_showThread($group, $first)
328 {
329 Banana::$page->setPage('thread');
330 if (!$this->loadSpool($group)) {
331 return _b_('Impossible charger la liste des messages de ') . $group;
332 }
333 if (Banana::$spool_boxlist) {
334 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
335 Banana::$page->assign('groups', $groups);
336 }
337 Banana::$page->assign('msgbypage', Banana::$spool_tmax);
338 return true;
339 }
340
341 protected function action_showMessage($group, $artid, $partid = 'text')
342 {
343 Banana::$page->setPage('message');
344 $istext = $partid == 'text' || $partid == 'source'
345 || preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
346 if ($istext) {
347 $this->loadSpool($group);
348 }
349 $msg =& $this->loadMessage($group, $artid);
350 if (is_null($msg)) {
351 $this->loadSpool($group);
352 $this->removeMessage($group, $artid);
353 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
354 }
355 if ($partid == 'xface') {
356 $msg->getXFace();
357 exit;
358 } elseif (!$istext) {
359 $part = $msg->getPartById($partid);
360 if (!is_null($part)) {
361 $part->send(true);
362 }
363 $part = $msg->getFile($partid);
364 if (!is_null($part)) {
365 $part->send();
366 }
367 exit;
368 } elseif ($partid == 'text') {
369 $partid = null;
370 Banana::$page->assign('body', $msg->getFormattedBody($partid));
371 } elseif ($partid == 'source') {
372 $text = Banana::$protocole->getMessageSource($artid);
373 if (!is_utf8($text)) {
374 $text = utf8_encode($text);
375 }
376 Banana::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
377 } else {
378 Banana::$page->assign('body', $msg->getFormattedBody($partid));
379 }
380
381 if (Banana::$profile['autoup']) {
382 Banana::$spool->markAsRead($artid);
383 }
384 if (Banana::$spool_boxlist) {
385 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
386 Banana::$page->assign('groups', $groups);
387 }
388 Banana::$page->assign_by_ref('message', $msg);
389 Banana::$page->assign('extimages', Banana::$msgshow_hasextimages);
390 Banana::$page->assign('headers', Banana::$msgshow_headers);
391 Banana::$page->assign('type', $partid);
392 return true;
393 }
394
395 protected function action_newMessage($group, $artid)
396 {
397 Banana::$page->setPage('new');
398 if (!Banana::$protocole->canSend()) {
399 return _b_('Vous n\'avez pas le droit de poster');
400 }
401 $hdrs = Banana::$protocole->requestedHeaders();
402 $headers = array();
403 foreach ($hdrs as $header) {
404 $headers[$header] = array('name' => BananaMessage::translateHeaderName($header));
405 if (isset(Banana::$profile['headers'][$header])) {
406 $headers[$header]['fixed'] = Banana::$profile['headers'][$header];
407 }
408 }
409 if (isset($_POST['sendmessage'])) {
410 $hdr_values = array();
411 foreach ($hdrs as $header) {
412 $hdr_values[$header] = isset($headers[$header]['fixed']) ? $headers[$header]['fixed'] : @$_POST[$header];
413 }
414 if ($artid) {
415 $old =& $this->loadMessage($group, $artid);
416 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
417 }
418 $msg = null;
419 if (empty($hdr_values['Subject'])) {
420 Banana::$page->trig(_b_('Le message doit avoir un sujet'));
421 } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment'])) {
422 $uploaded = $_FILES['attachment'];
423 if (!is_uploaded_file($uploaded['tmp_name'])) {
424 Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
425 } else {
426 $msg = BananaMessage::newMessage($hdr_values, $_POST['body'], $uploaded);
427 }
428 } else {
429 $msg = BananaMessage::newMessage($hdr_values, $_POST['body']);
430 }
431 if (!is_null($msg)) {
432 if (Banana::$protocole->send($msg)) {
433 Banana::$page->redirect(array('group' => $group, 'artid' => $artid));
434 }
435 Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
436 . Banana::$protocole->lastError());
437 }
438 } else {
439 if (!is_null($artid)) {
440 $msg =& $this->loadMessage($group, $artid);
441 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
442 $subject = $msg->getHeaderValue('subject');
443 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
444 $target = $msg->getHeaderValue($hdrs['reply']);
445 if (empty($target)) {
446 $target = $group;
447 }
448 $headers[$hdrs['dest']]['user'] =& $target;
449 } else {
450 $body = '';
451 $headers[$hdrs['dest']]['user'] = $group;
452 }
453 if (Banana::$profile['signature']) {
454 $body .= "\n\n-- \n" . Banana::$profile['signature'];
455 }
456 Banana::$page->assign('body', $body);
457 }
458
459 Banana::$page->assign('maxfilesize', Banana::$msgedit_maxfilesize);
460 Banana::$page->assign('can_attach', Banana::$msgedit_canattach);
461 Banana::$page->assign('headers', $headers);
462 return true;
463 }
464
465 protected function action_cancelMessage($group, $artid)
466 {
467 Banana::$page->setPage('cancel');
468 $msg =& $this->loadMessage($group, $artid);
469 if (!$msg->canCancel()) {
470 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
471 }
472 if (isset($_POST['cancel'])) {
473 $this->loadSpool($group);
474 $ndx = Banana::$spool->getNdX($id) - 1;
475 if (!Banana::$protocole->cancel($msg)) {
476 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
477 . Banana::$protocole->lastError();
478 }
479 if ($ndx < 50) {
480 $ndx = 0;
481 }
482 $this->removeMessage($group, $artid);
483 Banana::$page->redirect(Array('group' => $group, 'first' => $ndx));
484 }
485
486 Banana::$page->assign_by_ref('message', $msg);
487 Banana::$page->assign('body', $msg->getFormattedBody());
488 Banana::$page->assign('headers', Banana::$msgshow_headers);
489 return true;
490 }
491
492 /**************************************************************************/
493 /* Spoolgen functions */
494 /**************************************************************************/
495
496 private function checkErrors()
497 {
498 if (Banana::$protocole->lastErrno()) {
499 echo "\nL'erreur suivante s'est produite : "
500 . Banana::$protocole->lastErrno() . " "
501 . Banana::$protocole->lastError() . "\n";
502 return false;
503 }
504 return true;
505 }
506
507 static public function createAllSpool(array $protos)
508 {
509 foreach ($protos as $proto) {
510 $banana = new Banana(array(), $proto);
511
512 if (!$banana->checkErrors()) {
513 continue;
514 }
515 $groups = Banana::$protocole->getBoxList();
516 if (!$banana->checkErrors()) {
517 continue;
518 }
519
520 print "** $proto **\n";
521 foreach (array_keys($groups) as $g) {
522 print "Generating spool for $g : ";
523 Banana::$group = $g;
524 $spool = $banana->loadSpool($g);
525 if (!$banana->checkErrors()) {
526 break;
527 }
528 print "done.\n";
529 unset($spool);
530 }
531 print "\n";
532 }
533 }
534
535 /**************************************************************************/
536 /* Private functions */
537 /**************************************************************************/
538
539 protected function loadSpool($group)
540 {
541 Banana::load('spool');
542 if (!Banana::$spool || Banana::$spool->group != $group) {
543 $clean = false;
544 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
545 Banana::$spool = unserialize($_SESSION['banana_spool']);
546 $clean = @(Banana::$profile['lastnews'] != $_SESSION['banana_lastnews']);
547 } else {
548 unset($_SESSION['banana_message']);
549 unset($_SESSION['banana_artid']);
550 unset($_SESSION['banana_showhdr']);
551 }
552 BananaSpool::getSpool($group, Banana::$profile['lastnews'], Banana::$profile['autoup'] || $clean);
553 $_SESSION['banana_group'] = $group;
554 if (!Banana::$profile['display']) {
555 $_SESSION['banana_spool'] = serialize(Banana::$spool);
556 $_SESSION['banana_lastnews'] = Banana::$profile['lastnews'];
557 }
558 Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
559 }
560 return true;
561 }
562
563 protected function &loadMessage($group, $artid)
564 {
565 Banana::load('message');
566 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
567 && isset($_SESSION['banana_message'])) {
568 $message = unserialize($_SESSION['banana_message']);
569 Banana::$msgshow_headers = $_SESSION['banana_showhdr'];
570 } else {
571 $message = Banana::$protocole->getMessage($artid);
572 $_SESSION['banana_group'] = $group;
573 $_SESSION['banana_artid'] = $artid;
574 $_SESSION['banana_message'] = serialize($message);
575 $_SESSION['banana_showhdr'] = Banana::$msgshow_headers;
576 }
577 Banana::$message =& $message;
578 return $message;
579 }
580
581 protected function removeMessage($group, $artid)
582 {
583 Banana::$spool->delId($artid);
584 if ($group == $_SESSION['banana_group']) {
585 if (!Banana::$profile['display']) {
586 $_SESSION['banana_spool'] = serialize(Banana::$spool);
587 }
588 if ($artid == $_SESSION['banana_artid']) {
589 unset($_SESSION['banana_message']);
590 unset($_SESSION['banana_showhdr']);
591 unset($_SESSION['banana_artid']);
592 }
593 }
594 $this->loadSpool($group);
595 return true;
596 }
597
598 static private function load($file)
599 {
600 $file = strtolower($file) . '.inc.php';
601 if (!@include_once dirname(__FILE__) . "/$file") {
602 require_once $file;
603 }
604 }
605}
606
607// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
608?>