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