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