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_tcontext= 10;
37 static public $spool_tmax = 10;
38 static public $spool_boxlist = true
;
41 static public $tree_read = 'dg';
42 static public $tree_unread = 'b';
44 ### Message processing ###
45 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
46 'content-type', 'content-id', 'date', 'followup-to',
47 'from', 'message-id', 'newsgroups', 'organization',
48 'references', 'subject', 'x-face', 'in-reply-to',
49 'to', 'cc', 'reply-to');
51 ### Message display ###
52 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
53 'organization', 'date');
54 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed',
55 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
56 static public $msgshow_xface = true
;
57 static public $msgshow_wrap = 80;
58 static public $msgshow_externalimages = false
;
59 static public $msgshow_hasextimages = false
;
60 static public $msgshow_withthread = true
;
61 static public $msgshow_javascript = true
;
63 static public $msgshow_pgpcheck = true
;
64 static public $msgshow_pgppath = 'gpg';
65 static public $msgshow_pgpoptions = '';
68 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
69 * If it matches, return 3 main parts :
70 * \\1 and \\3 are delimiters
73 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
75 * $matches[2] = "http://www.polytechnique.org"
78 static public $msgshow_url = '(["\[\<])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]\>])?';
80 ### Message edition ###
81 static public $msgedit_canattach = true
;
82 static public $msgedit_maxfilesize = 100000;
83 /** Global headers to use for messages
85 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
86 /** Mime type order for quoting
88 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
90 ### Feed configuration ###
91 static public $feed_active = true
;
92 static public $feed_format = 'rss2';
93 static public $feed_updateOnDemand = false
; // Update the feed each time sbd check it
94 static public $feed_copyright = null
; // Global copyright informations
95 static public $feed_generator = 'Banana @VERSION@'; // Feed generator
96 static public $feed_email = null
; // Admin e-mail
97 static public $feed_namePrefix = 'Banana :: ';
98 static public $feed_size = 15; // Number of messages in the feed
101 /** News serveur to use
103 static public $nntp_host = 'news://localhost:119/';
105 static public $mbox_path = '/var/mail';
106 static public $mbox_helper = './mbox-helper';
109 static public $debug_nntp = false
;
110 static public $debug_mbox = false
;
111 static public $debug_smarty = false
;
119 const ACTION_BOX_NEEDED
= 1; // mask
120 const ACTION_BOX_LIST
= 2;
121 const ACTION_BOX_SUBS
= 4;
122 const ACTION_BOX_FEED
= 8;
123 const ACTION_MSG_LIST
= 3;
124 const ACTION_MSG_READ
= 5;
125 const ACTION_MSG_NEW
= 9;
126 const ACTION_MSG_CANCEL
= 17;
127 const ACTION_MSG_IMAGES
= 33;
136 const SPOOL_UNREAD
= 1;
143 static public $protocole = null
;
144 static public $spool = null
;
145 static public $message = null
;
146 static public $page = null
;
148 static public $group = null
;
149 static public $artid = null
;
150 static public $action = null
;
151 static public $part = null
;
152 static public $first = null
;
154 /** Class parameters storage
160 # Banana Implementation
163 /** Build the instance of Banana
164 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
165 * @param protocole Protocole to use
167 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
169 if (is_null($params)) {
170 $this->params
= $_GET;
172 $this->params
= $params;
176 // connect to protocole handler
177 $classname = 'Banana' . $protocole;
178 if (!class_exists($classname)) {
179 Banana
::load($protocole);
181 Banana
::$protocole = new $classname(Banana
::$group);
184 if ($pageclass == 'BananaPage') {
185 Banana
::load('page');
187 Banana
::$page = new $pageclass;
188 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
189 'multipart/mixed' => _b_('Composition'),
190 'text/html' => _b_('Texte formaté'),
191 'text/plain' => _b_('Texte brut'),
192 'text/enriched' => _b_('Texte enrichi'),
193 'text' => _b_('Texte'),
194 'message/rfc822' => _b_('Mail'),
195 'message' => _b_('Message'),
196 'source' => _b_('Source'));
197 Banana
::$mimeparts = array_merge($types, Banana
::$mimeparts);
200 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
202 protected function loadParams()
204 foreach ($this->params
as &$value) {
209 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
210 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
211 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
212 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
214 $action = @$this->params
['action'];
215 if ($action == 'rss' ||
$action == 'rss2' ||
$action == 'atom') {
216 if ($action == 'rss') {
219 Banana
::$feed_format = $action;
220 Banana
::$action = Banana
::ACTION_BOX_FEED
;
224 // Look for the action to execute
225 if (is_null(Banana
::$group)) {
226 if ($action == 'subscribe') {
227 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
229 Banana
::$action = Banana
::ACTION_BOX_LIST
;
234 if (is_null(Banana
::$artid)) {
235 if ($action == 'new') {
236 Banana
::$action = Banana
::ACTION_MSG_NEW
;
238 Banana
::$action = Banana
::ACTION_MSG_LIST
;
244 Banana
::$action = Banana
::ACTION_MSG_NEW
;
247 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
250 Banana
::$action = Banana
::ACTION_MSG_IMAGES
;
253 Banana
::$action = Banana
::ACTION_MSG_READ
;
258 * This function need user profile to be initialised
260 public function run()
263 setlocale(LC_ALL
, Banana
::$profile['locale']);
265 // Check if the state is valid
266 if (Banana
::$protocole->lastErrNo()) {
267 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
268 . Banana
::$protocole->lastError());
270 if (!Banana
::$protocole->isValid()) {
271 return Banana
::$page->kill(_b_('Connexion non-valide'));
273 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
274 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', Banana
::$group)) {
275 Banana
::$page->setPage('group');
276 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
280 // Dispatch to the action handlers
281 switch (Banana
::$action) {
282 case Banana
::ACTION_BOX_SUBS
:
283 $error = $this->action_subscribe();
285 case Banana
::ACTION_BOX_LIST
:
286 $error = $this->action_listBoxes();
288 case Banana
::ACTION_BOX_FEED
:
289 $this->action_feed(); // generate its own xml
291 case Banana
::ACTION_MSG_LIST
:
292 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
294 case Banana
::ACTION_MSG_IMAGES
:
295 Banana
::$msgshow_externalimages = true
;
296 case Banana
::ACTION_MSG_READ
:
297 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
299 case Banana
::ACTION_MSG_NEW
:
300 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
302 case Banana
::ACTION_MSG_CANCEL
:
303 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
306 $error = _b_("L'action demandée n'est pas supportée par Banana");
310 if (is_string($error)) {
311 return Banana
::$page->kill($error);
313 return Banana
::$page->run();
316 /** Build and post a new message
317 * @return postid (or -1 if the message has not been found)
319 public function post($dest, $reply, $subject, $body)
321 $hdrs = Banana
::$protocole->requestedHeaders();
322 $headers = Banana
::$profile['headers'];
323 $headers[$hdrs['dest']] = $dest;
325 $headers[$hdrs['reply']] = $reply;
327 $headers['Subject'] = $subject;
328 $msg = BananaMessage
::newMessage($headers, $body);
329 if (Banana
::$protocole->send($msg)) {
330 Banana
::$group = ($reply ?
$reply : $dest);
331 $this->loadSpool(Banana
::$group);
332 return Banana
::$spool->getPostId($subject);
337 /** Return the CSS code to include in the headers
339 public function css()
341 return Banana
::$page->css
;
344 /** Return the Link to the feed of the page
346 public function feed()
348 if (!Banana
::$feed_active) {
351 return Banana
::$page->makeURL(array('group' => Banana
::$group, 'action' => Banana
::$feed_format));
354 /** Return the execution backtrace of the current BananaProtocole
356 public function backtrace()
358 if (Banana
::$protocole) {
359 return Banana
::$protocole->backtrace();
364 /**************************************************************************/
366 /**************************************************************************/
367 protected function action_saveSubs($groups)
369 Banana
::$profile['subscribe'] = $groups;
373 protected function action_subscribe()
375 Banana
::$page->setPage('subscribe');
376 if (isset($_POST['validsubs'])) {
377 $this->action_saveSubs(array_keys($_POST['subscribe']));
378 Banana
::$page->redirect();
380 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
381 Banana
::$page->assign('groups', $groups);
385 protected function action_listBoxes()
387 Banana
::$page->setPage('forums');
388 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
389 Banana
::$page->assign('groups', $groups);
390 if (empty(Banana
::$profile['subscribe']) || Banana
::$profile['lastnews']) {
391 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
392 Banana
::$page->assign('newgroups', $newgroups);
397 protected function action_feed()
399 Banana
::load('feed');
400 if (Banana
::$group) {
401 if (Banana
::$feed_updateOnDemand) {
402 $this->loadSpool(Banana
::$group);
404 $feed =& BananaFeed
::getFeed();
407 if (Banana
::$profile['subscribe']) {
409 foreach (Banana
::$profile['subscribe'] as $group) {
410 Banana
::$group = $group;
411 if (Banana
::$feed_updateOnDemand) {
412 $this->loadSpool($group);
414 $feed =& BananaFeed
::getFeed();
415 $subfeed =& BananaFeed
::merge($subfeed, $feed, _b_('Abonnements'), _b_('Mes abonnements Banana'));
419 Banana
::$page->feed();
422 protected function action_showThread($group, $first)
424 Banana
::$page->setPage('thread');
425 if (!$this->loadSpool($group)) {
426 return _b_('Impossible charger la liste des messages de ') . $group;
428 if (Banana
::$spool_boxlist) {
429 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
430 Banana
::$page->assign('groups', $groups);
432 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
436 protected function action_showMessage($group, $artid, $partid = 'text')
438 Banana
::$page->setPage('message');
439 $istext = $partid == 'text' ||
$partid == 'source'
440 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
442 $this->loadSpool($group);
444 $msg =& $this->loadMessage($group, $artid);
446 $this->loadSpool($group);
447 $this->removeMessage($group, $artid);
448 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
450 if ($partid == 'xface') {
453 } elseif (!$istext) {
454 $part = $msg->getPartById($partid);
455 if (!is_null($part)) {
458 $part = $msg->getFile($partid);
459 if (!is_null($part)) {
463 } elseif ($partid == 'text') {
465 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
466 } elseif ($partid == 'source') {
467 $text = Banana
::$protocole->getMessageSource($artid);
468 if (!is_utf8($text)) {
469 $text = utf8_encode($text);
471 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
473 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
476 if (Banana
::$profile['autoup']) {
477 Banana
::$spool->markAsRead($artid);
479 if (Banana
::$spool_boxlist) {
480 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
481 Banana
::$page->assign('groups', $groups);
483 Banana
::$page->assign_by_ref('message', $msg);
484 Banana
::$page->assign('extimages', Banana
::$msgshow_hasextimages);
485 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
486 Banana
::$page->assign('type', $partid);
490 protected function action_newMessage($group, $artid)
492 Banana
::$page->setPage('new');
493 if (!Banana
::$protocole->canSend()) {
494 return _b_('Vous n\'avez pas le droit de poster');
496 $hdrs = Banana
::$protocole->requestedHeaders();
498 foreach ($hdrs as $header) {
499 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
500 if (isset(Banana
::$profile['headers'][$header])) {
501 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
504 if (isset($_POST['sendmessage'])) {
505 $hdr_values = array();
506 foreach ($hdrs as $header) {
507 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
508 if (!is_utf8($hdr_values[$header])) {
509 $hdr_values[$header] = utf8_encode($hdr_values[$header]);
512 $values = preg_split('/[,; ]/', $hdr_values[$hdrs['dest']]);
513 $hdr_values[$hdrs['dest']] = preg_replace('/,+/', ',', implode(',', $values));
514 if (!is_null($artid)) {
515 $old =& $this->loadMessage($group, $artid);
516 $hdr_values['References'] = $old->getHeaderValue('references') . ' ' . $old->getHeaderValue('message-id');
519 if (isset($_POST['body']) && !is_utf8($_POST['body'])) {
520 $_POST['body'] = utf8_encode($_POST['body']);
522 if (empty($hdr_values['Subject'])) {
523 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
524 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment']) && $_FILES['attachment']['name']) {
525 $uploaded =& $_FILES['attachment'];
526 if (!is_uploaded_file($uploaded['tmp_name'])) {
527 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
529 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
532 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
534 if (!is_null($msg)) {
535 if (Banana
::$protocole->send($msg)) {
536 $this->loadSpool($group);
537 $newid = Banana
::$spool->updateUnread(Banana
::$profile['lastnews']);
538 Banana
::$page->redirect(array('group' => $group, 'artid' => $newid ?
$newid : $artid));
540 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
541 . Banana
::$protocole->lastError());
542 $body = $_POST['body'];
546 if (!is_null($artid)) {
547 $msg =& $this->loadMessage($group, $artid);
548 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
549 $subject = $msg->getHeaderValue('subject');
550 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
551 $target = $msg->getHeaderValue($hdrs['reply']);
552 if (empty($target)) {
555 $headers[$hdrs['dest']]['user'] =& $target;
558 $headers[$hdrs['dest']]['user'] = $group;
560 if (Banana
::$profile['signature']) {
561 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
564 Banana
::$page->assign('body', $body);
566 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
567 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
568 Banana
::$page->assign('headers', $headers);
572 protected function action_cancelMessage($group, $artid)
574 Banana
::$page->setPage('cancel');
575 $msg =& $this->loadMessage($group, $artid);
576 if (!$msg->canCancel()) {
577 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
579 if (isset($_POST['cancel'])) {
580 $this->loadSpool($group);
581 if (!Banana
::$protocole->cancel($msg)) {
582 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
583 . Banana
::$protocole->lastError();
585 $this->removeMessage($group, $artid);
586 Banana
::$page->redirect(Array('group' => $group));
589 Banana
::$page->assign_by_ref('message', $msg);
590 Banana
::$page->assign('body', $msg->getFormattedBody());
591 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
595 /**************************************************************************/
596 /* Spoolgen functions */
597 /**************************************************************************/
599 private function checkErrors()
601 if (Banana
::$protocole->lastErrno()) {
602 echo "\nL'erreur suivante s'est produite : "
603 . Banana
::$protocole->lastErrno() . " "
604 . Banana
::$protocole->lastError() . "\n";
610 static public function createAllSpool(array $protos)
612 foreach ($protos as $proto) {
613 $banana = new Banana(array(), $proto);
615 if (!$banana->checkErrors()) {
618 $groups = Banana
::$protocole->getBoxList();
619 if (!$banana->checkErrors()) {
623 print "** $proto **\n";
624 foreach (array_keys($groups) as $g) {
625 print "Generating spool for $g: ";
627 $spool = $banana->loadSpool($g);
628 if (!$banana->checkErrors()) {
633 Banana
::$spool = null
;
639 static public function refreshAllFeeds(array $protos)
641 Banana
::load('feed');
642 Banana
::$feed_updateOnDemand = true
; // In order to force update
643 foreach ($protos as $proto) {
644 $banana = new Banana(array(), $proto);
646 if (!$banana->checkErrors()) {
649 $groups = Banana
::$protocole->getBoxList();
650 if (!$banana->checkErrors()) {
654 print "** $proto **\n";
655 foreach (array_keys($groups) as $g) {
656 print "Generating feed cache for $g: ";
658 $spool = $banana->loadSpool($g);
659 if (!$banana->checkErrors()) {
662 $feed =& BananaFeed
::getFeed();
666 Banana
::$spool = null
;
672 /**************************************************************************/
673 /* Private functions */
674 /**************************************************************************/
676 protected function loadSpool($group)
678 Banana
::load('spool');
679 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
680 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup']);
681 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
686 protected function &loadMessage($group, $artid)
688 Banana
::load('message');
689 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
690 && isset($_SESSION['banana_message'])) {
691 $message = unserialize($_SESSION['banana_message']);
692 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
694 $message = Banana
::$protocole->getMessage($artid);
695 $_SESSION['banana_group'] = $group;
696 $_SESSION['banana_artid'] = $artid;
697 $_SESSION['banana_message'] = serialize($message);
698 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
700 Banana
::$message =& $message;
704 protected function removeMessage($group, $artid)
706 Banana
::$spool->delId($artid);
707 if ($group == $_SESSION['banana_group']) {
708 if (!Banana
::$profile['display']) {
709 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
711 if ($artid == $_SESSION['banana_artid']) {
712 unset($_SESSION['banana_message']);
713 unset($_SESSION['banana_showhdr']);
714 unset($_SESSION['banana_artid']);
717 $this->loadSpool($group);
721 static public function load($file)
723 $file = strtolower($file) . '.inc.php';
724 if (!@include_once
dirname(__FILE__
) . "/$file") {
730 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: