2c223bf5edffe8a20b5ca2c62b8036edef480e41
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
;
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');
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
;
59 static public $msgshow_pgpcheck = true
;
60 static public $msgshow_pgppath = 'gpg';
61 static public $msgshow_pgpoptions = '';
64 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
65 * If it matches, return 3 main parts :
66 * \\1 and \\3 are delimiters
69 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
71 * $matches[2] = "http://www.polytechnique.org"
74 static public $msgshow_url = '(["\[\<])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]\>])?';
76 ### Message edition ###
77 static public $msgedit_canattach = true
;
78 static public $msgedit_maxfilesize = 100000;
79 /** Global headers to use for messages
81 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
82 /** Mime type order for quoting
84 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
86 ### Feed configuration ###
87 static public $feed_active = true
;
88 static public $feed_format = 'rss2';
89 static public $feed_updateOnDemand = false
; // Update the feed each time sbd check it
90 static public $feed_copyright = null
; // Global copyright informations
91 static public $feed_generator = 'Banana @VERSION@'; // Feed generator
92 static public $feed_email = null
; // Admin e-mail
93 static public $feed_namePrefix = 'Banana :: ';
94 static public $feed_size = 15; // Number of messages in the feed
97 /** News serveur to use
99 static public $nntp_host = 'news://localhost:119/';
101 static public $mbox_path = '/var/mail';
102 static public $mbox_helper = './mbox-helper';
105 static public $debug_nntp = false
;
106 static public $debug_mbox = false
;
107 static public $debug_smarty = false
;
115 const ACTION_BOX_NEEDED
= 1; // mask
116 const ACTION_BOX_LIST
= 2;
117 const ACTION_BOX_SUBS
= 4;
118 const ACTION_BOX_FEED
= 8;
119 const ACTION_MSG_LIST
= 3;
120 const ACTION_MSG_READ
= 5;
121 const ACTION_MSG_NEW
= 9;
122 const ACTION_MSG_CANCEL
= 17;
123 const ACTION_MSG_IMAGES
= 33;
132 const SPOOL_UNREAD
= 1;
139 static public $protocole = null
;
140 static public $spool = null
;
141 static public $message = null
;
142 static public $page = null
;
144 static public $group = null
;
145 static public $artid = null
;
146 static public $action = null
;
147 static public $part = null
;
148 static public $first = null
;
150 /** Class parameters storage
156 # Banana Implementation
159 /** Build the instance of Banana
160 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
161 * @param protocole Protocole to use
163 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
165 if (is_null($params)) {
166 $this->params
= $_GET;
168 $this->params
= $params;
172 // connect to protocole handler
173 $classname = 'Banana' . $protocole;
174 if (!class_exists($classname)) {
175 Banana
::load($protocole);
177 Banana
::$protocole = new $classname(Banana
::$group);
180 if ($pageclass == 'BananaPage') {
181 Banana
::load('page');
183 Banana
::$page = new $pageclass;
184 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
185 'multipart/mixed' => _b_('Composition'),
186 'text/html' => _b_('Texte formaté'),
187 'text/plain' => _b_('Texte brut'),
188 'text/enriched' => _b_('Texte enrichi'),
189 'text' => _b_('Texte'),
190 'message/rfc822' => _b_('Mail'),
191 'message' => _b_('Message'),
192 'source' => _b_('Source'));
193 Banana
::$mimeparts = array_merge($types, Banana
::$mimeparts);
196 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
198 protected function loadParams()
200 foreach ($this->params
as &$value) {
205 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
206 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
207 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
208 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
210 $action = @$this->params
['action'];
211 if ($action == 'rss' ||
$action == 'rss2' ||
$action == 'atom') {
212 if ($action == 'rss') {
215 Banana
::$feed_format = $action;
216 Banana
::$action = Banana
::ACTION_BOX_FEED
;
220 // Look for the action to execute
221 if (is_null(Banana
::$group)) {
222 if ($action == 'subscribe') {
223 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
225 Banana
::$action = Banana
::ACTION_BOX_LIST
;
230 if (is_null(Banana
::$artid)) {
231 if ($action == 'new') {
232 Banana
::$action = Banana
::ACTION_MSG_NEW
;
234 Banana
::$action = Banana
::ACTION_MSG_LIST
;
240 Banana
::$action = Banana
::ACTION_MSG_NEW
;
243 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
246 Banana
::$action = Banana
::ACTION_MSG_IMAGES
;
249 Banana
::$action = Banana
::ACTION_MSG_READ
;
254 * This function need user profile to be initialised
256 public function run()
259 setlocale(LC_ALL
, Banana
::$profile['locale']);
261 // Check if the state is valid
262 if (Banana
::$protocole->lastErrNo()) {
263 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
264 . Banana
::$protocole->lastError());
266 if (!Banana
::$protocole->isValid()) {
267 return Banana
::$page->kill(_b_('Connexion non-valide'));
269 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
270 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', Banana
::$group)) {
271 Banana
::$page->setPage('group');
272 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
276 // Dispatch to the action handlers
277 switch (Banana
::$action) {
278 case Banana
::ACTION_BOX_SUBS
:
279 $error = $this->action_subscribe();
281 case Banana
::ACTION_BOX_LIST
:
282 $error = $this->action_listBoxes();
284 case Banana
::ACTION_BOX_FEED
:
285 $this->action_feed(); // generate its own xml
287 case Banana
::ACTION_MSG_LIST
:
288 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
290 case Banana
::ACTION_MSG_IMAGES
:
291 Banana
::$msgshow_externalimages = true
;
292 case Banana
::ACTION_MSG_READ
:
293 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
295 case Banana
::ACTION_MSG_NEW
:
296 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
298 case Banana
::ACTION_MSG_CANCEL
:
299 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
302 $error = _b_("L'action demandée n'est pas supportée par Banana");
306 if (is_string($error)) {
307 return Banana
::$page->kill($error);
309 return Banana
::$page->run();
312 /** Build and post a new message
313 * @return postid (or -1 if the message has not been found)
315 public function post($dest, $reply, $subject, $body)
317 $hdrs = Banana
::$protocole->requestedHeaders();
318 $headers = Banana
::$profile['headers'];
319 $headers[$hdrs['dest']] = $dest;
321 $headers[$hdrs['reply']] = $reply;
323 $headers['Subject'] = $subject;
324 $msg = BananaMessage
::newMessage($headers, $body);
325 if (Banana
::$protocole->send($msg)) {
326 Banana
::$group = ($reply ?
$reply : $dest);
327 $this->loadSpool(Banana
::$group);
328 return Banana
::$spool->getPostId($subject);
333 /** Return the CSS code to include in the headers
335 public function css()
337 return Banana
::$page->css
;
340 /** Return the Link to the feed of the page
342 public function feed()
344 if (!Banana
::$feed_active) {
347 return Banana
::$page->makeURL(array('group' => Banana
::$group, 'action' => Banana
::$feed_format));
350 /** Return the execution backtrace of the current BananaProtocole
352 public function backtrace()
354 if (Banana
::$protocole) {
355 return Banana
::$protocole->backtrace();
360 /**************************************************************************/
362 /**************************************************************************/
363 protected function action_saveSubs($groups)
365 Banana
::$profile['subscribe'] = $groups;
369 protected function action_subscribe()
371 Banana
::$page->setPage('subscribe');
372 if (isset($_POST['validsubs'])) {
373 $this->action_saveSubs(array_keys($_POST['subscribe']));
374 Banana
::$page->redirect();
376 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
377 Banana
::$page->assign('groups', $groups);
381 protected function action_listBoxes()
383 Banana
::$page->setPage('forums');
384 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
385 Banana
::$page->assign('groups', $groups);
386 if (empty(Banana
::$profile['subscribe']) || Banana
::$profile['lastnews']) {
387 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
388 Banana
::$page->assign('newgroups', $newgroups);
393 protected function action_feed()
395 Banana
::load('feed');
396 if (Banana
::$group) {
397 if (Banana
::$feed_updateOnDemand) {
398 $this->loadSpool(Banana
::$group);
400 $feed =& BananaFeed
::getFeed();
403 if (Banana
::$profile['subscribe']) {
405 foreach (Banana
::$profile['subscribe'] as $group) {
406 Banana
::$group = $group;
407 if (Banana
::$feed_updateOnDemand) {
408 $this->loadSpool($group);
410 $feed =& BananaFeed
::getFeed();
411 $subfeed =& BananaFeed
::merge($subfeed, $feed, _b_('Abonnements'), _b_('Mes abonnements Banana'));
415 Banana
::$page->feed();
418 protected function action_showThread($group, $first)
420 Banana
::$page->setPage('thread');
421 if (!$this->loadSpool($group)) {
422 return _b_('Impossible charger la liste des messages de ') . $group;
424 if (Banana
::$spool_boxlist) {
425 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
426 Banana
::$page->assign('groups', $groups);
428 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
432 protected function action_showMessage($group, $artid, $partid = 'text')
434 Banana
::$page->setPage('message');
435 $istext = $partid == 'text' ||
$partid == 'source'
436 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
438 $this->loadSpool($group);
440 $msg =& $this->loadMessage($group, $artid);
442 $this->loadSpool($group);
443 $this->removeMessage($group, $artid);
444 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
446 if ($partid == 'xface') {
449 } elseif (!$istext) {
450 $part = $msg->getPartById($partid);
451 if (!is_null($part)) {
454 $part = $msg->getFile($partid);
455 if (!is_null($part)) {
459 } elseif ($partid == 'text') {
461 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
462 } elseif ($partid == 'source') {
463 $text = Banana
::$protocole->getMessageSource($artid);
464 if (!is_utf8($text)) {
465 $text = utf8_encode($text);
467 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
469 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
472 if (Banana
::$profile['autoup']) {
473 Banana
::$spool->markAsRead($artid);
475 if (Banana
::$spool_boxlist) {
476 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
477 Banana
::$page->assign('groups', $groups);
479 Banana
::$page->assign_by_ref('message', $msg);
480 Banana
::$page->assign('extimages', Banana
::$msgshow_hasextimages);
481 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
482 Banana
::$page->assign('type', $partid);
486 protected function action_newMessage($group, $artid)
488 Banana
::$page->setPage('new');
489 if (!Banana
::$protocole->canSend()) {
490 return _b_('Vous n\'avez pas le droit de poster');
492 $hdrs = Banana
::$protocole->requestedHeaders();
494 foreach ($hdrs as $header) {
495 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
496 if (isset(Banana
::$profile['headers'][$header])) {
497 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
500 if (isset($_POST['sendmessage'])) {
501 $hdr_values = array();
502 foreach ($hdrs as $header) {
503 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
504 if (!is_utf8($hdr_values[$header])) {
505 $hdr_values[$header] = utf8_encode($hdr_values[$header]);
508 $values = split('[,; ]', $hdr_values[$hdrs['dest']]);
509 $hdr_values[$hdrs['dest']] = preg_replace('/,+/', ',', implode(',', $values));
510 if (!is_null($artid)) {
511 $old =& $this->loadMessage($group, $artid);
512 $hdr_values['References'] = $old->getHeaderValue('references') . ' ' . $old->getHeaderValue('message-id');
515 if (isset($_POST['body']) && !is_utf8($_POST['body'])) {
516 $_POST['body'] = utf8_encode($_POST['body']);
518 if (empty($hdr_values['Subject'])) {
519 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
520 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment']) && $_FILES['attachment']['name']) {
521 $uploaded =& $_FILES['attachment'];
522 if (!is_uploaded_file($uploaded['tmp_name'])) {
523 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
525 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
528 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
530 if (!is_null($msg)) {
531 if (Banana
::$protocole->send($msg)) {
532 $this->loadSpool($group);
533 $newid = Banana
::$spool->updateUnread(Banana
::$profile['lastnews']);
534 Banana
::$page->redirect(array('group' => $group, 'artid' => $newid ?
$newid : $artid));
536 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
537 . Banana
::$protocole->lastError());
538 $body = $_POST['body'];
542 if (!is_null($artid)) {
543 $msg =& $this->loadMessage($group, $artid);
544 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
545 $subject = $msg->getHeaderValue('subject');
546 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
547 $target = $msg->getHeaderValue($hdrs['reply']);
548 if (empty($target)) {
551 $headers[$hdrs['dest']]['user'] =& $target;
554 $headers[$hdrs['dest']]['user'] = $group;
556 if (Banana
::$profile['signature']) {
557 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
560 Banana
::$page->assign('body', $body);
562 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
563 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
564 Banana
::$page->assign('headers', $headers);
568 protected function action_cancelMessage($group, $artid)
570 Banana
::$page->setPage('cancel');
571 $msg =& $this->loadMessage($group, $artid);
572 if (!$msg->canCancel()) {
573 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
575 if (isset($_POST['cancel'])) {
576 $this->loadSpool($group);
577 $ndx = Banana
::$spool->getNdX($id) - 1;
578 if (!Banana
::$protocole->cancel($msg)) {
579 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
580 . Banana
::$protocole->lastError();
585 $this->removeMessage($group, $artid);
586 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
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) {
681 if (php_sapi_name() != 'cli') {
682 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
683 Banana
::$spool =& $_SESSION['banana_spool'];
684 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
686 unset($_SESSION['banana_message']);
687 unset($_SESSION['banana_artid']);
688 unset($_SESSION['banana_showhdr']);
691 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
692 if (php_sapi_name() != 'cli') {
693 $_SESSION['banana_group'] = $group;
694 if (!Banana
::$profile['display']) {
695 $_SESSION['banana_spool'] =& Banana
::$spool;
696 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
699 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
704 protected function &loadMessage($group, $artid)
706 Banana
::load('message');
707 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
708 && isset($_SESSION['banana_message'])) {
709 $message =& $_SESSION['banana_message'];
710 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
712 $message = Banana
::$protocole->getMessage($artid);
713 $_SESSION['banana_group'] = $group;
714 $_SESSION['banana_artid'] = $artid;
715 $_SESSION['banana_message'] =& $message;
716 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
718 Banana
::$message =& $message;
722 protected function removeMessage($group, $artid)
724 Banana
::$spool->delId($artid);
725 if ($group == $_SESSION['banana_group']) {
726 if (!Banana
::$profile['display']) {
727 $_SESSION['banana_spool'] =& Banana
::$spool;
729 if ($artid == $_SESSION['banana_artid']) {
730 unset($_SESSION['banana_message']);
731 unset($_SESSION['banana_showhdr']);
732 unset($_SESSION['banana_artid']);
735 $this->loadSpool($group);
739 static public function load($file)
741 $file = strtolower($file) . '.inc.php';
742 if (!@include_once
dirname(__FILE__
) . "/$file") {
748 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: