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