Should fix "unread state" issues.
[banana.git] / banana / banana.inc.php.in
CommitLineData
c4f176d8 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
10require_once dirname(__FILE__) . '/text.func.inc.php';
11
12class Banana
13{
14
15#######
16# Configuration variables
17#######
18
19### General ###
20 static public $profile = Array( 'signature' => '',
21 'headers' => array('From' => 'Anonymous <anonymouse@example.com>'),
22 'display' => 0,
23 'lastnews' => 0,
24 'locale' => 'fr_FR.UTF-8',
25 'subscribe' => array(),
26 'autoup' => 1);
27 static public $boxpattern;
28 static public $withtabs = true;
29 static public $mimeparts = array();
30
31### Spool ###
32 static public $spool_root = '/var/spool/banana';
33 static public $spool_max = 3000;
34 static public $spool_tbefore = 5;
35 static public $spool_tafter = 5;
36 static public $spool_tmax = 50;
37 static public $spool_boxlist = true;
38
39### Message processing ###
40 static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
41 'content-type', 'content-id', 'date', 'followup-to',
42 'from', 'message-id', 'newsgroups', 'organization',
43 'references', 'subject', 'x-face', 'in-reply-to',
44 'to', 'cc', 'reply-to');
45
46### Message display ###
47 static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
48 'organization', 'date', 'references', 'in-reply-to');
49 static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed',
50 'text/html', 'text/plain', 'text/enriched', 'text', 'message');
51 static public $msgshow_xface = true;
52 static public $msgshow_wrap = 78;
53 static public $msgshow_externalimages = false;
54 static public $msgshow_hasextimages = false;
55 static public $msgshow_withthread = true;
56
57 /** Match an url
58 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
59 * If it matches, return 3 main parts :
60 * \\1 and \\3 are delimiters
61 * \\2 is the url
62 *
63 * eg : preg_match("!$url_regexp!i", "[http://www.polytechnique.org]", $matches);
64 * $matches[1] = "["
65 * $matches[2] = "http://www.polytechnique.org"
66 * $matches[3] = "]"
67 */
68 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
69
70### Message edition ###
71 static public $msgedit_canattach = true;
72 static public $msgedit_maxfilesize = 100000;
73 /** Global headers to use for messages
74 */
75 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
76 /** Mime type order for quoting
77 */
78 static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
79
80### Protocole ###
81 /** News serveur to use
82 */
83 static public $nntp_host = 'news://localhost:119/';
84
85 static public $mbox_path = '/var/mail';
b10e2813 86 static public $mbox_helper = './mbox-helper';
c4f176d8 87
88### Debug ###
89 static public $debug_nntp = false;
90 static public $debug_mbox = false;
91 static public $debug_smarty = false;
92
93
94#######
95# Constants
96#######
97
98 // Actions
99 const ACTION_BOX_NEEDED = 1; // mask
100 const ACTION_BOX_LIST = 2;
101 const ACTION_BOX_SUBS = 4;
102 const ACTION_MSG_LIST = 3;
103 const ACTION_MSG_READ = 5;
104 const ACTION_MSG_NEW = 9;
105 const ACTION_MSG_CANCEL = 17;
106 const ACTION_MSG_IMAGES = 33;
107
108 // Box list view
109 const BOXES_ALL = 0;
110 const BOXES_SUB = 1;
111 const BOXES_NEW = 2;
112
113 // Spool view mode
114 const SPOOL_ALL = 0;
115 const SPOOL_UNREAD = 1;
116
117
118#######
119# Runtime variables
120#######
121
122 static public $protocole = null;
123 static public $spool = null;
124 static public $message = null;
125 static public $page = null;
126
127 static public $group = null;
128 static public $artid = null;
129 static public $action = null;
130 static public $part = null;
131 static public $first = null;
132
133 /** Class parameters storage
134 */
135 public $params;
136
137
138#######
139# Banana Implementation
140#######
141
142 /** Build the instance of Banana
143 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
144 * @param protocole Protocole to use
145 */
146 public function __construct($params = null, $protocole = 'NNTP', $pageclass = 'BananaPage')
147 {
148 if (is_null($params)) {
149 $this->params = $_GET;
150 } else {
151 $this->params = $params;
152 }
153 $this->loadParams();
154
155 // connect to protocole handler
156 $classname = 'Banana' . $protocole;
157 if (!class_exists($classname)) {
158 Banana::load($protocole);
159 }
160 Banana::$protocole = new $classname(Banana::$group);
161
162 // build the page
163 if ($pageclass == 'BananaPage') {
164 Banana::load('page');
165 }
166 Banana::$page = new $pageclass;
167 $types = array('multipart/report' => _b_('Rapport d\'erreur'),
168 'multipart/mixed' => _b_('Composition'),
169 'text/html' => _b_('Texte formaté'),
170 'text/plain' => _b_('Texte brut'),
171 'text/enriched' => _b_('Texte enrichi'),
172 'text' => _b_('Texte'),
173 'message/rfc822' => _b_('Mail'),
174 'message' => _b_('Message'),
175 'source' => _b_('Source'));
176 Banana::$mimeparts = array_merge($types, Banana::$mimeparts);
177 }
178
179 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
180 */
181 protected function loadParams()
182 {
183 Banana::$group = isset($this->params['group']) ? $this->params['group'] : null;
184 Banana::$artid = isset($this->params['artid']) ? $this->params['artid'] : null;
185 Banana::$first = isset($this->params['first']) ? $this->params['first'] : null;
186 Banana::$part = isset($this->params['part']) ? $this->params['part'] : 'text';
187
188 // Look for the action to execute
189 if (is_null(Banana::$group)) {
190 if (isset($this->params['action']) && $this->params['action'] == 'subscribe') {
191 Banana::$action = Banana::ACTION_BOX_SUBS;
192 } else {
193 Banana::$action = Banana::ACTION_BOX_LIST;
194 }
195 return;
196 }
197 $action = isset($this->params['action']) ? $this->params['action'] : null;
198 if (is_null(Banana::$artid)) {
199 if ($action == 'new') {
200 Banana::$action = Banana::ACTION_MSG_NEW;
201 } else {
202 Banana::$action = Banana::ACTION_MSG_LIST;
203 }
204 return;
205 }
206 switch ($action) {
207 case 'new':
208 Banana::$action = Banana::ACTION_MSG_NEW;
209 return;
210 case 'cancel':
211 Banana::$action = Banana::ACTION_MSG_CANCEL;
212 return;
213 case 'showext':
214 Banana::$action = Banana::ACTION_MSG_IMAGES;
215 return;
216 default:
217 Banana::$action = Banana::ACTION_MSG_READ;
218 }
219 }
220
221 /** Run Banana
222 * This function need user profile to be initialised
223 */
224 public function run()
225 {
226 // Configure locales
227 setlocale(LC_ALL, Banana::$profile['locale']);
228
229 // Check if the state is valid
230 if (Banana::$protocole->lastErrNo()) {
231 return Banana::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
232 . Banana::$protocole->lastError());
233 }
234 if (!Banana::$protocole->isValid()) {
235 return Banana::$page->kill(_b_('Connexion non-valide'));
236 }
237 if (Banana::$action & Banana::ACTION_BOX_NEEDED) {
238 if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', $group)) {
239 Banana::$page->setPage('group');
240 return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
241 }
242 }
243
244 // Dispatch to the action handlers
245 switch (Banana::$action) {
246 case Banana::ACTION_BOX_SUBS:
247 $error = $this->action_subscribe();
248 break;
249 case Banana::ACTION_BOX_LIST:
250 $error = $this->action_listBoxes();
251 break;
252 case Banana::ACTION_MSG_LIST:
253 $error = $this->action_showThread(Banana::$group, Banana::$first);
254 break;
255 case Banana::ACTION_MSG_IMAGES:
256 Banana::$msgshow_externalimages = true;
257 case Banana::ACTION_MSG_READ:
258 $error = $this->action_showMessage(Banana::$group, Banana::$artid, Banana::$part);
259 break;
260 case Banana::ACTION_MSG_NEW:
261 $error = $this->action_newMessage(Banana::$group, Banana::$artid);
262 break;
263 case Banana::ACTION_MSG_CANCEL:
264 $error = $this->action_cancelMessage(Banana::$group, Banana::$artid);
265 break;
266 default:
267 $error = _b_("L'action demandée n'est pas supportée par Banana");
268 }
269
270 // Generate the page
271 if (is_string($error)) {
272 return Banana::$page->kill($error);
273 }
274 return Banana::$page->run();
275 }
276
277 /** Return the CSS code to include in the headers
278 */
279 public function css()
280 {
281 return Banana::$page->css;
282 }
283
284 /**************************************************************************/
285 /* actions */
286 /**************************************************************************/
287 protected function action_saveSubs($groups)
288 {
289 Banana::$profile['subscribe'] = $groups;
290 return true;
291 }
292
293 protected function action_subscribe()
294 {
295 Banana::$page->setPage('subscribe');
296 if (isset($_POST['validsubs'])) {
297 $this->action_saveSubs(array_keys($_POST['subscribe']));
298 Banana::$page->redirect();
299 }
300 $groups = Banana::$protocole->getBoxList(Banana::BOXES_ALL);
301 Banana::$page->assign('groups', $groups);
302 return true;
303 }
304
305 protected function action_listBoxes()
306 {
307 Banana::$page->setPage('forums');
308 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
309 Banana::$page->assign('groups', $groups);
310 if (empty(Banana::$profile['subscribe']) || Banana::$profile['lastnews']) {
311 $newgroups = Banana::$protocole->getBoxList(Banana::BOXES_NEW, Banana::$profile['lastnews'], true);
312 Banana::$page->assign('newgroups', $newgroups);
313 }
314 return true;
315 }
316
317 protected function action_showThread($group, $first)
318 {
319 Banana::$page->setPage('thread');
320 if (!$this->loadSpool($group)) {
321 return _b_('Impossible charger la liste des messages de ') . $group;
322 }
323 if (Banana::$spool_boxlist) {
324 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
325 Banana::$page->assign('groups', $groups);
326 }
327 Banana::$page->assign('msgbypage', Banana::$spool_tmax);
328 return true;
329 }
330
331 protected function action_showMessage($group, $artid, $partid = 'text')
332 {
333 Banana::$page->setPage('message');
334 $istext = $partid == 'text' || $partid == 'source'
335 || preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
336 if ($istext) {
337 $this->loadSpool($group);
338 }
339 $msg =& $this->loadMessage($group, $artid);
340 if (is_null($msg)) {
341 $this->loadSpool($group);
342 $this->removeMessage($group, $artid);
343 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
344 }
345 if ($partid == 'xface') {
346 $msg->getXFace();
347 exit;
348 } elseif (!$istext) {
349 $part = $msg->getPartById($partid);
350 if (!is_null($part)) {
351 $part->send(true);
352 }
353 $part = $msg->getFile($partid);
354 if (!is_null($part)) {
355 $part->send();
356 }
357 exit;
358 } elseif ($partid == 'text') {
359 $partid = null;
360 Banana::$page->assign('body', $msg->getFormattedBody($partid));
361 } elseif ($partid == 'source') {
362 $text = Banana::$protocole->getMessageSource($artid);
363 if (!is_utf8($text)) {
364 $text = utf8_encode($text);
365 }
366 Banana::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
367 } else {
368 Banana::$page->assign('body', $msg->getFormattedBody($partid));
369 }
370
371 if (Banana::$profile['autoup']) {
372 Banana::$spool->markAsRead($artid);
373 }
374 if (Banana::$spool_boxlist) {
375 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
376 Banana::$page->assign('groups', $groups);
377 }
378 Banana::$page->assign_by_ref('message', $msg);
379 Banana::$page->assign('extimages', Banana::$msgshow_hasextimages);
380 Banana::$page->assign('headers', Banana::$msgshow_headers);
381 Banana::$page->assign('type', $partid);
382 return true;
383 }
384
385 protected function action_newMessage($group, $artid)
386 {
387 Banana::$page->setPage('new');
388 if (!Banana::$protocole->canSend()) {
389 return _b_('Vous n\'avez pas le droit de poster');
390 }
391 $hdrs = Banana::$protocole->requestedHeaders();
392 $headers = array();
393 foreach ($hdrs as $header) {
394 $headers[$header] = array('name' => BananaMessage::translateHeaderName($header));
395 if (isset(Banana::$profile['headers'][$header])) {
396 $headers[$header]['fixed'] = Banana::$profile['headers'][$header];
397 }
398 }
399 if (isset($_POST['sendmessage'])) {
400 $hdr_values = array();
401 foreach ($hdrs as $header) {
402 $hdr_values[$header] = isset($headers[$header]['fixed']) ? $headers[$header]['fixed'] : @$_POST[$header];
403 }
404 if ($artid) {
405 $old =& $this->loadMessage($group, $artid);
406 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
407 }
408 $msg = null;
409 if (empty($hdr_values['Subject'])) {
410 Banana::$page->trig(_b_('Le message doit avoir un sujet'));
411 } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment'])) {
412 $uploaded = $_FILES['attachment'];
413 if (!is_uploaded_file($uploaded['tmp_name'])) {
414 Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
415 } else {
416 $msg = BananaMessage::newMessage($hdr_values, $_POST['body'], $uploaded);
417 }
418 } else {
419 $msg = BananaMessage::newMessage($hdr_values, $_POST['body']);
420 }
421 if (!is_null($msg)) {
422 if (Banana::$protocole->send($msg)) {
423 Banana::$page->redirect(array('group' => $group, 'artid' => $artid));
424 }
425 Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
426 . Banana::$protocole->lastError());
427 }
428 } else {
429 if (!is_null($artid)) {
430 $msg =& $this->loadMessage($group, $artid);
431 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
432 $subject = $msg->getHeaderValue('subject');
433 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
434 $target = $msg->getHeaderValue($hdrs['reply']);
435 if (empty($target)) {
436 $target = $group;
437 }
438 $headers[$hdrs['dest']]['user'] =& $target;
439 } else {
440 $body = '';
441 $headers[$hdrs['dest']]['user'] = $group;
442 }
443 if (Banana::$profile['signature']) {
444 $body .= "\n\n-- \n" . Banana::$profile['signature'];
445 }
446 Banana::$page->assign('body', $body);
447 }
448
449 Banana::$page->assign('maxfilesize', Banana::$msgedit_maxfilesize);
450 Banana::$page->assign('can_attach', Banana::$msgedit_canattach);
451 Banana::$page->assign('headers', $headers);
452 return true;
453 }
454
455 protected function action_cancelMessage($group, $artid)
456 {
457 Banana::$page->setPage('cancel');
458 $msg =& $this->loadMessage($group, $artid);
459 if (!$msg->canCancel()) {
460 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
461 }
462 if (isset($_POST['cancel'])) {
463 $this->loadSpool($group);
464 $ndx = Banana::$spool->getNdX($id) - 1;
465 if (!Banana::$protocole->cancel($msg)) {
466 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
467 . Banana::$protocole->lastError();
468 }
469 if ($ndx < 50) {
470 $ndx = 0;
471 }
472 $this->removeMessage($group, $artid);
473 Banana::$page->redirect(Array('group' => $group, 'first' => $ndx));
474 }
475
476 Banana::$page->assign_by_ref('message', $msg);
477 Banana::$page->assign('body', $msg->getFormattedBody());
478 Banana::$page->assign('headers', Banana::$msgshow_headers);
479 return true;
480 }
481
482 /**************************************************************************/
483 /* Spoolgen functions */
484 /**************************************************************************/
485
486 private function checkErrors()
487 {
488 if (Banana::$protocole->lastErrno()) {
489 echo "\nL'erreur suivante s'est produite : "
490 . Banana::$protocole->lastErrno() . " "
491 . Banana::$protocole->lastError() . "\n";
492 return false;
493 }
494 return true;
495 }
496
497 static public function createAllSpool(array $protos)
498 {
499 foreach ($protos as $proto) {
500 $banana = new Banana(array(), $proto);
501
502 if (!$banana->checkErrors()) {
503 continue;
504 }
505 $groups = Banana::$protocole->getBoxList();
506 if (!$banana->checkErrors()) {
507 continue;
508 }
509
510 print "** $proto **\n";
511 foreach (array_keys($groups) as $g) {
512 print "Generating spool for $g : ";
513 Banana::$group = $g;
514 $spool = $banana->loadSpool($g);
515 if (!$banana->checkErrors()) {
516 break;
517 }
518 print "done.\n";
519 unset($spool);
520 }
521 print "\n";
522 }
523 }
524
525 /**************************************************************************/
526 /* Private functions */
527 /**************************************************************************/
528
529 protected function loadSpool($group)
530 {
531 Banana::load('spool');
532 if (!Banana::$spool || Banana::$spool->group != $group) {
533 $clean = false;
534 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
535 Banana::$spool = unserialize($_SESSION['banana_spool']);
536 $clean = @(Banana::$profile['lastnews'] != $_SESSION['banana_lastnews']);
537 } else {
538 unset($_SESSION['banana_message']);
539 unset($_SESSION['banana_artid']);
540 unset($_SESSION['banana_showhdr']);
541 }
542 BananaSpool::getSpool($group, Banana::$profile['lastnews'], Banana::$profile['autoup'] || $clean);
543 $_SESSION['banana_group'] = $group;
544 if (!Banana::$profile['display']) {
545 $_SESSION['banana_spool'] = serialize(Banana::$spool);
546 $_SESSION['banana_lastnews'] = Banana::$profile['lastnews'];
547 }
548 Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
549 }
550 return true;
551 }
552
553 protected function &loadMessage($group, $artid)
554 {
555 Banana::load('message');
556 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
557 && isset($_SESSION['banana_message'])) {
558 $message = unserialize($_SESSION['banana_message']);
559 Banana::$msgshow_headers = $_SESSION['banana_showhdr'];
560 } else {
561 $message = Banana::$protocole->getMessage($artid);
562 $_SESSION['banana_group'] = $group;
563 $_SESSION['banana_artid'] = $artid;
564 $_SESSION['banana_message'] = serialize($message);
565 $_SESSION['banana_showhdr'] = Banana::$msgshow_headers;
566 }
567 Banana::$message =& $message;
568 return $message;
569 }
570
571 protected function removeMessage($group, $artid)
572 {
573 Banana::$spool->delId($artid);
574 if ($group == $_SESSION['banana_group']) {
575 if (!Banana::$profile['display']) {
576 $_SESSION['banana_spool'] = serialize(Banana::$spool);
577 }
578 if ($artid == $_SESSION['banana_artid']) {
579 unset($_SESSION['banana_message']);
580 unset($_SESSION['banana_showhdr']);
581 unset($_SESSION['banana_artid']);
582 }
583 }
584 $this->loadSpool($group);
585 return true;
586 }
587
588 static private function load($file)
589 {
590 $file = strtolower($file) . '.inc.php';
591 if (!@include_once dirname(__FILE__) . "/$file") {
592 require_once $file;
593 }
594 }
595}
596
597// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
598?>