require_once("xorg.misc.inc.php");
-if (isset($page)) {
- $page->addCssLink('nl.css');
-}
-
define('FEMME', 1);
define('HOMME', 0);
// }}}
+// {{{ class MassMailer
+
+abstract class MassMailer
+{
+ private $_tpl;
+ private $_css;
+ private $_prefix;
+
+ public $_id;
+ public $_shortname;
+ public $_title;
+ public $_title_mail;
+
+ public $_head;
+
+ function __construct($tpl, $css, $prefix)
+ {
+ $this->_tpl = $tpl;
+ $this->_css = $css;
+ $this->_prefix = $prefix;
+ }
+
+ public function id()
+ {
+ return is_null($this->_shortname) ? $this->_id : $this->_shortname;
+ }
+
+ public function title($mail = false)
+ {
+ return $mail ? $this->_title_mail : $this->_title;
+ }
+
+ public function head($prenom = null, $nom = null, $sexe = null, $type = 'text')
+ {
+ if (is_null($prenom)) {
+ return $this->_head;
+ } else {
+ $head = $this->_head;
+ $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head);
+ $head = str_replace('<prenom>', $prenom, $head);
+ $head = str_replace('<nom>', $nom, $head);
+ if ($type == 'text') {
+ $head = enriched_to_text($head, false, true, 2, 64);
+ } else {
+ $head = enriched_to_text($head, true);
+ }
+ return $head;
+ }
+ }
+
+ public function css(&$page = null)
+ {
+ if (!is_null($page)) {
+ $page->addCssLink($this->_css);
+ return true;
+ } else {
+ $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->_css);
+ return preg_replace('@/\*.*?\*/@s', '', $css);
+ }
+ }
+
+ public function toText(&$page, $prenom, $nom, $sexe)
+ {
+ $this->css($page);
+ $page->assign('is_mail', false);
+ $page->assign('html_version', false);
+ $page->assign('prenom', $prenom);
+ $page->assign('nom', $nom);
+ $page->assign('sexe', $sexe);
+ $this->assignData($page);
+ }
+
+ public function toHtml(&$page, $prenom, $nom, $sexe)
+ {
+ $this->css($page);
+ $page->assign('prefix', $this->_prefix . '/' . $this->id());
+ $page->assign('is_mail', false);
+ $page->assign('html_version', true);
+ $page->assign('prenom', $prenom);
+ $page->assign('nom', $nom);
+ $page->assign('sexe', $sexe);
+ $this->assignData($page);
+ }
+
+ public function sendTo($prenom, $nom, $login, $sexe, $html)
+ {
+ global $globals;
+
+ $mailer = new PlMailer($this->_tpl);
+ $this->assignData($mailer);
+ $mailer->assign('is_mail', true);
+ $mailer->assign('prenom', $prenom);
+ $mailer->assign('nom', $nom);
+ $mailer->assign('sexe', $sexe);
+ $mailer->assign('prefix', null);
+ $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
+ $mailer->send($html);
+ }
+
+ public function sendToAll()
+ {
+ $this->setSent();
+ $query = "SELECT u.user_id, a.alias,
+ u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
+ FIND_IN_SET('femme', u.flags),
+ q.core_mail_fmt AS pref
+ FROM {$this->subscriptionTable()} AS ni
+ INNER JOIN auth_user_md5 AS u USING(user_id)
+ INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
+ INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
+ LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
+ WHERE ({$this->subscriptionWhere()}) AND e.email IS NOT NULL
+ GROUP BY u.user_id
+ LIMIT 60";
+ while (true) {
+ $res = XDB::iterRow($query);
+ if (!$res->total()) {
+ exit;
+ }
+ $sent = array();
+ while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt) = $res->next()) {
+ $sent[] = "user_id='$uid'";
+ $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html');
+ }
+ XDB::execute("UPDATE {$this->subscriptionTable()}
+ SET {$this->subscriptionUpdate()}
+ WHERE " . implode(' OR ', $sent));
+ sleep(60);
+ }
+ }
+
+ abstract protected function assignData(&$smarty);
+ abstract protected function setSent();
+ abstract static public function subscribe($uid = -1);
+ abstract static public function unsubscribe($uid = -1);
+ abstract static public function subscriptionState($uid = -1);
+
+ abstract protected function subscriptionTable();
+ abstract protected function subscriptionWhere();
+ abstract protected function subscriptionUpdate();
+}
+
+// }}}
// {{{ class NewsLetter
-class NewsLetter
+class NewsLetter extends MassMailer
{
- // {{{ properties
-
- var $_id;
- var $_shortname;
- var $_date;
- var $_title;
- var $_title_mail;
- var $_head;
- var $_cats = Array();
- var $_arts = Array();
+ public $_date;
+ public $_cats = Array();
+ public $_arts = Array();
- // }}}
- // {{{ constructor
-
- function NewsLetter($id=null)
+ function __construct($id = null)
{
+ parent::__construct('newsletter/nl.tpl', 'nl.css', 'nl/show');
if (isset($id)) {
if ($id == 'last') {
$res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
} else {
$res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
if (!$res->numRows()) {
- insert_new_nl();
+ Newsletter::create();
}
$res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
}
}
}
- // }}}
- // {{{ function setSent()
-
- function setSent()
- {
- XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
- }
-
- // }}}
- // {{{ function save()
-
- function save()
+ public function save()
{
XDB::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
$this->_date, $this->_title, $this->_title_mail, $this->_head, $this->_shortname,$this->_id);
}
-
- // }}}
- // {{{ function id()
-
- function id()
+
+ public function getArt($aid)
{
- return is_null($this->_shortname) ? $this->_id : $this->_shortname;
- }
-
- // }}}
- // {{{ function title()
-
- function title($mail = false) {
- if ($mail) {
- return $this->_title_mail;
- }
- return $this->_title;
- }
-
- // }}}
- // {{{ function head()
-
- function head()
- { return $this->_head; }
-
- // }}}
- // {{{ function getArt()
-
- function getArt($aid)
- {
- foreach ($this->_arts as $key=>$artlist) {
- if (isset($artlist["a$aid"])) {
+ foreach ($this->_arts as $key=>$artlist) {
+ if (isset($artlist["a$aid"])) {
return $artlist["a$aid"];
}
- }
- return null;
+ }
+ return null;
}
- // }}}
- // {{{ function saveArticle()
-
- function saveArticle(&$a)
+ public function saveArticle(&$a)
{
- if ($a->_aid>=0) {
- XDB::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
- VALUES ({?},{?},{?},{?},{?},{?},{?})',
- $this->_id, $a->_aid, $a->_cid, $a->_pos,
- $a->_title, $a->_body, $a->_append);
- $this->_arts['a'.$a->_aid] = $a;
- } else {
- XDB::execute(
- 'INSERT INTO newsletter_art
- SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
- FROM newsletter_art AS a
- WHERE a.id={?}',
- $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
- $this->_arts['a'.$a->_aid] = $a;
+ if ($a->_aid>=0) {
+ XDB::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
+ VALUES ({?},{?},{?},{?},{?},{?},{?})',
+ $this->_id, $a->_aid, $a->_cid, $a->_pos,
+ $a->_title, $a->_body, $a->_append);
+ $this->_arts['a'.$a->_aid] = $a;
+ } else {
+ XDB::execute('INSERT INTO newsletter_art
+ SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
+ FROM newsletter_art AS a
+ WHERE a.id={?}',
+ $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
+ $this->_arts['a'.$a->_aid] = $a;
+ }
}
+
+ public function delArticle($aid)
+ {
+ XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
+ foreach ($this->_arts as $key=>$art) {
+ unset($this->_arts[$key]["a$aid"]);
+ }
}
- // }}}
- // {{{ function delArticle()
-
- function delArticle($aid)
+ protected function assignData(&$smarty)
{
- XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
- foreach ($this->_arts as $key=>$art) {
- unset($this->_arts[$key]["a$aid"]);
- }
+ $smarty->assign_by_ref('nl', $this);
}
- // }}}
- // {{{ function footer
-
- function footer($html)
+ protected function setSent()
{
- global $globals;
- $url = 'https://www.polytechnique.org';
-
- if ($html) {
- return '<div class="foot1">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
- . '<div class="foot2">'
- . "[<a href=\"$url/nl\">archives</a> | "
- . "<a href=\"$url/nl/submit\">écrire dans la NL</a> | "
- . "<a href=\"$url/nl/out\">ne plus recevoir</a>]"
- . '</div>';
- } else {
- return "\n\n--------------------------------------------------------------------\n"
- . "Cette lettre est envoyée à tous les Polytechniciens sur Internet par\n"
- . "l'intermédiaire de Polytechnique.org.\n"
- . "\n"
- . "archives : [$url/nl]\n"
- . "écrire : [$url/nl/submit]\n"
- . "ne plus recevoir: [$url/nl/out]\n";
- }
+ XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
}
- // }}}
- // {{{ function toText()
-
- function toText($prenom,$nom,$sexe)
+ protected function subscriptionTable()
{
- $res = "====================================================================\n";
- $res .= ' '.$this->title()."\n";
- $res .= "====================================================================\n\n";
-
- $head = $this->head();
- $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head);
- $head = str_replace('<prenom>', $prenom, $head);
- $head = str_replace('<nom>', $nom, $head);
- $head = enriched_to_text($head,false,true,2,64);
-
- if ($head) {
- $res .= "\n$head\n\n\n";
- }
-
- $i = 1;
- foreach ($this->_arts as $cid=>$arts) {
- $res .= "\n$i *{$this->_cats[$cid]}*\n";
- foreach ($arts as $art) {
- $res .= '- '.$art->title()."\n";
- }
- $i ++;
- }
- $res .= "\n\n";
-
- foreach ($this->_arts as $cid=>$arts) {
- $res .= "--------------------------------------------------------------------\n";
- $res .= "*{$this->_cats[$cid]}*\n";
- $res .= "--------------------------------------------------------------------\n\n";
- foreach ($arts as $art) {
- $res .= $art->toText();
- $res .= "\n\n";
- }
+ return 'newsletter_ins';
}
-
- $res .= $this->footer(false);
-
- return $res;
+
+ protected function subscriptionWhere()
+ {
+ return 'ni.last<' . $this->_id;
}
- // }}}
- // {{{ function toHtml()
-
- function toHtml($prenom, $nom, $sexe, $body=false, $urlprefix = false)
+ protected function subscriptionUpdate()
{
- $u = $urlprefix ? 'nl/show/'.$this->id() : '';
- $res = '<div class="title">'.$this->title().'</div>';
-
- $head = $this->head();
- $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head);
- $head = str_replace('<prenom>', $prenom, $head);
- $head = str_replace('<nom>', $nom, $head);
- $head = enriched_to_text($head, true);
-
- if($head) {
- $res .= "<div class='intro'>$head</div>";
- }
+ return 'last=' . $this->_id;
+ }
- $i = 1;
- $res .= "<a id='top_lnk'></a>";
- foreach ($this->_arts as $cid=>$arts) {
- $res .= "<div class='lnk'><a href='$u#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
- foreach ($arts as $art) {
- $res .= "<a href='$u#art{$art->_aid}'> - ".htmlentities($art->title())."</a>";
- }
- $res .= '</div>';
- $i ++;
+ static public function subscriptionState($uid = -1)
+ {
+ $user = ($uid == -1) ? S::v('uid') : $uid;
+ $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', $user);
+ return $res->fetchOneCell();
}
- foreach ($this->_arts as $cid=>$arts) {
- $res .= "<h1 class='xorg_nl'><a id='cat$cid'></a>".$this->_cats[$cid].'</h1>';
- foreach($arts as $art) {
- $res .= $art->toHtml();
- $res .= "<div class='top_lnk'><a href='$u#top_lnk'>Revenir au sommaire</a></div>";
- }
+ static public function unsubscribe($uid = -1)
+ {
+ $user = ($uid == -1) ? S::v('uid') : $uid;
+ XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', $user);
}
- $res .= $this->footer(true);
-
- if ($body) {
- $res = <<<EOF
-<?xml version="1.0" encoding="iso-8859-15"?>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- <style type="text/css">
- <!--
- body { background-color: #ddd; color: #000; }
- div.nl { margin: auto; width: 72ex;
- font-family: "Georgia", "Times New Roman", serif; font-size: 11pt;
- text-align: justify;
- background-color: #fff; color: #000; }
- a[href] { text-decoration: none;
- background-color: #fff; color: #36c; }
- a[href]:hover { background-color: #fff; color: #6c0; }
-
- div.title { margin: 0ex 0ex 3ex 0ex; padding: 5ex 1ex 1ex 15ex;
- font-size: 130%; font-weight: bold; text-align: right;
- background-color: #369; color: #fff;
- background-image: url(http://dev.m4x.org/images/logo_xorg_nl.png);
- background-repeat: no-repeat; background-position: 0.5ex 0.5ex; }
- div.intro { margin: 4ex 3ex; }
- div.lnk { margin: 2ex 6ex;}
- div.lnk a { display: block; font-size: 95%; }
- div.top_lnk { margin: 2ex; padding: 0ex; font-size: 85%; text-align: right; }
- h1.xorg_nl { margin: 3ex 0ex 2ex 0ex; padding: 1.5ex 2ex 0.5ex 1ex;
- font-size: 120%; font-weight: bold; text-align: right;
- background-color: #369; color: #fff; }
- h2.xorg_nl { margin: 2ex 0ex 0ex 0ex; padding: 0.4ex 2ex;
- font-size: 100%; font-weight: bold; font-style: italic;
- background-color: #fff; color: #369;
- border-width: thin 0; border-style: solid; border-color: #369; }
- div.art { margin: 2ex 3ex; }
- div.app { margin: 2ex 6ex 0ex 3ex; font-size: 95%; text-align: left; }
- div.foot1 { margin: 8ex 0ex 0ex 0ex; padding: 0.5ex 2ex;
- font-size: 90%; background-color: #fff; color: #999;
- border-width: thin 0; border-style: solid; border-color: #ddd;
- text-align: center; }
- div.foot2 { padding: 1ex 0ex;
- font-size: 90%; background-color: #fff; color: #999;
- text-align: center; }
- -->
- </style>
- </head>
- <body>
- <div class='nl'>
- $res
- </div>
- </body>
-</html>
-EOF;
- }
- return $res;
+ static public function subscribe($uid = -1)
+ {
+ $user = ($uid == -1) ? S::v('uid') : $uid;
+ XDB::execute('REPLACE INTO newsletter_ins (user_id,last) VALUES ({?}, 0)', $user);
}
- // }}}
- // {{{ function sendTo()
-
- function sendTo($prenom, $nom, $login, $sex, $html)
+ static public function create()
{
- global $globals;
- require_once('diogenes/diogenes.hermes.inc.php');
-
- $mailer = new HermesMailer();
- $mailer->setFrom($globals->newsletter->from);
- $mailer->setSubject($this->title(true));
- $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
- if (!empty($globals->newsletter->replyto)) {
- $mailer->addHeader('Reply-To',$globals->newsletter->replyto);
- }
- if (!empty($globals->newsletter->retpath)) {
- $mailer->addHeader('Return-Path',$globals->newsletter->retpath);
- }
- $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
- if ($html) {
- $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
+ XDB::execute("INSERT INTO newsletter
+ SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
}
- $mailer->send();
+
+ static public function listSent()
+ {
+ $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
+ FROM newsletter
+ WHERE bits!='new'
+ ORDER BY date DESC");
+ return $res->fetchAllAssoc();
}
- // }}}
+ static public function listAll()
+ {
+ $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
+ FROM newsletter
+ ORDER BY date DESC");
+ return $res->fetchAllAssoc();
+ }
}
// }}}
// }}}
// {{{ constructor
- function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
+ function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
{
- $this->_body = $body;
- $this->_title = $title;
- $this->_append = $append;
- $this->_aid = $aid;
- $this->_cid = $cid;
- $this->_pos = $pos;
+ $this->_body = $body;
+ $this->_title = $title;
+ $this->_append = $append;
+ $this->_aid = $aid;
+ $this->_cid = $cid;
+ $this->_pos = $pos;
}
// }}}
// {{{ function title()
- function title()
+ public function title()
{ return trim($this->_title); }
// }}}
// {{{ function body()
- function body()
+ public function body()
{ return trim($this->_body); }
// }}}
// {{{ function append()
- function append()
+ public function append()
{ return trim($this->_append); }
// }}}
// {{{ function toText()
- function toText()
+ public function toText()
{
- $title = '*'.$this->title().'*';
- $body = enriched_to_text($this->_body,false,true);
- $app = enriched_to_text($this->_append,false,false,4);
- return trim("$title\n\n$body\n\n$app")."\n";
+ $title = '*'.$this->title().'*';
+ $body = enriched_to_text($this->_body,false,true);
+ $app = enriched_to_text($this->_append,false,false,4);
+ return trim("$title\n\n$body\n\n$app")."\n";
}
// }}}
// {{{ function toHtml()
- function toHtml()
+ public function toHtml()
{
- $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
- $body = enriched_to_text($this->_body,true);
- $app = enriched_to_text($this->_append,true);
+ $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
+ $body = enriched_to_text($this->_body,true);
+ $app = enriched_to_text($this->_append,true);
- $art = "$title\n";
- $art .= "<div class='art'>\n$body\n";
- if ($app) {
+ $art = "$title\n";
+ $art .= "<div class='art'>\n$body\n";
+ if ($app) {
$art .= "<div class='app'>$app</div>";
}
- $art .= "</div>\n";
+ $art .= "</div>\n";
- return $art;
+ return $art;
}
// }}}
// {{{ function check()
- function check()
+ public function check()
{
- $text = enriched_to_text($this->_body);
- $arr = explode("\n",wordwrap($text,68));
- $c = 0;
- foreach ($arr as $line) {
+ $text = enriched_to_text($this->_body);
+ $arr = explode("\n",wordwrap($text,68));
+ $c = 0;
+ foreach ($arr as $line) {
if (trim($line)) {
$c++;
}
}
- return $c<9;
+ return $c<9;
}
// }}}
// }}}
// {{{ Functions
-function insert_new_nl()
-{
- XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
-}
-
-function get_nl_slist()
-{
- $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter ORDER BY date DESC");
- return $res->fetchAllAssoc();
-}
-
-function get_nl_list()
-{
- $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
- return $res->fetchAllAssoc();
-}
-
-function get_nl_state()
-{
- $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
- return $res->fetchOneCell();
-}
-
-function unsubscribe_nl()
-{
- XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
-}
-
-function subscribe_nl($uid=-1)
-{
- $user = ($uid == -1) ? S::v('uid') : $uid;
- XDB::execute('REPLACE INTO newsletter_ins (user_id,last)
- VALUES ({?}, 0)', $user);
-}
-
function justify($text,$n)
{
$arr = explode("\n",wordwrap($text,$n));