2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ requires + defines
24 require_once("xorg.misc.inc.php");
27 $page->addCssLink('nl.css');
34 // {{{ class NewsLetter
52 function NewsLetter($id=null
)
56 $res = XDB
::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
57 $id = $res->fetchOneCell();
59 $res = XDB
::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
61 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
62 if (!$res->numRows()) {
65 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
67 $nl = $res->fetchOneAssoc();
69 $this->_id
= $nl['id'];
70 $this->_shortname
= $nl['short_name'];
71 $this->_date
= $nl['date'];
72 $this->_title
= $nl['titre'];
73 $this->_title_mail
= $nl['titre_mail'];
74 $this->_head
= $nl['head'];
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;
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)
87 ORDER BY c.pos,a.pos", $this->_id
);
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);
94 // {{{ function setSent()
98 XDB
::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id
);
102 // {{{ function save()
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
);
115 return is_null($this->_shortname
) ?
$this->_id
: $this->_shortname
;
119 // {{{ function title()
121 function title($mail = false
) {
123 return $this->_title_mail
;
125 return $this->_title
;
129 // {{{ function head()
131 function head($prenom = null
, $nom = null
, $sexe = null
, $type = 'text')
133 if (is_null($prenom)) {
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);
143 $head = enriched_to_text($head, true
);
150 // {{{ funciton getCss()
154 $css = file_get_contents(dirname(__FILE__
) . '/../htdocs/css/nl.css');
155 return preg_replace('@/\*.*?\*/@s', '', $css);
159 // {{{ function getArt()
161 function getArt($aid)
163 foreach ($this->_arts
as $key=>$artlist) {
164 if (isset($artlist["a$aid"])) {
165 return $artlist["a$aid"];
172 // {{{ function saveArticle()
174 function saveArticle(&$a)
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;
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
187 $this->_id
, $a->_cid
, $a->_title
, $a->_body
, $a->_append
, $this->_id
);
188 $this->_arts
['a'.$a->_aid
] = $a;
193 // {{{ function delArticle()
195 function delArticle($aid)
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"]);
204 // {{{ function toText()
206 function toText(&$page, $prenom,$nom,$sexe)
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);
217 // {{{ function toHtml()
219 function toHtml(&$page, $prenom, $nom, $sexe)
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);
231 // {{{ function sendTo()
233 function sendTo($prenom, $nom, $login, $sex, $html)
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
);
244 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
245 $mailer->send($html);
252 // {{{ class NLArticle
268 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
270 $this->_body
= $body;
271 $this->_title
= $title;
272 $this->_append
= $append;
279 // {{{ function title()
282 { return trim($this->_title
); }
285 // {{{ function body()
288 { return trim($this->_body
); }
291 // {{{ function append()
294 { return trim($this->_append
); }
297 // {{{ function toText()
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";
308 // {{{ function toHtml()
312 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
313 $body = enriched_to_text($this->_body
,true
);
314 $app = enriched_to_text($this->_append
,true
);
317 $art .= "<div class='art'>\n$body\n";
319 $art .= "<div class='app'>$app</div>";
327 // {{{ function check()
331 $text = enriched_to_text($this->_body
);
332 $arr = explode("\n",wordwrap($text,68));
334 foreach ($arr as $line) {
348 function insert_new_nl()
350 XDB
::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
353 function get_nl_slist()
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");
356 return $res->fetchAllAssoc();
359 function get_nl_list()
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");
362 return $res->fetchAllAssoc();
365 function get_nl_state()
367 $res = XDB
::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
368 return $res->fetchOneCell();
371 function unsubscribe_nl()
373 XDB
::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
376 function subscribe_nl($uid=-1)
378 $user = ($uid == -1) ? S
::v('uid') : $uid;
379 XDB
::execute('REPLACE INTO newsletter_ins (user_id,last)
380 VALUES ({?}, 0)', $user);
383 function justify($text,$n)
385 $arr = explode("\n",wordwrap($text,$n));
386 $arr = array_map('trim',$arr);
388 foreach ($arr as $key => $line) {
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;
394 if (strlen($line)+
1+
$nxw_len < $n) {
399 if (preg_match('![.:;]$!',$line)) {
404 $tmp = preg_split('! +!',trim($line));
405 $words = count($tmp);
411 $len = array_sum(array_map('strlen',$tmp));
413 $sw = floatval($empty) / floatval($words-1);
417 foreach ($tmp as $word) {
419 $cur +
= $sw +
strlen($word);
420 $l = str_pad($l,intval($cur+
0.5));
422 $res .= trim($l)."\n";
427 function enriched_to_text($input,$html=false
,$just=false
,$indent=0,$width=68)
429 $text = trim($input);
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);
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);
449 $ind = str_pad('',$indent);
450 $text = $ind.str_replace("\n","\n$ind",$text);
458 // vim:set et sw=4 sts=4 sws=4: