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