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