Interpret **, // and __ as formatting in plain text articles
[banana.git] / banana / misc.inc.php
CommitLineData
3ee590a9 1<?php
2/********************************************************************************
73d5bf46 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/********************************************************************************
2dbc0167 11 * MISC
73d5bf46 12 */
13
0ca6e016 14function _b_($str) { return utf8_decode(dgettext('banana', utf8_encode($str))); }
0a65ec9d 15
987299b4 16function to_entities($str) {
6918d66a 17 require_once dirname(__FILE__).'/utf8.php';
987299b4 18 return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8'));
382606fb
PHM
19}
20
c42efe2f
PHM
21function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
22
8f6f50fb 23function 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
0eb1e7ef 33/** Redirect to the page with the given parameter
34 * @ref makeLink
35 */
9cac5c87 36function redirect($params)
37{
38 header('Location: ' . makeLink($params));
39}
40
0eb1e7ef 41/** Make a link using the given parameters
42 * @param ARRAY params, the parameters with
43 * key => value
44 * Known key are:
45 * - group = group name
46 * - artid/first = article id the the group
47 * - subscribe = to show the subscription page
48 * - action = action to do (new, cancel, view)
49 * - part = to show the given MIME part of the article
50 * - pj = to get the given attachment
51 *
52 * Can be overloaded by defining a hook_makeLink function
53 */
9cac5c87 54function makeLink($params)
55{
0eb1e7ef 56 if (function_exists('hook_makeLink')
57 && $res = hook_makeLink($params)) {
58 return $res;
59 }
60 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
9cac5c87 61 $host = $_SERVER['HTTP_HOST'];
62 $file = $_SERVER['PHP_SELF'];
63
0eb1e7ef 64 if (count($params) != 0) {
65 $get = '?';
66 foreach ($params as $key=>$value) {
67 if (strlen($get) != 1) {
68 $get .= '&';
69 }
70 $get .= $key . '=' . $value;
71 }
72 } else {
73 $get = '';
74 }
75
76 return $proto . $host . $file . $get;
77}
78
79/** Format a link to be use in a link
80 * @ref makeLink
81 */
82function makeHREF($params, $text = null)
83{
84 $link = makeLink($params);
85 if (is_null($text)) {
86 $text = $link;
87 }
88 if ($params['action'] == 'view') {
89 $target = ' target="_blank"';
90 }
91 return '<a href="' . htmlentities($link) . $target . '">' . $text . '</a>';
9cac5c87 92}
93
8f6f50fb 94/********************************************************************************
95 * HTML STUFF
96 * Taken from php.net
97 */
98
5d133234 99/**
8f6f50fb 100 * @return string
101 * @param string
102 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
103 */
104function removeEvilTags($source)
105{
73ab762c 106 $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p><small><big><sup><sub><code><em>';
76032c26 107 $source = preg_replace('|</div>|i', '<br />', $source);
8f6f50fb 108 $source = strip_tags($source, $allowedTags);
109 return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
110}
111
112/**
113 * @return string
114 * @param string
115 * @desc Strip forbidden attributes from a tag
116 */
117function removeEvilAttributes($tagSource)
118{
119 $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
120 'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
121 return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
122}
123
5d133234 124/** Convert html to plain text
125 */
126function htmlToPlainText($res)
127{
76032c26 128 $res = trim(html_entity_decode(strip_tags($res, '<div><br><p>')));
129 $res = preg_replace("@</?(br|p|div)[^>]*>@i", "\n", $res);
e27ae1a3 130 if (!is_utf8($res)) {
131 $res = utf8_encode($res);
132 }
5d133234 133 return $res;
134}
135
136/********************************************************************************
137 * RICHTEXT STUFF
138 */
139
140/** Convert richtext to html
141 */
142function richtextToHtml($source)
143{
144 $tags = Array('bold' => 'b',
145 'italic' => 'i',
146 'smaller' => 'small',
147 'bigger' => 'big',
148 'underline' => 'u',
149 'subscript' => 'sub',
150 'superscript' => 'sup',
151 'excerpt' => 'blockquote',
152 'paragraph' => 'p',
153 'nl' => 'br'
154 );
155
156 // clean unsupported tags
157 $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
158 $source = strip_tags($source, $protectedTags);
159
160 // convert richtext tags to html
161 foreach (array_keys($tags) as $tag) {
162 $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
163 }
164
165 // some special cases
166 $source = preg_replace('@<signature>@i', '<br>-- <br>', $source);
167 $source = preg_replace('@</signature>@i', '', $source);
168 $source = preg_replace('@<lt>@i', '&lt;', $source);
169 $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
170 return removeEvilAttributes($source);
171}
172
73d5bf46 173/********************************************************************************
174 * HEADER STUFF
175 */
176
dd7d1c59 177function _headerdecode($charset, $c, $str) {
67d2dcfc 178 $s = ($c == 'Q' || $c == 'q') ? quoted_printable_decode($str) : base64_decode($str);
dd7d1c59 179 $s = iconv($charset, 'iso-8859-15', $s);
180 return str_replace('_', ' ', $s);
181}
182
183function headerDecode($value) {
67d2dcfc 184 $val = preg_replace('/(=\?[^?]*\?[BQbq]\?[^?]*\?=) (=\?[^?]*\?[BQbq]\?[^?]*\?=)/', '\1\2', $value);
185 return preg_replace('/=\?([^?]*)\?([BQbq])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
dd7d1c59 186}
187
9cf3f056
PHM
188function headerEncode($value, $trim = 0) {
189 if ($trim) {
d752880f
PHM
190 if (strlen($value) > $trim) {
191 $value = substr($value, 0, $trim) . "[...]";
192 }
9cf3f056
PHM
193 }
194 return "=?UTF-8?B?".base64_encode($value)."?=";
195}
196
e2cae7e3 197function header_translate($hdr) {
d4c19591 198 switch ($hdr) {
0a65ec9d 199 case 'from': return _b_('De');
200 case 'subject': return _b_('Sujet');
201 case 'newsgroups': return _b_('Forums');
d4c19591 202 case 'followup-to': return _b_('Suivi-à');
0a65ec9d 203 case 'date': return _b_('Date');
204 case 'organization': return _b_('Organisation');
205 case 'references': return _b_('Références');
d4c19591 206 case 'x-face': return _b_('Image');
e2cae7e3 207 default:
559be3d6 208 if (function_exists('hook_headerTranslate')
209 && $res = hook_headerTranslate($hdr)) {
210 return $res;
2dbc0167 211 }
e2cae7e3 212 return $hdr;
213 }
214}
215
2dbc0167 216function formatDisplayHeader($_header,$_text) {
217 global $banana;
218 switch ($_header) {
219 case "date":
220 return formatDate($_text);
221
222 case "followup-to":
223 case "newsgroups":
224 $res = "";
225 $groups = preg_split("/[\t ]*,[\t ]*/",$_text);
226 foreach ($groups as $g) {
0eb1e7ef 227 $res .= makeHREF(Array('group' => $g), $g) . ', ';
2dbc0167 228 }
229 return substr($res,0, -2);
230
231 case "from":
232 return formatFrom($_text);
233
234 case "references":
235 $rsl = "";
236 $ndx = 1;
237 $text = str_replace("><","> <",$_text);
238 $text = preg_split("/[ \t]/",strtr($text,$banana->spool->ids));
239 $parents = preg_grep("/^\d+$/",$text);
240 $p = array_pop($parents);
58d1740e 241 $par_ok = Array();
2dbc0167 242
243 while ($p) {
58d1740e 244 $par_ok[]=$p;
2dbc0167 245 $p = $banana->spool->overview[$p]->parent;
246 }
58d1740e 247 foreach (array_reverse($par_ok) as $p) {
0eb1e7ef 248 $rsl .= makeHREF(Array('group' => $banana->spool->group), $ndx);
2dbc0167 249 $ndx++;
250 }
251 return $rsl;
252
253 case "x-face":
09a8b2b0 254 return '<img src="xface.php?face='.urlencode(base64_encode($_text)).'" alt="x-face" />';
2dbc0167 255
256 default:
559be3d6 257 if (function_exists('hook_formatDisplayHeader')
258 && $res = hook_formatDisplayHeader($_header, $_text))
259 {
260 return $res;
2dbc0167 261 }
262 return htmlentities($_text);
263 }
264}
265
73d5bf46 266/********************************************************************************
267 * FORMATTING STUFF
268 */
269
e2cae7e3 270function formatDate($_text) {
271 return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($_text));
272}
273
274function fancyDate($stamp) {
275 $today = intval(time() / (24*3600));
276 $dday = intval($stamp / (24*3600));
277
278 if ($today == $dday) {
279 $format = "%H:%M";
280 } elseif ($today == 1 + $dday) {
0a65ec9d 281 $format = _b_('hier')." %H:%M";
e2cae7e3 282 } elseif ($today < 7 + $dday) {
1248ebac 283 $format = '%a %H:%M';
e2cae7e3 284 } else {
285 $format = '%a %e %b';
286 }
287 return strftime($format, $stamp);
288}
289
dd7d1c59 290function formatFrom($text) {
291# From: mark@cbosgd.ATT.COM
292# From: mark@cbosgd.ATT.COM (Mark Horton)
293# From: Mark Horton <mark@cbosgd.ATT.COM>
294 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
295
296 $result = htmlentities($text);
297 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
298 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
299 }
300 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
301 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
302 }
303 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
304 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
305 }
306 return preg_replace("/\\\(\(|\))/","\\1",$result);
307}
308
f78f34d2 309function displayshortcuts($first = -1) {
41cf00eb 310 global $banana;
8d99c683 311 extract($banana->state);
312
559be3d6 313 $res = '<div class="banana_scuts">';
0eb1e7ef 314 $res .= '[' . makeHREF(Array(), _b_('Liste des forums')) . '] ';
8d99c683 315 if (is_null($group)) {
0eb1e7ef 316 return $res.'[' . makeHREF(Array('subscribe' => 1), _b_('Abonnements')) . ']</div>';
8d99c683 317 }
318
0eb1e7ef 319 $res .= '[' . makeHREF(Array('group' => $group), $group) . '] ';
8d99c683 320
321 if (is_null($artid)) {
0eb1e7ef 322 $res .= '[' . makeHREF(Array('group' => $group,
323 'action' => 'new'),
324 _b_('Nouveau message'))
325 . '] ';
8d99c683 326 if (sizeof($banana->spool->overview)>$banana->tmax) {
327 $res .= '<br />';
328 $n = intval(log(count($banana->spool->overview), 10))+1;
329 for ($ndx=1; $ndx <= sizeof($banana->spool->overview); $ndx += $banana->tmax) {
330 if ($first==$ndx) {
331 $fmt = "[%0{$n}u-%0{$n}u] ";
332 } else {
0eb1e7ef 333 $fmt = '[' . makeHREF(Array('group' => $group,
334 'first' => $ndx),
335 '%0' . $n . 'u-%0' . $n . 'u')
336 . '] ';
f78f34d2 337 }
8d99c683 338 $res .= sprintf($fmt, $ndx, min($ndx+$banana->tmax-1,sizeof($banana->spool->overview)));
f78f34d2 339 }
8d99c683 340 }
341 } else {
0eb1e7ef 342 $res .= '[' . makeHREF(Array('group' => $group,
343 'artid' => $artid,
344 'action' => 'new'),
345 _b_('Répondre'))
346 . '] ';
940ae667 347 if ($banana->post && $banana->post->checkcancel()) {
0eb1e7ef 348 $res .= '[' . makeHREF(Array('group' => $group,
349 'artid' => $artid,
350 'action' => 'cancel'),
351 _b_('Annuler ce message'))
352 . '] ';
8d99c683 353 }
f78f34d2 354 }
8d99c683 355 return $res.'</div>';
f78f34d2 356}
357
358/********************************************************************************
359 * FORMATTING STUFF : BODY
360 */
361
28fb1083 362function autoformat($text)
363{
364 global $banana;
365 $length = $banana->wrap;
366
367 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length, all=>1 };'";
368 exec($cmd, $result, $ret);
369 if ($ret != 0) {
9ee36ea4 370 $result = split("\n", $text);
28fb1083 371 }
372 return $result;
373}
374
e04e6059 375function wrap($text, $_prefix="", $_force=false)
cced14b6 376{
dd7d1c59 377 $parts = preg_split("/\n-- ?\n/", $text);
276debfc 378 if (count($parts) >1) {
379 $sign = "\n-- \n" . array_pop($parts);
380 $text = join("\n-- \n", $parts);
3ee590a9 381 } else {
276debfc 382 $sign = '';
3ee590a9 383 }
2dbc0167 384
385 global $banana;
e04e6059 386 $url = $banana->url_regexp;
2dbc0167 387 $length = $banana->wrap;
e04e6059 388 $max = $length + ($length/10);
389 $splits = split("\n", $text);
28fb1083 390 $result = array();
391 $next = array();
392 $format = false;
e04e6059 393 foreach ($splits as $line) {
394 if ($_force || strlen($line) > $max) {
28fb1083 395 if (preg_match("!^(.*)($url)(.*)!i", $line, $matches) && strlen($matches[2]) > $length && strlen($matches) < 900) {
396 if (strlen($matches[1]) != 0) {
397 array_push($next, rtrim($matches[1]));
398 if (strlen($matches[1]) > $max) {
399 $format = true;
400 }
401 }
402
403 if ($format) {
404 $result = array_merge($result, autoformat(join("\n", $next)));
405 } else {
406 $result = array_merge($result, $next);
407 }
408 $format = false;
409 $next = array();
410 array_push($result, $matches[2]);
411
412 if (strlen($matches[6]) != 0) {
413 array_push($next, ltrim($matches[6]));
414 if (strlen($matches[6]) > $max) {
415 $format = true;
416 }
417 }
418 } else {
419 $format = true;
420 array_push($next, $line);
e04e6059 421 }
28fb1083 422 } else {
423 array_push($next, $line);
e04e6059 424 }
425 }
28fb1083 426 if ($format) {
427 $result = array_merge($result, autoformat(join("\n", $next)));
428 } else {
429 $result = array_merge($result, $next);
ee4709c7 430 }
3ee590a9 431
f8e23519 432 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
3ee590a9 433}
434
e04e6059 435function cutlink($link)
436{
437 global $banana;
438
439 if (strlen($link) > $banana->wrap) {
440 $link = substr($link, 0, $banana->wrap - 3)."...";
441 }
442 return $link;
443}
444
4c921acf 445function cleanurl($url)
446{
447 $url = str_replace('@', '%40', $url);
448 return '<a href="'.$url.'" title="'.$url.'">'.cutlink($url).'</a>';
449}
450
e04e6059 451function formatbody($_text, $format='plain', $flowed=false)
8f6f50fb 452{
453 if ($format == 'html') {
76032c26 454 $res = '<br/>'.html_entity_decode(to_entities(removeEvilTags($_text))).'<br/>';
5d133234 455 } else if ($format == 'richtext') {
09f71105 456 $res = '<br/>'.html_entity_decode(to_entities(richtextToHtml($_text))).'<br/>';
8f6f50fb 457 } else {
e04e6059 458 $res = "\n\n" . to_entities(wrap($_text, "", $flowed))."\n\n";
8f6f50fb 459 }
8f6f50fb 460
09f71105 461 if ($format != 'html') {
462 global $banana;
463 $url = $banana->url_regexp;
464 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
4c921acf 465 $res = preg_replace("!$url!ie", "'\\1'.cleanurl('\\2').'\\3'", $res);
09f71105 466 $res = preg_replace('/(["\[])?(?:mailto:)?([a-z0-9.\-+_]+@[a-z0-9.\-+_]+)(["\]])?/i', '\1<a href="mailto:\2">\2</a>\3', $res);
467 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
468
469 if ($format == 'richtext') {
470 $format = 'html';
471 }
472 }
e04e6059 473
8f6f50fb 474 if ($format == 'html') {
9c192e03 475 $res = preg_replace("@(</p>)\n?-- ?\n?(<p[^>]*>|<br[^>]*>)@", "\\1<br/>-- \\2", $res);
476 $res = preg_replace("@<br[^>]*>\n?-- ?\n?(<p[^>]*>)@", "<br/>-- <br/>\\2", $res);
477 $res = preg_replace("@(<pre[^>]*>)\n?-- ?\n@", "<br/>-- <br/>\\1", $res);
478 $parts = preg_split("@(:?<p[^>]*>\n?-- ?\n?</p>|<br[^>]*>\n?-- ?\n?<br[^>]*>)@", $res);
f5eb6c66 479 } else {
9ee36ea4 480 while (preg_match("@(^|<pre>|\n)&gt;@i", $res)) {
b7a8ff33 481 $res = preg_replace("@(^|<pre>|\n)((&gt;[^\n]*\n)+)@ie",
9ee36ea4 482 "'\\1</pre><blockquote><pre>'"
4c1df590 483 .".stripslashes(preg_replace('@(^|<pre>|\n)&gt;[ \\t\\r]*@i', '\\1', '\\2'))"
484 .".'</pre></blockquote><pre>'",
485 $res);
73afa785 486 }
73dfe367 487 $formatting = Array('\*' => 'strong',
488 '_' => 'u',
489 '/' => 'em');
490 foreach ($formatting as $limit=>$mark) {
491 $res = preg_replace('@' . $limit . '([^\s]+)' . $limit . '@', "<$mark>\\1</$mark>", $res);
492 }
493 $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res);
f5eb6c66 494 $parts = preg_split("/\n-- ?\n/", $res);
8f6f50fb 495 }
496
dd7d1c59 497 if (count($parts) > 1) {
f5eb6c66 498 $sign = array_pop($parts);
499 if ($format == 'html') {
500 $res = join('<br/>-- <br/>', $parts);
73afa785 501 $sign = '<hr style="width: 100%; margin: 1em 0em; " />'.$sign.'<br/>';
f5eb6c66 502 } else {
503 $res = join('\n-- \n', $parts);
504 $sign = '</pre><hr style="width: 100%; margin: 1em 0em; " /><pre>'.$sign;
505 }
506 return $res.$sign;
dd7d1c59 507 } else {
508 return $res;
509 }
cced14b6 510}
511
3ee590a9 512?>