0337d704 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 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 | |
22 | // {{{ requires + defines |
23 | |
24 | require_once("xorg.misc.inc.php"); |
0337d704 |
25 | |
0337d704 |
26 | define('FEMME', 1); |
27 | define('HOMME', 0); |
28 | |
29 | // }}} |
4882f4a5 |
30 | // {{{ class MassMailer |
31 | |
32 | abstract class MassMailer |
33 | { |
34 | private $_tpl; |
35 | private $_css; |
36 | private $_prefix; |
37 | |
38 | public $_id; |
39 | public $_shortname; |
40 | public $_title; |
41 | public $_title_mail; |
42 | |
43 | public $_head; |
44 | |
45 | function __construct($tpl, $css, $prefix) |
46 | { |
47 | $this->_tpl = $tpl; |
48 | $this->_css = $css; |
49 | $this->_prefix = $prefix; |
50 | } |
51 | |
52 | public function id() |
53 | { |
54 | return is_null($this->_shortname) ? $this->_id : $this->_shortname; |
55 | } |
56 | |
57 | public function title($mail = false) |
58 | { |
59 | return $mail ? $this->_title_mail : $this->_title; |
60 | } |
61 | |
62 | public function head($prenom = null, $nom = null, $sexe = null, $type = 'text') |
63 | { |
64 | if (is_null($prenom)) { |
65 | return $this->_head; |
66 | } else { |
67 | $head = $this->_head; |
68 | $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head); |
69 | $head = str_replace('<prenom>', $prenom, $head); |
70 | $head = str_replace('<nom>', $nom, $head); |
71 | if ($type == 'text') { |
72 | $head = enriched_to_text($head, false, true, 2, 64); |
73 | } else { |
74 | $head = enriched_to_text($head, true); |
75 | } |
76 | return $head; |
77 | } |
78 | } |
79 | |
80 | public function css(&$page = null) |
81 | { |
82 | if (!is_null($page)) { |
83 | $page->addCssLink($this->_css); |
84 | return true; |
85 | } else { |
86 | $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->_css); |
87 | return preg_replace('@/\*.*?\*/@s', '', $css); |
88 | } |
89 | } |
90 | |
91 | public function toText(&$page, $prenom, $nom, $sexe) |
92 | { |
93 | $this->css($page); |
94 | $page->assign('is_mail', false); |
95 | $page->assign('html_version', false); |
96 | $page->assign('prenom', $prenom); |
97 | $page->assign('nom', $nom); |
98 | $page->assign('sexe', $sexe); |
99 | $this->assignData($page); |
100 | } |
101 | |
102 | public function toHtml(&$page, $prenom, $nom, $sexe) |
103 | { |
104 | $this->css($page); |
105 | $page->assign('prefix', $this->_prefix . '/' . $this->id()); |
106 | $page->assign('is_mail', false); |
107 | $page->assign('html_version', true); |
108 | $page->assign('prenom', $prenom); |
109 | $page->assign('nom', $nom); |
110 | $page->assign('sexe', $sexe); |
111 | $this->assignData($page); |
112 | } |
113 | |
114 | public function sendTo($prenom, $nom, $login, $sexe, $html) |
115 | { |
116 | global $globals; |
117 | |
118 | $mailer = new PlMailer($this->_tpl); |
119 | $this->assignData($mailer); |
120 | $mailer->assign('is_mail', true); |
121 | $mailer->assign('prenom', $prenom); |
122 | $mailer->assign('nom', $nom); |
123 | $mailer->assign('sexe', $sexe); |
124 | $mailer->assign('prefix', null); |
125 | $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>"); |
126 | $mailer->send($html); |
127 | } |
128 | |
129 | public function sendToAll() |
130 | { |
131 | $this->setSent(); |
132 | $query = "SELECT u.user_id, a.alias, |
133 | u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage), |
134 | FIND_IN_SET('femme', u.flags), |
135 | q.core_mail_fmt AS pref |
136 | FROM {$this->subscriptionTable()} AS ni |
137 | INNER JOIN auth_user_md5 AS u USING(user_id) |
138 | INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id) |
139 | INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) |
140 | LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active') |
141 | WHERE ({$this->subscriptionWhere()}) AND e.email IS NOT NULL |
142 | GROUP BY u.user_id |
143 | LIMIT 60"; |
144 | while (true) { |
145 | $res = XDB::iterRow($query); |
146 | if (!$res->total()) { |
147 | exit; |
148 | } |
149 | $sent = array(); |
150 | while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt) = $res->next()) { |
151 | $sent[] = "user_id='$uid'"; |
152 | $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html'); |
153 | } |
154 | XDB::execute("UPDATE {$this->subscriptionTable()} |
155 | SET {$this->subscriptionUpdate()} |
156 | WHERE " . implode(' OR ', $sent)); |
157 | sleep(60); |
158 | } |
159 | } |
160 | |
161 | abstract protected function assignData(&$smarty); |
162 | abstract protected function setSent(); |
163 | abstract static public function subscribe($uid = -1); |
164 | abstract static public function unsubscribe($uid = -1); |
165 | abstract static public function subscriptionState($uid = -1); |
166 | |
167 | abstract protected function subscriptionTable(); |
168 | abstract protected function subscriptionWhere(); |
169 | abstract protected function subscriptionUpdate(); |
170 | } |
171 | |
172 | // }}} |
0337d704 |
173 | // {{{ class NewsLetter |
174 | |
4882f4a5 |
175 | class NewsLetter extends MassMailer |
0337d704 |
176 | { |
4882f4a5 |
177 | public $_date; |
178 | public $_cats = Array(); |
179 | public $_arts = Array(); |
0337d704 |
180 | |
4882f4a5 |
181 | function __construct($id = null) |
0337d704 |
182 | { |
4882f4a5 |
183 | parent::__construct('newsletter/nl.tpl', 'nl.css', 'nl/show'); |
ed76a506 |
184 | if (isset($id)) { |
185 | if ($id == 'last') { |
186 | $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'"); |
0337d704 |
187 | $id = $res->fetchOneCell(); |
ed76a506 |
188 | } |
189 | $res = XDB::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id); |
190 | } else { |
191 | $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'"); |
192 | if (!$res->numRows()) { |
4882f4a5 |
193 | Newsletter::create(); |
ed76a506 |
194 | } |
195 | $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'"); |
ad6ca77d |
196 | } |
ed76a506 |
197 | $nl = $res->fetchOneAssoc(); |
0337d704 |
198 | |
ed76a506 |
199 | $this->_id = $nl['id']; |
200 | $this->_shortname = $nl['short_name']; |
201 | $this->_date = $nl['date']; |
202 | $this->_title = $nl['titre']; |
8269d2d6 |
203 | $this->_title_mail = $nl['titre_mail']; |
ed76a506 |
204 | $this->_head = $nl['head']; |
0337d704 |
205 | |
ed76a506 |
206 | $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos"); |
207 | while (list($cid, $title) = $res->next()) { |
208 | $this->_cats[$cid] = $title; |
209 | } |
ad6ca77d |
210 | |
ed76a506 |
211 | $res = XDB::iterRow( |
0337d704 |
212 | "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos |
213 | FROM newsletter_art AS a |
214 | INNER JOIN newsletter AS n USING(id) |
215 | LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid) |
216 | WHERE a.id={?} |
217 | ORDER BY c.pos,a.pos", $this->_id); |
ed76a506 |
218 | while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) { |
219 | $this->_arts[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos); |
220 | } |
0337d704 |
221 | } |
222 | |
4882f4a5 |
223 | public function save() |
0337d704 |
224 | { |
8269d2d6 |
225 | XDB::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}', |
226 | $this->_date, $this->_title, $this->_title_mail, $this->_head, $this->_shortname,$this->_id); |
ed76a506 |
227 | } |
4882f4a5 |
228 | |
229 | public function getArt($aid) |
0337d704 |
230 | { |
11f44323 |
231 | foreach ($this->_arts as $key=>$artlist) { |
232 | if (isset($artlist["a$aid"])) { |
0337d704 |
233 | return $artlist["a$aid"]; |
234 | } |
11f44323 |
235 | } |
236 | return null; |
0337d704 |
237 | } |
238 | |
4882f4a5 |
239 | public function saveArticle(&$a) |
0337d704 |
240 | { |
11f44323 |
241 | if ($a->_aid>=0) { |
242 | XDB::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append) |
243 | VALUES ({?},{?},{?},{?},{?},{?},{?})', |
244 | $this->_id, $a->_aid, $a->_cid, $a->_pos, |
245 | $a->_title, $a->_body, $a->_append); |
246 | $this->_arts['a'.$a->_aid] = $a; |
247 | } else { |
248 | XDB::execute('INSERT INTO newsletter_art |
249 | SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?} |
250 | FROM newsletter_art AS a |
251 | WHERE a.id={?}', |
252 | $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id); |
253 | $this->_arts['a'.$a->_aid] = $a; |
254 | } |
0337d704 |
255 | } |
4882f4a5 |
256 | |
257 | public function delArticle($aid) |
0337d704 |
258 | { |
11f44323 |
259 | XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid); |
260 | foreach ($this->_arts as $key=>$art) { |
261 | unset($this->_arts[$key]["a$aid"]); |
262 | } |
0337d704 |
263 | } |
264 | |
4882f4a5 |
265 | protected function assignData(&$smarty) |
266 | { |
267 | $smarty->assign_by_ref('nl', $this); |
268 | } |
0337d704 |
269 | |
4882f4a5 |
270 | protected function setSent() |
0337d704 |
271 | { |
4882f4a5 |
272 | XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id); |
0337d704 |
273 | } |
274 | |
4882f4a5 |
275 | protected function subscriptionTable() |
0337d704 |
276 | { |
4882f4a5 |
277 | return 'newsletter_ins'; |
0337d704 |
278 | } |
279 | |
4882f4a5 |
280 | protected function subscriptionWhere() |
0337d704 |
281 | { |
4882f4a5 |
282 | return 'ni.last<' . $this->_id; |
283 | } |
0337d704 |
284 | |
4882f4a5 |
285 | protected function subscriptionUpdate() |
286 | { |
287 | return 'last=' . $this->_id; |
0337d704 |
288 | } |
289 | |
4882f4a5 |
290 | static public function subscriptionState($uid = -1) |
291 | { |
292 | $user = ($uid == -1) ? S::v('uid') : $uid; |
293 | $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', $user); |
294 | return $res->fetchOneCell(); |
295 | } |
296 | |
297 | static public function unsubscribe($uid = -1) |
298 | { |
299 | $user = ($uid == -1) ? S::v('uid') : $uid; |
300 | XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', $user); |
301 | } |
302 | |
303 | static public function subscribe($uid = -1) |
304 | { |
305 | $user = ($uid == -1) ? S::v('uid') : $uid; |
306 | XDB::execute('REPLACE INTO newsletter_ins (user_id,last) VALUES ({?}, 0)', $user); |
307 | } |
308 | |
309 | static public function create() |
310 | { |
311 | XDB::execute("INSERT INTO newsletter |
312 | SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'"); |
313 | } |
314 | |
315 | static public function listSent() |
316 | { |
317 | $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre |
318 | FROM newsletter |
319 | WHERE bits!='new' |
320 | ORDER BY date DESC"); |
321 | return $res->fetchAllAssoc(); |
322 | } |
323 | |
324 | static public function listAll() |
325 | { |
326 | $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre |
327 | FROM newsletter |
328 | ORDER BY date DESC"); |
329 | return $res->fetchAllAssoc(); |
330 | } |
0337d704 |
331 | } |
332 | |
333 | // }}} |
334 | // {{{ class NLArticle |
335 | |
336 | class NLArticle |
337 | { |
338 | // {{{ properties |
339 | |
340 | var $_aid; |
341 | var $_cid; |
342 | var $_pos; |
343 | var $_title; |
344 | var $_body; |
345 | var $_append; |
346 | |
347 | // }}} |
348 | // {{{ constructor |
349 | |
4882f4a5 |
350 | function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0) |
0337d704 |
351 | { |
4882f4a5 |
352 | $this->_body = $body; |
353 | $this->_title = $title; |
354 | $this->_append = $append; |
355 | $this->_aid = $aid; |
356 | $this->_cid = $cid; |
357 | $this->_pos = $pos; |
0337d704 |
358 | } |
359 | |
360 | // }}} |
361 | // {{{ function title() |
362 | |
4882f4a5 |
363 | public function title() |
0337d704 |
364 | { return trim($this->_title); } |
365 | |
366 | // }}} |
367 | // {{{ function body() |
368 | |
4882f4a5 |
369 | public function body() |
0337d704 |
370 | { return trim($this->_body); } |
371 | |
372 | // }}} |
373 | // {{{ function append() |
374 | |
4882f4a5 |
375 | public function append() |
0337d704 |
376 | { return trim($this->_append); } |
377 | |
378 | // }}} |
379 | // {{{ function toText() |
380 | |
4882f4a5 |
381 | public function toText() |
0337d704 |
382 | { |
4882f4a5 |
383 | $title = '*'.$this->title().'*'; |
384 | $body = enriched_to_text($this->_body,false,true); |
385 | $app = enriched_to_text($this->_append,false,false,4); |
386 | return trim("$title\n\n$body\n\n$app")."\n"; |
0337d704 |
387 | } |
388 | |
389 | // }}} |
390 | // {{{ function toHtml() |
391 | |
4882f4a5 |
392 | public function toHtml() |
0337d704 |
393 | { |
4882f4a5 |
394 | $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>'; |
395 | $body = enriched_to_text($this->_body,true); |
396 | $app = enriched_to_text($this->_append,true); |
ad6ca77d |
397 | |
4882f4a5 |
398 | $art = "$title\n"; |
399 | $art .= "<div class='art'>\n$body\n"; |
400 | if ($app) { |
0337d704 |
401 | $art .= "<div class='app'>$app</div>"; |
402 | } |
4882f4a5 |
403 | $art .= "</div>\n"; |
ad6ca77d |
404 | |
4882f4a5 |
405 | return $art; |
0337d704 |
406 | } |
407 | |
408 | // }}} |
409 | // {{{ function check() |
410 | |
4882f4a5 |
411 | public function check() |
0337d704 |
412 | { |
4882f4a5 |
413 | $text = enriched_to_text($this->_body); |
414 | $arr = explode("\n",wordwrap($text,68)); |
415 | $c = 0; |
416 | foreach ($arr as $line) { |
0337d704 |
417 | if (trim($line)) { |
418 | $c++; |
419 | } |
420 | } |
4882f4a5 |
421 | return $c<9; |
0337d704 |
422 | } |
423 | |
424 | // }}} |
425 | } |
426 | |
427 | // }}} |
428 | // {{{ Functions |
429 | |
0337d704 |
430 | function justify($text,$n) |
431 | { |
575dd9be |
432 | $arr = explode("\n",wordwrap($text,$n)); |
0337d704 |
433 | $arr = array_map('trim',$arr); |
434 | $res = ''; |
435 | foreach ($arr as $key => $line) { |
ad6ca77d |
436 | $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : ''; |
437 | $nxl_split = preg_split('! +!',$nxl); |
438 | $nxw_len = count($nxl_split) ? strlen($nxl_split[0]) : 0; |
439 | $line = trim($line); |
440 | |
441 | if (strlen($line)+1+$nxw_len < $n) { |
442 | $res .= "$line\n"; |
443 | continue; |
444 | } |
445 | |
446 | if (preg_match('![.:;]$!',$line)) { |
447 | $res .= "$line\n"; |
448 | continue; |
449 | } |
450 | |
451 | $tmp = preg_split('! +!',trim($line)); |
452 | $words = count($tmp); |
453 | if ($words <= 1) { |
454 | $res .= "$line\n"; |
455 | continue; |
456 | } |
457 | |
458 | $len = array_sum(array_map('strlen',$tmp)); |
459 | $empty = $n - $len; |
460 | $sw = floatval($empty) / floatval($words-1); |
461 | |
462 | $cur = 0; |
463 | $l = ''; |
464 | foreach ($tmp as $word) { |
465 | $l .= $word; |
466 | $cur += $sw + strlen($word); |
467 | $l = str_pad($l,intval($cur+0.5)); |
468 | } |
469 | $res .= trim($l)."\n"; |
0337d704 |
470 | } |
471 | return trim($res); |
472 | } |
473 | |
474 | function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) |
475 | { |
476 | $text = trim($input); |
477 | if ($html) { |
ad6ca77d |
478 | $text = htmlspecialchars($text); |
479 | $text = str_replace('[b]','<strong>', $text); |
480 | $text = str_replace('[/b]','</strong>', $text); |
481 | $text = str_replace('[i]','<em>', $text); |
482 | $text = str_replace('[/i]','</em>', $text); |
483 | $text = str_replace('[u]','<span style="text-decoration: underline">', $text); |
484 | $text = str_replace('[/u]','</span>', $text); |
485 | require_once('url_catcher.inc.php'); |
486 | $text = url_catcher($text); |
487 | return nl2br($text); |
0337d704 |
488 | } else { |
ad6ca77d |
489 | $text = preg_replace('!\[\/?b\]!','*',$text); |
490 | $text = preg_replace('!\[\/?u\]!','_',$text); |
491 | $text = preg_replace('!\[\/?i\]!','/',$text); |
492 | $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text); |
493 | $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text); |
494 | $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent); |
495 | if($indent) { |
496 | $ind = str_pad('',$indent); |
497 | $text = $ind.str_replace("\n","\n$ind",$text); |
498 | } |
499 | return $text; |
0337d704 |
500 | } |
501 | } |
502 | |
503 | // }}} |
504 | |
505 | // vim:set et sw=4 sts=4 sws=4: |
506 | ?> |