simplifications
[banana.git] / include / misc.inc.php
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 function _headerdecode($charset, $c, $str) {
11 $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str);
12 $s = iconv($charset, 'iso-8859-15', $s);
13 return str_replace('_', ' ', $s);
14 }
15
16 function headerDecode($value) {
17 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
18 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
19 }
20
21 function formatFrom($text) {
22 # From: mark@cbosgd.ATT.COM
23 # From: mark@cbosgd.ATT.COM (Mark Horton)
24 # From: Mark Horton <mark@cbosgd.ATT.COM>
25 $mailto = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
26
27 $result = htmlentities($text);
28 if (preg_match("/^([^ ]+)@([^ ]+)$/",$text,$regs)) {
29 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[1]."&#64;".$regs[2])."</a>";
30 }
31 if (preg_match("/^([^ ]+)@([^ ]+) \((.*)\)$/",$text,$regs)) {
32 $result="$mailto{$regs[1]}&#64;{$regs[2]}\">".htmlentities($regs[3])."</a>";
33 }
34 if (preg_match("/^\"?([^<>\"]+)\"? +<(.+)@(.+)>$/",$text,$regs)) {
35 $result="$mailto{$regs[2]}&#64;{$regs[3]}\">".htmlentities($regs[1])."</a>";
36 }
37 return preg_replace("/\\\(\(|\))/","\\1",$result);
38 }
39
40 function wrap($text, $_prefix="", $_length=72)
41 {
42 $parts = preg_split("/\n-- ?\n/", $text);
43 if (count($parts) >1) {
44 $sign = "\n-- \n" . array_pop($parts);
45 $text = join("\n-- \n", $parts);
46 } else {
47 $sign = '';
48 $text = $text;
49 }
50
51 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$_length, all=>1 };'";
52 exec($cmd, $result);
53
54 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
55 }
56
57 function formatbody($_text) {
58 global $news;
59 $res = "\n\n" . htmlentities(wrap($_text, "", $news['wrap']))."\n\n";
60 $res = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
61 $res = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
62 $res = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
63
64 $parts = preg_split("/\n-- ?\n/", $res);
65
66 if (count($parts) > 1) {
67 $sign = "</pre><hr style='width: 100%; margin: 1em 0em; ' /><pre>" . array_pop($parts);
68 return join("\n-- \n", $parts).$sign;
69 } else {
70 return $res;
71 }
72 }
73
74 ?>