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 $mimeparts = array();
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
;
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');
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 = 80;
53 static public $msgshow_externalimages = false
;
54 static public $msgshow_hasextimages = false
;
55 static public $msgshow_withthread = true
;
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
63 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
65 * $matches[2] = "http://www.polytechnique.org"
68 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
70 ### Message edition ###
71 static public $msgedit_canattach = true
;
72 static public $msgedit_maxfilesize = 100000;
73 /** Global headers to use for messages
75 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
76 /** Mime type order for quoting
78 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
80 ### Feed configuration ###
81 static public $feed_active = true
;
82 static public $feed_format = 'rss2';
83 static public $feed_updateOnDemand = false
; // Update the feed each time sbd check it
84 static public $feed_copyright = null
; // Global copyright informations
85 static public $feed_generator = 'Banana @VERSION@'; // Feed generator
86 static public $feed_email = null
; // Admin e-mail
87 static public $feed_namePrefix = 'Banana :: ';
88 static public $feed_size = 15; // Number of messages in the feed
91 /** News serveur to use
93 static public $nntp_host = 'news://localhost:119/';
95 static public $mbox_path = '/var/mail';
96 static public $mbox_helper = './mbox-helper';
99 static public $debug_nntp = false
;
100 static public $debug_mbox = false
;
101 static public $debug_smarty = false
;
109 const ACTION_BOX_NEEDED
= 1; // mask
110 const ACTION_BOX_LIST
= 2;
111 const ACTION_BOX_SUBS
= 4;
112 const ACTION_BOX_FEED
= 8;
113 const ACTION_MSG_LIST
= 3;
114 const ACTION_MSG_READ
= 5;
115 const ACTION_MSG_NEW
= 9;
116 const ACTION_MSG_CANCEL
= 17;
117 const ACTION_MSG_IMAGES
= 33;
126 const SPOOL_UNREAD
= 1;
133 static public $protocole = null
;
134 static public $spool = null
;
135 static public $message = null
;
136 static public $page = null
;
138 static public $group = null
;
139 static public $artid = null
;
140 static public $action = null
;
141 static public $part = null
;
142 static public $first = null
;
144 /** Class parameters storage
150 # Banana Implementation
153 /** Build the instance of Banana
154 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
155 * @param protocole Protocole to use
157 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
159 if (is_null($params)) {
160 $this->params
= $_GET;
162 $this->params
= $params;
166 // connect to protocole handler
167 $classname = 'Banana' . $protocole;
168 if (!class_exists($classname)) {
169 Banana
::load($protocole);
171 Banana
::$protocole = new $classname(Banana
::$group);
174 if ($pageclass == 'BananaPage') {
175 Banana
::load('page');
177 Banana
::$page = new $pageclass;
178 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
179 'multipart/mixed' => _b_('Composition'),
180 'text/html' => _b_('Texte formaté'),
181 'text/plain' => _b_('Texte brut'),
182 'text/enriched' => _b_('Texte enrichi'),
183 'text' => _b_('Texte'),
184 'message/rfc822' => _b_('Mail'),
185 'message' => _b_('Message'),
186 'source' => _b_('Source'));
187 Banana
::$mimeparts = array_merge($types, Banana
::$mimeparts);
190 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
192 protected function loadParams()
194 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
195 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
196 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
197 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
199 $action = @$this->params
['action'];
200 if ($action == 'rss' ||
$action == 'rss2' ||
$action == 'atom') {
201 if ($action == 'rss') {
204 Banana
::$feed_format = $action;
205 Banana
::$action = Banana
::ACTION_BOX_FEED
;
209 // Look for the action to execute
210 if (is_null(Banana
::$group)) {
211 if ($action == 'subscribe') {
212 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
214 Banana
::$action = Banana
::ACTION_BOX_LIST
;
219 if (is_null(Banana
::$artid)) {
220 if ($action == 'new') {
221 Banana
::$action = Banana
::ACTION_MSG_NEW
;
223 Banana
::$action = Banana
::ACTION_MSG_LIST
;
229 Banana
::$action = Banana
::ACTION_MSG_NEW
;
232 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
235 Banana
::$action = Banana
::ACTION_MSG_IMAGES
;
238 Banana
::$action = Banana
::ACTION_MSG_READ
;
243 * This function need user profile to be initialised
245 public function run()
248 setlocale(LC_ALL
, Banana
::$profile['locale']);
250 // Check if the state is valid
251 if (Banana
::$protocole->lastErrNo()) {
252 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
253 . Banana
::$protocole->lastError());
255 if (!Banana
::$protocole->isValid()) {
256 return Banana
::$page->kill(_b_('Connexion non-valide'));
258 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
259 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', Banana
::$group)) {
260 Banana
::$page->setPage('group');
261 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
265 // Dispatch to the action handlers
266 switch (Banana
::$action) {
267 case Banana
::ACTION_BOX_SUBS
:
268 $error = $this->action_subscribe();
270 case Banana
::ACTION_BOX_LIST
:
271 $error = $this->action_listBoxes();
273 case Banana
::ACTION_BOX_FEED
:
274 $this->action_feed(); // generate its own xml
276 case Banana
::ACTION_MSG_LIST
:
277 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
279 case Banana
::ACTION_MSG_IMAGES
:
280 Banana
::$msgshow_externalimages = true
;
281 case Banana
::ACTION_MSG_READ
:
282 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
284 case Banana
::ACTION_MSG_NEW
:
285 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
287 case Banana
::ACTION_MSG_CANCEL
:
288 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
291 $error = _b_("L'action demandée n'est pas supportée par Banana");
295 if (is_string($error)) {
296 return Banana
::$page->kill($error);
298 return Banana
::$page->run();
301 /** Build and post a new message
302 * @return postid (or -1 if the message has not been found)
304 public function post($dest, $reply, $subject, $body)
306 $hdrs = Banana
::$protocole->requestedHeaders();
307 $headers = Banana
::$profile['headers'];
308 $headers[$hdrs['dest']] = $dest;
310 $headers[$hdrs['reply']] = $reply;
312 $headers['Subject'] = $subject;
313 $msg = BananaMessage
::newMessage($headers, $body);
314 if (Banana
::$protocole->send($msg)) {
315 Banana
::$group = ($reply ?
$reply : $dest);
316 $this->loadSpool(Banana
::$group);
317 return Banana
::$spool->getPostId($subject);
322 /** Return the CSS code to include in the headers
324 public function css()
326 return Banana
::$page->css
;
329 /** Return the Link to the feed of the page
331 public function feed()
333 if (!Banana
::$feed_active) {
336 return Banana
::$page->makeURL(array('group' => Banana
::$group, 'action' => Banana
::$feed_format));
339 /** Return the execution backtrace of the current BananaProtocole
341 public function backtrace()
343 if (Banana
::$protocole) {
344 return Banana
::$protocole->backtrace();
349 /**************************************************************************/
351 /**************************************************************************/
352 protected function action_saveSubs($groups)
354 Banana
::$profile['subscribe'] = $groups;
358 protected function action_subscribe()
360 Banana
::$page->setPage('subscribe');
361 if (isset($_POST['validsubs'])) {
362 $this->action_saveSubs(array_keys($_POST['subscribe']));
363 Banana
::$page->redirect();
365 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
366 Banana
::$page->assign('groups', $groups);
370 protected function action_listBoxes()
372 Banana
::$page->setPage('forums');
373 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
374 Banana
::$page->assign('groups', $groups);
375 if (empty(Banana
::$profile['subscribe']) || Banana
::$profile['lastnews']) {
376 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
377 Banana
::$page->assign('newgroups', $newgroups);
382 protected function action_feed()
384 Banana
::load('feed');
385 if (Banana
::$group) {
386 if (Banana
::$feed_updateOnDemand) {
387 $this->loadSpool(Banana
::$group);
389 $feed =& BananaFeed
::getFeed();
392 if (Banana
::$profile['subscribe']) {
394 foreach (Banana
::$profile['subscribe'] as $group) {
395 Banana
::$group = $group;
396 if (Banana
::$feed_updateOnDemand) {
397 $this->loadSpool($group);
399 $feed =& BananaFeed
::getFeed();
400 $subfeed =& BananaFeed
::merge($subfeed, $feed, _b_('Abonnements'), _b_('Mes abonnements Banana'));
404 Banana
::$page->feed();
407 protected function action_showThread($group, $first)
409 Banana
::$page->setPage('thread');
410 if (!$this->loadSpool($group)) {
411 return _b_('Impossible charger la liste des messages de ') . $group;
413 if (Banana
::$spool_boxlist) {
414 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
415 Banana
::$page->assign('groups', $groups);
417 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
421 protected function action_showMessage($group, $artid, $partid = 'text')
423 Banana
::$page->setPage('message');
424 $istext = $partid == 'text' ||
$partid == 'source'
425 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
427 $this->loadSpool($group);
429 $msg =& $this->loadMessage($group, $artid);
431 $this->loadSpool($group);
432 $this->removeMessage($group, $artid);
433 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
435 if ($partid == 'xface') {
438 } elseif (!$istext) {
439 $part = $msg->getPartById($partid);
440 if (!is_null($part)) {
443 $part = $msg->getFile($partid);
444 if (!is_null($part)) {
448 } elseif ($partid == 'text') {
450 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
451 } elseif ($partid == 'source') {
452 $text = Banana
::$protocole->getMessageSource($artid);
453 if (!is_utf8($text)) {
454 $text = utf8_encode($text);
456 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
458 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
461 if (Banana
::$profile['autoup']) {
462 Banana
::$spool->markAsRead($artid);
464 if (Banana
::$spool_boxlist) {
465 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
466 Banana
::$page->assign('groups', $groups);
468 Banana
::$page->assign_by_ref('message', $msg);
469 Banana
::$page->assign('extimages', Banana
::$msgshow_hasextimages);
470 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
471 Banana
::$page->assign('type', $partid);
475 protected function action_newMessage($group, $artid)
477 Banana
::$page->setPage('new');
478 if (!Banana
::$protocole->canSend()) {
479 return _b_('Vous n\'avez pas le droit de poster');
481 $hdrs = Banana
::$protocole->requestedHeaders();
483 foreach ($hdrs as $header) {
484 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
485 if (isset(Banana
::$profile['headers'][$header])) {
486 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
489 if (isset($_POST['sendmessage'])) {
490 $hdr_values = array();
491 foreach ($hdrs as $header) {
492 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
493 if (!is_utf8($hdr_values[$header])) {
494 $hdr_values[$header] = utf8_encode($hdr_values[$header]);
496 if ($headers != 'Subject') {
497 $hdr_values[$header] = str_replace(', ', ',', $hdr_values[$header]);
501 $old =& $this->loadMessage($group, $artid);
502 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
505 if (isset($_POST['body']) && !is_utf8($_POST['body'])) {
506 $_POST['body'] = utf8_encode($_POST['body']);
508 if (empty($hdr_values['Subject'])) {
509 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
510 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment'])) {
511 $uploaded = $_FILES['attachment'];
512 if (!is_uploaded_file($uploaded['tmp_name'])) {
513 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
515 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
518 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
520 if (!is_null($msg)) {
521 if (Banana
::$protocole->send($msg)) {
522 $this->loadSpool($group);
523 $newid = Banana
::$spool->updateUnread(Banana
::$profile['lastnews']);
524 Banana
::$page->redirect(array('group' => $group, 'artid' => $newid ?
$newid : $artid));
526 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
527 . Banana
::$protocole->lastError());
530 if (!is_null($artid)) {
531 $msg =& $this->loadMessage($group, $artid);
532 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
533 $subject = $msg->getHeaderValue('subject');
534 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
535 $target = $msg->getHeaderValue($hdrs['reply']);
536 if (empty($target)) {
539 $headers[$hdrs['dest']]['user'] =& $target;
542 $headers[$hdrs['dest']]['user'] = $group;
544 if (Banana
::$profile['signature']) {
545 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
547 Banana
::$page->assign('body', $body);
550 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
551 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
552 Banana
::$page->assign('headers', $headers);
556 protected function action_cancelMessage($group, $artid)
558 Banana
::$page->setPage('cancel');
559 $msg =& $this->loadMessage($group, $artid);
560 if (!$msg->canCancel()) {
561 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
563 if (isset($_POST['cancel'])) {
564 $this->loadSpool($group);
565 $ndx = Banana
::$spool->getNdX($id) - 1;
566 if (!Banana
::$protocole->cancel($msg)) {
567 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
568 . Banana
::$protocole->lastError();
573 $this->removeMessage($group, $artid);
574 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
577 Banana
::$page->assign_by_ref('message', $msg);
578 Banana
::$page->assign('body', $msg->getFormattedBody());
579 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
583 /**************************************************************************/
584 /* Spoolgen functions */
585 /**************************************************************************/
587 private function checkErrors()
589 if (Banana
::$protocole->lastErrno()) {
590 echo "\nL'erreur suivante s'est produite : "
591 . Banana
::$protocole->lastErrno() . " "
592 . Banana
::$protocole->lastError() . "\n";
598 static public function createAllSpool(array $protos)
600 foreach ($protos as $proto) {
601 $banana = new Banana(array(), $proto);
603 if (!$banana->checkErrors()) {
606 $groups = Banana
::$protocole->getBoxList();
607 if (!$banana->checkErrors()) {
611 print "** $proto **\n";
612 foreach (array_keys($groups) as $g) {
613 print "Generating spool for $g: ";
615 $spool = $banana->loadSpool($g);
616 if (!$banana->checkErrors()) {
621 Banana
::$spool = null
;
627 static public function refreshAllFeeds(array $protos)
629 Banana
::load('feed');
630 Banana
::$feed_updateOnDemand = true
; // In order to force update
631 foreach ($protos as $proto) {
632 $banana = new Banana(array(), $proto);
634 if (!$banana->checkErrors()) {
637 $groups = Banana
::$protocole->getBoxList();
638 if (!$banana->checkErrors()) {
642 print "** $proto **\n";
643 foreach (array_keys($groups) as $g) {
644 print "Generating feed cache for $g: ";
646 $spool = $banana->loadSpool($g);
647 if (!$banana->checkErrors()) {
650 $feed =& BananaFeed
::getFeed();
654 Banana
::$spool = null
;
660 /**************************************************************************/
661 /* Private functions */
662 /**************************************************************************/
664 protected function loadSpool($group)
666 Banana
::load('spool');
667 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
669 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
670 Banana
::$spool = unserialize($_SESSION['banana_spool']);
671 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
673 unset($_SESSION['banana_message']);
674 unset($_SESSION['banana_artid']);
675 unset($_SESSION['banana_showhdr']);
677 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
678 $_SESSION['banana_group'] = $group;
679 if (!Banana
::$profile['display']) {
680 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
681 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
683 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
688 protected function &loadMessage($group, $artid)
690 Banana
::load('message');
691 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
692 && isset($_SESSION['banana_message'])) {
693 $message = unserialize($_SESSION['banana_message']);
694 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
696 $message = Banana
::$protocole->getMessage($artid);
697 $_SESSION['banana_group'] = $group;
698 $_SESSION['banana_artid'] = $artid;
699 $_SESSION['banana_message'] = serialize($message);
700 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
702 Banana
::$message =& $message;
706 protected function removeMessage($group, $artid)
708 Banana
::$spool->delId($artid);
709 if ($group == $_SESSION['banana_group']) {
710 if (!Banana
::$profile['display']) {
711 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
713 if ($artid == $_SESSION['banana_artid']) {
714 unset($_SESSION['banana_message']);
715 unset($_SESSION['banana_showhdr']);
716 unset($_SESSION['banana_artid']);
719 $this->loadSpool($group);
723 static private function load($file)
725 $file = strtolower($file) . '.inc.php';
726 if (!@include_once
dirname(__FILE__
) . "/$file") {
732 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: