X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fnewsletter.inc.php;h=a8c424ea3d6937702131c37324fd0eb0232c8815;hb=9e1816c7666767755abbc24f6a0df290efff81b6;hp=426bc2729a8497fe81964d0bc9ed6e30d44cabfa;hpb=1e33266a2f91e1af7a5cb3e8fbbc1c49ff9341f3;p=platal.git
diff --git a/include/newsletter.inc.php b/include/newsletter.inc.php
index 426bc27..a8c424e 100644
--- a/include/newsletter.inc.php
+++ b/include/newsletter.inc.php
@@ -1,6 +1,6 @@
addCssLink('nl.css');
-}
-
-define('FEMME', 1);
-define('HOMME', 0);
-
-// }}}
// {{{ 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.mail.tpl', 'nl.css', 'nl/show', 'newsletter', 'newsletter_ins');
if (isset($id)) {
if ($id == 'last') {
$res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
@@ -60,24 +41,27 @@ class NewsLetter
} 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'");
+ $res = XDB::query("SELECT * FROM newsletter WHERE bits='new' ORDER BY id DESC LIMIT 1");
+ }
+ if ($res->numRows() != 1) {
+ throw new MailNotFound();
}
$nl = $res->fetchOneAssoc();
- $this->_id = $nl['id'];
- $this->_shortname = $nl['short_name'];
- $this->_date = $nl['date'];
- $this->_title = $nl['titre'];
+ $this->_id = $nl['id'];
+ $this->_shortname = $nl['short_name'];
+ $this->_date = $nl['date'];
+ $this->_title = $nl['titre'];
$this->_title_mail = $nl['titre_mail'];
- $this->_head = $nl['head'];
+ $this->_head = $nl['head'];
$res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
while (list($cid, $title) = $res->next()) {
$this->_cats[$cid] = $title;
}
-
+
$res = XDB::iterRow(
"SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
FROM newsletter_art AS a
@@ -90,284 +74,110 @@ class NewsLetter
}
}
- // }}}
- // {{{ 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;
+ }
}
- // }}}
- // {{{ function delArticle()
-
- function delArticle($aid)
+ 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"]);
- }
+ 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 footer
-
- function footer($html)
+ protected function assignData(&$smarty)
{
- global $globals;
- $url = 'https://www.polytechnique.org';
-
- if ($html) {
- return '
'
- . '';
- } 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";
- }
+ $smarty->assign_by_ref('nl', $this);
}
- // }}}
- // {{{ function toText()
-
- function toText($prenom,$nom,$sexe)
+ protected function setSent()
{
- $res = "====================================================================\n";
- $res .= ' '.$this->title()."\n";
- $res .= "====================================================================\n\n";
-
- $head = $this->head();
- $head = str_replace('', $sexe ? 'Chère' : 'Cher', $head);
- $head = str_replace('', $prenom, $head);
- $head = str_replace('', $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";
- }
- }
-
- $res .= $this->footer(false);
-
- return $res;
+ XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
}
- // }}}
- // {{{ function toHtml()
-
- function toHtml($prenom, $nom, $sexe, $body=false, $urlprefix = false)
+ static public function subscriptionState($uid = null)
{
- $u = $urlprefix ? 'nl/show/'.$this->id() : '';
- $res = ''.$this->title().'
';
-
- $head = $this->head();
- $head = str_replace('', $sexe ? 'Chère' : 'Cher', $head);
- $head = str_replace('', $prenom, $head);
- $head = str_replace('', $nom, $head);
- $head = enriched_to_text($head, true);
-
- if($head) {
- $res .= "$head
";
- }
+ $user = is_null($uid) ? S::v('uid') : $uid;
+ $res = XDB::query("SELECT 1
+ FROM newsletter_ins
+ WHERE user_id={?}", $user);
+ return $res->fetchOneCell();
+ }
- $i = 1;
- $res .= "";
- foreach ($this->_arts as $cid=>$arts) {
- $res .= "';
- $i ++;
+ static public function unsubscribe($uid = null)
+ {
+ $user = is_null($uid) ? S::v('uid') : $uid;
+ XDB::execute("DELETE FROM newsletter_ins
+ WHERE user_id={?}", $user);
}
- foreach ($this->_arts as $cid=>$arts) {
- $res .= "".$this->_cats[$cid].'
';
- foreach($arts as $art) {
- $res .= $art->toHtml();
- $res .= "";
- }
+ static public function subscribe($uid = null)
+ {
+ $user = is_null($uid) ? S::v('uid') : $uid;
+ XDB::execute("REPLACE INTO newsletter_ins (user_id,last)
+ VALUES ({?}, 0)", $user);
}
- $res .= $this->footer(true);
-
- if ($body) {
- $res = <<
-
-
-
-
-
-
-
-
- $res
-
-
-
-EOF;
+ protected function subscriptionWhere()
+ {
+ return '1';
}
- return $res;
+
+ static public function create()
+ {
+ XDB::execute("INSERT INTO newsletter
+ SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
}
- // }}}
- // {{{ function sendTo()
-
- function sendTo($prenom, $nom, $login, $sex, $html)
+ static public function listSent()
{
- global $globals;
-
- $mailer = new PlMailer();
- $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));
- }
- $mailer->send();
+ $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();
+ }
}
// }}}
@@ -376,7 +186,7 @@ EOF;
class NLArticle
{
// {{{ properties
-
+
var $_aid;
var $_cid;
var $_pos;
@@ -386,196 +196,150 @@ class NLArticle
// }}}
// {{{ 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($hash = null, $login = null)
{
- $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 = MiniWiki::WikiToText($this->_body, true);
+ $app = MiniWiki::WikiToText($this->_append,false,4);
+ $text = trim("$title\n\n$body\n\n$app")."\n";
+ if (!is_null($hash) && !is_null($login)) {
+ $text = str_replace('%HASH%', "$hash/$login", $text);
+ } else {
+ $text = str_replace('%HASH%', '', $text);
+ }
+ return $text;
}
// }}}
// {{{ function toHtml()
- function toHtml()
+ public function toHtml($hash = null, $login = null)
{
- $title = "".htmlentities($this->title()).'
';
- $body = enriched_to_text($this->_body,true);
- $app = enriched_to_text($this->_append,true);
-
- $art = "$title\n";
- $art .= "\n$body\n";
- if ($app) {
+ $title = "
".pl_entities($this->title()).'
';
+ $body = MiniWiki::WikiToHTML($this->_body);
+ $app = MiniWiki::WikiToHTML($this->_append);
+
+ $art = "$title\n";
+ $art .= "
\n$body\n";
+ if ($app) {
$art .= "
$app
";
}
- $art .= "
\n";
-
- return $art;
+ $art .= "
\n";
+ if (!is_null($hash) && !is_null($login)) {
+ $art = str_replace('%HASH%', "$hash/$login", $art);
+ } else {
+ $art = str_replace('%HASH%', '', $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 = MiniWiki::WikiToText($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 parseUrlsFromArticle()
-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();
-}
+ private function parseUrlsFromArticle()
+ {
+ $email_regex = '([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)';
+ $url_regex = '((https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+)';
+ $regex = '{' . $email_regex . '|' . $url_regex . '}i';
+
+ $matches = array();
+ $body_matches = array();
+ if (preg_match_all($regex, $this->body(), $body_matches)) {
+ $matches = array_merge($matches, $body_matches[0]);
+ }
-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));
- $arr = array_map('trim',$arr);
- $res = '';
- foreach ($arr as $key => $line) {
- $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
- $nxl_split = preg_split('! +!',$nxl);
- $nxw_len = count($nxl_split) ? strlen($nxl_split[0]) : 0;
- $line = trim($line);
-
- if (strlen($line)+1+$nxw_len < $n) {
- $res .= "$line\n";
- continue;
- }
-
- if (preg_match('![.:;]$!',$line)) {
- $res .= "$line\n";
- continue;
- }
+ $append_matches = array();
+ if (preg_match_all($regex, $this->append(), $append_matches)) {
+ $matches = array_merge($matches, $append_matches[0]);
+ }
- $tmp = preg_split('! +!',trim($line));
- $words = count($tmp);
- if ($words <= 1) {
- $res .= "$line\n";
- continue;
+ return $matches;
}
- $len = array_sum(array_map('strlen',$tmp));
- $empty = $n - $len;
- $sw = floatval($empty) / floatval($words-1);
-
- $cur = 0;
- $l = '';
- foreach ($tmp as $word) {
- $l .= $word;
- $cur += $sw + strlen($word);
- $l = str_pad($l,intval($cur+0.5));
- }
- $res .= trim($l)."\n";
- }
- return trim($res);
-}
+ // }}}
+ // {{{ function getLinkIps()
-function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
-{
- $text = trim($input);
- if ($html) {
- $text = htmlspecialchars($text);
- $text = str_replace('[b]','', $text);
- $text = str_replace('[/b]','', $text);
- $text = str_replace('[i]','', $text);
- $text = str_replace('[/i]','', $text);
- $text = str_replace('[u]','', $text);
- $text = str_replace('[/u]','', $text);
- require_once('url_catcher.inc.php');
- $text = url_catcher($text);
- return nl2br($text);
- } else {
- $text = preg_replace('!\[\/?b\]!','*',$text);
- $text = preg_replace('!\[\/?u\]!','_',$text);
- $text = preg_replace('!\[\/?i\]!','/',$text);
- $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
- $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
- $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
- if($indent) {
- $ind = str_pad('',$indent);
- $text = $ind.str_replace("\n","\n$ind",$text);
+ public function getLinkIps(&$blacklist_host_resolution_count)
+ {
+ $matches = $this->parseUrlsFromArticle();
+ $article_ips = array();
+
+ if (!empty($matches)) {
+ global $globals;
+
+ foreach ($matches as $match) {
+ $host = parse_url($match, PHP_URL_HOST);
+ if ($host == '') {
+ list(, $host) = explode('@', $match);
+ }
+
+ if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) {
+ break;
+ }
+
+ if (!preg_match('/^(' . str_replace(' ', '|', $globals->mail->domain_whitelist) . ')$/i', $host)) {
+ $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host));
+ ++$blacklist_host_resolution_count;
+ }
+ }
}
- return $text;
+
+ return $article_ips;
}
+
+ // }}}
}
// }}}
-// vim:set et sw=4 sts=4 sws=4:
+// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
?>