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