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