-// {{{ Functions
-
-function insert_new_nl()
-{
- XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
-}
-
-function get_nl_slist()
-{
- $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre FROM newsletter ORDER BY date DESC");
- return $res->fetchAllAssoc();
-}
-
-function get_nl_list()
-{
- $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
- return $res->fetchAllAssoc();
-}
-
-function get_nl_state()
-{
- $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
- return $res->fetchOneCell();
-}
-
-function unsubscribe_nl()
-{
- XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
-}
-
-function subscribe_nl($uid=-1)
-{
- $user = ($uid == -1) ? S::v('uid') : $uid;
- XDB::execute('REPLACE INTO newsletter_ins (user_id,last)
- VALUES ({?}, 0)', $user);
-}
-
-function justify($text,$n)
-{
- $arr = explode("\n",wordwrap($text,$n));
- $arr = array_map('trim',$arr);
- $res = '';
- foreach ($arr as $key => $line) {
- $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
- $nxl_split = preg_split('! +!',$nxl);
- $nxw_len = count($nxl_split) ? strlen($nxl_split[0]) : 0;
- $line = trim($line);
-
- if (strlen($line)+1+$nxw_len < $n) {
- $res .= "$line\n";
- continue;
- }
-
- if (preg_match('![.:;]$!',$line)) {
- $res .= "$line\n";
- continue;
- }
-
- $tmp = preg_split('! +!',trim($line));
- $words = count($tmp);
- if ($words <= 1) {
- $res .= "$line\n";
- continue;
- }
-
- $len = array_sum(array_map('strlen',$tmp));
- $empty = $n - $len;
- $sw = floatval($empty) / floatval($words-1);
-
- $cur = 0;
- $l = '';
- foreach ($tmp as $word) {
- $l .= $word;
- $cur += $sw + strlen($word);
- $l = str_pad($l,intval($cur+0.5));
- }
- $res .= trim($l)."\n";
- }
- return trim($res);
-}
-
-function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
-{
- $text = trim($input);
- if ($html) {
- $text = htmlspecialchars($text);
- $text = str_replace('[b]','<strong>', $text);
- $text = str_replace('[/b]','</strong>', $text);
- $text = str_replace('[i]','<em>', $text);
- $text = str_replace('[/i]','</em>', $text);
- $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
- $text = str_replace('[/u]','</span>', $text);
- require_once('url_catcher.inc.php');
- $text = url_catcher($text);
- return nl2br($text);
- } else {
- $text = preg_replace('!\[\/?b\]!','*',$text);
- $text = preg_replace('!\[\/?u\]!','_',$text);
- $text = preg_replace('!\[\/?i\]!','/',$text);
- $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
- $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
- $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
- if($indent) {
- $ind = str_pad('',$indent);
- $text = $ind.str_replace("\n","\n$ind",$text);
- }
- return $text;
- }
-}
-
-// }}}