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>'),
25 'subscribe' => array(),
27 static public $boxpattern;
28 static public $withtabs = true
;
29 static public $mimeparts = array();
32 static public $spool_max = 3000;
33 static public $spool_tbefore = 5;
34 static public $spool_tafter = 5;
35 static public $spool_tmax = 50;
36 static public $spool_boxlist = true
;
38 ### Message processing ###
39 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
40 'content-type', 'content-id', 'date', 'followup-to',
41 'from', 'message-id', 'newsgroups', 'organization',
42 'references', 'subject', 'x-face', 'in-reply-to',
43 'to', 'cc', 'reply-to');
45 ### Message display ###
46 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
47 'organization', 'date', 'references', 'in-reply-to');
48 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed',
49 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
50 static public $msgshow_xface = true
;
51 static public $msgshow_wrap = 78;
52 static public $msgshow_externalimages = false
;
53 static public $msgshow_withthread = true
;
56 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
57 * If it matches, return 3 main parts :
58 * \\1 and \\3 are delimiters
61 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
63 * $matches[2] = "http://www.polytechnique.org"
66 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
68 ### Message edition ###
69 static public $msgedit_canattach = true
;
70 static public $msgedit_maxfilesize = 100000;
71 /** Global headers to use for messages
73 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
74 /** Mime type order for quoting
76 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
79 /** News serveur to use
81 static public $nntp_host = 'news://localhost:119/';
83 static public $mbox_path = '/var/mail';
86 static public $debug_nntp = false
;
87 static public $debug_smarty = false
;
95 const ACTION_BOX_NEEDED
= 1; // mask
96 const ACTION_BOX_LIST
= 2;
97 const ACTION_BOX_SUBS
= 4;
98 const ACTION_MSG_LIST
= 3;
99 const ACTION_MSG_READ
= 5;
100 const ACTION_MSG_NEW
= 9;
101 const ACTION_MSG_CANCEL
= 17;
110 const SPOOL_UNREAD
= 1;
117 static public $protocole = null
;
118 static public $spool = null
;
119 static public $message = null
;
120 static public $page = null
;
122 static public $group = null
;
123 static public $artid = null
;
124 static public $action = null
;
125 static public $part = null
;
126 static public $first = null
;
128 /** Class parameters storage
134 # Banana Implementation
137 /** Build the instance of Banana
138 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
139 * @param protocole Protocole to use
141 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
143 if (is_null($params)) {
144 $this->params
= $_GET;
146 $this->params
= $params;
150 // connect to protocole handler
151 $classname = 'Banana' . $protocole;
152 if (!class_exists($classname)) {
153 Banana
::load($protocole);
155 Banana
::$protocole = new $classname(Banana
::$group);
158 if ($pageclass == 'BananaPage') {
159 Banana
::load('page');
161 Banana
::$page = new $pageclass;
163 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
164 'multipart/mixed' => _b_('Composition'),
165 'text/html' => _b_('Texte formaté'),
166 'text/plain' => _b_('Texte brut'),
167 'text/enriched' => _b_('Texte enrichi'),
168 'text' => _b_('Texte'),
169 'message/rfc822' => _b_('Mail'),
170 'message' => _b_('Message'),
171 'source' => _b_('Source'));
172 Banana
::$mimeparts = array_merge($types, Banana
::$mimeparts);
175 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
177 protected function loadParams()
179 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
180 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
181 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
182 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
184 // Look for the action to execute
185 if (is_null(Banana
::$group)) {
186 if (isset($this->params
['action']) && $this->params
['action'] == 'subscribe') {
187 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
189 Banana
::$action = Banana
::ACTION_BOX_LIST
;
193 $action = isset($this->params
['action']) ?
$this->params
['action'] : null
;
194 if (is_null(Banana
::$artid)) {
195 if ($action == 'new') {
196 Banana
::$action = Banana
::ACTION_MSG_NEW
;
198 Banana
::$action = Banana
::ACTION_MSG_LIST
;
204 Banana
::$action = Banana
::ACTION_MSG_NEW
;
207 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
210 Banana
::$action = Banana
::ACTION_MSG_READ
;
215 * This function need user profile to be initialised
217 public function run()
220 setlocale(LC_ALL
, Banana
::$profile['locale']);
222 // Check if the state is valid
223 if (Banana
::$protocole->lastErrNo()) {
224 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
225 . Banana
::$protocole->lastError());
227 if (!Banana
::$protocole->isValid()) {
228 return Banana
::$page->kill(_b_('Connexion non-valide'));
230 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
231 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', $group)) {
232 Banana
::$page->setPage('group');
233 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
237 // Dispatch to the action handlers
238 switch (Banana
::$action) {
239 case Banana
::ACTION_BOX_SUBS
:
240 $error = $this->action_subscribe();
242 case Banana
::ACTION_BOX_LIST
:
243 $error = $this->action_listBoxes();
245 case Banana
::ACTION_MSG_LIST
:
246 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
248 case Banana
::ACTION_MSG_READ
:
249 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
251 case Banana
::ACTION_MSG_NEW
:
252 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
254 case Banana
::ACTION_MSG_CANCEL
:
255 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
258 $error = _b_("L'action demandée n'est pas supportée par Banana");
262 if (is_string($error)) {
263 return Banana
::$page->kill($error);
265 return Banana
::$page->run();
268 /** Return the CSS code to include in the headers
270 public function css()
272 return Banana
::$page->css
;
275 /**************************************************************************/
277 /**************************************************************************/
278 protected function action_saveSubs($groups)
280 Banana
::$profile['subscribe'] = $groups;
284 protected function action_subscribe()
286 Banana
::$page->setPage('subscribe');
287 if (isset($_POST['validsubs'])) {
288 $this->action_saveSubs(array_keys($_POST['subscribe']));
289 Banana
::$page->redirect();
291 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
292 Banana
::$page->assign('groups', $groups);
296 protected function action_listBoxes()
298 Banana
::$page->setPage('forums');
299 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
300 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
301 Banana
::$page->assign('groups', $groups);
302 Banana
::$page->assign('newgroups', $newgroups);
306 protected function action_showThread($group, $first)
308 Banana
::$page->setPage('thread');
309 if (!$this->loadSpool($group)) {
310 return _b_('Impossible charger la liste des messages de ') . $group;
312 if (Banana
::$spool_boxlist) {
313 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
314 Banana
::$page->assign('groups', $groups);
316 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
320 protected function action_showMessage($group, $artid, $partid = 'text')
322 Banana
::$page->setPage('message');
323 $istext = $partid == 'text' ||
$partid == 'source'
324 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
326 $this->loadSpool($group);
328 $msg =& $this->loadMessage($group, $artid);
330 $this->loadSpool($group);
331 $this->removeMessage($group, $artid);
332 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
334 if ($partid == 'xface') {
337 } elseif (!$istext) {
338 $part = $msg->getPartById($partid);
339 if (!is_null($part)) {
342 $part = $msg->getFile($partid);
343 if (!is_null($part)) {
347 } elseif ($partid == 'text') {
349 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
350 } elseif ($partid == 'source') {
351 $text = Banana
::$protocole->getMessageSource($artid);
352 if (!is_utf8($text)) {
353 $text = utf8_encode($text);
355 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
357 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
360 if (Banana
::$profile['autoup']) {
361 Banana
::$spool->markAsRead($artid);
363 if (Banana
::$spool_boxlist) {
364 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
365 Banana
::$page->assign('groups', $groups);
367 Banana
::$page->assign_by_ref('message', $msg);
368 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
369 Banana
::$page->assign('type', $partid);
373 protected function action_newMessage($group, $artid)
375 Banana
::$page->setPage('new');
376 if (!Banana
::$protocole->canSend()) {
377 return _b_('Vous n\'avez pas le droit de poster');
379 $hdrs = Banana
::$protocole->requestedHeaders();
381 foreach ($hdrs as $header) {
382 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
383 if (isset(Banana
::$profile['headers'][$header])) {
384 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
387 if (isset($_POST['sendmessage'])) {
388 $hdr_values = array();
389 foreach ($hdrs as $header) {
390 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
393 $old =& $this->loadMessage($group, $artid);
394 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
397 if (empty($hdr_values['Subject'])) {
398 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
399 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment'])) {
400 $uploaded = $_FILES['attachment'];
401 if (!is_uploaded_file($uploaded['tmp_name'])) {
402 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
404 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
407 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
409 if (!is_null($msg)) {
410 if (Banana
::$protocole->send($msg)) {
411 Banana
::$page->redirect(array('group' => $group, 'artid' => $artid));
413 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
414 . Banana
::$protocole->lastError());
417 if (!is_null($artid)) {
418 $msg =& $this->loadMessage($group, $artid);
419 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
420 $subject = $msg->getHeaderValue('subject');
421 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
422 $target = $msg->getHeaderValue($hdrs['reply']);
423 if (empty($target)) {
426 $headers[$hdrs['dest']]['user'] =& $target;
429 $headers[$hdrs['dest']]['user'] = $group;
431 if (Banana
::$profile['signature']) {
432 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
434 Banana
::$page->assign('body', $body);
437 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
438 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
439 Banana
::$page->assign('headers', $headers);
443 protected function action_cancelMessage($group, $artid)
445 Banana
::$page->setPage('cancel');
446 $msg =& $this->loadMessage($group, $artid);
447 if (!$msg->canCancel()) {
448 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
450 if (isset($_POST['cancel'])) {
451 $this->loadSpool($group);
452 $ndx = Banana
::$spool->getNdX($id) - 1;
453 if (!Banana
::$protocole->cancel($msg)) {
454 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
455 . Banana
::$protocole->lastError();
460 $this->removeMessage($group, $artid);
461 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
464 Banana
::$page->assign_by_ref('message', $msg);
465 Banana
::$page->assign('body', $msg->getFormattedBody());
466 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
470 /**************************************************************************/
471 /* Spoolgen functions */
472 /**************************************************************************/
474 private function checkErrors()
476 if (Banana
::$protocole->lastErrno()) {
477 echo "\nL'erreur suivante s'est produite : "
478 . Banana
::$protocole->lastErrno() . " "
479 . Banana
::$protocole->lastError() . "\n";
485 static public function createAllSpool(array $protos)
487 foreach ($protos as $proto) {
488 $banana = new Banana(array(), $proto);
490 if (!$banana->checkErrors()) {
493 $groups = Banana
::$protocole->getBoxList();
494 if (!$banana->checkErrors()) {
498 print "** $proto **\n";
499 foreach (array_keys($groups) as $g) {
500 print "Generating spool for $g : ";
502 $spool = $banana->loadSpool($g);
503 if (!$banana->checkErrors()) {
513 /**************************************************************************/
514 /* Private functions */
515 /**************************************************************************/
517 protected function loadSpool($group)
519 Banana
::load('spool');
520 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
522 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
523 Banana
::$spool = unserialize($_SESSION['banana_spool']);
524 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
526 unset($_SESSION['banana_message']);
527 unset($_SESSION['banana_artid']);
528 unset($_SESSION['banana_showhdr']);
530 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
531 $_SESSION['banana_group'] = $group;
532 if (!Banana
::$profile['display']) {
533 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
534 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
536 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
541 protected function &loadMessage($group, $artid)
543 Banana
::load('message');
544 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
545 && isset($_SESSION['banana_message'])) {
546 $message = unserialize($_SESSION['banana_message']);
547 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
549 $message = Banana
::$protocole->getMessage($artid);
550 $_SESSION['banana_group'] = $group;
551 $_SESSION['banana_artid'] = $artid;
552 $_SESSION['banana_message'] = serialize($message);
553 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
555 Banana
::$message =& $message;
559 protected function removeMessage($group, $artid)
561 Banana
::$spool->delId($artid);
562 if ($group == $_SESSION['banana_group']) {
563 if (!Banana
::$profile['display']) {
564 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
566 if ($artid == $_SESSION['banana_artid']) {
567 unset($_SESSION['banana_message']);
568 unset($_SESSION['banana_showhdr']);
569 unset($_SESSION['banana_artid']);
575 static private function load($file)
577 $file = strtolower($file) . '.inc.php';
578 if (!@include_once
dirname(__FILE__
) . "/$file") {
584 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: