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 ********************************************************************************/
14 # Configuration variables
18 static public $profile = Array( 'signature' => '',
19 'headers' => array('From' => 'Anonymous <anonymouse@example.com>'),
23 'subscribe' => array(),
25 static public $boxpattern;
26 static public $withtabs = true
;
29 static public $spool_max = 3000;
30 static public $spool_tbefore = 5;
31 static public $spool_tafter = 5;
32 static public $spool_tmax = 50;
33 static public $spool_boxlist = true
;
35 ### Message processing ###
36 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
37 'content-type', 'content-id', 'date', 'followup-to',
38 'from', 'message-id', 'newsgroups', 'organization',
39 'references', 'subject', 'x-face', 'in-reply-to',
40 'to', 'cc', 'reply-to');
42 ### Message display ###
43 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
44 'organization', 'date', 'references', 'in-reply-to');
45 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed', 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
46 static public $msgshow_xface = true
;
47 static public $msgshow_wrap = 78;
48 static public $msgshow_withthread = true
;
51 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
52 * If it matches, return 3 main parts :
53 * \\1 and \\3 are delimiters
56 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
58 * $matches[2] = "http://www.polytechnique.org"
61 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
63 ### Message edition ###
64 static public $msgedit_canattach = true
;
65 static public $msgedit_maxfilesize = 100000;
66 /** Global headers to use for messages
68 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
69 /** Mime type order for quoting
71 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
74 /** News serveur to use
76 static public $nntp_host = 'news://localhost:119/';
78 static public $mbox_path = '/var/mail';
81 static public $debug_nntp = false
;
82 static public $debug_smarty = false
;
90 const ACTION_BOX_NEEDED
= 1; // mask
91 const ACTION_BOX_LIST
= 2;
92 const ACTION_BOX_SUBS
= 4;
93 const ACTION_MSG_LIST
= 3;
94 const ACTION_MSG_READ
= 5;
95 const ACTION_MSG_NEW
= 9;
96 const ACTION_MSG_CANCEL
= 17;
105 const SPOOL_UNREAD
= 1;
112 static public $protocole = null
;
113 static public $spool = null
;
114 static public $message = null
;
115 static public $page = null
;
117 static public $group = null
;
118 static public $artid = null
;
119 static public $action = null
;
120 static public $part = null
;
121 static public $first = null
;
123 /** Class parameters storage
129 # Banana Implementation
132 /** Build the instance of Banana
133 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
134 * @param protocole Protocole to use
136 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
138 Banana
::load('text.func');
139 if (is_null($params)) {
140 $this->params
= $_GET;
142 $this->params
= $params;
146 // connect to protocole handler
147 $classname = 'Banana' . $protocole;
148 if (!class_exists($classname)) {
149 Banana
::load($protocole);
151 Banana
::$protocole = new $classname(Banana
::$group);
154 if ($pageclass == 'BananaPage') {
155 Banana
::load('page');
157 Banana
::$page = new $pageclass;
160 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
162 protected function loadParams()
164 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
165 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
166 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
167 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
169 // Look for the action to execute
170 if (is_null(Banana
::$group)) {
171 if (isset($this->params
['action']) && $this->params
['action'] == 'subscribe') {
172 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
174 Banana
::$action = Banana
::ACTION_BOX_LIST
;
178 $action = isset($this->params
['action']) ?
$this->params
['action'] : null
;
179 if (is_null(Banana
::$artid)) {
180 if ($action == 'new') {
181 Banana
::$action = Banana
::ACTION_MSG_NEW
;
183 Banana
::$action = Banana
::ACTION_MSG_LIST
;
189 Banana
::$action = Banana
::ACTION_MSG_NEW
;
192 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
195 Banana
::$action = Banana
::ACTION_MSG_READ
;
200 * This function need user profile to be initialised
202 public function run()
205 setlocale(LC_ALL
, Banana
::$profile['locale']);
207 // Check if the state is valid
208 if (Banana
::$protocole->lastErrNo()) {
209 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
210 . Banana
::$protocole->lastError());
212 if (!Banana
::$protocole->isValid()) {
213 return Banana
::$page->kill(_b_('Connexion non-valide'));
215 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
216 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', $group)) {
217 Banana
::$page->setPage('group');
218 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
222 // Dispatch to the action handlers
223 switch (Banana
::$action) {
224 case Banana
::ACTION_BOX_SUBS
:
225 $error = $this->action_subscribe();
227 case Banana
::ACTION_BOX_LIST
:
228 $error = $this->action_listBoxes();
230 case Banana
::ACTION_MSG_LIST
:
231 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
233 case Banana
::ACTION_MSG_READ
:
234 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
236 case Banana
::ACTION_MSG_NEW
:
237 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
239 case Banana
::ACTION_MSG_CANCEL
:
240 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
243 $error = _b_("L'action demandée n'est pas supportée par Banana");
247 if (is_string($error)) {
248 return Banana
::$page->kill($error);
250 return Banana
::$page->run();
253 /**************************************************************************/
255 /**************************************************************************/
256 protected function action_saveSubs($groups)
258 Banana
::$profile['subscribe'] = $groups;
262 protected function action_subscribe()
264 Banana
::$page->setPage('subscribe');
265 if (isset($_POST['validsubs'])) {
266 $this->action_saveSubs(array_keys($_POST['subscribe']));
267 Banana
::$page->redirect();
269 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
270 Banana
::$page->assign('groups', $groups);
274 protected function action_listBoxes()
276 Banana
::$page->setPage('forums');
277 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
278 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
279 Banana
::$page->assign('groups', $groups);
280 Banana
::$page->assign('newgroups', $newgroups);
284 protected function action_showThread($group, $first)
286 Banana
::$page->setPage('thread');
287 if (!$this->loadSpool($group)) {
288 return _b_('Impossible charger la liste des messages de ') . $group;
290 if (Banana
::$spool_boxlist) {
291 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
292 Banana
::$page->assign('groups', $groups);
294 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
298 protected function action_showMessage($group, $artid, $partid = 'text')
300 Banana
::$page->setPage('message');
301 $istext = $partid == 'text' ||
$partid == 'source'
302 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
304 $this->loadSpool($group);
306 $msg =& $this->loadMessage($group, $artid);
308 $this->loadSpool($group);
309 $this->removeMessage($group, $artid);
310 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
312 if ($partid == 'xface') {
315 } elseif (!$istext) {
316 $part = $msg->getPartById($partid);
317 if (!is_null($part)) {
320 $part = $msg->getFile($partid);
321 if (!is_null($part)) {
325 } elseif ($partid == 'text') {
326 Banana
::$page->assign('body', $msg->getFormattedBody());
327 } elseif ($partid == 'source') {
328 $text = Banana
::$protocole->getMessageSource($artid);
329 if (!is_utf8($text)) {
330 $text = utf8_encode($text);
332 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
334 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
337 if (Banana
::$profile['autoup']) {
338 Banana
::$spool->markAsRead($artid);
340 if (Banana
::$spool_boxlist) {
341 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
342 Banana
::$page->assign('groups', $groups);
344 Banana
::$page->assign_by_ref('message', $msg);
345 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
349 protected function action_newMessage($group, $artid)
351 Banana
::$page->setPage('new');
352 if (!Banana
::$protocole->canSend()) {
353 return _b_('Vous n\'avez pas le droit de poster');
355 $hdrs = Banana
::$protocole->requestedHeaders();
357 foreach ($hdrs as $header) {
358 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
359 if (isset(Banana
::$profile['headers'][$header])) {
360 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
363 if (isset($_POST['sendmessage'])) {
364 $hdr_values = array();
365 foreach ($hdrs as $header) {
366 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
369 $old =& $this->loadMessage($group, $artid);
370 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
373 if (empty($hdr_values['Subject'])) {
374 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
375 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment'])) {
376 $uploaded = $_FILES['attachment'];
377 if (!is_uploaded_file($uploaded['tmp_name'])) {
378 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
380 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
383 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
385 if (!is_null($msg)) {
386 if (Banana
::$protocole->send($msg)) {
387 Banana
::$page->redirect(array('group' => $group, 'artid' => $artid));
389 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
390 . Banana
::$protocole->lastError());
393 if (!is_null($artid)) {
394 $msg =& $this->loadMessage($group, $artid);
395 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
396 $subject = $msg->getHeaderValue('subject');
397 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
398 $target = $msg->getHeaderValue($hdrs['reply']);
399 if (empty($target)) {
402 $headers[$hdrs['dest']]['user'] =& $target;
405 $headers[$hdrs['dest']]['user'] = $group;
407 if (Banana
::$profile['signature']) {
408 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
410 Banana
::$page->assign('body', $body);
413 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
414 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
415 Banana
::$page->assign('headers', $headers);
419 protected function action_cancelMessage($group, $artid)
421 Banana
::$page->setPage('cancel');
422 $msg =& $this->loadMessage($group, $artid);
423 if (!$msg->canCancel()) {
424 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
426 if (isset($_POST['cancel'])) {
427 $this->loadSpool($group);
428 $ndx = Banana
::$spool->getNdX($id) - 1;
429 if (!Banana
::$protocole->cancel($msg)) {
430 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
431 . Banana
::$protocole->lastError();
436 $this->removeMessage($group, $artid);
437 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
440 Banana
::$page->assign_by_ref('message', $msg);
441 Banana
::$page->assign('body', $msg->getFormattedBody());
442 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
446 /**************************************************************************/
447 /* Private functions */
448 /**************************************************************************/
450 protected function loadSpool($group)
452 Banana
::load('spool');
453 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
455 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
456 Banana
::$spool = unserialize($_SESSION['banana_spool']);
457 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
459 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
460 $_SESSION['banana_group'] = $group;
461 if (!Banana
::$profile['display']) {
462 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
463 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
465 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
470 protected function &loadMessage($group, $artid)
472 Banana
::load('message');
473 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
474 && isset($_SESSION['banana_message'])) {
475 $message = unserialize($_SESSION['banana_message']);
476 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
478 $message = Banana
::$protocole->getMessage($artid);
479 $_SESSION['banana_group'] = $group;
480 $_SESSION['banana_artid'] = $artid;
481 $_SESSION['banana_message'] = serialize($message);
482 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
484 Banana
::$message =& $message;
488 protected function removeMessage($group, $artid)
490 Banana
::$spool->delId($artid);
491 if ($group == $_SESSION['banana_group']) {
492 if (!Banana
::$profile['display']) {
493 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
495 if ($artid == $_SESSION['banana_artid']) {
496 unset($_SESSION['banana_message']);
497 unset($_SESSION['banana_showhdr']);
498 unset($_SESSION['banana_artid']);
504 static private function load($file)
506 $file = strtolower($file) . '.inc.php';
507 if (!@include_once
dirname(__FILE__
) . "/$file") {
513 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: