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