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
;
56 static public $msgshow_javascript = true
;
59 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
60 * If it matches, return 3 main parts :
61 * \\1 and \\3 are delimiters
64 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
66 * $matches[2] = "http://www.polytechnique.org"
69 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
71 ### Message edition ###
72 static public $msgedit_canattach = true
;
73 static public $msgedit_maxfilesize = 100000;
74 /** Global headers to use for messages
76 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
77 /** Mime type order for quoting
79 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
81 ### Feed configuration ###
82 static public $feed_active = true
;
83 static public $feed_format = 'rss2';
84 static public $feed_updateOnDemand = false
; // Update the feed each time sbd check it
85 static public $feed_copyright = null
; // Global copyright informations
86 static public $feed_generator = 'Banana @VERSION@'; // Feed generator
87 static public $feed_email = null
; // Admin e-mail
88 static public $feed_namePrefix = 'Banana :: ';
89 static public $feed_size = 15; // Number of messages in the feed
92 /** News serveur to use
94 static public $nntp_host = 'news://localhost:119/';
96 static public $mbox_path = '/var/mail';
97 static public $mbox_helper = './mbox-helper';
100 static public $debug_nntp = false
;
101 static public $debug_mbox = false
;
102 static public $debug_smarty = false
;
110 const ACTION_BOX_NEEDED
= 1; // mask
111 const ACTION_BOX_LIST
= 2;
112 const ACTION_BOX_SUBS
= 4;
113 const ACTION_BOX_FEED
= 8;
114 const ACTION_MSG_LIST
= 3;
115 const ACTION_MSG_READ
= 5;
116 const ACTION_MSG_NEW
= 9;
117 const ACTION_MSG_CANCEL
= 17;
118 const ACTION_MSG_IMAGES
= 33;
127 const SPOOL_UNREAD
= 1;
134 static public $protocole = null
;
135 static public $spool = null
;
136 static public $message = null
;
137 static public $page = null
;
139 static public $group = null
;
140 static public $artid = null
;
141 static public $action = null
;
142 static public $part = null
;
143 static public $first = null
;
145 /** Class parameters storage
151 # Banana Implementation
154 /** Build the instance of Banana
155 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
156 * @param protocole Protocole to use
158 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
160 if (is_null($params)) {
161 $this->params
= $_GET;
163 $this->params
= $params;
167 // connect to protocole handler
168 $classname = 'Banana' . $protocole;
169 if (!class_exists($classname)) {
170 Banana
::load($protocole);
172 Banana
::$protocole = new $classname(Banana
::$group);
175 if ($pageclass == 'BananaPage') {
176 Banana
::load('page');
178 Banana
::$page = new $pageclass;
179 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
180 'multipart/mixed' => _b_('Composition'),
181 'text/html' => _b_('Texte formaté'),
182 'text/plain' => _b_('Texte brut'),
183 'text/enriched' => _b_('Texte enrichi'),
184 'text' => _b_('Texte'),
185 'message/rfc822' => _b_('Mail'),
186 'message' => _b_('Message'),
187 'source' => _b_('Source'));
188 Banana
::$mimeparts = array_merge($types, Banana
::$mimeparts);
191 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
193 protected function loadParams()
195 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
196 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
197 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
198 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
200 $action = @$this->params
['action'];
201 if ($action == 'rss' ||
$action == 'rss2' ||
$action == 'atom') {
202 if ($action == 'rss') {
205 Banana
::$feed_format = $action;
206 Banana
::$action = Banana
::ACTION_BOX_FEED
;
210 // Look for the action to execute
211 if (is_null(Banana
::$group)) {
212 if ($action == 'subscribe') {
213 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
215 Banana
::$action = Banana
::ACTION_BOX_LIST
;
220 if (is_null(Banana
::$artid)) {
221 if ($action == 'new') {
222 Banana
::$action = Banana
::ACTION_MSG_NEW
;
224 Banana
::$action = Banana
::ACTION_MSG_LIST
;
230 Banana
::$action = Banana
::ACTION_MSG_NEW
;
233 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
236 Banana
::$action = Banana
::ACTION_MSG_IMAGES
;
239 Banana
::$action = Banana
::ACTION_MSG_READ
;
244 * This function need user profile to be initialised
246 public function run()
249 setlocale(LC_ALL
, Banana
::$profile['locale']);
251 // Check if the state is valid
252 if (Banana
::$protocole->lastErrNo()) {
253 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
254 . Banana
::$protocole->lastError());
256 if (!Banana
::$protocole->isValid()) {
257 return Banana
::$page->kill(_b_('Connexion non-valide'));
259 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
260 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', Banana
::$group)) {
261 Banana
::$page->setPage('group');
262 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
266 // Dispatch to the action handlers
267 switch (Banana
::$action) {
268 case Banana
::ACTION_BOX_SUBS
:
269 $error = $this->action_subscribe();
271 case Banana
::ACTION_BOX_LIST
:
272 $error = $this->action_listBoxes();
274 case Banana
::ACTION_BOX_FEED
:
275 $this->action_feed(); // generate its own xml
277 case Banana
::ACTION_MSG_LIST
:
278 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
280 case Banana
::ACTION_MSG_IMAGES
:
281 Banana
::$msgshow_externalimages = true
;
282 case Banana
::ACTION_MSG_READ
:
283 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
285 case Banana
::ACTION_MSG_NEW
:
286 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
288 case Banana
::ACTION_MSG_CANCEL
:
289 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
292 $error = _b_("L'action demandée n'est pas supportée par Banana");
296 if (is_string($error)) {
297 return Banana
::$page->kill($error);
299 return Banana
::$page->run();
302 /** Build and post a new message
303 * @return postid (or -1 if the message has not been found)
305 public function post($dest, $reply, $subject, $body)
307 $hdrs = Banana
::$protocole->requestedHeaders();
308 $headers = Banana
::$profile['headers'];
309 $headers[$hdrs['dest']] = $dest;
311 $headers[$hdrs['reply']] = $reply;
313 $headers['Subject'] = $subject;
314 $msg = BananaMessage
::newMessage($headers, $body);
315 if (Banana
::$protocole->send($msg)) {
316 Banana
::$group = ($reply ?
$reply : $dest);
317 $this->loadSpool(Banana
::$group);
318 return Banana
::$spool->getPostId($subject);
323 /** Return the CSS code to include in the headers
325 public function css()
327 return Banana
::$page->css
;
330 /** Return the Link to the feed of the page
332 public function feed()
334 if (!Banana
::$feed_active) {
337 return Banana
::$page->makeURL(array('group' => Banana
::$group, 'action' => Banana
::$feed_format));
340 /** Return the execution backtrace of the current BananaProtocole
342 public function backtrace()
344 if (Banana
::$protocole) {
345 return Banana
::$protocole->backtrace();
350 /**************************************************************************/
352 /**************************************************************************/
353 protected function action_saveSubs($groups)
355 Banana
::$profile['subscribe'] = $groups;
359 protected function action_subscribe()
361 Banana
::$page->setPage('subscribe');
362 if (isset($_POST['validsubs'])) {
363 $this->action_saveSubs(array_keys($_POST['subscribe']));
364 Banana
::$page->redirect();
366 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
367 Banana
::$page->assign('groups', $groups);
371 protected function action_listBoxes()
373 Banana
::$page->setPage('forums');
374 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
375 Banana
::$page->assign('groups', $groups);
376 if (empty(Banana
::$profile['subscribe']) || Banana
::$profile['lastnews']) {
377 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
378 Banana
::$page->assign('newgroups', $newgroups);
383 protected function action_feed()
385 Banana
::load('feed');
386 if (Banana
::$group) {
387 if (Banana
::$feed_updateOnDemand) {
388 $this->loadSpool(Banana
::$group);
390 $feed =& BananaFeed
::getFeed();
393 if (Banana
::$profile['subscribe']) {
395 foreach (Banana
::$profile['subscribe'] as $group) {
396 Banana
::$group = $group;
397 if (Banana
::$feed_updateOnDemand) {
398 $this->loadSpool($group);
400 $feed =& BananaFeed
::getFeed();
401 $subfeed =& BananaFeed
::merge($subfeed, $feed, _b_('Abonnements'), _b_('Mes abonnements Banana'));
405 Banana
::$page->feed();
408 protected function action_showThread($group, $first)
410 Banana
::$page->setPage('thread');
411 if (!$this->loadSpool($group)) {
412 return _b_('Impossible charger la liste des messages de ') . $group;
414 if (Banana
::$spool_boxlist) {
415 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
416 Banana
::$page->assign('groups', $groups);
418 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
422 protected function action_showMessage($group, $artid, $partid = 'text')
424 Banana
::$page->setPage('message');
425 $istext = $partid == 'text' ||
$partid == 'source'
426 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
428 $this->loadSpool($group);
430 $msg =& $this->loadMessage($group, $artid);
432 $this->loadSpool($group);
433 $this->removeMessage($group, $artid);
434 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
436 if ($partid == 'xface') {
439 } elseif (!$istext) {
440 $part = $msg->getPartById($partid);
441 if (!is_null($part)) {
444 $part = $msg->getFile($partid);
445 if (!is_null($part)) {
449 } elseif ($partid == 'text') {
451 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
452 } elseif ($partid == 'source') {
453 $text = Banana
::$protocole->getMessageSource($artid);
454 if (!is_utf8($text)) {
455 $text = utf8_encode($text);
457 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
459 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
462 if (Banana
::$profile['autoup']) {
463 Banana
::$spool->markAsRead($artid);
465 if (Banana
::$spool_boxlist) {
466 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
467 Banana
::$page->assign('groups', $groups);
469 Banana
::$page->assign_by_ref('message', $msg);
470 Banana
::$page->assign('extimages', Banana
::$msgshow_hasextimages);
471 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
472 Banana
::$page->assign('type', $partid);
476 protected function action_newMessage($group, $artid)
478 Banana
::$page->setPage('new');
479 if (!Banana
::$protocole->canSend()) {
480 return _b_('Vous n\'avez pas le droit de poster');
482 $hdrs = Banana
::$protocole->requestedHeaders();
484 foreach ($hdrs as $header) {
485 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
486 if (isset(Banana
::$profile['headers'][$header])) {
487 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
490 if (isset($_POST['sendmessage'])) {
491 $hdr_values = array();
492 foreach ($hdrs as $header) {
493 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
494 if (!is_utf8($hdr_values[$header])) {
495 $hdr_values[$header] = utf8_encode($hdr_values[$header]);
497 if ($headers != 'Subject') {
498 $hdr_values[$header] = str_replace(', ', ',', $hdr_values[$header]);
501 if (!is_null($artid)) {
502 $old =& $this->loadMessage($group, $artid);
503 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
506 if (isset($_POST['body']) && !is_utf8($_POST['body'])) {
507 $_POST['body'] = utf8_encode($_POST['body']);
509 if (empty($hdr_values['Subject'])) {
510 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
511 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment']) && $_FILES['attachment']['name']) {
512 $uploaded =& $_FILES['attachment'];
513 if (!is_uploaded_file($uploaded['tmp_name'])) {
514 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
516 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
519 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
521 if (!is_null($msg)) {
522 if (Banana
::$protocole->send($msg)) {
523 $this->loadSpool($group);
524 $newid = Banana
::$spool->updateUnread(Banana
::$profile['lastnews']);
525 Banana
::$page->redirect(array('group' => $group, 'artid' => $newid ?
$newid : $artid));
527 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
528 . Banana
::$protocole->lastError());
532 if (!is_null($artid)) {
533 $msg =& $this->loadMessage($group, $artid);
534 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
535 $subject = $msg->getHeaderValue('subject');
536 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
537 $target = $msg->getHeaderValue($hdrs['reply']);
538 if (empty($target)) {
541 $headers[$hdrs['dest']]['user'] =& $target;
544 $headers[$hdrs['dest']]['user'] = $group;
546 if (Banana
::$profile['signature']) {
547 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
549 Banana
::$page->assign('body', $body);
552 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
553 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
554 Banana
::$page->assign('headers', $headers);
558 protected function action_cancelMessage($group, $artid)
560 Banana
::$page->setPage('cancel');
561 $msg =& $this->loadMessage($group, $artid);
562 if (!$msg->canCancel()) {
563 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
565 if (isset($_POST['cancel'])) {
566 $this->loadSpool($group);
567 $ndx = Banana
::$spool->getNdX($id) - 1;
568 if (!Banana
::$protocole->cancel($msg)) {
569 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
570 . Banana
::$protocole->lastError();
575 $this->removeMessage($group, $artid);
576 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
579 Banana
::$page->assign_by_ref('message', $msg);
580 Banana
::$page->assign('body', $msg->getFormattedBody());
581 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
585 /**************************************************************************/
586 /* Spoolgen functions */
587 /**************************************************************************/
589 private function checkErrors()
591 if (Banana
::$protocole->lastErrno()) {
592 echo "\nL'erreur suivante s'est produite : "
593 . Banana
::$protocole->lastErrno() . " "
594 . Banana
::$protocole->lastError() . "\n";
600 static public function createAllSpool(array $protos)
602 foreach ($protos as $proto) {
603 $banana = new Banana(array(), $proto);
605 if (!$banana->checkErrors()) {
608 $groups = Banana
::$protocole->getBoxList();
609 if (!$banana->checkErrors()) {
613 print "** $proto **\n";
614 foreach (array_keys($groups) as $g) {
615 print "Generating spool for $g: ";
617 $spool = $banana->loadSpool($g);
618 if (!$banana->checkErrors()) {
623 Banana
::$spool = null
;
629 static public function refreshAllFeeds(array $protos)
631 Banana
::load('feed');
632 Banana
::$feed_updateOnDemand = true
; // In order to force update
633 foreach ($protos as $proto) {
634 $banana = new Banana(array(), $proto);
636 if (!$banana->checkErrors()) {
639 $groups = Banana
::$protocole->getBoxList();
640 if (!$banana->checkErrors()) {
644 print "** $proto **\n";
645 foreach (array_keys($groups) as $g) {
646 print "Generating feed cache for $g: ";
648 $spool = $banana->loadSpool($g);
649 if (!$banana->checkErrors()) {
652 $feed =& BananaFeed
::getFeed();
656 Banana
::$spool = null
;
662 /**************************************************************************/
663 /* Private functions */
664 /**************************************************************************/
666 protected function loadSpool($group)
668 Banana
::load('spool');
669 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
671 if (php_sapi_name() != 'cli') {
672 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
673 Banana
::$spool = unserialize($_SESSION['banana_spool']);
674 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
676 unset($_SESSION['banana_message']);
677 unset($_SESSION['banana_artid']);
678 unset($_SESSION['banana_showhdr']);
681 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
682 if (php_sapi_name() != 'cli') {
683 $_SESSION['banana_group'] = $group;
684 if (!Banana
::$profile['display']) {
685 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
686 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
689 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
694 protected function &loadMessage($group, $artid)
696 Banana
::load('message');
697 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
698 && isset($_SESSION['banana_message'])) {
699 $message = unserialize($_SESSION['banana_message']);
700 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
702 $message = Banana
::$protocole->getMessage($artid);
703 $_SESSION['banana_group'] = $group;
704 $_SESSION['banana_artid'] = $artid;
705 $_SESSION['banana_message'] = serialize($message);
706 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
708 Banana
::$message =& $message;
712 protected function removeMessage($group, $artid)
714 Banana
::$spool->delId($artid);
715 if ($group == $_SESSION['banana_group']) {
716 if (!Banana
::$profile['display']) {
717 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
719 if ($artid == $_SESSION['banana_artid']) {
720 unset($_SESSION['banana_message']);
721 unset($_SESSION['banana_showhdr']);
722 unset($_SESSION['banana_artid']);
725 $this->loadSpool($group);
729 static private function load($file)
731 $file = strtolower($file) . '.inc.php';
732 if (!@include_once
dirname(__FILE__
) . "/$file") {
738 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: