9731d160be67e10a9bc8791c4ec803d869b4bf05
[banana.git] / include / misc.inc.php
1 <?php
2 /********************************************************************************
3 * include/wrapper.inc.php : text wrapper
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 wrap($_text, $_prefix="", $_length=72)
11 {
12 $parts = preg_split("/\n-- ?\n/", $_text);
13 if (count($parts) >1) {
14 $sign = "\n-- \n" . array_pop($parts);
15 $text = join("\n-- \n", $parts);
16 } else {
17 $sign = '';
18 $text = $_text;
19 }
20
21 $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$_length, all=>1 };'";
22 exec($cmd, $result);
23
24 return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
25 }
26
27 function _headerdecode($charset, $c, $str) {
28 $s = ($c == 'Q') ? quoted_printable_decode($str) : base64_decode($str);
29 $s = iconv($charset, 'iso-8859-15', $s);
30 return str_replace('_', ' ', $s);
31 }
32
33 function headerDecode($value) {
34 $val = preg_replace('/(=\?[^?]*\?[BQ]\?[^?]*\?=) (=\?[^?]*\?[BQ]\?[^?]*\?=)/', '\1\2', $value);
35 return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
36 }
37
38 ?>