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