From 7972645bb364b21a1b23d8818bb1ca6f45d36d93 Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Wed, 10 Jan 2007 22:01:00 +0000 Subject: [PATCH] Code cleaning and bug fixes git-svn-id: svn+ssh://murphy/home/svn/banana/trunk@150 9869982d-c50d-0410-be91-f2a2ec7c7c7b --- banana/banana.inc.php.in | 152 ++++++++++++++++++++++---------------- banana/mbox.inc.php | 18 +++-- banana/message.func.inc.php | 12 +-- banana/message.inc.php | 8 +- banana/mimepart.inc.php | 2 +- banana/nntp.inc.php | 40 +++++----- banana/nntpcore.inc.php | 2 +- banana/protocoleinterface.inc.php | 5 ++ banana/spool.inc.php | 64 +++++++++++----- 9 files changed, 180 insertions(+), 123 deletions(-) diff --git a/banana/banana.inc.php.in b/banana/banana.inc.php.in index c564a0c..f7ae502 100644 --- a/banana/banana.inc.php.in +++ b/banana/banana.inc.php.in @@ -9,41 +9,41 @@ class Banana { - static public $maxspool = 3000; - static public $parse_hdr = array('content-disposition', 'content-transfer-encoding', +####### +# Configuration variables +####### + +### General ### + static public $profile = Array( 'signature' => '', + 'headers' => array('From' => 'Anonymous '), + 'display' => 0, + 'lastnews' => 0, + 'locale' => 'fr_FR', + 'subscribe' => array(), + 'autoup' => 1); + static public $boxpattern; + +### Spool ### + static public $spool_max = 3000; + static public $spool_tbefore = 5; + static public $spool_tafter = 5; + static public $spool_tmax = 50; + +### Message processing ### + static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding', 'content-type', 'content-id', 'date', 'followup-to', 'from', 'message-id', 'newsgroups', 'organization', 'references', 'subject', 'x-face', 'in-reply-to', 'to', 'cc', 'reply-to'); - static public $show_hdr = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to', - 'organization', 'date', 'references', 'in-reply-to'); - - /** Favorites MIMEtypes to use, by order for reading multipart messages - */ - static public $body_mime = array('text/html', 'text/plain', 'text/enriched', 'text', 'message'); - - /** Indicate wether posting attachment is allowed - */ - static public $can_attach = true; - /** Maximum allowed file size for attachment - */ - static public $maxfilesize = 100000; - /** Indicate wether x-face should be skinned as specials data or not - */ - static public $formatxface = true; - - /** Regexp for selecting newsgroups to show (if empty, match all newsgroups) - * ex : '^xorg\..*' for xorg.* - */ - static public $grp_pattern = null; - static public $tbefore = 5; - static public $tafter = 5; - static public $tmax = 50; +### Message display ### + static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to', + 'organization', 'date', 'references', 'in-reply-to'); + static public $msgshow_mimeparts = array('text/html', 'text/plain', 'text/enriched', 'text', 'message'); + static public $msgshow_xface = true; + static public $msgshow_wrap = 78; - static public $wrap = 78; - /** Match an url * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/i") * If it matches, return 3 main parts : @@ -55,37 +55,31 @@ class Banana * $matches[2] = "http://www.polytechnique.org" * $matches[3] = "]" */ - static public $url_regexp = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?'; + static public $msgshow_url = '(["\[])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]])?'; +### Message edition ### + static public $msgedit_canattach = true; + static public $msgedit_maxfilesize = 100000; /** Global headers to use for messages */ - static public $custom_hdr = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@'); - + static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@'); + +### Protocole ### /** News serveur to use */ - static public $host = 'news://localhost:119/'; - - /** User profile - */ - static public $profile = Array( 'From' => 'Anonymous ', 'sig' => '', - 'Organization' => '', 'custom_hdr' => array(), 'display' => 0, - 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array()); + static public $nntp_host = 'news://localhost:119/'; - static public $protocole = null; - static public $spool = null; - static public $message = null; - static public $page = null; - - static public $group = null; - static public $artid = null; - static public $action = null; - static public $part = null; - static public $first = null; + static public $mbox_path = '/var/mail'; +### Debug ### static public $debug_nntp = false; static public $debug_smarty = false; +####### +# Constants +####### + // Actions const ACTION_BOX_NEEDED = 1; // mask const ACTION_BOX_LIST = 2; @@ -104,10 +98,31 @@ class Banana const SPOOL_ALL = 0; const SPOOL_UNREAD = 1; + +####### +# Runtime variables +####### + + static public $protocole = null; + static public $spool = null; + static public $message = null; + static public $page = null; + + static public $group = null; + static public $artid = null; + static public $action = null; + static public $part = null; + static public $first = null; + /** Class parameters storage */ public $params; + +####### +# Banana Implementation +####### + /** Build the instance of Banana * This constructor only call \ref loadParams, connect to the server, and build the Smarty page * @param protocole Protocole to use @@ -123,8 +138,10 @@ class Banana $this->loadParams(); // connect to protocole handler - Banana::load($protocole); $classname = 'Banana' . $protocole; + if (!class_exists($classname)) { + Banana::load($protocole); + } Banana::$protocole = new $classname(Banana::$group); // build the page @@ -190,7 +207,7 @@ class Banana return Banana::$page->kill(_b_('Connexion non-valide')); } if (Banana::$action & Banana::ACTION_BOX_NEEDED) { - if(isset(Banana::$grp_pattern) && !preg_match('/' . Banana::$grp_pattern . '/', $group)) { + if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', $group)) { Banana::$page->setPage('group'); return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder")); } @@ -265,7 +282,7 @@ class Banana return _b_('Impossible charger la liste des messages de ') . $group; } $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true); - Banana::$page->assign('msgbypage', Banana::$tmax); + Banana::$page->assign('msgbypage', Banana::$spool_tmax); Banana::$page->assign('groups', $groups); return true; } @@ -296,10 +313,13 @@ class Banana } exit; } + if (Banana::$profile['autoup']) { + Banana::$spool->markAsRead($artid); + } $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true); Banana::$page->assign('groups', $groups); Banana::$page->assign_by_ref('message', $msg); - Banana::$page->assign('headers', Banana::$show_hdr); + Banana::$page->assign('headers', Banana::$msgshow_headers); return true; } @@ -313,8 +333,8 @@ class Banana $headers = array(); foreach ($hdrs as $header) { $headers[$header] = array('name' => BananaMessage::translateHeaderName($header)); - if (isset(Banana::$profile[$header])) { - $headers[$header]['fixed'] = Banana::$profile[$header]; + if (isset(Banana::$profile['headers'][$header])) { + $headers[$header]['fixed'] = Banana::$profile['headers'][$header]; } } if (isset($_POST['sendmessage'])) { @@ -329,7 +349,7 @@ class Banana $msg = null; if (empty($hdr_values['Subject'])) { Banana::$page->trig(_b_('Le message doit avoir un sujet')); - } elseif (Banana::$can_attach && isset($_FILES['attachment'])) { + } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment'])) { $uploaded = $_FILES['attachment']; if (!is_uploaded_file($uploaded['tmp_name'])) { Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier')); @@ -361,14 +381,14 @@ class Banana $body = ''; $headers[$hdrs['dest']]['user'] = $group; } - if (Banana::$profile['sig']) { - $body .= "\n\n-- \n" . Banana::$profile['sig']; + if (Banana::$profile['signature']) { + $body .= "\n\n-- \n" . Banana::$profile['signature']; } Banana::$page->assign('body', $body); } - Banana::$page->assign('maxfilesize', Banana::$maxfilesize); - Banana::$page->assign('can_attach', Banana::$can_attach); + Banana::$page->assign('maxfilesize', Banana::$msgedit_maxfilesize); + Banana::$page->assign('can_attach', Banana::$msgedit_canattach); Banana::$page->assign('headers', $headers); return true; } @@ -407,10 +427,12 @@ class Banana if (!Banana::$spool || Banana::$spool->group != $group) { if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) { Banana::$spool = unserialize($_SESSION['banana_spool']); - } + } BananaSpool::getSpool($group, Banana::$profile['lastnews']); $_SESSION['banana_group'] = $group; - $_SESSION['banana_spool'] = serialize(Banana::$spool); + if (!Banana::$profile['display']) { + $_SESSION['banana_spool'] = serialize(Banana::$spool); + } Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL); } return true; @@ -422,13 +444,13 @@ class Banana if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid'] && isset($_SESSION['banana_message'])) { $message = unserialize($_SESSION['banana_message']); - Banana::$show_hdr = $_SESSION['banana_showhdr']; + Banana::$msgshow_headers = $_SESSION['banana_showhdr']; } else { $message = Banana::$protocole->getMessage($artid); $_SESSION['banana_group'] = $group; $_SESSION['banana_artid'] = $artid; $_SESSION['banana_message'] = serialize($message); - $_SESSION['banana_showhdr'] = Banana::$show_hdr; + $_SESSION['banana_showhdr'] = Banana::$msgshow_headers; } Banana::$message =& $message; return $message; @@ -438,7 +460,9 @@ class Banana { Banana::$spool->delId($artid); if ($group == $_SESSION['banana_group']) { - $_SESSION['banana_spool'] = serialize(Banana::$spool); + if (!Banana::$profile['display']) { + $_SESSION['banana_spool'] = serialize(Banana::$spool); + } if ($artid == $_SESSION['banana_artid']) { unset($_SESSION['banana_message']); unset($_SESSION['banana_showhdr']); diff --git a/banana/mbox.inc.php b/banana/mbox.inc.php index c2f7d4a..4cfa382 100644 --- a/banana/mbox.inc.php +++ b/banana/mbox.inc.php @@ -235,6 +235,18 @@ class BananaMBox implements BananaProtocoleInterface return 'MBOX'; } + /** Return the spool filename + */ + public function filename() + { + @list($mail, $domain) = explode('@', Banana::$group); + $file = ""; + if (isset($domain)) { + $file = $domain . '_'; + } + return $file . $mail; + } + ####### # Filesystem functions ####### @@ -245,11 +257,7 @@ class BananaMBox implements BananaProtocoleInterface return null; } @list($mail, $domain) = explode('@', $box); - if ($mail == 'staff') { - return '/home/x2003bruneau/staff.polytechnique.org_innovation.mbox'; - } else { - return '/var/mail/' . $mail; - } + return Banana::$mbox_path . '/' . $mail; } ####### diff --git a/banana/message.func.inc.php b/banana/message.func.inc.php index 04ad7cb..7f33856 100644 --- a/banana/message.func.inc.php +++ b/banana/message.func.inc.php @@ -73,11 +73,11 @@ function banana_unflowed($text) function banana_wordwrap($text, $quote_level) { if ($quote_level > 0) { - $length = Banana::$wrap - $quote_level - 1; + $length = Banana::$msgshow_wrap - $quote_level - 1; return banana_quote(wordwrap($text, $length), $quote_level); } - return wordwrap($text, Banana::$wrap); + return wordwrap($text, Banana::$msgshow_wrap); } function banana_catchFormats($text) @@ -85,7 +85,7 @@ function banana_catchFormats($text) $formatting = Array('/' => 'em', // match / first in order not to match closing markups <> '_' => 'u', '*' => 'strong'); - $url = Banana::$url_regexp; + $url = Banana::$msgshow_url; preg_match_all("/$url/i", $text, $urls); $text = str_replace($urls[0], "&&&urls&&&", $text); foreach ($formatting as $limit=>$mark) { @@ -101,8 +101,8 @@ function banana_catchFormats($text) function banana__cutlink($link) { $link = banana_html_entity_decode($link, ENT_QUOTES); - if (strlen($link) > Banana::$wrap) { - $link = substr($link, 0, Banana::$wrap - 3) . "..."; + if (strlen($link) > Banana::$msgshow_wrap) { + $link = substr($link, 0, Banana::$msgshow_wrap - 3) . "..."; } return banana_htmlentities($link, ENT_QUOTES); } @@ -133,7 +133,7 @@ function banana__catchMailLink($email) function banana_catchURLs($text) { - $url = Banana::$url_regexp; + $url = Banana::$msgshow_url; $res = preg_replace("/&(lt|gt|quot);/", " &\\1; ", $text); $res = preg_replace("/$url/ie", "'\\1'.banana__cleanurl('\\2').'\\3'", $res); diff --git a/banana/message.inc.php b/banana/message.inc.php index c83d17b..87485bc 100644 --- a/banana/message.inc.php +++ b/banana/message.inc.php @@ -22,7 +22,7 @@ final class BananaMessage extends BananaMimePart if (isset($this->headers['in-reply-to']) && isset($this->headers['references'])) { unset($this->headers['in-reply-to']); } - Banana::$show_hdr = array_intersect(Banana::$show_hdr, array_keys($this->headers)); + Banana::$msgshow_headers = array_intersect(Banana::$msgshow_headers, array_keys($this->headers)); Banana::$message =& $this; } } @@ -144,7 +144,7 @@ final class BananaMessage extends BananaMimePart public function getHeaders() { - $this->msg_headers = array_merge($this->msg_headers, Banana::$custom_hdr, Banana::$profile['custom_hdr']); + $this->msg_headers = array_merge($this->msg_headers, Banana::$msgedit_headers, Banana::$profile['headers']); $headers = array_map(array($this, 'encodeHeader'), $this->msg_headers); return array_merge($headers, parent::getHeaders()); } @@ -218,7 +218,7 @@ final class BananaMessage extends BananaMimePart public function hasXFace() { - return Banana::$formatxface && isset($this->headers['x-face']); + return Banana::$msgshow_xface && isset($this->headers['x-face']); } public function getXFace() @@ -233,7 +233,7 @@ final class BananaMessage extends BananaMimePart public function getFormattedBody($type = null) { - $types = Banana::$body_mime; + $types = Banana::$msgshow_mimeparts; if (!is_null($type)) { array_unshift($types, $type); } diff --git a/banana/mimepart.inc.php b/banana/mimepart.inc.php index c486add..d90a22b 100644 --- a/banana/mimepart.inc.php +++ b/banana/mimepart.inc.php @@ -277,7 +277,7 @@ class BananaMimePart if (preg_match("/:[ \t\r]*/", $line)) { list($hdr, $val) = split(":[ \t\r]*", $line, 2); $hdr = strtolower($hdr); - if (in_array($hdr, Banana::$parse_hdr)) { + if (in_array($hdr, Banana::$msgparse_headers)) { $headers[$hdr] = $val; } else { unset($hdr); diff --git a/banana/nntp.inc.php b/banana/nntp.inc.php index 51c5716..1c5ce40 100644 --- a/banana/nntp.inc.php +++ b/banana/nntp.inc.php @@ -24,7 +24,7 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface */ public function __construct() { - $url = parse_url(Banana::$host); + $url = parse_url(Banana::$nntp_host); if ($url['scheme'] == 'nntps' || $url['scheme'] == 'snntp') { $url['host'] = 'ssl://' . $url['host']; } @@ -205,7 +205,7 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface 'Newsgroups' => Banana::$group, 'Subject' => 'cmsg ' . $message->getHeaderValue('message-id'), 'Control' => 'cancel ' . $message->getHeaderValue('message-id')); - $headers = array_merge($headers, Banana::$custom_hdr); + $headers = array_merge($headers, Banana::$msgedit_headers); $body = 'Message canceled with Banana'; $msg = BananaMessage::newMessage($headers, $body); return $this->send($msg); @@ -217,29 +217,23 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface { return 'NNTP'; } -} -/* -require_once dirname(__FILE__) . '/spool.inc.php'; -$time = microtime(true); -$nntp = new BananaNNTP('xorg.promo.x2002'); -if (!$nntp->isValid()) { - echo "Beuh !\n"; - exit; + /** Return the filename for the spool + */ + public function filename() + { + $url = parse_url(Banana::$nntp_host); + $file = ''; + if (isset($url['host'])) { + $file .= $url['host'] . '_'; + } + if (isset($url['port'])) { + $file .= $url['port'] . '_'; + } + $file .= Banana::$group; + return $file; + } } -Banana::$protocole =& $nntp; -Banana::$spool =& BananaSpool::getSpool('xorg.promo.x2002'); -$msg = $nntp->getMessage(3424); -echo ' - - -
'; -//echo $msg->getFormattedBody('plain'); -echo $msg->getFormattedBody(); -echo '
', "\n"; -$end = microtime(true); -echo ($end - $time) . "s\n"; -*/ // vim:set et sw=4 sts=4 ts=4: ?> diff --git a/banana/nntpcore.inc.php b/banana/nntpcore.inc.php index 1ab299e..071ae5f 100644 --- a/banana/nntpcore.inc.php +++ b/banana/nntpcore.inc.php @@ -334,7 +334,7 @@ class BananaNNTPCore $groups = array(); foreach ($list as $result) { list($group, $last, $first, $p) = explode(' ', $result, 4); - if (!is_null(Banana::$grp_pattern) || preg_match('@' .Banana::$grp_pattern . '@', $group)) { + if (!is_null(Banana::$boxpattern) || preg_match('@' . Banana::$boxpattern . '@i', $group)) { $groups[$group] = array(intval($last), intval($first), $p); } } diff --git a/banana/protocoleinterface.inc.php b/banana/protocoleinterface.inc.php index bc3e1db..0029c9a 100644 --- a/banana/protocoleinterface.inc.php +++ b/banana/protocoleinterface.inc.php @@ -96,6 +96,11 @@ interface BananaProtocoleInterface /** Return the protocole name */ public function name(); + + /** Return the spool filename to use for the given box + * @param box STRING boxname + */ + public function filename(); } // vim:set et sw=4 sts=4 ts=4: diff --git a/banana/spool.inc.php b/banana/spool.inc.php index 9514d6a..8bb5537 100644 --- a/banana/spool.inc.php +++ b/banana/spool.inc.php @@ -61,6 +61,7 @@ class BananaSpoolHead class BananaSpool { private $version; + private $mode; /** group name */ public $group; @@ -80,6 +81,7 @@ class BananaSpool protected function __construct($group) { $this->version = BANANA_SPOOL_VERSION; + $this->mode = Banana::SPOOL_ALL; $this->group = $group; } @@ -106,15 +108,7 @@ class BananaSpool if (!is_dir($file)) { mkdir($file); } - $url = parse_url(Banana::$host); - if (isset($url['host'])) { - $file .= $url['host'] . '_'; - } - if (isset($url['port'])) { - $file .= $url['port'] . '_'; - } - $file .= $group; - return $file; + return $file . Banana::$protocole->filename(); } private static function readFromFile($group) @@ -124,7 +118,7 @@ class BananaSpool return null; } $spool = unserialize(file_get_contents($file)); - if ($spool->version != BANANA_SPOOL_VERSION) { + if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { return null; } return $spool; @@ -147,7 +141,9 @@ class BananaSpool } } - file_put_contents($file, serialize($this)); + if ($this->mode == Banana::SPOOL_ALL) { + file_put_contents($file, serialize($this)); + } } private function build() @@ -161,8 +157,8 @@ class BananaSpool $threshold = (int)(log($threshold)/log(2)); $threshold = (2 ^ ($threshold + 1)) - 1; } - if (Banana::$maxspool && Banana::$maxspool < $msgnum) { - $first = $last - Banana::$maxspool; + if (Banana::$spool_max && Banana::$spool_max < $msgnum) { + $first = $last - Banana::$spool_max; if ($first < 0) { $first += $threshold; } @@ -182,8 +178,7 @@ class BananaSpool $mids = array_keys($this->overview); foreach ($mids as $id) { if (($first <= $last && ($id < $first || $id > $last)) - || ($first > $last && $id < $first && $id > $last)) - { + || ($first > $last && $id < $first && $id > $last)) { $this->delid($id, false); $do_save = true; } @@ -261,7 +256,6 @@ class BananaSpool $id = $this->ids[$mid]; if ($this->overview[$id]->isread) { $this->overview[$id]->isread = false; - $this->overview[$id]->descunread = 1; while (isset($id)) { $this->overview[$id]->descunread ++; $id = $this->overview[$id]->parent; @@ -272,6 +266,7 @@ class BananaSpool public function setMode($mode) { + $this->mode = $mode; switch ($mode) { case Banana::SPOOL_UNREAD: foreach ($this->roots as $k=>$i) { @@ -284,6 +279,37 @@ class BananaSpool } } + /** Mark the given id as read + * @param id MSGNUM of post + */ + public function markAsRead($id) + { + if (!$this->overview[$id]->isread) { + $this->overview[$id]->isread = true; + while (isset($id)) { + $this->overview[$id]->descunread--; + $id = $this->overview[$id]->parent; + } + } + } + + /** Mark all unread messages as read + */ + public function markAllAsRead(array &$array = null) + { + if (is_null($array)) { + $array =& $this->roots; + } + foreach ($array as $id) { + if (!$this->overview[$id]->isread) { + $this->markAsRead($id); + } + if ($this->overview[$id]->descunread) { + $this->markAllAsRead($this->overview[$id]->children); + } + } + } + /** kill post and childrens * @param $_id MSGNUM of post */ @@ -455,12 +481,12 @@ class BananaSpool if (!$overview) { $_first = $first; - $_last = $first + Banana::$tmax - 1; + $_last = $first + Banana::$spool_tmax - 1; $_ref = null; } else { $_ref = $this->getNdx($first); - $_last = $_ref + Banana::$tafter; - $_first = $_ref - Banana::$tbefore; + $_last = $_ref + Banana::$spool_tafter; + $_first = $_ref - Banana::$spool_tbefore; if ($_first < 0) { $_last -= $_first; } -- 2.1.4