Can hide the box-list in thread view
[banana.git] / banana / banana.inc.php.in
1 <?php
2 /********************************************************************************
3 * banana/banana.inc.php : banana main file
4 * --------------------------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 class Banana
11 {
12
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;
32 static public $spool_boxlist = true;
33
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');
40
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;
47
48 /** Match an url
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
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 */
59 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
60
61 ### Message edition ###
62 static public $msgedit_canattach = true;
63 static public $msgedit_maxfilesize = 100000;
64 /** Global headers to use for messages
65 */
66 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
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
71 ### Protocole ###
72 /** News serveur to use
73 */
74 static public $nntp_host = 'news://localhost:119/';
75
76 static public $mbox_path = '/var/mail';
77
78 ### Debug ###
79 static public $debug_nntp = false;
80 static public $debug_smarty = false;
81
82
83 #######
84 # Constants
85 #######
86
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
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
121 /** Class parameters storage
122 */
123 public $params;
124
125
126 #######
127 # Banana Implementation
128 #######
129
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
133 */
134 public function __construct($params = null, $protocole = 'NNTP', $pageclass = 'BananaPage')
135 {
136 Banana::load('text.func');
137 if (is_null($params)) {
138 $this->params = $_GET;
139 } else {
140 $this->params = $params;
141 }
142 $this->loadParams();
143
144 // connect to protocole handler
145 $classname = 'Banana' . $protocole;
146 if (!class_exists($classname)) {
147 Banana::load($protocole);
148 }
149 Banana::$protocole = new $classname(Banana::$group);
150
151 // build the page
152 if ($pageclass == 'BananaPage') {
153 Banana::load('page');
154 }
155 Banana::$page = new $pageclass;
156 }
157
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)) {
169 if (isset($this->params['action']) && $this->params['action'] == 'subscribe') {
170 Banana::$action = Banana::ACTION_BOX_SUBS;
171 } else {
172 Banana::$action = Banana::ACTION_BOX_LIST;
173 }
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;
182 }
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;
194 }
195 }
196
197 /** Run Banana
198 * This function need user profile to be initialised
199 */
200 public function run()
201 {
202 // Configure locales
203 setlocale(LC_ALL, Banana::$profile['locale']);
204
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());
209 }
210 if (!Banana::$protocole->isValid()) {
211 return Banana::$page->kill(_b_('Connexion non-valide'));
212 }
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"));
217 }
218 }
219
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:
241 $error = _b_("L'action demandée n'est pas supportée par Banana");
242 }
243
244 // Generate the page
245 if (is_string($error)) {
246 return Banana::$page->kill($error);
247 }
248 return Banana::$page->run();
249 }
250
251 /**************************************************************************/
252 /* actions */
253 /**************************************************************************/
254 protected function action_saveSubs($groups)
255 {
256 Banana::$profile['subscribe'] = $groups;
257 return true;
258 }
259
260 protected function action_subscribe()
261 {
262 Banana::$page->setPage('subscribe');
263 if (isset($_POST['validsubs'])) {
264 $this->action_saveSubs(array_keys($_POST['subscribe']));
265 Banana::$page->redirect();
266 }
267 $groups = Banana::$protocole->getBoxList(Banana::BOXES_ALL);
268 Banana::$page->assign('groups', $groups);
269 return true;
270 }
271
272 protected function action_listBoxes()
273 {
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;
280 }
281
282 protected function action_showThread($group, $first)
283 {
284 Banana::$page->setPage('thread');
285 if (!$this->loadSpool($group)) {
286 return _b_('Impossible charger la liste des messages de ') . $group;
287 }
288 if (Banana::$spool_boxlist) {
289 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
290 Banana::$page->assign('groups', $groups);
291 }
292 Banana::$page->assign('msgbypage', Banana::$spool_tmax);
293 return true;
294 }
295
296 protected function action_showMessage($group, $artid, $partid = 'text')
297 {
298 Banana::$page->setPage('message');
299 $istext = $partid == 'text' || $partid == 'source'
300 || preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
301 if ($istext) {
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);
308 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
309 }
310 if ($partid == 'xface') {
311 $msg->getXFace();
312 exit;
313 } elseif (!$istext) {
314 $part = $msg->getPartById($partid);
315 if (!is_null($part)) {
316 $part->send(true);
317 }
318 $part = $msg->getFile($partid);
319 if (!is_null($part)) {
320 $part->send();
321 }
322 exit;
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);
329 }
330 Banana::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
331 } else {
332 Banana::$page->assign('body', $msg->getFormattedBody($partid));
333 }
334
335 if (Banana::$profile['autoup']) {
336 Banana::$spool->markAsRead($artid);
337 }
338 if (Banana::$spool_boxlist) {
339 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
340 Banana::$page->assign('groups', $groups);
341 }
342 Banana::$page->assign_by_ref('message', $msg);
343 Banana::$page->assign('headers', Banana::$msgshow_headers);
344 return true;
345 }
346
347 protected function action_newMessage($group, $artid)
348 {
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));
357 if (isset(Banana::$profile['headers'][$header])) {
358 $headers[$header]['fixed'] = Banana::$profile['headers'][$header];
359 }
360 }
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];
365 }
366 if ($artid) {
367 $old =& $this->loadMessage($group, $artid);
368 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
369 }
370 $msg = null;
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'));
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());
389 }
390 } else {
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)) {
398 $target = $group;
399 }
400 $headers[$hdrs['dest']]['user'] =& $target;
401 } else {
402 $body = '';
403 $headers[$hdrs['dest']]['user'] = $group;
404 }
405 if (Banana::$profile['signature']) {
406 $body .= "\n\n-- \n" . Banana::$profile['signature'];
407 }
408 Banana::$page->assign('body', $body);
409 }
410
411 Banana::$page->assign('maxfilesize', Banana::$msgedit_maxfilesize);
412 Banana::$page->assign('can_attach', Banana::$msgedit_canattach);
413 Banana::$page->assign('headers', $headers);
414 return true;
415 }
416
417 protected function action_cancelMessage($group, $artid)
418 {
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));
436 }
437
438 Banana::$page->assign_by_ref('message', $msg);
439 Banana::$page->assign('body', $msg->getFormattedBody());
440 Banana::$page->assign('headers', Banana::$msgshow_headers);
441 return true;
442 }
443
444 /**************************************************************************/
445 /* Private functions */
446 /**************************************************************************/
447
448 protected function loadSpool($group)
449 {
450 Banana::load('spool');
451 if (!Banana::$spool || Banana::$spool->group != $group) {
452 $clean = false;
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']);
456 }
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'];
462 }
463 Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
464 }
465 return true;
466 }
467
468 protected function &loadMessage($group, $artid)
469 {
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'];
475 } else {
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;
481 }
482 Banana::$message =& $message;
483 return $message;
484 }
485
486 protected function removeMessage($group, $artid)
487 {
488 Banana::$spool->delId($artid);
489 if ($group == $_SESSION['banana_group']) {
490 if (!Banana::$profile['display']) {
491 $_SESSION['banana_spool'] = serialize(Banana::$spool);
492 }
493 if ($artid == $_SESSION['banana_artid']) {
494 unset($_SESSION['banana_message']);
495 unset($_SESSION['banana_showhdr']);
496 unset($_SESSION['banana_artid']);
497 }
498 }
499 return true;
500 }
501
502 static private function load($file)
503 {
504 $file = strtolower($file) . '.inc.php';
505 if (!@include_once dirname(__FILE__) . "/$file") {
506 require_once $file;
507 }
508 }
509 }
510
511 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
512 ?>