From 24cec3d821a65b3b4a116d41bf393bae35dcb7e4 Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Sun, 14 Jan 2007 18:28:31 +0000 Subject: [PATCH] Use Banana to preview the mails for ML moderation git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1333 839d8a87-29fc-0310-9880-83ba4fa771e5 --- include/banana/forum.inc.php | 155 ++++++++++++++++++++++++ include/banana/hooks.inc.php | 115 ++++++++++++++++++ include/banana/moderate.inc.php | 239 ++++++++++++++++++++++++++++++++++++++ modules/banana.php | 4 +- templates/lists/moderate_mail.tpl | 47 +------- 5 files changed, 512 insertions(+), 48 deletions(-) create mode 100644 include/banana/forum.inc.php create mode 100644 include/banana/hooks.inc.php create mode 100644 include/banana/moderate.inc.php diff --git a/include/banana/forum.inc.php b/include/banana/forum.inc.php new file mode 100644 index 0000000..ca845dd --- /dev/null +++ b/include/banana/forum.inc.php @@ -0,0 +1,155 @@ +baseurl . '/banana'; + if (isset($params['page'])) { + return $base . '/' . $params['page']; + } + if (@$params['action'] == 'subscribe') { + return $base . '/subscription'; + } + + if (!isset($params['group'])) { + return $base; + } + $base .= '/' . $params['group']; + + return $base . hook_platalMessageLink($params); +} + +class PlatalForums extends Banana +{ + function __construct($params = null) + { + global $globals; + Banana::$msgedit_canattach = false; + array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail'); + Banana::$nntp_host = 'news://web_'.S::v('forlife') + . ":{$globals->banana->password}@{$globals->banana->server}:{$globals->banana->port}/"; + parent::__construct($params); + } + + public function run() + { + global $platal, $globals; + + // Update last unread time + $time = null; + if (!is_null($this->params) && isset($this->params['updateall'])) { + $time = intval($this->params['updateall']); + $_SESSION['banana_last'] = $time; + } + + // Get user profile from SQL + $req = XDB::query("SELECT nom, mail, sig, + FIND_IN_SET('threads',flags), FIND_IN_SET('automaj',flags) + FROM {$globals->banana->table_prefix}profils + WHERE uid={?}", S::i('uid')); + if (!(list($nom,$mail,$sig,$disp,$maj) = $req->fetchOneRow())) { + $nom = S::v('prenom')." ".S::v('nom'); + $mail = S::v('forlife')."@polytechnique.org"; + $sig = $nom." (".S::v('promo').")"; + $disp = 0; + $maj = 1; + } + if ($maj) { + $time = time(); + } + + // Build user profile + $req = XDB::query(" + SELECT nom + FROM {$globals->banana->table_prefix}abos + LEFT JOIN {$globals->banana->table_prefix}list ON list.fid=abos.fid + WHERE uid={?}", S::i('uid')); + Banana::$profile['headers']['From'] = utf8_encode("$nom <$mail>"); + Banana::$profile['headers']['Organization'] = 'Utilisateur de Polytechnique.org'; + Banana::$profile['signature'] = utf8_encode($sig); + Banana::$profile['display'] = $disp; + Banana::$profile['autoup'] = $maj; + Banana::$profile['lastnews'] = S::v('banana_last'); + Banana::$profile['subscribe'] = $req->fetchColumn(); + + // Update the "unread limit" + if (!is_null($time)) { + XDB::execute("UPDATE auth_user_quick + SET banana_last = FROM_UNIXTIME({?}) + WHERE user_id={?}", + $time, S::i('uid')); + } + + // Register custom Banana links and tabs + if (!Banana::$profile['autoup']) { + Banana::$page->registerAction('path . '", "updateall", ' . time() . ')\'>' + . 'Marquer tous les messages comme lus' + . '', array('forums', 'thread', 'message')); + } + Banana::$page->registerPage('profile', utf8_encode('Préférences'), null); + + + // Run Banana + return parent::run(); + } + + protected function action_saveSubs($groups) + { + global $globals; + $uid = S::v('uid'); + + Banana::$profile['subscribe'] = array(); + XDB::execute("DELETE FROM {$globals->banana->table_prefix}abos WHERE uid={?}", $uid); + if (!count($groups)) { + return true; + } + + $req = XDB::iterRow("SELECT fid,nom FROM {$globals->banana->table_prefix}list"); + $fids = array(); + while (list($fid,$fnom) = $req->next()) { + $fids[$fnom] = $fid; + } + + $diff = array_diff($groups, array_keys($fids)); + foreach ($diff as $g) { + XDB::execute("INSERT INTO {$globals->banana->table_prefix}list (nom) VALUES ({?})", $g); + $fids[$g] = XDB::insertId(); + } + + foreach ($groups as $g) { + XDB::execute("INSERT INTO {$globals->banana->table_prefix}abos (fid,uid) VALUES ({?},{?})", + $fids[$g], $uid); + Banana::$profile['subscribe'][] = $g; + } + } +} + +?> diff --git a/include/banana/hooks.inc.php b/include/banana/hooks.inc.php new file mode 100644 index 0000000..6cc035c --- /dev/null +++ b/include/banana/hooks.inc.php @@ -0,0 +1,115 @@ +getHeaderValue('x-org-id')) { + return $address . ' ' + . ''; + } elseif ($id = Banana::$message->getHeaderValue('x-org-mail')) { + list($id, $domain) = explode('@', $id); + return $address . ' ' + . ''; + } else { + return $address; + } + } + if (!empty($text)) { + $text .= ', '; + } + $text .= $address; + } + return $text; + + case 'subject': + $link = null; + $text = stripslashes($_text); + if (preg_match('/^(.+?)\s*\[=> (.*?)\]\s*$/u', $text, $matches)) { + $text = $matches[1]; + $group = $matches[2]; + if (Banana::$group == $group) { + $link = ' [=> ' . $group . ']'; + } else { + $link = ' [=> ' . Banana::$page->makeLink(array('group' => $group, 'text' => $group)) . ']'; + } + } + $text = banana_catchFormats(banana_htmlentities($text)); + if ($in_spool) { + return array($text, $link); + } + return $text . $link; + } + return null; +} + +function hook_platalMessageLink($params) +{ + $base = ''; + if (isset($params['first'])) { + return $base . '/from/' . $params['first']; + } + if (isset($params['artid'])) { + if (@$params['part'] == 'xface') { + $base .= '/xface'; + } elseif (@$params['action'] == 'new') { + $base .= '/reply'; + } elseif (@$params['action'] == 'cancel') { + $base .= '/cancel'; + } else { + $base .= '/read'; + } + if (isset($params['part']) && $params['part'] != 'xface') { + return $base . '/' . $params['artid'] . '?part=' . urlencode($params['part']); + } else { + return $base . '/' . $params['artid']; + } + } + + if (@$params['action'] == 'new') { + return $base . '/new'; + } + return $base; +} + +function hook_makeImg($img, $alt, $height, $width) +{ + $url = 'images/banana/' . $img; + + if (!is_null($width)) { + $width = ' width="' . $width . '"'; + } + if (!is_null($height)) { + $height = ' height="' . $height . '"'; + } + + return '' . $alt . ''; +} + +?> diff --git a/include/banana/moderate.inc.php b/include/banana/moderate.inc.php new file mode 100644 index 0000000..0faf662 --- /dev/null +++ b/include/banana/moderate.inc.php @@ -0,0 +1,239 @@ +baseurl . '/' . $platal->ns . '/lists/moderate/' . ModerationBanana::$listname . '?'; + $get = ''; + foreach ($params as $key=>$value) { + if ($key == 'artid') { + $key = 'mid'; + } + if ($key == 'group') { + continue; + } + if ($key == 'action') { + continue; + } + if (!empty($get)) { + $get .= '&'; + } + $get .= $key . '=' . $value; + } + return $base . $get; +} + +class ModerationBanana extends Banana +{ + static public $listname; + static public $domain; + static public $client; + + function __construct($params = null, &$client) + { + global $globals; + ModerationBanana::$client = $client; + ModerationBanana::$listname = $params['listname']; + ModerationBanana::$domain = isset($params['domain']) ? $params['domain'] : $globals->mail->domain; + $params['group'] = ModerationBanana::$listname . '@' . ModerationBanana::$domain; + Banana::$spool_boxlist = false; + Banana::$msgshow_withthread = false; + Banana::$withtabs = false; + array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail'); + parent::__construct($params, 'MLInterface', 'ModerationPage'); + } +} + +require_once('banana/page.inc.php'); + +class ModerationPage extends BananaPage +{ + protected function prepare() + { + $this->killPage('subscribe'); + $this->killPage('forums'); + $this->assign('noactions', true); + return parent::prepare(); + } + + public function trig($msg) + { + global $page; + if ($page) { + $page->trig(utf8_decode($msg)); + } + return true; + } +} + +require_once('banana/protocoleinterface.inc.php'); +require_once('banana/message.inc.php'); + +class BananaMLInterface implements BananaProtocoleInterface +{ + private $infos; //(list, addr, host, desc, info, diff, ins, priv, sub, own, nbsub) + private $helds; //(id, sender, size, subj, date) + + public function __construct() + { + $this->infos = ModerationBanana::$client->get_members(ModerationBanana::$listname); + $this->infos = $this->infos[0]; + + $mods = ModerationBanana::$client->get_pending_ops(ModerationBanana::$listname); + $this->helds = $mods[1]; + } + + public function isValid() + { + return !is_null(ModerationBanana::$client); + } + + public function lastErrNo() + { + return 0; + } + + public function lastError() + { + return null; + } + + public function getDescription() + { + return $this->infos['desc']; + } + + public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false) + { + return array(Banana::$group => Array( + 'desc' => $this->infos['desc'], + 'msgnum' => count($this->helds), + 'unread' => count($this->helds))); + } + + public function &getMessage($id) + { + $message = null; + $msg = ModerationBanana::$client->get_pending_mail(ModerationBanana::$listname, $id, 1); + if ($msg) { + $message = new BananaMessage($msg); + } + return $message; + } + + public function getMessageSource($id) + { + return ModerationBanana::$client->get_pending_mail(ModerationBanana::$listname, $id, 1); + } + + public function getIndexes() + { + $ids = array(); + foreach ($this->helds as &$desc) { + $ids[] = intval($desc['id']); + } + sort($ids); + return array(count($ids), min($ids), max($ids)); + } + + public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array()) + { + $conv = array('from' => 'sender', 'subject' => 'subj', 'date' => 'stamp', 'message-id' => 'id'); + $headers = array(); + foreach ($msg_headers as $hdr) { + $hdr = strtolower($hdr); + $mlhdr = isset($conv[$hdr]) ? $conv[$hdr] : $hdr; + foreach ($this->helds as &$desc) { + $id = intval($desc['id']); + if (!isset($headers[$id])) { + $headers[$id] = array(); + } + if ($mlhdr == 'id') { + $headers[$id][$hdr] = $desc['stamp'] . '$' . $desc['id'] . '@' . Banana::$group; + } else { + $headers[$id][$hdr] = isset($desc[$mlhdr]) ? banana_html_entity_decode($desc[$mlhdr]) : null; + } + } + } + return $headers; + } + + public function updateSpool(array &$messages) { } + + public function getNewIndexes($since) + { + $ids = array(); + foreach ($this->helds as &$desc) { + if ($desc['stamp'] > $since) { + $ids[] = intval($desc['id']); + } + } + sort($ids); + return $ids; + } + + public function canSend() + { + return false; + } + + public function canCancel() + { + return false; + } + + public function requestedHeaders() + { + return array(); + } + + public function send(BananaMessage &$message) + { + return true; + } + + public function cancel(BananaMessage &$message) + { + return true; + } + + public function name() + { + return 'MLModeration'; + } + + public function filename() + { + return ModerationBanana::$domain . '_' . ModerationBanana::$listname; + } +} + +// vim:set et sw=4 sts=4 ts=4: +?> diff --git a/modules/banana.php b/modules/banana.php index b7cbfbd..9a1ebca 100644 --- a/modules/banana.php +++ b/modules/banana.php @@ -149,9 +149,9 @@ class BananaModule extends PLModule $page->addCssLink('banana.css'); $page->assign('xorg_title','Polytechnique.org - Forums & PA'); - require_once dirname(__FILE__).'/banana/banana.inc.php'; + require_once 'banana/forum.inc.php'; - $banana = new PlatalBanana($params); + $banana = new PlatalForums($params); $res = $banana->run(); $page->assign_by_ref('banana', $banana); $page->assign('banana_res', $res); diff --git a/templates/lists/moderate_mail.tpl b/templates/lists/moderate_mail.tpl index 4cded5d..5604eb3 100644 --- a/templates/lists/moderate_mail.tpl +++ b/templates/lists/moderate_mail.tpl @@ -23,55 +23,10 @@ {include file="lists/header_listes.tpl"}

- Propriétés du mail en attente -

- - - - - - - - - - - - - - - - - - -
émetteur{mailto address=$mail.sender}
sujet{$mail.subj|hdc}
taille{$mail.size} octets
date{$mail.stamp|date_format:"%X le %x"}
- -

Contenu du mail en attente

-{if $mail.parts_plain|@count} - - {foreach from=$mail.parts_plain item=part key=i} - - - - - {/foreach} -
Partie n°{$i}
{$part|qpd|nl2br}
-
-{/if} - -{if $mail.parts_html|@count} - - {foreach from=$mail.parts_html item=part key=i} - - - - - {/foreach} -
Partie n°{$i} (Le texte original est formaté en HTML)
{$part|qpd|clean_html|nl2br}
-
-{/if} +{$mail|smarty:nodefaults}
-- 2.1.4