Can choose to view the message sources
[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
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',
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');
39
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;
46
47 /** Match an url
48 * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/i")
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 */
58 static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
59
60 ### Message edition ###
61 static public $msgedit_canattach = true;
62 static public $msgedit_maxfilesize = 100000;
63 /** Global headers to use for messages
64 */
65 static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
66
67 ### Protocole ###
68 /** News serveur to use
69 */
70 static public $nntp_host = 'news://localhost:119/';
71
72 static public $mbox_path = '/var/mail';
73
74 ### Debug ###
75 static public $debug_nntp = false;
76 static public $debug_smarty = false;
77
78
79 #######
80 # Constants
81 #######
82
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
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
117 /** Class parameters storage
118 */
119 public $params;
120
121
122 #######
123 # Banana Implementation
124 #######
125
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
129 */
130 public function __construct($params = null, $protocole = 'NNTP', $pageclass = 'BananaPage')
131 {
132 Banana::load('text.func');
133 if (is_null($params)) {
134 $this->params = $_GET;
135 } else {
136 $this->params = $params;
137 }
138 $this->loadParams();
139
140 // connect to protocole handler
141 $classname = 'Banana' . $protocole;
142 if (!class_exists($classname)) {
143 Banana::load($protocole);
144 }
145 Banana::$protocole = new $classname(Banana::$group);
146
147 // build the page
148 if ($pageclass == 'BananaPage') {
149 Banana::load('page');
150 }
151 Banana::$page = new $pageclass;
152 }
153
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)) {
165 if (isset($this->params['action']) && $this->params['action'] == 'subscribe') {
166 Banana::$action = Banana::ACTION_BOX_SUBS;
167 } else {
168 Banana::$action = Banana::ACTION_BOX_LIST;
169 }
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;
178 }
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;
190 }
191 }
192
193 /** Run Banana
194 * This function need user profile to be initialised
195 */
196 public function run()
197 {
198 // Configure locales
199 setlocale(LC_ALL, Banana::$profile['locale']);
200
201 // Check if the state is valid
202 if (Banana::$protocole->lastErrNo()) {
203 return Banana::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
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) {
210 if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', $group)) {
211 Banana::$page->setPage('group');
212 return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
213 }
214 }
215
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:
237 $error = _b_("L'action demandée n'est pas supportée par Banana");
238 }
239
240 // Generate the page
241 if (is_string($error)) {
242 return Banana::$page->kill($error);
243 }
244 return Banana::$page->run();
245 }
246
247 /**************************************************************************/
248 /* actions */
249 /**************************************************************************/
250 protected function action_saveSubs($groups)
251 {
252 Banana::$profile['subscribe'] = $groups;
253 return true;
254 }
255
256 protected function action_subscribe()
257 {
258 Banana::$page->setPage('subscribe');
259 if (isset($_POST['validsubs'])) {
260 $this->action_saveSubs(array_keys($_POST['subscribe']));
261 Banana::$page->redirect();
262 }
263 $groups = Banana::$protocole->getBoxList(Banana::BOXES_ALL);
264 Banana::$page->assign('groups', $groups);
265 return true;
266 }
267
268 protected function action_listBoxes()
269 {
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;
276 }
277
278 protected function action_showThread($group, $first)
279 {
280 Banana::$page->setPage('thread');
281 if (!$this->loadSpool($group)) {
282 return _b_('Impossible charger la liste des messages de ') . $group;
283 }
284 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
285 Banana::$page->assign('msgbypage', Banana::$spool_tmax);
286 Banana::$page->assign('groups', $groups);
287 return true;
288 }
289
290 protected function action_showMessage($group, $artid, $partid = 'text')
291 {
292 Banana::$page->setPage('message');
293 $istext = $partid == 'text' || $partid == 'source'
294 || preg_match("/[-a-z0-9_]+\/[-a-z0-9_]+/", $partid);
295 if ($istext) {
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);
302 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
303 }
304 if ($partid == 'xface') {
305 $msg->getXFace();
306 exit;
307 } elseif (!$istext) {
308 $part = $msg->getPartById($partid);
309 if (!is_null($part)) {
310 $part->send(true);
311 }
312 $part = $msg->getFile($partid);
313 if (!is_null($part)) {
314 $part->send();
315 }
316 exit;
317 } elseif ($partid == 'text') {
318 Banana::$page->assign('body', $msg->getFormattedBody());
319 } elseif ($partid == 'source') {
320 Banana::$page->assign('body',
321 '<pre>' . banana_htmlentities(Banana::$protocole->getMessageSource($artid)) . '</pre>');
322 } else {
323 Banana::$page->assign('body', $msg->getFormattedBody($partid));
324 }
325
326 if (Banana::$profile['autoup']) {
327 Banana::$spool->markAsRead($artid);
328 }
329 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
330 Banana::$page->assign('groups', $groups);
331 Banana::$page->assign_by_ref('message', $msg);
332 Banana::$page->assign('headers', Banana::$msgshow_headers);
333 return true;
334 }
335
336 protected function action_newMessage($group, $artid)
337 {
338 Banana::$page->setPage('new');
339 if (!Banana::$protocole->canSend()) {
340 return _b_('Vous n\'avez pas le droit de poster');
341 }
342 $hdrs = Banana::$protocole->requestedHeaders();
343 $headers = array();
344 foreach ($hdrs as $header) {
345 $headers[$header] = array('name' => BananaMessage::translateHeaderName($header));
346 if (isset(Banana::$profile['headers'][$header])) {
347 $headers[$header]['fixed'] = Banana::$profile['headers'][$header];
348 }
349 }
350 if (isset($_POST['sendmessage'])) {
351 $hdr_values = array();
352 foreach ($hdrs as $header) {
353 $hdr_values[$header] = isset($headers[$header]['fixed']) ? $headers[$header]['fixed'] : @$_POST[$header];
354 }
355 if ($artid) {
356 $old =& $this->loadMessage($group, $artid);
357 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
358 }
359 $msg = null;
360 if (empty($hdr_values['Subject'])) {
361 Banana::$page->trig(_b_('Le message doit avoir un sujet'));
362 } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment'])) {
363 $uploaded = $_FILES['attachment'];
364 if (!is_uploaded_file($uploaded['tmp_name'])) {
365 Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
366 } else {
367 $msg = BananaMessage::newMessage($hdr_values, $_POST['body'], $uploaded);
368 }
369 } else {
370 $msg = BananaMessage::newMessage($hdr_values, $_POST['body']);
371 }
372 if (!is_null($msg)) {
373 if (Banana::$protocole->send($msg)) {
374 Banana::$page->redirect(array('group' => $group, 'artid' => $artid));
375 }
376 Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
377 . Banana::$protocole->lastError());
378 }
379 } else {
380 if (!is_null($artid)) {
381 $msg =& $this->loadMessage($group, $artid);
382 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
383 $subject = $msg->getHeaderValue('subject');
384 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
385 $target = $msg->getHeaderValue($hdrs['reply']);
386 if (empty($target)) {
387 $target = $group;
388 }
389 $headers[$hdrs['dest']]['user'] =& $target;
390 } else {
391 $body = '';
392 $headers[$hdrs['dest']]['user'] = $group;
393 }
394 if (Banana::$profile['signature']) {
395 $body .= "\n\n-- \n" . Banana::$profile['signature'];
396 }
397 Banana::$page->assign('body', $body);
398 }
399
400 Banana::$page->assign('maxfilesize', Banana::$msgedit_maxfilesize);
401 Banana::$page->assign('can_attach', Banana::$msgedit_canattach);
402 Banana::$page->assign('headers', $headers);
403 return true;
404 }
405
406 protected function action_cancelMessage($group, $artid)
407 {
408 Banana::$page->setPage('cancel');
409 $msg =& $this->loadMessage($group, $artid);
410 if (!$msg->canCancel()) {
411 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
412 }
413 if (isset($_POST['cancel'])) {
414 $this->loadSpool($group);
415 $ndx = Banana::$spool->getNdX($id) - 1;
416 if (!Banana::$protocole->cancel($msg)) {
417 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
418 . Banana::$protocole->lastError();
419 }
420 if ($ndx < 50) {
421 $ndx = 0;
422 }
423 $this->removeMessage($group, $artid);
424 Banana::$page->redirect(Array('group' => $group, 'first' => $ndx));
425 }
426 Banana::$page->assign_by_ref('message', $msg);
427 return true;
428 }
429
430 /**************************************************************************/
431 /* Private functions */
432 /**************************************************************************/
433
434 protected function loadSpool($group)
435 {
436 Banana::load('spool');
437 if (!Banana::$spool || Banana::$spool->group != $group) {
438 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
439 Banana::$spool = unserialize($_SESSION['banana_spool']);
440 }
441 BananaSpool::getSpool($group, Banana::$profile['lastnews']);
442 $_SESSION['banana_group'] = $group;
443 if (!Banana::$profile['display']) {
444 $_SESSION['banana_spool'] = serialize(Banana::$spool);
445 }
446 Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
447 }
448 return true;
449 }
450
451 protected function &loadMessage($group, $artid)
452 {
453 Banana::load('message');
454 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
455 && isset($_SESSION['banana_message'])) {
456 $message = unserialize($_SESSION['banana_message']);
457 Banana::$msgshow_headers = $_SESSION['banana_showhdr'];
458 } else {
459 $message = Banana::$protocole->getMessage($artid);
460 $_SESSION['banana_group'] = $group;
461 $_SESSION['banana_artid'] = $artid;
462 $_SESSION['banana_message'] = serialize($message);
463 $_SESSION['banana_showhdr'] = Banana::$msgshow_headers;
464 }
465 Banana::$message =& $message;
466 return $message;
467 }
468
469 protected function removeMessage($group, $artid)
470 {
471 Banana::$spool->delId($artid);
472 if ($group == $_SESSION['banana_group']) {
473 if (!Banana::$profile['display']) {
474 $_SESSION['banana_spool'] = serialize(Banana::$spool);
475 }
476 if ($artid == $_SESSION['banana_artid']) {
477 unset($_SESSION['banana_message']);
478 unset($_SESSION['banana_showhdr']);
479 unset($_SESSION['banana_artid']);
480 }
481 }
482 return true;
483 }
484
485 static private function load($file)
486 {
487 $file = strtolower($file) . '.inc.php';
488 if (!@include_once dirname(__FILE__) . "/$file") {
489 require_once $file;
490 }
491 }
492 }
493
494 // vim:set et sw=4 sts=4 ts=4
495 ?>