| 1 | <?php |
| 2 | /******************************************************************************** |
| 3 | * include/misc.inc.php : Misc functions |
| 4 | * ------------------------- |
| 5 | * |
| 6 | * This file is part of the banana distribution |
| 7 | * Copyright: See COPYING files that comes with this distribution |
| 8 | ********************************************************************************/ |
| 9 | |
| 10 | /******************************************************************************** |
| 11 | * MISC |
| 12 | */ |
| 13 | |
| 14 | function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); } |
| 15 | |
| 16 | function to_entities($str) { |
| 17 | require_once dirname(__FILE__).'/utf8.php'; |
| 18 | return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8')); |
| 19 | } |
| 20 | |
| 21 | function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; } |
| 22 | |
| 23 | function textFormat_translate($format) |
| 24 | { |
| 25 | switch (strtolower($format)) { |
| 26 | case 'plain': return _b_('Texte brut'); |
| 27 | case 'richtext': return _b_('Texte enrichi'); |
| 28 | case 'html': return _b_('HTML'); |
| 29 | default: return $format; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /******************************************************************************** |
| 34 | * HTML STUFF |
| 35 | * Taken from php.net |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * @return string |
| 40 | * @param string |
| 41 | * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes() |
| 42 | */ |
| 43 | function removeEvilTags($source) |
| 44 | { |
| 45 | $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p>'; |
| 46 | $source = strip_tags($source, $allowedTags); |
| 47 | return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @return string |
| 52 | * @param string |
| 53 | * @desc Strip forbidden attributes from a tag |
| 54 | */ |
| 55 | function removeEvilAttributes($tagSource) |
| 56 | { |
| 57 | $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'. |
| 58 | 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup'; |
| 59 | return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource)); |
| 60 | } |
| 61 | |
| 62 | /******************************************************************************** |
| 63 | * HEADER STUFF |
| 64 | */ |
| 65 | |
| 66 | function _headerdecode($charset, $c, $str) { |
| 67 | $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str); |
| 68 | $s = iconv($charset, 'iso-8859-15', $s); |
| 69 | return str_replace('_', ' ', $s); |
| 70 | } |
| 71 | |
| 72 | function headerDecode($value) { |
| 73 | $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value); |
| 74 | return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val); |
| 75 | } |
| 76 | |
| 77 | function headerEncode($value, $trim = 0) { |
| 78 | if ($trim) { |
| 79 | if (strlen($value) > $trim) { |
| 80 | $value = substr($value, 0, $trim) . "[...]"; |
| 81 | } |
| 82 | } |
| 83 | return "=?UTF-8?B?".base64_encode($value)."?="; |
| 84 | } |
| 85 | |
| 86 | function header_translate($hdr) { |
| 87 | switch ($hdr) { |
| 88 | case 'from': return _b_('De'); |
| 89 | case 'subject': return _b_('Sujet'); |
| 90 | case 'newsgroups': return _b_('Forums'); |
| 91 | case 'followup-to': return _b_('Suivi-à'); |
| 92 | case 'date': return _b_('Date'); |
| 93 | case 'organization': return _b_('Organisation'); |
| 94 | case 'references': return _b_('Références'); |
| 95 | case 'x-face': return _b_('Image'); |
| 96 | default: |
| 97 | if (function_exists('hook_headerTranslate') |
| 98 | && $res = hook_headerTranslate($hdr)) { |
| 99 | return $res; |
| 100 | } |
| 101 | return $hdr; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function formatDisplayHeader($_header,$_text) { |
| 106 | global $banana; |
| 107 | switch ($_header) { |
| 108 | case "date": |
| 109 | return formatDate($_text); |
| 110 | |
| 111 | case "followup-to": |
| 112 | case "newsgroups": |
| 113 | $res = ""; |
| 114 | $groups = preg_split("/[\t ]*,[\t ]*/",$_text); |
| 115 | foreach ($groups as $g) { |
| 116 | $res.="<a href='?group=$g'>$g</a>, "; |
| 117 | } |
| 118 | return substr($res,0, -2); |
| 119 | |
| 120 | case "from": |
| 121 | return formatFrom($_text); |
| 122 | |
| 123 | case "references": |
| 124 | $rsl = ""; |
| 125 | $ndx = 1; |
| 126 | $text = str_replace("><","> <",$_text); |
| 127 | $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids)); |
| 128 | $parents = preg_grep("/^\d+$/",$text); |
| 129 | $p = array_pop($parents); |
| 130 | $par_ok = Array(); |
| 131 | |
| 132 | while ($p) { |
| 133 | $par_ok[]=$p; |
| 134 | $p = $banana->spool->overview[$p]->parent; |
| 135 | } |
| 136 | foreach (array_reverse($par_ok) as $p) { |
| 137 | $rsl .= "<a href=\"?group={$banana->spool->group}&artid=$p\">$ndx</a> "; |
| 138 | $ndx++; |
| 139 | } |
| 140 | return $rsl; |
| 141 | |
| 142 | case "x-face": |
| 143 | return '<img src="xface.php?face='.base64_encode($_text).'" alt="x-face" />'; |
| 144 | |
| 145 | default: |
| 146 | if (function_exists('hook_formatDisplayHeader') |
| 147 | && $res = hook_formatDisplayHeader($_header, $_text)) |
| 148 | { |
| 149 | return $res; |
| 150 | } |
| 151 | return htmlentities($_text); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /******************************************************************************** |
| 156 | * FORMATTING STUFF |
| 157 | */ |
| 158 | |
| 159 | function formatDate($_text) { |
| 160 | return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text)); |
| 161 | } |
| 162 | |
| 163 | function fancyDate($stamp) { |
| 164 | $today = intval(time() / (24*3600)); |
| 165 | $dday = intval($stamp / (24*3600)); |
| 166 | |
| 167 | if ($today == $dday) { |
| 168 | $format = "%H:%M"; |
| 169 | } elseif ($today == 1 + $dday) { |
| 170 | $format = _b_('hier')." %H:%M"; |
| 171 | } elseif ($today < 7 + $dday) { |
| 172 | $format = '%a %H:%M'; |
| 173 | } else { |
| 174 | $format = '%a %e %b'; |
| 175 | } |
| 176 | return strftime($format, $stamp); |
| 177 | } |
| 178 | |
| 179 | function formatFrom($text) { |
| 180 | # From: mark@cbosgd.ATT.COM |
| 181 | # From: mark@cbosgd.ATT.COM (Mark Horton) |
| 182 | # From: Mark Horton <mark@cbosgd.ATT.COM> |
| 183 | $mailto = '<a href="mailto:'; |
| 184 | |
| 185 | $result = htmlentities($text); |
| 186 | if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) { |
| 187 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[1]."@".$regs[2])."</a>"; |
| 188 | } |
| 189 | if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) { |
| 190 | $result="$mailto{$regs[1]}@{$regs[2]}\">".htmlentities($regs[3])."</a>"; |
| 191 | } |
| 192 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) { |
| 193 | $result="$mailto{$regs[2]}@{$regs[3]}\">".htmlentities($regs[1])."</a>"; |
| 194 | } |
| 195 | return preg_replace("/\\\(\(|\))/","\\1",$result); |
| 196 | } |
| 197 | |
| 198 | function displayshortcuts($first = -1) { |
| 199 | global $banana; |
| 200 | extract($banana->state); |
| 201 | |
| 202 | $res = '<div class="banana_scuts">'; |
| 203 | $res .= '[<a href="?">'._b_('Liste des forums').'</a>] '; |
| 204 | if (is_null($group)) { |
| 205 | return $res.'[<a href="?subscribe=1">'._b_('Abonnements').'</a>]</div>'; |
| 206 | } |
| 207 | |
| 208 | $res .= "[<a href=\"?group=$group\">$group</a>] "; |
| 209 | |
| 210 | if (is_null($artid)) { |
| 211 | $res .= "[<a href=\"?group=$group&action=new\">"._b_('Nouveau message')."</a>] "; |
| 212 | if (sizeof($banana->spool->overview)>$banana->tmax) { |
| 213 | $res .= '<br />'; |
| 214 | $n = intval(log(count($banana->spool->overview), 10))+1; |
| 215 | for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) { |
| 216 | if ($first==$ndx) { |
| 217 | $fmt = "[%0{$n}u-%0{$n}u] "; |
| 218 | } else { |
| 219 | $fmt = "[<a href=\"?group=$group&first=$ndx\">%0{$n}u-%0{$n}u</a>] "; |
| 220 | } |
| 221 | $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview))); |
| 222 | } |
| 223 | } |
| 224 | } else { |
| 225 | $res .= "[<a href=\"?group=$group&artid=$artid&action=new\">" |
| 226 | ._b_('Répondre')."</a>] "; |
| 227 | if ($banana->post->checkcancel()) { |
| 228 | $res .= "[<a href=\"?group=$group&artid=$artid&action=cancel\">" |
| 229 | ._b_('Annuler ce message')."</a>] "; |
| 230 | } |
| 231 | } |
| 232 | return $res.'</div>'; |
| 233 | } |
| 234 | |
| 235 | /******************************************************************************** |
| 236 | * FORMATTING STUFF : BODY |
| 237 | */ |
| 238 | |
| 239 | function wrap($text, $_prefix="") |
| 240 | { |
| 241 | $parts = preg_split("/\n-- ?\n/", $text); |
| 242 | if (count($parts) >1) { |
| 243 | $sign = "\n-- \n" . array_pop($parts); |
| 244 | $text = join("\n-- \n", $parts); |
| 245 | } else { |
| 246 | $sign = ''; |
| 247 | $text = $text; |
| 248 | } |
| 249 | |
| 250 | global $banana; |
| 251 | $length = $banana->wrap; |
| 252 | $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'"; |
| 253 | exec($cmd, $result); |
| 254 | |
| 255 | return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign); |
| 256 | } |
| 257 | |
| 258 | function formatbody($_text, $format='plain') |
| 259 | { |
| 260 | if ($format == 'html') { |
| 261 | $res = '<br/>'.removeEvilTags(html_entity_decode(to_entities($_text))).'<br/>'; |
| 262 | } else { |
| 263 | $res = "\n\n" . to_entities(wrap($_text, ""))."\n\n"; |
| 264 | } |
| 265 | $res = preg_replace("/(<|>|")/", " \\1 ", $res); |
| 266 | $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res); |
| 267 | $res = preg_replace("/ (<|>|") /", "\\1", $res); |
| 268 | |
| 269 | if ($format == 'html') { |
| 270 | $res = preg_replace("@(</p>)\n?-- \n?(<p[^>]*>|<br>)@", "\\1<br>-- \\2", $res); |
| 271 | $res = preg_replace("@<br>\n?-- \n?(<p[^>]*>)@", "<br>-- <br>\\2", $res); |
| 272 | $parts = preg_split("@(:?<p[^>]*>\n?-- \n?</p>|<br[^>]*>\n?-- \n?<br>)@", $res); |
| 273 | } else { |
| 274 | $parts = preg_split("/\n-- ?\n/", $res); |
| 275 | } |
| 276 | |
| 277 | if (count($parts) > 1) { |
| 278 | $sign = array_pop($parts); |
| 279 | if ($format == 'html') { |
| 280 | $res = join('<br/>-- <br/>', $parts); |
| 281 | $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign; |
| 282 | } else { |
| 283 | $res = join('\n-- \n', $parts); |
| 284 | $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign; |
| 285 | } |
| 286 | return $res.$sign; |
| 287 | } else { |
| 288 | return $res; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | ?> |