Add documentation in page.inc.php
[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 static public $maxspool = 3000;
13
14 static public $parse_hdr = array('content-disposition', 'content-transfer-encoding',
15 'content-type', 'content-id', 'date', 'followup-to',
16 'from', 'message-id', 'newsgroups', 'organization',
17 'references', 'subject', 'x-face', 'in-reply-to',
18 'to', 'cc', 'reply-to');
19 static public $show_hdr = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
20 'organization', 'date', 'references', 'in-reply-to');
21
22 /** Favorites MIMEtypes to use, by order for reading multipart messages
23 */
24 static public $body_mime = array('text/html', 'text/plain', 'text/enriched', 'text', 'message');
25
26 /** Indicate wether posting attachment is allowed
27 */
28 static public $can_attach = true;
29 /** Maximum allowed file size for attachment
30 */
31 static public $maxfilesize = 100000;
32 /** Indicate wether x-face should be skinned as specials data or not
33 */
34 static public $formatxface = true;
35
36 /** Regexp for selecting newsgroups to show (if empty, match all newsgroups)
37 * ex : '^xorg\..*' for xorg.*
38 */
39 static public $grp_pattern = null;
40
41 static public $tbefore = 5;
42 static public $tafter = 5;
43 static public $tmax = 50;
44
45 static public $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 $url_regexp = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?';
59
60 /** Global headers to use for messages
61 */
62 static public $custom_hdr = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
63
64 /** News serveur to use
65 */
66 static public $host = 'news://localhost:119/';
67
68 /** User profile
69 */
70 static public $profile = Array( 'From' => 'Anonymous <anonymouse@example.com>', 'sig' => '',
71 'Organization' => '', 'custom_hdr' => array(), 'display' => 0,
72 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array());
73
74 static public $protocole = null;
75 static public $spool = null;
76 static public $message = null;
77 static public $page = null;
78
79 static public $group = null;
80 static public $artid = null;
81 static public $action = null;
82 static public $part = null;
83 static public $first = null;
84
85 static public $debug_nntp = false;
86 static public $debug_smarty = false;
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 /** Class parameters storage
108 */
109 public $params;
110
111 /** Build the instance of Banana
112 * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
113 * @param protocole Protocole to use
114 */
115 public function __construct($params = null, $protocole = 'NNTP', $pageclass = 'BananaPage')
116 {
117 Banana::load('text.func');
118 if (is_null($params)) {
119 $this->params = $_GET;
120 } else {
121 $this->params = $params;
122 }
123 $this->loadParams();
124
125 // connect to protocole handler
126 Banana::load($protocole);
127 $classname = 'Banana' . $protocole;
128 Banana::$protocole = new $classname(Banana::$group);
129
130 // build the page
131 if ($pageclass == 'BananaPage') {
132 Banana::load('page');
133 }
134 Banana::$page = new $pageclass;
135 }
136
137 /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
138 */
139 protected function loadParams()
140 {
141 Banana::$group = isset($this->params['group']) ? $this->params['group'] : null;
142 Banana::$artid = isset($this->params['artid']) ? $this->params['artid'] : null;
143 Banana::$first = isset($this->params['first']) ? $this->params['first'] : null;
144 Banana::$part = isset($this->params['part']) ? $this->params['part'] : 'text';
145
146 // Look for the action to execute
147 if (is_null(Banana::$group)) {
148 if (isset($this->params['action']) && $this->params['action'] == 'subscribe') {
149 Banana::$action = Banana::ACTION_BOX_SUBS;
150 } else {
151 Banana::$action = Banana::ACTION_BOX_LIST;
152 }
153 return;
154 }
155 $action = isset($this->params['action']) ? $this->params['action'] : null;
156 if (is_null(Banana::$artid)) {
157 if ($action == 'new') {
158 Banana::$action = Banana::ACTION_MSG_NEW;
159 } else {
160 Banana::$action = Banana::ACTION_MSG_LIST;
161 }
162 return;
163 }
164 switch ($action) {
165 case 'new':
166 Banana::$action = Banana::ACTION_MSG_NEW;
167 return;
168 case 'cancel':
169 Banana::$action = Banana::ACTION_MSG_CANCEL;
170 return;
171 default:
172 Banana::$action = Banana::ACTION_MSG_READ;
173 }
174 }
175
176 /** Run Banana
177 * This function need user profile to be initialised
178 */
179 public function run()
180 {
181 // Configure locales
182 setlocale(LC_ALL, Banana::$profile['locale']);
183
184 // Check if the state is valid
185 if (Banana::$protocole->lastErrNo()) {
186 return Banana::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
187 . Banana::$protocole->lastError());
188 }
189 if (!Banana::$protocole->isValid()) {
190 return Banana::$page->kill(_b_('Connexion non-valide'));
191 }
192 if (Banana::$action & Banana::ACTION_BOX_NEEDED) {
193 if(isset(Banana::$grp_pattern) && !preg_match('/' . Banana::$grp_pattern . '/', $group)) {
194 Banana::$page->setPage('group');
195 return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
196 }
197 }
198
199 // Dispatch to the action handlers
200 switch (Banana::$action) {
201 case Banana::ACTION_BOX_SUBS:
202 $error = $this->action_subscribe();
203 break;
204 case Banana::ACTION_BOX_LIST:
205 $error = $this->action_listBoxes();
206 break;
207 case Banana::ACTION_MSG_LIST:
208 $error = $this->action_showThread(Banana::$group, Banana::$first);
209 break;
210 case Banana::ACTION_MSG_READ:
211 $error = $this->action_showMessage(Banana::$group, Banana::$artid, Banana::$part);
212 break;
213 case Banana::ACTION_MSG_NEW:
214 $error = $this->action_newMessage(Banana::$group, Banana::$artid);
215 break;
216 case Banana::ACTION_MSG_CANCEL:
217 $error = $this->action_cancelMessage(Banana::$group, Banana::$artid);
218 break;
219 default:
220 $error = _b_("L'action demandée n'est pas supportée par Banana");
221 }
222
223 // Generate the page
224 if (is_string($error)) {
225 return Banana::$page->kill($error);
226 }
227 return Banana::$page->run();
228 }
229
230 /**************************************************************************/
231 /* actions */
232 /**************************************************************************/
233 protected function action_saveSubs($groups)
234 {
235 Banana::$profile['subscribe'] = $groups;
236 return true;
237 }
238
239 protected function action_subscribe()
240 {
241 Banana::$page->setPage('subscribe');
242 if (isset($_POST['validsubs'])) {
243 $this->action_saveSubs(array_keys($_POST['subscribe']));
244 Banana::$page->redirect();
245 }
246 $groups = Banana::$protocole->getBoxList(Banana::BOXES_ALL);
247 Banana::$page->assign('groups', $groups);
248 return true;
249 }
250
251 protected function action_listBoxes()
252 {
253 Banana::$page->setPage('forums');
254 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
255 $newgroups = Banana::$protocole->getBoxList(Banana::BOXES_NEW, Banana::$profile['lastnews'], true);
256 Banana::$page->assign('groups', $groups);
257 Banana::$page->assign('newgroups', $newgroups);
258 return true;
259 }
260
261 protected function action_showThread($group, $first)
262 {
263 Banana::$page->setPage('thread');
264 if (!$this->loadSpool($group)) {
265 return _b_('Impossible charger la liste des messages de ') . $group;
266 }
267 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
268 Banana::$page->assign('msgbypage', Banana::$tmax);
269 Banana::$page->assign('groups', $groups);
270 return true;
271 }
272
273 protected function action_showMessage($group, $artid, $partid = 'text')
274 {
275 Banana::$page->setPage('message');
276 if ($partid == 'text') {
277 $this->loadSpool($group);
278 }
279 $msg =& $this->loadMessage($group, $artid);
280 if (is_null($msg)) {
281 $this->loadSpool($group);
282 $this->removeMessage($group, $artid);
283 return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
284 }
285 if ($partid == 'xface') {
286 $msg->getXFace();
287 exit;
288 } elseif ($partid != 'text') {
289 $part = $msg->getPartById($partid);
290 if (!is_null($part)) {
291 $part->send(true);
292 }
293 $part = $msg->getFile($partid);
294 if (!is_null($part)) {
295 $part->send();
296 }
297 exit;
298 }
299 $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
300 Banana::$page->assign('groups', $groups);
301 Banana::$page->assign_by_ref('message', $msg);
302 Banana::$page->assign('headers', Banana::$show_hdr);
303 return true;
304 }
305
306 protected function action_newMessage($group, $artid)
307 {
308 Banana::$page->setPage('new');
309 if (!Banana::$protocole->canSend()) {
310 return _b_('Vous n\'avez pas le droit de poster');
311 }
312 $hdrs = Banana::$protocole->requestedHeaders();
313 $headers = array();
314 foreach ($hdrs as $header) {
315 $headers[$header] = array('name' => BananaMessage::translateHeaderName($header));
316 if (isset(Banana::$profile[$header])) {
317 $headers[$header]['fixed'] = Banana::$profile[$header];
318 }
319 }
320 if (isset($_POST['sendmessage'])) {
321 $hdr_values = array();
322 foreach ($hdrs as $header) {
323 $hdr_values[$header] = isset($headers[$header]['fixed']) ? $headers[$header]['fixed'] : @$_POST[$header];
324 }
325 if ($artid) {
326 $old =& $this->loadMessage($group, $artid);
327 $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
328 }
329 $msg = null;
330 if (empty($hdr_values['Subject'])) {
331 Banana::$page->trig(_b_('Le message doit avoir un sujet'));
332 } elseif (Banana::$can_attach && isset($_FILES['attachment'])) {
333 $uploaded = $_FILES['attachment'];
334 if (!is_uploaded_file($uploaded['tmp_name'])) {
335 Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
336 } else {
337 $msg = BananaMessage::newMessage($hdr_values, $_POST['body'], $uploaded);
338 }
339 } else {
340 $msg = BananaMessage::newMessage($hdr_values, $_POST['body']);
341 }
342 if (!is_null($msg)) {
343 if (Banana::$protocole->send($msg)) {
344 Banana::$page->redirect(array('group' => $group, 'artid' => $artid));
345 }
346 Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
347 . Banana::$protocole->lastError());
348 }
349 } else {
350 if (!is_null($artid)) {
351 $msg =& $this->loadMessage($group, $artid);
352 $body = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
353 $subject = $msg->getHeaderValue('subject');
354 $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
355 $target = $msg->getHeaderValue($hdrs['reply']);
356 if (empty($target)) {
357 $target = $group;
358 }
359 $headers[$hdrs['dest']]['user'] =& $target;
360 } else {
361 $body = '';
362 $headers[$hdrs['dest']]['user'] = $group;
363 }
364 if (Banana::$profile['sig']) {
365 $body .= "\n\n-- \n" . Banana::$profile['sig'];
366 }
367 Banana::$page->assign('body', $body);
368 }
369
370 Banana::$page->assign('maxfilesize', Banana::$maxfilesize);
371 Banana::$page->assign('can_attach', Banana::$can_attach);
372 Banana::$page->assign('headers', $headers);
373 return true;
374 }
375
376 protected function action_cancelMessage($group, $artid)
377 {
378 Banana::$page->setPage('cancel');
379 $msg =& $this->loadMessage($group, $artid);
380 if (!$msg->canCancel()) {
381 return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
382 }
383 if (isset($_POST['cancel'])) {
384 $this->loadSpool($group);
385 $ndx = Banana::$spool->getNdX($id) - 1;
386 if (!Banana::$protocole->cancel($msg)) {
387 return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
388 . Banana::$protocole->lastError();
389 }
390 if ($ndx < 50) {
391 $ndx = 0;
392 }
393 $this->removeMessage($group, $artid);
394 Banana::$page->redirect(Array('group' => $group, 'first' => $ndx));
395 }
396 Banana::$page->assign_by_ref('message', $msg);
397 return true;
398 }
399
400 /**************************************************************************/
401 /* Private functions */
402 /**************************************************************************/
403
404 protected function loadSpool($group)
405 {
406 Banana::load('spool');
407 if (!Banana::$spool || Banana::$spool->group != $group) {
408 if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
409 Banana::$spool = unserialize($_SESSION['banana_spool']);
410 }
411 BananaSpool::getSpool($group, Banana::$profile['lastnews']);
412 $_SESSION['banana_group'] = $group;
413 $_SESSION['banana_spool'] = serialize(Banana::$spool);
414 Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
415 }
416 return true;
417 }
418
419 protected function &loadMessage($group, $artid)
420 {
421 Banana::load('message');
422 if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
423 && isset($_SESSION['banana_message'])) {
424 $message = unserialize($_SESSION['banana_message']);
425 Banana::$show_hdr = $_SESSION['banana_showhdr'];
426 } else {
427 $message = Banana::$protocole->getMessage($artid);
428 $_SESSION['banana_group'] = $group;
429 $_SESSION['banana_artid'] = $artid;
430 $_SESSION['banana_message'] = serialize($message);
431 $_SESSION['banana_showhdr'] = Banana::$show_hdr;
432 }
433 Banana::$message =& $message;
434 return $message;
435 }
436
437 protected function removeMessage($group, $artid)
438 {
439 Banana::$spool->delId($artid);
440 if ($group == $_SESSION['banana_group']) {
441 $_SESSION['banana_spool'] = serialize(Banana::$spool);
442 if ($artid == $_SESSION['banana_artid']) {
443 unset($_SESSION['banana_message']);
444 unset($_SESSION['banana_showhdr']);
445 unset($_SESSION['banana_artid']);
446 }
447 }
448 return true;
449 }
450
451 static private function load($file)
452 {
453 $file = strtolower($file) . '.inc.php';
454 if (!@include_once dirname(__FILE__) . "/$file") {
455 require_once $file;
456 }
457 }
458 }
459
460 // vim:set et sw=4 sts=4 ts=4
461 ?>