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;
28 static public $spool_max = 3000;
29 static public $spool_tbefore = 5;
30 static public $spool_tafter = 5;
31 static public $spool_tmax = 50;
32 static public $spool_boxlist = true
;
34 ### Message processing ###
35 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
36 'content-type', 'content-id', 'date', 'followup-to',
37 'from', 'message-id', 'newsgroups', 'organization',
38 'references', 'subject', 'x-face', 'in-reply-to',
39 'to', 'cc', 'reply-to');
41 ### Message display ###
42 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
43 'organization', 'date', 'references', 'in-reply-to');
44 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed', 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
45 static public $msgshow_xface = true
;
46 static public $msgshow_wrap = 78;
49 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
50 * If it matches, return 3 main parts :
51 * \\1 and \\3 are delimiters
54 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
56 * $matches[2] = "http://www.polytechnique.org"
59 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
61 ### Message edition ###
62 static public $msgedit_canattach = true
;
63 static public $msgedit_maxfilesize = 100000;
64 /** Global headers to use for messages
66 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
67 /** Mime type order for quoting
69 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
72 /** News serveur to use
74 static public $nntp_host = 'news://localhost:119/';
76 static public $mbox_path = '/var/mail';
79 static public $debug_nntp = false
;
80 static public $debug_smarty = false
;
88 const ACTION_BOX_NEEDED
= 1; // mask
89 const ACTION_BOX_LIST
= 2;
90 const ACTION_BOX_SUBS
= 4;
91 const ACTION_MSG_LIST
= 3;
92 const ACTION_MSG_READ
= 5;
93 const ACTION_MSG_NEW
= 9;
94 const ACTION_MSG_CANCEL
= 17;
103 const SPOOL_UNREAD
= 1;
110 static public $protocole = null
;
111 static public $spool = null
;
112 static public $message = null
;
113 static public $page = null
;
115 static public $group = null
;
116 static public $artid = null
;
117 static public $action = null
;
118 static public $part = null
;
119 static public $first = null
;
121 /** Class parameters storage
127 # Banana Implementation
130 /** Build the instance of Banana
131 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
132 * @param protocole Protocole to use
134 public function __construct($params = null
, $protocole = 'NNTP', $pageclass = 'BananaPage')
136 Banana
::load('text.func');
137 if (is_null($params)) {
138 $this->params
= $_GET;
140 $this->params
= $params;
144 // connect to protocole handler
145 $classname = 'Banana' . $protocole;
146 if (!class_exists($classname)) {
147 Banana
::load($protocole);
149 Banana
::$protocole = new $classname(Banana
::$group);
152 if ($pageclass == 'BananaPage') {
153 Banana
::load('page');
155 Banana
::$page = new $pageclass;
158 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
160 protected function loadParams()
162 Banana
::$group = isset($this->params
['group']) ?
$this->params
['group'] : null
;
163 Banana
::$artid = isset($this->params
['artid']) ?
$this->params
['artid'] : null
;
164 Banana
::$first = isset($this->params
['first']) ?
$this->params
['first'] : null
;
165 Banana
::$part = isset($this->params
['part']) ?
$this->params
['part'] : 'text';
167 // Look for the action to execute
168 if (is_null(Banana
::$group)) {
169 if (isset($this->params
['action']) && $this->params
['action'] == 'subscribe') {
170 Banana
::$action = Banana
::ACTION_BOX_SUBS
;
172 Banana
::$action = Banana
::ACTION_BOX_LIST
;
176 $action = isset($this->params
['action']) ?
$this->params
['action'] : null
;
177 if (is_null(Banana
::$artid)) {
178 if ($action == 'new') {
179 Banana
::$action = Banana
::ACTION_MSG_NEW
;
181 Banana
::$action = Banana
::ACTION_MSG_LIST
;
187 Banana
::$action = Banana
::ACTION_MSG_NEW
;
190 Banana
::$action = Banana
::ACTION_MSG_CANCEL
;
193 Banana
::$action = Banana
::ACTION_MSG_READ
;
198 * This function need user profile to be initialised
200 public function run()
203 setlocale(LC_ALL
, Banana
::$profile['locale']);
205 // Check if the state is valid
206 if (Banana
::$protocole->lastErrNo()) {
207 return Banana
::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
208 . Banana
::$protocole->lastError());
210 if (!Banana
::$protocole->isValid()) {
211 return Banana
::$page->kill(_b_('Connexion non-valide'));
213 if (Banana
::$action & Banana
::ACTION_BOX_NEEDED
) {
214 if(Banana
::$boxpattern && !preg_match('/' . Banana
::$boxpattern . '/i', $group)) {
215 Banana
::$page->setPage('group');
216 return Banana
::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
220 // Dispatch to the action handlers
221 switch (Banana
::$action) {
222 case Banana
::ACTION_BOX_SUBS
:
223 $error = $this->action_subscribe();
225 case Banana
::ACTION_BOX_LIST
:
226 $error = $this->action_listBoxes();
228 case Banana
::ACTION_MSG_LIST
:
229 $error = $this->action_showThread(Banana
::$group, Banana
::$first);
231 case Banana
::ACTION_MSG_READ
:
232 $error = $this->action_showMessage(Banana
::$group, Banana
::$artid, Banana
::$part);
234 case Banana
::ACTION_MSG_NEW
:
235 $error = $this->action_newMessage(Banana
::$group, Banana
::$artid);
237 case Banana
::ACTION_MSG_CANCEL
:
238 $error = $this->action_cancelMessage(Banana
::$group, Banana
::$artid);
241 $error = _b_("L'action demandée n'est pas supportée par Banana");
245 if (is_string($error)) {
246 return Banana
::$page->kill($error);
248 return Banana
::$page->run();
251 /**************************************************************************/
253 /**************************************************************************/
254 protected function action_saveSubs($groups)
256 Banana
::$profile['subscribe'] = $groups;
260 protected function action_subscribe()
262 Banana
::$page->setPage('subscribe');
263 if (isset($_POST['validsubs'])) {
264 $this->action_saveSubs(array_keys($_POST['subscribe']));
265 Banana
::$page->redirect();
267 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_ALL
);
268 Banana
::$page->assign('groups', $groups);
272 protected function action_listBoxes()
274 Banana
::$page->setPage('forums');
275 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
276 $newgroups = Banana
::$protocole->getBoxList(Banana
::BOXES_NEW
, Banana
::$profile['lastnews'], true
);
277 Banana
::$page->assign('groups', $groups);
278 Banana
::$page->assign('newgroups', $newgroups);
282 protected function action_showThread($group, $first)
284 Banana
::$page->setPage('thread');
285 if (!$this->loadSpool($group)) {
286 return _b_('Impossible charger la liste des messages de ') . $group;
288 if (Banana
::$spool_boxlist) {
289 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
290 Banana
::$page->assign('groups', $groups);
292 Banana
::$page->assign('msgbypage', Banana
::$spool_tmax);
296 protected function action_showMessage($group, $artid, $partid = 'text')
298 Banana
::$page->setPage('message');
299 $istext = $partid == 'text' ||
$partid == 'source'
300 ||
preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
302 $this->loadSpool($group);
304 $msg =& $this->loadMessage($group, $artid);
306 $this->loadSpool($group);
307 $this->removeMessage($group, $artid);
308 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
310 if ($partid == 'xface') {
313 } elseif (!$istext) {
314 $part = $msg->getPartById($partid);
315 if (!is_null($part)) {
318 $part = $msg->getFile($partid);
319 if (!is_null($part)) {
323 } elseif ($partid == 'text') {
324 Banana
::$page->assign('body', $msg->getFormattedBody());
325 } elseif ($partid == 'source') {
326 $text = Banana
::$protocole->getMessageSource($artid);
327 if (!is_utf8($text)) {
328 $text = utf8_encode($text);
330 Banana
::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
332 Banana
::$page->assign('body', $msg->getFormattedBody($partid));
335 if (Banana
::$profile['autoup']) {
336 Banana
::$spool->markAsRead($artid);
338 if (Banana
::$spool_boxlist) {
339 $groups = Banana
::$protocole->getBoxList(Banana
::BOXES_SUB
, Banana
::$profile['lastnews'], true
);
340 Banana
::$page->assign('groups', $groups);
342 Banana
::$page->assign_by_ref('message', $msg);
343 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
347 protected function action_newMessage($group, $artid)
349 Banana
::$page->setPage('new');
350 if (!Banana
::$protocole->canSend()) {
351 return _b_('Vous n\'avez pas le droit de poster');
353 $hdrs = Banana
::$protocole->requestedHeaders();
355 foreach ($hdrs as $header) {
356 $headers[$header] = array('name' => BananaMessage
::translateHeaderName($header));
357 if (isset(Banana
::$profile['headers'][$header])) {
358 $headers[$header]['fixed'] = Banana
::$profile['headers'][$header];
361 if (isset($_POST['sendmessage'])) {
362 $hdr_values = array();
363 foreach ($hdrs as $header) {
364 $hdr_values[$header] = isset($headers[$header]['fixed']) ?
$headers[$header]['fixed'] : @$_POST[$header];
367 $old =& $this->loadMessage($group, $artid);
368 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
371 if (empty($hdr_values['Subject'])) {
372 Banana
::$page->trig(_b_('Le message doit avoir un sujet'));
373 } elseif (Banana
::$msgedit_canattach && isset($_FILES['attachment'])) {
374 $uploaded = $_FILES['attachment'];
375 if (!is_uploaded_file($uploaded['tmp_name'])) {
376 Banana
::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
378 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body'], $uploaded);
381 $msg = BananaMessage
::newMessage($hdr_values, $_POST['body']);
383 if (!is_null($msg)) {
384 if (Banana
::$protocole->send($msg)) {
385 Banana
::$page->redirect(array('group' => $group, 'artid' => $artid));
387 Banana
::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
388 . Banana
::$protocole->lastError());
391 if (!is_null($artid)) {
392 $msg =& $this->loadMessage($group, $artid);
393 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
394 $subject = $msg->getHeaderValue('subject');
395 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
396 $target = $msg->getHeaderValue($hdrs['reply']);
397 if (empty($target)) {
400 $headers[$hdrs['dest']]['user'] =& $target;
403 $headers[$hdrs['dest']]['user'] = $group;
405 if (Banana
::$profile['signature']) {
406 $body .= "\n\n-- \n" . Banana
::$profile['signature'];
408 Banana
::$page->assign('body', $body);
411 Banana
::$page->assign('maxfilesize', Banana
::$msgedit_maxfilesize);
412 Banana
::$page->assign('can_attach', Banana
::$msgedit_canattach);
413 Banana
::$page->assign('headers', $headers);
417 protected function action_cancelMessage($group, $artid)
419 Banana
::$page->setPage('cancel');
420 $msg =& $this->loadMessage($group, $artid);
421 if (!$msg->canCancel()) {
422 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
424 if (isset($_POST['cancel'])) {
425 $this->loadSpool($group);
426 $ndx = Banana
::$spool->getNdX($id) - 1;
427 if (!Banana
::$protocole->cancel($msg)) {
428 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
429 . Banana
::$protocole->lastError();
434 $this->removeMessage($group, $artid);
435 Banana
::$page->redirect(Array('group' => $group, 'first' => $ndx));
438 Banana
::$page->assign_by_ref('message', $msg);
439 Banana
::$page->assign('body', $msg->getFormattedBody());
440 Banana
::$page->assign('headers', Banana
::$msgshow_headers);
444 /**************************************************************************/
445 /* Private functions */
446 /**************************************************************************/
448 protected function loadSpool($group)
450 Banana
::load('spool');
451 if (!Banana
::$spool || Banana
::$spool->group
!= $group) {
453 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
454 Banana
::$spool = unserialize($_SESSION['banana_spool']);
455 $clean = @(Banana
::$profile['lastnews'] != $_SESSION['banana_lastnews']);
457 BananaSpool
::getSpool($group, Banana
::$profile['lastnews'], Banana
::$profile['autoup'] ||
$clean);
458 $_SESSION['banana_group'] = $group;
459 if (!Banana
::$profile['display']) {
460 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
461 $_SESSION['banana_lastnews'] = Banana
::$profile['lastnews'];
463 Banana
::$spool->setMode(Banana
::$profile['display'] ? Banana
::SPOOL_UNREAD
: Banana
::SPOOL_ALL
);
468 protected function &loadMessage($group, $artid)
470 Banana
::load('message');
471 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
472 && isset($_SESSION['banana_message'])) {
473 $message = unserialize($_SESSION['banana_message']);
474 Banana
::$msgshow_headers = $_SESSION['banana_showhdr'];
476 $message = Banana
::$protocole->getMessage($artid);
477 $_SESSION['banana_group'] = $group;
478 $_SESSION['banana_artid'] = $artid;
479 $_SESSION['banana_message'] = serialize($message);
480 $_SESSION['banana_showhdr'] = Banana
::$msgshow_headers;
482 Banana
::$message =& $message;
486 protected function removeMessage($group, $artid)
488 Banana
::$spool->delId($artid);
489 if ($group == $_SESSION['banana_group']) {
490 if (!Banana
::$profile['display']) {
491 $_SESSION['banana_spool'] = serialize(Banana
::$spool);
493 if ($artid == $_SESSION['banana_artid']) {
494 unset($_SESSION['banana_message']);
495 unset($_SESSION['banana_showhdr']);
496 unset($_SESSION['banana_artid']);
502 static private function load($file)
504 $file = strtolower($file) . '.inc.php';
505 if (!@include_once
dirname(__FILE__
) . "/$file") {
511 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: