Merge branch 'account'
[platal.git] / include / newsletter.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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 22require_once("massmailer.inc.php");
0337d704 23
0337d704 24// {{{ class NewsLetter
25
4882f4a5 26class 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 {
9e2a6a32
SJ
95 if ($a->_aid >= 0) {
96 XDB::execute('REPLACE INTO newsletter_art (id, aid, cid, pos, title, body, append)
97 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
11f44323 98 $this->_id, $a->_aid, $a->_cid, $a->_pos,
99 $a->_title, $a->_body, $a->_append);
9e2a6a32 100 $this->_arts['a' . $a->_aid] = $a;
11f44323 101 } else {
102 XDB::execute('INSERT INTO newsletter_art
9e2a6a32
SJ
103 SELECT {?}, MAX(aid)+1, {?}, '
104 . ($a->_pos ? intval($a->_pos) : 'MAX(pos)+1')
105 . ', {?}, {?}, {?}
11f44323 106 FROM newsletter_art AS a
9e2a6a32 107 WHERE a.id = {?}',
11f44323 108 $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
9e2a6a32 109 $this->_arts['a' . $a->_aid] = $a;
11f44323 110 }
0337d704 111 }
eaf30d86 112
4882f4a5 113 public function delArticle($aid)
0337d704 114 {
11f44323 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"]);
118 }
0337d704 119 }
120
4882f4a5 121 protected function assignData(&$smarty)
122 {
123 $smarty->assign_by_ref('nl', $this);
124 }
0337d704 125
4882f4a5 126 protected function setSent()
0337d704 127 {
16cd99fb 128 XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
eaf30d86 129 }
0337d704 130
16cd99fb 131 static public function subscriptionState($uid = null)
0337d704 132 {
16cd99fb 133 $user = is_null($uid) ? S::v('uid') : $uid;
134 $res = XDB::query("SELECT 1
135 FROM newsletter_ins
c0c4edb5 136 WHERE uid={?}", $user);
16cd99fb 137 return $res->fetchOneCell();
eaf30d86
PH
138 }
139
16cd99fb 140 static public function unsubscribe($uid = null)
4882f4a5 141 {
16cd99fb 142 $user = is_null($uid) ? S::v('uid') : $uid;
143 XDB::execute("DELETE FROM newsletter_ins
c0c4edb5 144 WHERE uid={?}", $user);
0337d704 145 }
146
16cd99fb 147 static public function subscribe($uid = null)
4882f4a5 148 {
16cd99fb 149 $user = is_null($uid) ? S::v('uid') : $uid;
c0c4edb5 150 XDB::execute("REPLACE INTO newsletter_ins (uid,last)
16cd99fb 151 VALUES ({?}, 0)", $user);
4882f4a5 152 }
153
16cd99fb 154 protected function subscriptionWhere()
4882f4a5 155 {
16cd99fb 156 return '1';
4882f4a5 157 }
158
159 static public function create()
160 {
161 XDB::execute("INSERT INTO newsletter
162 SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
163 }
164
165 static public function listSent()
166 {
167 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
168 FROM newsletter
169 WHERE bits!='new'
170 ORDER BY date DESC");
171 return $res->fetchAllAssoc();
172 }
173
174 static public function listAll()
175 {
176 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
177 FROM newsletter
178 ORDER BY date DESC");
179 return $res->fetchAllAssoc();
180 }
0337d704 181}
182
183// }}}
184// {{{ class NLArticle
185
186class NLArticle
187{
188 // {{{ properties
eaf30d86 189
0337d704 190 var $_aid;
191 var $_cid;
192 var $_pos;
193 var $_title;
194 var $_body;
195 var $_append;
196
197 // }}}
198 // {{{ constructor
eaf30d86 199
4882f4a5 200 function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
0337d704 201 {
4882f4a5 202 $this->_body = $body;
203 $this->_title = $title;
204 $this->_append = $append;
205 $this->_aid = $aid;
206 $this->_cid = $cid;
207 $this->_pos = $pos;
0337d704 208 }
209
210 // }}}
211 // {{{ function title()
212
4882f4a5 213 public function title()
0337d704 214 { return trim($this->_title); }
215
216 // }}}
217 // {{{ function body()
eaf30d86 218
4882f4a5 219 public function body()
0337d704 220 { return trim($this->_body); }
eaf30d86 221
0337d704 222 // }}}
223 // {{{ function append()
eaf30d86 224
4882f4a5 225 public function append()
0337d704 226 { return trim($this->_append); }
227
228 // }}}
229 // {{{ function toText()
230
e0e77c5a 231 public function toText($hash = null, $login = null)
0337d704 232 {
4882f4a5 233 $title = '*'.$this->title().'*';
dcdcd18a 234 $body = MiniWiki::WikiToText($this->_body, true);
02fdd1c8 235 $app = MiniWiki::WikiToText($this->_append,false,4);
e0e77c5a
FB
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);
239 } else {
240 $text = str_replace('%HASH%', '', $text);
241 }
242 return $text;
0337d704 243 }
244
245 // }}}
246 // {{{ function toHtml()
247
e0e77c5a 248 public function toHtml($hash = null, $login = null)
0337d704 249 {
493b6abe 250 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>';
02fdd1c8 251 $body = MiniWiki::WikiToHTML($this->_body);
252 $app = MiniWiki::WikiToHTML($this->_append);
eaf30d86 253
4882f4a5 254 $art = "$title\n";
255 $art .= "<div class='art'>\n$body\n";
256 if ($app) {
0337d704 257 $art .= "<div class='app'>$app</div>";
258 }
4882f4a5 259 $art .= "</div>\n";
e0e77c5a
FB
260 if (!is_null($hash) && !is_null($login)) {
261 $art = str_replace('%HASH%', "$hash/$login", $art);
262 } else {
263 $art = str_replace('%HASH%', '', $art);
264 }
eaf30d86 265
4882f4a5 266 return $art;
0337d704 267 }
268
269 // }}}
270 // {{{ function check()
271
4882f4a5 272 public function check()
0337d704 273 {
02fdd1c8 274 $text = MiniWiki::WikiToText($this->_body);
4882f4a5 275 $arr = explode("\n",wordwrap($text,68));
276 $c = 0;
277 foreach ($arr as $line) {
0337d704 278 if (trim($line)) {
279 $c++;
280 }
281 }
4882f4a5 282 return $c<9;
0337d704 283 }
284
285 // }}}
9e2a6a32
SJ
286 // {{{ function parseUrlsFromArticle()
287
288 private function parseUrlsFromArticle()
289 {
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';
293
294 $matches = array();
295 $body_matches = array();
296 if (preg_match_all($regex, $this->body(), $body_matches)) {
297 $matches = array_merge($matches, $body_matches[0]);
298 }
299
300 $append_matches = array();
301 if (preg_match_all($regex, $this->append(), $append_matches)) {
302 $matches = array_merge($matches, $append_matches[0]);
303 }
304
305 return $matches;
306 }
307
308 // }}}
309 // {{{ function getLinkIps()
310
955109ba 311 public function getLinkIps(&$blacklist_host_resolution_count)
9e2a6a32
SJ
312 {
313 $matches = $this->parseUrlsFromArticle();
314 $article_ips = array();
315
316 if (!empty($matches)) {
317 global $globals;
318
319 foreach ($matches as $match) {
320 $host = parse_url($match, PHP_URL_HOST);
321 if ($host == '') {
322 list(, $host) = explode('@', $match);
323 }
324
955109ba 325 if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) {
9e2a6a32
SJ
326 break;
327 }
328
955109ba 329 if (!preg_match('/^(' . str_replace(' ', '|', $globals->mail->domain_whitelist) . ')$/i', $host)) {
9e2a6a32 330 $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host));
955109ba 331 ++$blacklist_host_resolution_count;
9e2a6a32
SJ
332 }
333 }
334 }
335
336 return $article_ips;
337 }
338
339 // }}}
0337d704 340}
341
342// }}}
0337d704 343
a7de4ef7 344// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
0337d704 345?>