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