Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
0337d704 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
16cd99fb | 22 | require_once("massmailer.inc.php"); |
0337d704 | 23 | |
0337d704 | 24 | // {{{ class NewsLetter |
25 | ||
4882f4a5 | 26 | class NewsLetter extends MassMailer |
0337d704 | 27 | { |
4882f4a5 | 28 | public $_date; |
9e2a6a32 SJ |
29 | public $_cats = array(); |
30 | public $_arts = array(); | |
0337d704 | 31 | |
4882f4a5 | 32 | function __construct($id = null) |
0337d704 | 33 | { |
b71f7275 | 34 | parent::__construct('newsletter/nl.mail.tpl', 'nl.css', 'nl/show', 'newsletter', 'newsletter_ins'); |
ed76a506 | 35 | if (isset($id)) { |
36 | if ($id == 'last') { | |
37 | $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'"); | |
0337d704 | 38 | $id = $res->fetchOneCell(); |
ed76a506 | 39 | } |
40 | $res = XDB::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id); | |
41 | } else { | |
42 | $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'"); | |
43 | if (!$res->numRows()) { | |
b6ba1a04 | 44 | NewsLetter::create(); |
ed76a506 | 45 | } |
1e7c2797 | 46 | $res = XDB::query("SELECT * FROM newsletter WHERE bits='new' ORDER BY id DESC LIMIT 1"); |
ad6ca77d | 47 | } |
9da70671 SJ |
48 | if ($res->numRows() != 1) { |
49 | throw new MailNotFound(); | |
50 | } | |
ed76a506 | 51 | $nl = $res->fetchOneAssoc(); |
0337d704 | 52 | |
9e2a6a32 SJ |
53 | $this->_id = $nl['id']; |
54 | $this->_shortname = $nl['short_name']; | |
55 | $this->_date = $nl['date']; | |
56 | $this->_title = $nl['titre']; | |
8269d2d6 | 57 | $this->_title_mail = $nl['titre_mail']; |
9e2a6a32 | 58 | $this->_head = $nl['head']; |
0337d704 | 59 | |
ed76a506 | 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; | |
63 | } | |
eaf30d86 | 64 | |
ed76a506 | 65 | $res = XDB::iterRow( |
0337d704 | 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) | |
70 | WHERE a.id={?} | |
71 | ORDER BY c.pos,a.pos", $this->_id); | |
ed76a506 | 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); | |
74 | } | |
0337d704 | 75 | } |
76 | ||
4882f4a5 | 77 | public function save() |
0337d704 | 78 | { |
8269d2d6 | 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); | |
ed76a506 | 81 | } |
eaf30d86 | 82 | |
4882f4a5 | 83 | public function getArt($aid) |
0337d704 | 84 | { |
11f44323 | 85 | foreach ($this->_arts as $key=>$artlist) { |
86 | if (isset($artlist["a$aid"])) { | |
0337d704 | 87 | return $artlist["a$aid"]; |
88 | } | |
11f44323 | 89 | } |
90 | return null; | |
0337d704 | 91 | } |
92 | ||
4882f4a5 | 93 | public function saveArticle(&$a) |
0337d704 | 94 | { |
e387b26a | 95 | $a->_cid = ($a->_cid == 0) ? null : $a->_cid; |
9e2a6a32 | 96 | if ($a->_aid >= 0) { |
00ba8a74 SJ |
97 | XDB::execute('UPDATE newsletter_art |
98 | SET cid = {?}, pos = {?}, title = {?}, body = {?}, append = {?} | |
99 | WHERE id = {?} AND aid = {?}', | |
100 | $a->_cid, $a->_pos, $a->_title, $a->_body, $a->_append, $this->_id, $a->_aid); | |
11f44323 | 101 | } else { |
00ba8a74 SJ |
102 | XDB::startTransaction(); |
103 | list($aid, $pos) = XDB::fetchOneRow('SELECT MAX(aid) AS aid, MAX(pos) AS pos | |
104 | FROM newsletter_art AS a | |
105 | WHERE a.id = {?}', | |
106 | $this->_id); | |
107 | $a->_aid = ++$aid; | |
108 | $a->_pos = ($a->_pos ? $a->_pos : ++$pos); | |
109 | XDB::execute('INSERT INTO newsletter_art (id, aid, cid, pos, title, body, append) | |
110 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})', | |
111 | $this->_id, $a->_aid, $a->_cid, $a->_pos, | |
112 | $a->_title, $a->_body, $a->_append); | |
113 | XDB::commit(); | |
11f44323 | 114 | } |
00ba8a74 | 115 | $this->_arts['a' . $a->_aid] = $a; |
0337d704 | 116 | } |
eaf30d86 | 117 | |
4882f4a5 | 118 | public function delArticle($aid) |
0337d704 | 119 | { |
11f44323 | 120 | XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid); |
121 | foreach ($this->_arts as $key=>$art) { | |
122 | unset($this->_arts[$key]["a$aid"]); | |
123 | } | |
0337d704 | 124 | } |
125 | ||
4882f4a5 | 126 | protected function assignData(&$smarty) |
127 | { | |
128 | $smarty->assign_by_ref('nl', $this); | |
129 | } | |
0337d704 | 130 | |
4882f4a5 | 131 | protected function setSent() |
0337d704 | 132 | { |
16cd99fb | 133 | XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id); |
eaf30d86 | 134 | } |
0337d704 | 135 | |
16cd99fb | 136 | static public function subscriptionState($uid = null) |
0337d704 | 137 | { |
16cd99fb | 138 | $user = is_null($uid) ? S::v('uid') : $uid; |
139 | $res = XDB::query("SELECT 1 | |
140 | FROM newsletter_ins | |
c0c4edb5 | 141 | WHERE uid={?}", $user); |
16cd99fb | 142 | return $res->fetchOneCell(); |
eaf30d86 PH |
143 | } |
144 | ||
16cd99fb | 145 | static public function unsubscribe($uid = null) |
4882f4a5 | 146 | { |
16cd99fb | 147 | $user = is_null($uid) ? S::v('uid') : $uid; |
148 | XDB::execute("DELETE FROM newsletter_ins | |
c0c4edb5 | 149 | WHERE uid={?}", $user); |
0337d704 | 150 | } |
151 | ||
16cd99fb | 152 | static public function subscribe($uid = null) |
4882f4a5 | 153 | { |
16cd99fb | 154 | $user = is_null($uid) ? S::v('uid') : $uid; |
00ba8a74 SJ |
155 | XDB::execute('INSERT IGNORE INTO newsletter_ins (uid, last, hash) |
156 | VALUES ({?}, NULL, NULL)', $user); | |
4882f4a5 | 157 | } |
158 | ||
16cd99fb | 159 | protected function subscriptionWhere() |
4882f4a5 | 160 | { |
16cd99fb | 161 | return '1'; |
4882f4a5 | 162 | } |
163 | ||
164 | static public function create() | |
165 | { | |
166 | XDB::execute("INSERT INTO newsletter | |
167 | SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'"); | |
168 | } | |
169 | ||
170 | static public function listSent() | |
171 | { | |
172 | $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre | |
173 | FROM newsletter | |
174 | WHERE bits!='new' | |
175 | ORDER BY date DESC"); | |
176 | return $res->fetchAllAssoc(); | |
177 | } | |
178 | ||
179 | static public function listAll() | |
180 | { | |
181 | $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre | |
182 | FROM newsletter | |
183 | ORDER BY date DESC"); | |
184 | return $res->fetchAllAssoc(); | |
185 | } | |
0337d704 | 186 | } |
187 | ||
188 | // }}} | |
189 | // {{{ class NLArticle | |
190 | ||
191 | class NLArticle | |
192 | { | |
193 | // {{{ properties | |
eaf30d86 | 194 | |
0337d704 | 195 | var $_aid; |
196 | var $_cid; | |
197 | var $_pos; | |
198 | var $_title; | |
199 | var $_body; | |
200 | var $_append; | |
201 | ||
202 | // }}} | |
203 | // {{{ constructor | |
eaf30d86 | 204 | |
4882f4a5 | 205 | function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0) |
0337d704 | 206 | { |
4882f4a5 | 207 | $this->_body = $body; |
208 | $this->_title = $title; | |
209 | $this->_append = $append; | |
210 | $this->_aid = $aid; | |
211 | $this->_cid = $cid; | |
212 | $this->_pos = $pos; | |
0337d704 | 213 | } |
214 | ||
215 | // }}} | |
216 | // {{{ function title() | |
217 | ||
4882f4a5 | 218 | public function title() |
0337d704 | 219 | { return trim($this->_title); } |
220 | ||
221 | // }}} | |
222 | // {{{ function body() | |
eaf30d86 | 223 | |
4882f4a5 | 224 | public function body() |
0337d704 | 225 | { return trim($this->_body); } |
eaf30d86 | 226 | |
0337d704 | 227 | // }}} |
228 | // {{{ function append() | |
eaf30d86 | 229 | |
4882f4a5 | 230 | public function append() |
0337d704 | 231 | { return trim($this->_append); } |
232 | ||
233 | // }}} | |
234 | // {{{ function toText() | |
235 | ||
e0e77c5a | 236 | public function toText($hash = null, $login = null) |
0337d704 | 237 | { |
4882f4a5 | 238 | $title = '*'.$this->title().'*'; |
dcdcd18a | 239 | $body = MiniWiki::WikiToText($this->_body, true); |
02fdd1c8 | 240 | $app = MiniWiki::WikiToText($this->_append,false,4); |
e0e77c5a FB |
241 | $text = trim("$title\n\n$body\n\n$app")."\n"; |
242 | if (!is_null($hash) && !is_null($login)) { | |
243 | $text = str_replace('%HASH%', "$hash/$login", $text); | |
244 | } else { | |
245 | $text = str_replace('%HASH%', '', $text); | |
246 | } | |
247 | return $text; | |
0337d704 | 248 | } |
249 | ||
250 | // }}} | |
251 | // {{{ function toHtml() | |
252 | ||
e0e77c5a | 253 | public function toHtml($hash = null, $login = null) |
0337d704 | 254 | { |
493b6abe | 255 | $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>'; |
02fdd1c8 | 256 | $body = MiniWiki::WikiToHTML($this->_body); |
257 | $app = MiniWiki::WikiToHTML($this->_append); | |
eaf30d86 | 258 | |
4882f4a5 | 259 | $art = "$title\n"; |
260 | $art .= "<div class='art'>\n$body\n"; | |
261 | if ($app) { | |
0337d704 | 262 | $art .= "<div class='app'>$app</div>"; |
263 | } | |
4882f4a5 | 264 | $art .= "</div>\n"; |
e0e77c5a FB |
265 | if (!is_null($hash) && !is_null($login)) { |
266 | $art = str_replace('%HASH%', "$hash/$login", $art); | |
267 | } else { | |
268 | $art = str_replace('%HASH%', '', $art); | |
269 | } | |
eaf30d86 | 270 | |
4882f4a5 | 271 | return $art; |
0337d704 | 272 | } |
273 | ||
274 | // }}} | |
275 | // {{{ function check() | |
276 | ||
4882f4a5 | 277 | public function check() |
0337d704 | 278 | { |
02fdd1c8 | 279 | $text = MiniWiki::WikiToText($this->_body); |
4882f4a5 | 280 | $arr = explode("\n",wordwrap($text,68)); |
281 | $c = 0; | |
282 | foreach ($arr as $line) { | |
0337d704 | 283 | if (trim($line)) { |
284 | $c++; | |
285 | } | |
286 | } | |
4882f4a5 | 287 | return $c<9; |
0337d704 | 288 | } |
289 | ||
290 | // }}} | |
9e2a6a32 SJ |
291 | // {{{ function parseUrlsFromArticle() |
292 | ||
293 | private function parseUrlsFromArticle() | |
294 | { | |
295 | $email_regex = '([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)'; | |
296 | $url_regex = '((https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+)'; | |
297 | $regex = '{' . $email_regex . '|' . $url_regex . '}i'; | |
298 | ||
299 | $matches = array(); | |
300 | $body_matches = array(); | |
301 | if (preg_match_all($regex, $this->body(), $body_matches)) { | |
302 | $matches = array_merge($matches, $body_matches[0]); | |
303 | } | |
304 | ||
305 | $append_matches = array(); | |
306 | if (preg_match_all($regex, $this->append(), $append_matches)) { | |
307 | $matches = array_merge($matches, $append_matches[0]); | |
308 | } | |
309 | ||
310 | return $matches; | |
311 | } | |
312 | ||
313 | // }}} | |
314 | // {{{ function getLinkIps() | |
315 | ||
955109ba | 316 | public function getLinkIps(&$blacklist_host_resolution_count) |
9e2a6a32 SJ |
317 | { |
318 | $matches = $this->parseUrlsFromArticle(); | |
319 | $article_ips = array(); | |
320 | ||
321 | if (!empty($matches)) { | |
322 | global $globals; | |
323 | ||
324 | foreach ($matches as $match) { | |
325 | $host = parse_url($match, PHP_URL_HOST); | |
326 | if ($host == '') { | |
327 | list(, $host) = explode('@', $match); | |
328 | } | |
329 | ||
955109ba | 330 | if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) { |
9e2a6a32 SJ |
331 | break; |
332 | } | |
333 | ||
955109ba | 334 | if (!preg_match('/^(' . str_replace(' ', '|', $globals->mail->domain_whitelist) . ')$/i', $host)) { |
9e2a6a32 | 335 | $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host)); |
955109ba | 336 | ++$blacklist_host_resolution_count; |
9e2a6a32 SJ |
337 | } |
338 | } | |
339 | } | |
340 | ||
341 | return $article_ips; | |
342 | } | |
343 | ||
344 | // }}} | |
0337d704 | 345 | } |
346 | ||
347 | // }}} | |
0337d704 | 348 | |
a7de4ef7 | 349 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
0337d704 | 350 | ?> |