Can define elements to display
[banana.git] / banana / banana.inc.php.in
CommitLineData
abd0915a
PHM
1<?php
2/********************************************************************************
7027794f 3* banana/banana.inc.php : banana main file
abd0915a
PHM
4* --------------------------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
10class Banana
11{
abd0915a 12
e9360b11 13#######
14# Configuration variables
15#######
16
17### General ###
18 static public $profile = Array( 'signature' => '',
19 'headers' => array('From' => 'Anonymous <anonymouse@example.com>'),
20 'display' => 0,
21 'lastnews' => 0,
22 'locale' => 'fr_FR',
23 'subscribe' => array(),
24 'autoup' => 1);
25 static public $boxpattern;
e95f5b5e 26 static public $withtabs = true;
e9360b11 27
28### Spool ###
29 static public $spool_max = 3000;
30 static public $spool_tbefore = 5;
31 static public $spool_tafter = 5;
32 static public $spool_tmax = 50;
8bfa4595 33 static public $spool_boxlist = true;
e9360b11 34
35### Message processing ###
36 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
7027794f 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');
b035d759 41
e9360b11 42### Message display ###
43 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
44 'organization', 'date', 'references', 'in-reply-to');
3c3a3ce3 45 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed', 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
e9360b11 46 static public $msgshow_xface = true;
47 static public $msgshow_wrap = 78;
e95f5b5e 48 static public $msgshow_withthread = true;
abd0915a 49
e04e6059 50 /** Match an url
3c3a3ce3 51 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
e04e6059 52 * If it matches, return 3 main parts :
53 * \\1 and \\3 are delimiters
54 * \\2 is the url
55 *
56 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
57 * $matches[1] = "["
58 * $matches[2] = "http://www.polytechnique.org"
59 * $matches[3] = "]"
60 */
8bfa4595 61 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
abd0915a 62
e9360b11 63### Message edition ###
64 static public $msgedit_canattach = true;
65 static public $msgedit_maxfilesize = 100000;
73ab762c 66 /** Global headers to use for messages
2509bf96 67 */
e9360b11 68 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
3c3a3ce3 69 /** Mime type order for quoting
70 */
71 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
72
e9360b11 73### Protocole ###
2509bf96 74 /** News serveur to use
75 */
e9360b11 76 static public $nntp_host = 'news://localhost:119/';
7027794f 77
e9360b11 78 static public $mbox_path = '/var/mail';
7027794f 79
e9360b11 80### Debug ###
7027794f 81 static public $debug_nntp = false;
82 static public $debug_smarty = false;
83
84
e9360b11 85#######
86# Constants
87#######
88
7027794f 89 // Actions
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;
97
98 // Box list view
99 const BOXES_ALL = 0;
100 const BOXES_SUB = 1;
101 const BOXES_NEW = 2;
102
103 // Spool view mode
104 const SPOOL_ALL = 0;
105 const SPOOL_UNREAD = 1;
106
e9360b11 107
108#######
109# Runtime variables
110#######
111
112 static public $protocole = null;
113 static public $spool = null;
114 static public $message = null;
115 static public $page = null;
116
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;
122
7027794f 123 /** Class parameters storage
124 */
125 public $params;
abd0915a 126
e9360b11 127
128#######
129# Banana Implementation
130#######
131
7027794f 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
d5588318 135 */
7027794f 136 public function __construct($params = null, $protocole = 'NNTP', $pageclass = 'BananaPage')
abd0915a 137 {
7027794f 138 Banana::load('text.func');
139 if (is_null($params)) {
140 $this->params = $_GET;
5d58099e 141 } else {
7027794f 142 $this->params = $params;
5d58099e 143 }
7027794f 144 $this->loadParams();
5d58099e 145
7027794f 146 // connect to protocole handler
7027794f 147 $classname = 'Banana' . $protocole;
e9360b11 148 if (!class_exists($classname)) {
149 Banana::load($protocole);
150 }
7027794f 151 Banana::$protocole = new $classname(Banana::$group);
abd0915a 152
7027794f 153 // build the page
154 if ($pageclass == 'BananaPage') {
155 Banana::load('page');
02198ace 156 }
7027794f 157 Banana::$page = new $pageclass;
158 }
abd0915a 159
7027794f 160 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
161 */
162 protected function loadParams()
163 {
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';
168
169 // Look for the action to execute
170 if (is_null(Banana::$group)) {
2f0aa8ce 171 if (isset($this->params['action']) && $this->params['action'] == 'subscribe') {
7027794f 172 Banana::$action = Banana::ACTION_BOX_SUBS;
abd0915a 173 } else {
7027794f 174 Banana::$action = Banana::ACTION_BOX_LIST;
abd0915a 175 }
7027794f 176 return;
177 }
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;
182 } else {
183 Banana::$action = Banana::ACTION_MSG_LIST;
d43ebde4 184 }
7027794f 185 return;
186 }
187 switch ($action) {
188 case 'new':
189 Banana::$action = Banana::ACTION_MSG_NEW;
190 return;
191 case 'cancel':
192 Banana::$action = Banana::ACTION_MSG_CANCEL;
193 return;
194 default:
195 Banana::$action = Banana::ACTION_MSG_READ;
abd0915a
PHM
196 }
197 }
198
7027794f 199 /** Run Banana
200 * This function need user profile to be initialised
201 */
202 public function run()
abd0915a 203 {
7027794f 204 // Configure locales
205 setlocale(LC_ALL, Banana::$profile['locale']);
abd0915a 206
7027794f 207 // Check if the state is valid
208 if (Banana::$protocole->lastErrNo()) {
598a1c53 209 return Banana::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
7027794f 210 . Banana::$protocole->lastError());
211 }
212 if (!Banana::$protocole->isValid()) {
213 return Banana::$page->kill(_b_('Connexion non-valide'));
214 }
215 if (Banana::$action & Banana::ACTION_BOX_NEEDED) {
e9360b11 216 if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', $group)) {
7027794f 217 Banana::$page->setPage('group');
598a1c53 218 return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
7027794f 219 }
220 }
abd0915a 221
7027794f 222 // Dispatch to the action handlers
223 switch (Banana::$action) {
224 case Banana::ACTION_BOX_SUBS:
225 $error = $this->action_subscribe();
226 break;
227 case Banana::ACTION_BOX_LIST:
228 $error = $this->action_listBoxes();
229 break;
230 case Banana::ACTION_MSG_LIST:
231 $error = $this->action_showThread(Banana::$group, Banana::$first);
232 break;
233 case Banana::ACTION_MSG_READ:
234 $error = $this->action_showMessage(Banana::$group, Banana::$artid, Banana::$part);
235 break;
236 case Banana::ACTION_MSG_NEW:
237 $error = $this->action_newMessage(Banana::$group, Banana::$artid);
238 break;
239 case Banana::ACTION_MSG_CANCEL:
240 $error = $this->action_cancelMessage(Banana::$group, Banana::$artid);
241 break;
242 default:
598a1c53 243 $error = _b_("L'action demandée n'est pas supportée par Banana");
7027794f 244 }
245
246 // Generate the page
247 if (is_string($error)) {
248 return Banana::$page->kill($error);
249 }
250 return Banana::$page->run();
abd0915a
PHM
251 }
252
7027794f 253 /**************************************************************************/
254 /* actions */
255 /**************************************************************************/
256 protected function action_saveSubs($groups)
abd0915a 257 {
7027794f 258 Banana::$profile['subscribe'] = $groups;
259 return true;
abd0915a
PHM
260 }
261
7027794f 262 protected function action_subscribe()
abd0915a 263 {
7027794f 264 Banana::$page->setPage('subscribe');
265 if (isset($_POST['validsubs'])) {
266 $this->action_saveSubs(array_keys($_POST['subscribe']));
267 Banana::$page->redirect();
7c111d8d 268 }
7027794f 269 $groups = Banana::$protocole->getBoxList(Banana::BOXES_ALL);
270 Banana::$page->assign('groups', $groups);
271 return true;
abd0915a
PHM
272 }
273
7027794f 274 protected function action_listBoxes()
d43ebde4 275 {
7027794f 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);
281 return true;
d43ebde4 282 }
283
7027794f 284 protected function action_showThread($group, $first)
abd0915a 285 {
7027794f 286 Banana::$page->setPage('thread');
287 if (!$this->loadSpool($group)) {
288 return _b_('Impossible charger la liste des messages de ') . $group;
abd0915a 289 }
8bfa4595 290 if (Banana::$spool_boxlist) {
291 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
292 Banana::$page->assign('groups', $groups);
293 }
e9360b11 294 Banana::$page->assign('msgbypage', Banana::$spool_tmax);
7027794f 295 return true;
abd0915a
PHM
296 }
297
7027794f 298 protected function action_showMessage($group, $artid, $partid = 'text')
abd0915a 299 {
7027794f 300 Banana::$page->setPage('message');
7a5823f9 301 $istext = $partid == 'text' || $partid == 'source'
b18124be 302 || preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
7a5823f9 303 if ($istext) {
7027794f 304 $this->loadSpool($group);
305 }
306 $msg =& $this->loadMessage($group, $artid);
307 if (is_null($msg)) {
308 $this->loadSpool($group);
309 $this->removeMessage($group, $artid);
598a1c53 310 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
7027794f 311 }
312 if ($partid == 'xface') {
313 $msg->getXFace();
314 exit;
7a5823f9 315 } elseif (!$istext) {
7027794f 316 $part = $msg->getPartById($partid);
317 if (!is_null($part)) {
318 $part->send(true);
eb31725e 319 }
7027794f 320 $part = $msg->getFile($partid);
321 if (!is_null($part)) {
322 $part->send();
abd0915a 323 }
7027794f 324 exit;
7a5823f9 325 } elseif ($partid == 'text') {
326 Banana::$page->assign('body', $msg->getFormattedBody());
327 } elseif ($partid == 'source') {
7d3f4749 328 $text = Banana::$protocole->getMessageSource($artid);
329 if (!is_utf8($text)) {
330 $text = utf8_encode($text);
331 }
332 Banana::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
7a5823f9 333 } else {
334 Banana::$page->assign('body', $msg->getFormattedBody($partid));
abd0915a 335 }
7a5823f9 336
e9360b11 337 if (Banana::$profile['autoup']) {
338 Banana::$spool->markAsRead($artid);
339 }
8bfa4595 340 if (Banana::$spool_boxlist) {
341 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
342 Banana::$page->assign('groups', $groups);
343 }
7027794f 344 Banana::$page->assign_by_ref('message', $msg);
e9360b11 345 Banana::$page->assign('headers', Banana::$msgshow_headers);
7027794f 346 return true;
abd0915a
PHM
347 }
348
7027794f 349 protected function action_newMessage($group, $artid)
abd0915a 350 {
7027794f 351 Banana::$page->setPage('new');
352 if (!Banana::$protocole->canSend()) {
353 return _b_('Vous n\'avez pas le droit de poster');
354 }
355 $hdrs = Banana::$protocole->requestedHeaders();
356 $headers = array();
357 foreach ($hdrs as $header) {
358 $headers[$header] = array('name' => BananaMessage::translateHeaderName($header));
e9360b11 359 if (isset(Banana::$profile['headers'][$header])) {
360 $headers[$header]['fixed'] = Banana::$profile['headers'][$header];
c42efe2f
PHM
361 }
362 }
7027794f 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];
d5588318 367 }
7027794f 368 if ($artid) {
369 $old =& $this->loadMessage($group, $artid);
370 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
7c111d8d 371 }
7027794f 372 $msg = null;
373 if (empty($hdr_values['Subject'])) {
374 Banana::$page->trig(_b_('Le message doit avoir un sujet'));
e9360b11 375 } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment'])) {
7027794f 376 $uploaded = $_FILES['attachment'];
377 if (!is_uploaded_file($uploaded['tmp_name'])) {
598a1c53 378 Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
7027794f 379 } else {
380 $msg = BananaMessage::newMessage($hdr_values, $_POST['body'], $uploaded);
381 }
382 } else {
383 $msg = BananaMessage::newMessage($hdr_values, $_POST['body']);
384 }
385 if (!is_null($msg)) {
386 if (Banana::$protocole->send($msg)) {
387 Banana::$page->redirect(array('group' => $group, 'artid' => $artid));
388 }
389 Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
390 . Banana::$protocole->lastError());
d5588318 391 }
abd0915a 392 } else {
7027794f 393 if (!is_null($artid)) {
394 $msg =& $this->loadMessage($group, $artid);
598a1c53 395 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
7027794f 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)) {
400 $target = $group;
401 }
402 $headers[$hdrs['dest']]['user'] =& $target;
403 } else {
404 $body = '';
405 $headers[$hdrs['dest']]['user'] = $group;
7c111d8d 406 }
e9360b11 407 if (Banana::$profile['signature']) {
408 $body .= "\n\n-- \n" . Banana::$profile['signature'];
7027794f 409 }
410 Banana::$page->assign('body', $body);
abd0915a 411 }
7027794f 412
e9360b11 413 Banana::$page->assign('maxfilesize', Banana::$msgedit_maxfilesize);
414 Banana::$page->assign('can_attach', Banana::$msgedit_canattach);
7027794f 415 Banana::$page->assign('headers', $headers);
7c111d8d 416 return true;
abd0915a
PHM
417 }
418
7027794f 419 protected function action_cancelMessage($group, $artid)
abd0915a 420 {
7027794f 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');
425 }
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();
432 }
433 if ($ndx < 50) {
434 $ndx = 0;
435 }
436 $this->removeMessage($group, $artid);
437 Banana::$page->redirect(Array('group' => $group, 'first' => $ndx));
7c111d8d 438 }
fd57a15c 439
7027794f 440 Banana::$page->assign_by_ref('message', $msg);
fd57a15c 441 Banana::$page->assign('body', $msg->getFormattedBody());
442 Banana::$page->assign('headers', Banana::$msgshow_headers);
7c111d8d 443 return true;
abd0915a
PHM
444 }
445
7027794f 446 /**************************************************************************/
447 /* Private functions */
448 /**************************************************************************/
449
0e25d15d 450 protected function loadSpool($group)
abd0915a 451 {
7027794f 452 Banana::load('spool');
453 if (!Banana::$spool || Banana::$spool->group != $group) {
8bfa4595 454 $clean = false;
7027794f 455 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
456 Banana::$spool = unserialize($_SESSION['banana_spool']);
e1debf92 457 $clean = @(Banana::$profile['lastnews'] != $_SESSION['banana_lastnews']);
e9360b11 458 }
e1debf92 459 BananaSpool::getSpool($group, Banana::$profile['lastnews'], Banana::$profile['autoup'] || $clean);
7027794f 460 $_SESSION['banana_group'] = $group;
e9360b11 461 if (!Banana::$profile['display']) {
462 $_SESSION['banana_spool'] = serialize(Banana::$spool);
e1debf92 463 $_SESSION['banana_lastnews'] = Banana::$profile['lastnews'];
e9360b11 464 }
7027794f 465 Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
abd0915a 466 }
7027794f 467 return true;
abd0915a
PHM
468 }
469
0e25d15d 470 protected function &loadMessage($group, $artid)
abd0915a 471 {
7027794f 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']);
e9360b11 476 Banana::$msgshow_headers = $_SESSION['banana_showhdr'];
7027794f 477 } else {
478 $message = Banana::$protocole->getMessage($artid);
479 $_SESSION['banana_group'] = $group;
480 $_SESSION['banana_artid'] = $artid;
481 $_SESSION['banana_message'] = serialize($message);
e9360b11 482 $_SESSION['banana_showhdr'] = Banana::$msgshow_headers;
7027794f 483 }
484 Banana::$message =& $message;
485 return $message;
abd0915a 486 }
9e223b38 487
0e25d15d 488 protected function removeMessage($group, $artid)
9e223b38 489 {
7027794f 490 Banana::$spool->delId($artid);
491 if ($group == $_SESSION['banana_group']) {
e9360b11 492 if (!Banana::$profile['display']) {
493 $_SESSION['banana_spool'] = serialize(Banana::$spool);
494 }
7027794f 495 if ($artid == $_SESSION['banana_artid']) {
496 unset($_SESSION['banana_message']);
497 unset($_SESSION['banana_showhdr']);
498 unset($_SESSION['banana_artid']);
9e223b38 499 }
9e223b38 500 }
7027794f 501 return true;
9e223b38 502 }
73ab762c 503
7027794f 504 static private function load($file)
73ab762c 505 {
7027794f 506 $file = strtolower($file) . '.inc.php';
507 if (!@include_once dirname(__FILE__) . "/$file") {
508 require_once $file;
509 }
73ab762c 510 }
abd0915a
PHM
511}
512
598a1c53 513// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
abd0915a 514?>