2 /********************************************************************************
3 * banana/banana.inc.php : banana main file
4 * --------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/text.func.inc.php';
16 # Configuration variables
20 static public $profile = Array( 'signature' => '',
21 'headers' => array('From' => 'Anonymous <anonymouse@example.com>'),
24 'locale' => 'fr_FR.UTF-8',
25 'subscribe' => array(),
27 static public $boxpattern;
28 static public $withtabs = true
;
29 static public $baseurl = null
;
30 static public $mimeparts = array();
33 static public $spool_root = '/var/spool/banana';
34 static public $spool_max = 3000;
35 static public $spool_tbefore = 5;
36 static public $spool_tafter = 5;
37 static public $spool_tmax = 50;
38 static public $spool_boxlist = true
;
40 ### Message processing ###
41 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
42 'content-type', 'content-id', 'date', 'followup-to',
43 'from', 'message-id', 'newsgroups', 'organization',
44 'references', 'subject', 'x-face', 'in-reply-to',
45 'to', 'cc', 'reply-to');
47 ### Message display ###
48 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
49 'organization', 'date', 'references', 'in-reply-to');
50 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed',
51 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
52 static public $msgshow_xface = true
;
53 static public $msgshow_wrap = 80;
54 static public $msgshow_externalimages = false
;
55 static public $msgshow_hasextimages = false
;
56 static public $msgshow_withthread = true
;
57 static public $msgshow_javascript = true
;
60 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
61 * If it matches, return 3 main parts :
62 * \\1 and \\3 are delimiters
65 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
67 * $matches[2] = "http://www.polytechnique.org"
70 static public $msgshow_url = '(["\[\<])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]\>])?';
72 ### Message edition ###
73 static public $msgedit_canattach = true
;
74 static public $msgedit_maxfilesize = 100000;
75 /** Global headers to use for messages
77 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
78 /** Mime type order for quoting
80 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
82 ### Feed configuration ###
83 static public $feed_active = true
;
84 static public $feed_format = 'rss2';
85 static public $feed_updateOnDemand = false
; // Update the feed each time sbd check it
86 static public $feed_copyright = null
; // Global copyright informations
87 static public $feed_generator = 'Banana @VERSION@'; // Feed generator
88 static public $feed_email = null
; // Admin e-mail
89 static public $feed_namePrefix = 'Banana :: ';
90 static public $feed_size = 15; // Number of messages in the feed
93 /** News serveur to use
95 static public $nntp_host = 'news://localhost:119/';
97 static public $mbox_path = '/var/mail';
98 static public $mbox_helper = './mbox-helper';
101 static public $debug_nntp = false
;
102 static public $debug_mbox = false
;
103 static public $debug_smarty = false
;
111 const ACTION_BOX_NEEDED
= 1; // mask
112 const ACTION_BOX_LIST
= 2;
113 const ACTION_BOX_SUBS
= 4;
114 const ACTION_BOX_FEED
= 8;
115 const ACTION_MSG_LIST
= 3;
116 const ACTION_MSG_READ
= 5;
117 const ACTION_MSG_NEW
= 9;
118 const ACTION_MSG_CANCEL
= 17;
119 const ACTION_MSG_IMAGES
= 33;
128 const SPOOL_UNREAD
= 1;
135 static public $protocole = null
;
136 static public $spool = null
;
137 static public $message = null
;
138 static public $page = null
;
140 static public $group = null
;
141 static public $artid = null
;
142 static public $action = null
;
143 static public $part = null
;
144 static public $first = null
;
146 /** Class parameters storage
152 # Banana Implementation
155 /** Build the instance of Banana
156 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
157 * @param protocole Protocole to use
159 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
161 if (is_null($params)) {
162 $this->params
= $_GET;
164 $this->params
= $params;
168 // connect to protocole handler
169 $classname = 'Banana' . $protocole;
170 if (!class_exists($classname)) {
171 Banana
::load($protocole);
173 Banana
::$protocole = new $classname(Banana
::$group);
176 if ($pageclass == 'BananaPage') {
177 Banana
::load('page');
179 Banana
::$page = new $pageclass;
180 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
181 'multipart/mixed' => _b_('Composition'),
182 'text/html' => _b_('Texte formaté'),
183 'text/plain' => _b_('Texte brut'),
184 'text/enriched' => _b_('Texte enrichi'),
185 'text' => _b_('Texte'),
186 'message/rfc822' => _b_('Mail'),
187 'message' => _b_('Message'),
188 'source' => _b_('Source'));
189 Banana
::$mimeparts = array_merge($types, Banana
::$mimeparts);
192 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
194 protected function loadParams()
196 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
197 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
198 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
199 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
201 $action = @$this->params
['action'];
202 if ($action == 'rss' ||
$action == 'rss2' ||
$action == 'atom') {
203 if ($action == 'rss') {
206 Banana
::$feed_format = $action;
207 Banana
::$action = Banana
::ACTION_BOX_FEED
;
211 // Look for the action to execute
212 if (is_null(Banana
::$group)) {
213 if ($action == 'subscribe') {
214 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
216 Banana
::$action = Banana
::ACTION_BOX_LIST
;
221 if (is_null(Banana
::$artid)) {
222 if ($action == 'new') {
223 Banana
::$action = Banana
::ACTION_MSG_NEW
;
225 Banana
::$action = Banana
::ACTION_MSG_LIST
;
231 Banana
::$action = Banana
::ACTION_MSG_NEW
;
234 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
237 Banana
::$action = Banana
::ACTION_MSG_IMAGES
;
240 Banana
::$action = Banana
::ACTION_MSG_READ
;
245 * This function need user profile to be initialised
247 public function run()
250 setlocale(LC_ALL
, Banana
::$profile['locale']);
252 // Check if the state is valid
253 if (Banana
::$protocole->lastErrNo()) {
254 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
255 . Banana
::$protocole->lastError());
257 if (!Banana
::$protocole->isValid()) {
258 return Banana
::$page->kill(_b_('Connexion non-valide'));
260 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
261 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', Banana
::$group)) {
262 Banana
::$page->setPage('group');
263 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
267 // Dispatch to the action handlers
268 switch (Banana
::$action) {
269 case Banana
::ACTION_BOX_SUBS
:
270 $error = $this->action_subscribe();
272 case Banana
::ACTION_BOX_LIST
:
273 $error = $this->action_listBoxes();
275 case Banana
::ACTION_BOX_FEED
:
276 $this->action_feed(); // generate its own xml
278 case Banana
::ACTION_MSG_LIST
:
279 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
281 case Banana
::ACTION_MSG_IMAGES
:
282 Banana
::$msgshow_externalimages = true
;
283 case Banana
::ACTION_MSG_READ
:
284 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
286 case Banana
::ACTION_MSG_NEW
:
287 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
289 case Banana
::ACTION_MSG_CANCEL
:
290 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
293 $error = _b_("L'action demandée n'est pas supportée par Banana");
297 if (is_string($error)) {
298 return Banana
::$page->kill($error);
300 return Banana
::$page->run();
303 /** Build and post a new message
304 * @return postid (or -1 if the message has not been found)
306 public function post($dest, $reply, $subject, $body)
308 $hdrs = Banana
::$protocole->requestedHeaders();
309 $headers = Banana
::$profile['headers'];
310 $headers[$hdrs['dest']] = $dest;
312 $headers[$hdrs['reply']] = $reply;
314 $headers['Subject'] = $subject;
315 $msg = BananaMessage
::newMessage($headers, $body);
316 if (Banana
::$protocole->send($msg)) {
317 Banana
::$group = ($reply ?
$reply : $dest);
318 $this->loadSpool(Banana
::$group);
319 return Banana
::$spool->getPostId($subject);
324 /** Return the CSS code to include in the headers
326 public function css()
328 return Banana
::$page->css
;
331 /** Return the Link to the feed of the page
333 public function feed()
335 if (!Banana
::$feed_active) {
338 return Banana
::$page->makeURL(array('group' => Banana
::$group, 'action' => Banana
::$feed_format));
341 /** Return the execution backtrace of the current BananaProtocole
343 public function backtrace()
345 if (Banana
::$protocole) {
346 return Banana
::$protocole->backtrace();
351 /**************************************************************************/
353 /**************************************************************************/
354 protected function action_saveSubs($groups)
356 Banana
::$profile['subscribe'] = $groups;
360 protected function action_subscribe()
362 Banana
::$page->setPage('subscribe');
363 if (isset($_POST['validsubs'])) {
364 $this->action_saveSubs(array_keys($_POST['subscribe']));
365 Banana
::$page->redirect();
367 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
368 Banana
::$page->assign('groups', $groups);
372 protected function action_listBoxes()
374 Banana
::$page->setPage('forums');
375 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
376 Banana
::$page->assign('groups', $groups);
377 if (empty(Banana
::$profile['subscribe']) || Banana
::$profile['lastnews']) {
378 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
379 Banana
::$page->assign('newgroups', $newgroups);
384 protected function action_feed()
386 Banana
::load('feed');
387 if (Banana
::$group) {
388 if (Banana
::$feed_updateOnDemand) {
389 $this->loadSpool(Banana
::$group);
391 $feed =& BananaFeed
::getFeed();
394 if (Banana
::$profile['subscribe']) {
396 foreach (Banana
::$profile['subscribe'] as $group) {
397 Banana
::$group = $group;
398 if (Banana
::$feed_updateOnDemand) {
399 $this->loadSpool($group);
401 $feed =& BananaFeed
::getFeed();
402 $subfeed =& BananaFeed
::merge($subfeed, $feed, _b_('Abonnements'), _b_('Mes abonnements Banana'));
406 Banana
::$page->feed();
409 protected function action_showThread($group, $first)
411 Banana
::$page->setPage('thread');
412 if (!$this->loadSpool($group)) {
413 return _b_('Impossible charger la liste des messages de ') . $group;
415 if (Banana
::$spool_boxlist) {
416 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
417 Banana
::$page->assign('groups', $groups);
419 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
423 protected function action_showMessage($group, $artid, $partid = 'text')
425 Banana
::$page->setPage('message');
426 $istext = $partid == 'text' ||
$partid == 'source'
427 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
429 $this->loadSpool($group);
431 $msg =& $this->loadMessage($group, $artid);
433 $this->loadSpool($group);
434 $this->removeMessage($group, $artid);
435 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
437 if ($partid == 'xface') {
440 } elseif (!$istext) {
441 $part = $msg->getPartById($partid);
442 if (!is_null($part)) {
445 $part = $msg->getFile($partid);
446 if (!is_null($part)) {
450 } elseif ($partid == 'text') {
452 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
453 } elseif ($partid == 'source') {
454 $text = Banana
::$protocole->getMessageSource($artid);
455 if (!is_utf8($text)) {
456 $text = utf8_encode($text);
458 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
460 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
463 if (Banana
::$profile['autoup']) {
464 Banana
::$spool->markAsRead($artid);
466 if (Banana
::$spool_boxlist) {
467 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
468 Banana
::$page->assign('groups', $groups);
470 Banana
::$page->assign_by_ref('message', $msg);
471 Banana
::$page->assign('extimages', Banana
::$msgshow_hasextimages);
472 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
473 Banana
::$page->assign('type', $partid);
477 protected function action_newMessage($group, $artid)
479 Banana
::$page->setPage('new');
480 if (!Banana
::$protocole->canSend()) {
481 return _b_('Vous n\'avez pas le droit de poster');
483 $hdrs = Banana
::$protocole->requestedHeaders();
485 foreach ($hdrs as $header) {
486 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
487 if (isset(Banana
::$profile['headers'][$header])) {
488 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
491 if (isset($_POST['sendmessage'])) {
492 $hdr_values = array();
493 foreach ($hdrs as $header) {
494 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
495 if (!is_utf8($hdr_values[$header])) {
496 $hdr_values[$header] = utf8_encode($hdr_values[$header]);
498 if ($headers != 'Subject') {
499 $hdr_values[$header] = str_replace(', ', ',', $hdr_values[$header]);
502 if (!is_null($artid)) {
503 $old =& $this->loadMessage($group, $artid);
504 $hdr_values['References'] = $old->getHeaderValue('references') . ' ' . $old->getHeaderValue('message-id');
507 if (isset($_POST['body']) && !is_utf8($_POST['body'])) {
508 $_POST['body'] = utf8_encode($_POST['body']);
510 if (empty($hdr_values['Subject'])) {
511 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
512 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment']) && $_FILES['attachment']['name']) {
513 $uploaded =& $_FILES['attachment'];
514 if (!is_uploaded_file($uploaded['tmp_name'])) {
515 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
517 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
520 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
522 if (!is_null($msg)) {
523 if (Banana
::$protocole->send($msg)) {
524 $this->loadSpool($group);
525 $newid = Banana
::$spool->updateUnread(Banana
::$profile['lastnews']);
526 Banana
::$page->redirect(array('group' => $group, 'artid' => $newid ?
$newid : $artid));
528 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
529 . Banana
::$protocole->lastError());
533 if (!is_null($artid)) {
534 $msg =& $this->loadMessage($group, $artid);
535 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
536 $subject = $msg->getHeaderValue('subject');
537 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
538 $target = $msg->getHeaderValue($hdrs['reply']);
539 if (empty($target)) {
542 $headers[$hdrs['dest']]['user'] =& $target;
545 $headers[$hdrs['dest']]['user'] = $group;
547 if (Banana
::$profile['signature']) {
548 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
550 Banana
::$page->assign('body', $body);
553 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
554 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
555 Banana
::$page->assign('headers', $headers);
559 protected function action_cancelMessage($group, $artid)
561 Banana
::$page->setPage('cancel');
562 $msg =& $this->loadMessage($group, $artid);
563 if (!$msg->canCancel()) {
564 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
566 if (isset($_POST['cancel'])) {
567 $this->loadSpool($group);
568 $ndx = Banana
::$spool->getNdX($id) - 1;
569 if (!Banana
::$protocole->cancel($msg)) {
570 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
571 . Banana
::$protocole->lastError();
576 $this->removeMessage($group, $artid);
577 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
580 Banana
::$page->assign_by_ref('message', $msg);
581 Banana
::$page->assign('body', $msg->getFormattedBody());
582 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
586 /**************************************************************************/
587 /* Spoolgen functions */
588 /**************************************************************************/
590 private function checkErrors()
592 if (Banana
::$protocole->lastErrno()) {
593 echo "\nL'erreur suivante s'est produite : "
594 . Banana
::$protocole->lastErrno() . " "
595 . Banana
::$protocole->lastError() . "\n";
601 static public function createAllSpool(array $protos)
603 foreach ($protos as $proto) {
604 $banana = new Banana(array(), $proto);
606 if (!$banana->checkErrors()) {
609 $groups = Banana
::$protocole->getBoxList();
610 if (!$banana->checkErrors()) {
614 print "** $proto **\n";
615 foreach (array_keys($groups) as $g) {
616 print "Generating spool for $g: ";
618 $spool = $banana->loadSpool($g);
619 if (!$banana->checkErrors()) {
624 Banana
::$spool = null
;
630 static public function refreshAllFeeds(array $protos)
632 Banana
::load('feed');
633 Banana
::$feed_updateOnDemand = true
; // In order to force update
634 foreach ($protos as $proto) {
635 $banana = new Banana(array(), $proto);
637 if (!$banana->checkErrors()) {
640 $groups = Banana
::$protocole->getBoxList();
641 if (!$banana->checkErrors()) {
645 print "** $proto **\n";
646 foreach (array_keys($groups) as $g) {
647 print "Generating feed cache for $g: ";
649 $spool = $banana->loadSpool($g);
650 if (!$banana->checkErrors()) {
653 $feed =& BananaFeed
::getFeed();
657 Banana
::$spool = null
;
663 /**************************************************************************/
664 /* Private functions */
665 /**************************************************************************/
667 protected function loadSpool($group)
669 Banana
::load('spool');
670 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
672 if (php_sapi_name() != 'cli') {
673 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
674 Banana
::$spool = unserialize($_SESSION['banana_spool']);
675 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
677 unset($_SESSION['banana_message']);
678 unset($_SESSION['banana_artid']);
679 unset($_SESSION['banana_showhdr']);
682 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
683 if (php_sapi_name() != 'cli') {
684 $_SESSION['banana_group'] = $group;
685 if (!Banana
::$profile['display']) {
686 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
687 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
690 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
695 protected function &loadMessage($group, $artid)
697 Banana
::load('message');
698 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
699 && isset($_SESSION['banana_message'])) {
700 $message = unserialize($_SESSION['banana_message']);
701 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
703 $message = Banana
::$protocole->getMessage($artid);
704 $_SESSION['banana_group'] = $group;
705 $_SESSION['banana_artid'] = $artid;
706 $_SESSION['banana_message'] = serialize($message);
707 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
709 Banana
::$message =& $message;
713 protected function removeMessage($group, $artid)
715 Banana
::$spool->delId($artid);
716 if ($group == $_SESSION['banana_group']) {
717 if (!Banana
::$profile['display']) {
718 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
720 if ($artid == $_SESSION['banana_artid']) {
721 unset($_SESSION['banana_message']);
722 unset($_SESSION['banana_showhdr']);
723 unset($_SESSION['banana_artid']);
726 $this->loadSpool($group);
730 static private function load($file)
732 $file = strtolower($file) . '.inc.php';
733 if (!@include_once
dirname(__FILE__
) . "/$file") {
739 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: