2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once("massmailer.inc.php");
24 // {{{ class NewsLetter
26 class NewsLetter
extends MassMailer
29 public $_cats = array();
30 public $_arts = array();
32 function __construct($id = null
)
34 parent
::__construct('newsletter/nl.mail.tpl', 'nl.css', 'nl/show', 'newsletter', 'newsletter_ins');
37 $res = XDB
::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
38 $id = $res->fetchOneCell();
40 $res = XDB
::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
42 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
43 if (!$res->numRows()) {
46 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
48 if ($res->numRows() != 1) {
49 throw new MailNotFound();
51 $nl = $res->fetchOneAssoc();
53 $this->_id
= $nl['id'];
54 $this->_shortname
= $nl['short_name'];
55 $this->_date
= $nl['date'];
56 $this->_title
= $nl['titre'];
57 $this->_title_mail
= $nl['titre_mail'];
58 $this->_head
= $nl['head'];
60 $res = XDB
::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
61 while (list($cid, $title) = $res->next()) {
62 $this->_cats
[$cid] = $title;
66 "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
67 FROM newsletter_art AS a
68 INNER JOIN newsletter AS n USING(id)
69 LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid)
71 ORDER BY c.pos,a.pos", $this->_id
);
72 while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
73 $this->_arts
[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
77 public function save()
79 XDB
::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
80 $this->_date
, $this->_title
, $this->_title_mail
, $this->_head
, $this->_shortname
,$this->_id
);
83 public function getArt($aid)
85 foreach ($this->_arts
as $key=>$artlist) {
86 if (isset($artlist["a$aid"])) {
87 return $artlist["a$aid"];
93 public function saveArticle(&$a)
96 XDB
::execute('REPLACE INTO newsletter_art (id, aid, cid, pos, title, body, append)
97 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
98 $this->_id
, $a->_aid
, $a->_cid
, $a->_pos
,
99 $a->_title
, $a->_body
, $a->_append
);
100 $this->_arts
['a' . $a->_aid
] = $a;
102 XDB
::execute('INSERT INTO newsletter_art
103 SELECT {?}, MAX(aid)+1, {?}, '
104 . ($a->_pos ?
intval($a->_pos
) : 'MAX(pos)+1')
106 FROM newsletter_art AS a
108 $this->_id
, $a->_cid
, $a->_title
, $a->_body
, $a->_append
, $this->_id
);
109 $this->_arts
['a' . $a->_aid
] = $a;
113 public function delArticle($aid)
115 XDB
::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id
, $aid);
116 foreach ($this->_arts
as $key=>$art) {
117 unset($this->_arts
[$key]["a$aid"]);
121 protected function assignData(&$smarty)
123 $smarty->assign_by_ref('nl', $this);
126 protected function setSent()
128 XDB
::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id
);
131 static public function subscriptionState($uid = null
)
133 $user = is_null($uid) ? S
::v('uid') : $uid;
134 $res = XDB
::query("SELECT 1
136 WHERE user_id={?}", $user);
137 return $res->fetchOneCell();
140 static public function unsubscribe($uid = null
)
142 $user = is_null($uid) ? S
::v('uid') : $uid;
143 XDB
::execute("DELETE FROM newsletter_ins
144 WHERE user_id={?}", $user);
147 static public function subscribe($uid = null
)
149 $user = is_null($uid) ? S
::v('uid') : $uid;
150 XDB
::execute("REPLACE INTO newsletter_ins (user_id,last)
151 VALUES ({?}, 0)", $user);
154 protected function subscriptionWhere()
159 static public function create()
161 XDB
::execute("INSERT INTO newsletter
162 SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
165 static public function listSent()
167 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
170 ORDER BY date DESC");
171 return $res->fetchAllAssoc();
174 static public function listAll()
176 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
178 ORDER BY date DESC");
179 return $res->fetchAllAssoc();
184 // {{{ class NLArticle
200 function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
202 $this->_body
= $body;
203 $this->_title
= $title;
204 $this->_append
= $append;
211 // {{{ function title()
213 public function title()
214 { return trim($this->_title
); }
217 // {{{ function body()
219 public function body()
220 { return trim($this->_body
); }
223 // {{{ function append()
225 public function append()
226 { return trim($this->_append
); }
229 // {{{ function toText()
231 public function toText($hash = null
, $login = null
)
233 $title = '*'.$this->title().'*';
234 $body = MiniWiki
::WikiToText($this->_body
, true
);
235 $app = MiniWiki
::WikiToText($this->_append
,false
,4);
236 $text = trim("$title\n\n$body\n\n$app")."\n";
237 if (!is_null($hash) && !is_null($login)) {
238 $text = str_replace('%HASH%', "$hash/$login", $text);
240 $text = str_replace('%HASH%', '', $text);
246 // {{{ function toHtml()
248 public function toHtml($hash = null
, $login = null
)
250 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>';
251 $body = MiniWiki
::WikiToHTML($this->_body
);
252 $app = MiniWiki
::WikiToHTML($this->_append
);
255 $art .= "<div class='art'>\n$body\n";
257 $art .= "<div class='app'>$app</div>";
260 if (!is_null($hash) && !is_null($login)) {
261 $art = str_replace('%HASH%', "$hash/$login", $art);
263 $art = str_replace('%HASH%', '', $art);
270 // {{{ function check()
272 public function check()
274 $text = MiniWiki
::WikiToText($this->_body
);
275 $arr = explode("\n",wordwrap($text,68));
277 foreach ($arr as $line) {
286 // {{{ function parseUrlsFromArticle()
288 private function parseUrlsFromArticle()
290 $email_regex = '([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)';
291 $url_regex = '((https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+)';
292 $regex = '{' . $email_regex . '|' . $url_regex . '}i';
295 $body_matches = array();
296 if (preg_match_all($regex, $this->body(), $body_matches)) {
297 $matches = array_merge($matches, $body_matches[0]);
300 $append_matches = array();
301 if (preg_match_all($regex, $this->append(), $append_matches)) {
302 $matches = array_merge($matches, $append_matches[0]);
309 // {{{ function getLinkIps()
311 public function getLinkIps(&$blacklist_host_resolution_count)
313 $matches = $this->parseUrlsFromArticle();
314 $article_ips = array();
316 if (!empty($matches)) {
319 foreach ($matches as $match) {
320 $host = parse_url($match, PHP_URL_HOST
);
322 list(, $host) = explode('@', $match);
325 if ($blacklist_host_resolution_count >= $globals->mail
->blacklist_host_resolution_limit
) {
329 if (!preg_match('/^(' . str_replace(' ', '|', $globals->mail
->domain_whitelist
) . ')$/i', $host)) {
330 $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host));
331 ++
$blacklist_host_resolution_count;
344 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: