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 = 78;
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');
81 /** News serveur to use
83 static public $nntp_host = 'news://localhost:119/';
85 static public $mbox_path = '/var/mail';
86 static public $mbox_helper = './mbox-helper';
89 static public $debug_nntp = false
;
90 static public $debug_mbox = false
;
91 static public $debug_smarty = false
;
99 const ACTION_BOX_NEEDED
= 1; // mask
100 const ACTION_BOX_LIST
= 2;
101 const ACTION_BOX_SUBS
= 4;
102 const ACTION_MSG_LIST
= 3;
103 const ACTION_MSG_READ
= 5;
104 const ACTION_MSG_NEW
= 9;
105 const ACTION_MSG_CANCEL
= 17;
106 const ACTION_MSG_IMAGES
= 33;
115 const SPOOL_UNREAD
= 1;
122 static public $protocole = null
;
123 static public $spool = null
;
124 static public $message = null
;
125 static public $page = null
;
127 static public $group = null
;
128 static public $artid = null
;
129 static public $action = null
;
130 static public $part = null
;
131 static public $first = null
;
133 /** Class parameters storage
139 # Banana Implementation
142 /** Build the instance of Banana
143 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
144 * @param protocole Protocole to use
146 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
148 if (is_null($params)) {
149 $this->params
= $_GET;
151 $this->params
= $params;
155 // connect to protocole handler
156 $classname = 'Banana' . $protocole;
157 if (!class_exists($classname)) {
158 Banana
::load($protocole);
160 Banana
::$protocole = new $classname(Banana
::$group);
163 if ($pageclass == 'BananaPage') {
164 Banana
::load('page');
166 Banana
::$page = new $pageclass;
167 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
168 'multipart/mixed' => _b_('Composition'),
169 'text/html' => _b_('Texte formaté'),
170 'text/plain' => _b_('Texte brut'),
171 'text/enriched' => _b_('Texte enrichi'),
172 'text' => _b_('Texte'),
173 'message/rfc822' => _b_('Mail'),
174 'message' => _b_('Message'),
175 'source' => _b_('Source'));
176 Banana
::$mimeparts = array_merge($types, Banana
::$mimeparts);
179 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
181 protected function loadParams()
183 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
184 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
185 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
186 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
188 // Look for the action to execute
189 if (is_null(Banana
::$group)) {
190 if (isset($this->params
['action']) && $this->params
['action'] == 'subscribe') {
191 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
193 Banana
::$action = Banana
::ACTION_BOX_LIST
;
197 $action = isset($this->params
['action']) ?
$this->params
['action'] : null
;
198 if (is_null(Banana
::$artid)) {
199 if ($action == 'new') {
200 Banana
::$action = Banana
::ACTION_MSG_NEW
;
202 Banana
::$action = Banana
::ACTION_MSG_LIST
;
208 Banana
::$action = Banana
::ACTION_MSG_NEW
;
211 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
214 Banana
::$action = Banana
::ACTION_MSG_IMAGES
;
217 Banana
::$action = Banana
::ACTION_MSG_READ
;
222 * This function need user profile to be initialised
224 public function run()
227 setlocale(LC_ALL
, Banana
::$profile['locale']);
229 // Check if the state is valid
230 if (Banana
::$protocole->lastErrNo()) {
231 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
232 . Banana
::$protocole->lastError());
234 if (!Banana
::$protocole->isValid()) {
235 return Banana
::$page->kill(_b_('Connexion non-valide'));
237 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
238 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', $group)) {
239 Banana
::$page->setPage('group');
240 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
244 // Dispatch to the action handlers
245 switch (Banana
::$action) {
246 case Banana
::ACTION_BOX_SUBS
:
247 $error = $this->action_subscribe();
249 case Banana
::ACTION_BOX_LIST
:
250 $error = $this->action_listBoxes();
252 case Banana
::ACTION_MSG_LIST
:
253 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
255 case Banana
::ACTION_MSG_IMAGES
:
256 Banana
::$msgshow_externalimages = true
;
257 case Banana
::ACTION_MSG_READ
:
258 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
260 case Banana
::ACTION_MSG_NEW
:
261 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
263 case Banana
::ACTION_MSG_CANCEL
:
264 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
267 $error = _b_("L'action demandée n'est pas supportée par Banana");
271 if (is_string($error)) {
272 return Banana
::$page->kill($error);
274 return Banana
::$page->run();
277 /** Return the CSS code to include in the headers
279 public function css()
281 return Banana
::$page->css
;
284 /** Return the execution backtrace of the current BananaProtocole
286 public function backtrace()
288 if (Banana
::$protocole) {
289 return Banana
::$protocole->backtrace();
294 /**************************************************************************/
296 /**************************************************************************/
297 protected function action_saveSubs($groups)
299 Banana
::$profile['subscribe'] = $groups;
303 protected function action_subscribe()
305 Banana
::$page->setPage('subscribe');
306 if (isset($_POST['validsubs'])) {
307 $this->action_saveSubs(array_keys($_POST['subscribe']));
308 Banana
::$page->redirect();
310 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
311 Banana
::$page->assign('groups', $groups);
315 protected function action_listBoxes()
317 Banana
::$page->setPage('forums');
318 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
319 Banana
::$page->assign('groups', $groups);
320 if (empty(Banana
::$profile['subscribe']) || Banana
::$profile['lastnews']) {
321 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
322 Banana
::$page->assign('newgroups', $newgroups);
327 protected function action_showThread($group, $first)
329 Banana
::$page->setPage('thread');
330 if (!$this->loadSpool($group)) {
331 return _b_('Impossible charger la liste des messages de ') . $group;
333 if (Banana
::$spool_boxlist) {
334 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
335 Banana
::$page->assign('groups', $groups);
337 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
341 protected function action_showMessage($group, $artid, $partid = 'text')
343 Banana
::$page->setPage('message');
344 $istext = $partid == 'text' ||
$partid == 'source'
345 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
347 $this->loadSpool($group);
349 $msg =& $this->loadMessage($group, $artid);
351 $this->loadSpool($group);
352 $this->removeMessage($group, $artid);
353 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
355 if ($partid == 'xface') {
358 } elseif (!$istext) {
359 $part = $msg->getPartById($partid);
360 if (!is_null($part)) {
363 $part = $msg->getFile($partid);
364 if (!is_null($part)) {
368 } elseif ($partid == 'text') {
370 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
371 } elseif ($partid == 'source') {
372 $text = Banana
::$protocole->getMessageSource($artid);
373 if (!is_utf8($text)) {
374 $text = utf8_encode($text);
376 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
378 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
381 if (Banana
::$profile['autoup']) {
382 Banana
::$spool->markAsRead($artid);
384 if (Banana
::$spool_boxlist) {
385 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
386 Banana
::$page->assign('groups', $groups);
388 Banana
::$page->assign_by_ref('message', $msg);
389 Banana
::$page->assign('extimages', Banana
::$msgshow_hasextimages);
390 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
391 Banana
::$page->assign('type', $partid);
395 protected function action_newMessage($group, $artid)
397 Banana
::$page->setPage('new');
398 if (!Banana
::$protocole->canSend()) {
399 return _b_('Vous n\'avez pas le droit de poster');
401 $hdrs = Banana
::$protocole->requestedHeaders();
403 foreach ($hdrs as $header) {
404 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
405 if (isset(Banana
::$profile['headers'][$header])) {
406 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
409 if (isset($_POST['sendmessage'])) {
410 $hdr_values = array();
411 foreach ($hdrs as $header) {
412 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
415 $old =& $this->loadMessage($group, $artid);
416 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
419 if (empty($hdr_values['Subject'])) {
420 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
421 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment'])) {
422 $uploaded = $_FILES['attachment'];
423 if (!is_uploaded_file($uploaded['tmp_name'])) {
424 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
426 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
429 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
431 if (!is_null($msg)) {
432 if (Banana
::$protocole->send($msg)) {
433 Banana
::$page->redirect(array('group' => $group, 'artid' => $artid));
435 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
436 . Banana
::$protocole->lastError());
439 if (!is_null($artid)) {
440 $msg =& $this->loadMessage($group, $artid);
441 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
442 $subject = $msg->getHeaderValue('subject');
443 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
444 $target = $msg->getHeaderValue($hdrs['reply']);
445 if (empty($target)) {
448 $headers[$hdrs['dest']]['user'] =& $target;
451 $headers[$hdrs['dest']]['user'] = $group;
453 if (Banana
::$profile['signature']) {
454 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
456 Banana
::$page->assign('body', $body);
459 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
460 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
461 Banana
::$page->assign('headers', $headers);
465 protected function action_cancelMessage($group, $artid)
467 Banana
::$page->setPage('cancel');
468 $msg =& $this->loadMessage($group, $artid);
469 if (!$msg->canCancel()) {
470 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
472 if (isset($_POST['cancel'])) {
473 $this->loadSpool($group);
474 $ndx = Banana
::$spool->getNdX($id) - 1;
475 if (!Banana
::$protocole->cancel($msg)) {
476 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
477 . Banana
::$protocole->lastError();
482 $this->removeMessage($group, $artid);
483 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
486 Banana
::$page->assign_by_ref('message', $msg);
487 Banana
::$page->assign('body', $msg->getFormattedBody());
488 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
492 /**************************************************************************/
493 /* Spoolgen functions */
494 /**************************************************************************/
496 private function checkErrors()
498 if (Banana
::$protocole->lastErrno()) {
499 echo "\nL'erreur suivante s'est produite : "
500 . Banana
::$protocole->lastErrno() . " "
501 . Banana
::$protocole->lastError() . "\n";
507 static public function createAllSpool(array $protos)
509 foreach ($protos as $proto) {
510 $banana = new Banana(array(), $proto);
512 if (!$banana->checkErrors()) {
515 $groups = Banana
::$protocole->getBoxList();
516 if (!$banana->checkErrors()) {
520 print "** $proto **\n";
521 foreach (array_keys($groups) as $g) {
522 print "Generating spool for $g : ";
524 $spool = $banana->loadSpool($g);
525 if (!$banana->checkErrors()) {
535 /**************************************************************************/
536 /* Private functions */
537 /**************************************************************************/
539 protected function loadSpool($group)
541 Banana
::load('spool');
542 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
544 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
545 Banana
::$spool = unserialize($_SESSION['banana_spool']);
546 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
548 unset($_SESSION['banana_message']);
549 unset($_SESSION['banana_artid']);
550 unset($_SESSION['banana_showhdr']);
552 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
553 $_SESSION['banana_group'] = $group;
554 if (!Banana
::$profile['display']) {
555 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
556 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
558 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
563 protected function &loadMessage($group, $artid)
565 Banana
::load('message');
566 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
567 && isset($_SESSION['banana_message'])) {
568 $message = unserialize($_SESSION['banana_message']);
569 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
571 $message = Banana
::$protocole->getMessage($artid);
572 $_SESSION['banana_group'] = $group;
573 $_SESSION['banana_artid'] = $artid;
574 $_SESSION['banana_message'] = serialize($message);
575 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
577 Banana
::$message =& $message;
581 protected function removeMessage($group, $artid)
583 Banana
::$spool->delId($artid);
584 if ($group == $_SESSION['banana_group']) {
585 if (!Banana
::$profile['display']) {
586 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
588 if ($artid == $_SESSION['banana_artid']) {
589 unset($_SESSION['banana_message']);
590 unset($_SESSION['banana_showhdr']);
591 unset($_SESSION['banana_artid']);
594 $this->loadSpool($group);
598 static private function load($file)
600 $file = strtolower($file) . '.inc.php';
601 if (!@include_once
dirname(__FILE__
) . "/$file") {
607 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: