<?php
/***************************************************************************
- * Copyright (C) 2003-2009 Polytechnique.org *
+ * Copyright (C) 2003-2010 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
class NewsLetter extends MassMailer
{
public $_date;
- public $_cats = Array();
- public $_arts = Array();
+ public $_cats = array();
+ public $_arts = array();
function __construct($id = null)
{
} else {
$res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
if (!$res->numRows()) {
- Newsletter::create();
+ 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()) {
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;
+ $a->_cid = ($a->_cid == 0) ? null : $a->_cid;
+ if ($a->_aid >= 0) {
+ XDB::execute('UPDATE newsletter_art
+ SET cid = {?}, pos = {?}, title = {?}, body = {?}, append = {?}
+ WHERE id = {?} AND aid = {?}',
+ $a->_cid, $a->_pos, $a->_title, $a->_body, $a->_append, $this->_id, $a->_aid);
} 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;
+ XDB::startTransaction();
+ list($aid, $pos) = XDB::fetchOneRow('SELECT MAX(aid) AS aid, MAX(pos) AS pos
+ FROM newsletter_art AS a
+ WHERE a.id = {?}',
+ $this->_id);
+ $a->_aid = ++$aid;
+ $a->_pos = ($a->_pos ? $a->_pos : ++$pos);
+ XDB::execute('INSERT 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);
+ XDB::commit();
}
+ $this->_arts['a' . $a->_aid] = $a;
}
public function delArticle($aid)
$user = is_null($uid) ? S::v('uid') : $uid;
$res = XDB::query("SELECT 1
FROM newsletter_ins
- WHERE user_id={?}", $user);
+ WHERE uid={?}", $user);
return $res->fetchOneCell();
}
{
$user = is_null($uid) ? S::v('uid') : $uid;
XDB::execute("DELETE FROM newsletter_ins
- WHERE user_id={?}", $user);
+ WHERE uid={?}", $user);
}
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);
+ XDB::execute('INSERT IGNORE INTO newsletter_ins (uid, last, hash)
+ VALUES ({?}, NULL, NULL)', $user);
}
protected function subscriptionWhere()
}
// }}}
+ // {{{ function parseUrlsFromArticle()
+
+ 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]);
+ }
+
+ $append_matches = array();
+ if (preg_match_all($regex, $this->append(), $append_matches)) {
+ $matches = array_merge($matches, $append_matches[0]);
+ }
+
+ return $matches;
+ }
+
+ // }}}
+ // {{{ function getLinkIps()
+
+ 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 $article_ips;
+ }
+
+ // }}}
}
// }}}