/usr/share/php/Horde/Text/Filter/Text2html.php is in php-horde-text-filter 2.3.3-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | <?php
/**
* Turn text into HTML with varying levels of parsing. For no html
* whatsoever, use htmlspecialchars() instead.
*
* Copyright 2002-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Text_Filter
*/
class Horde_Text_Filter_Text2html extends Horde_Text_Filter_Base
{
const PASSTHRU = 0;
const SYNTAX = 1;
const MICRO = 2;
const MICRO_LINKURL = 3;
const NOHTML = 4;
const NOHTML_NOBREAK = 5;
/**
* Filter parameters.
*
* @var array
*/
protected $_params = array(
'charset' => 'ISO-8859-1',
'class' => 'fixed',
'emails' => false,
'flowed' => '<blockquote>',
'linkurls' => false,
'text2html' => false,
'parselevel' => 0,
'space2html' => false
);
/**
* Constructor.
*
* @param array $params Parameters specific to this driver:
* <ul>
* <li>charset: (string) The charset to use for htmlspecialchars()
* calls.</li>
* <li>class: (string) See Horde_Text_Filter_Linkurls::.</li>
* <li>emails: (array) TODO</li>
* <li>flowed: (string) For flowed text, the HTML blockquote tag to
* insert before each level.
* <li>linkurls: (array) TODO</li>
* <li>parselevel: (integer) The parselevel of the output.
* <ul>
* <li>PASSTHRU: No action. Pass-through. Included for
* completeness.</li>
* <li>SYNTAX: Allow full html, also do line-breaks, in-lining,
* syntax-parsing.</li>
* <li>MICRO: Micro html (only line-breaks, in-line linking).</li>
* <li>MICRO_LINKURL: Micro html (only line-breaks, in-line linking of
* URLS; no email addresses are linked).</li>
* <li>NOHTML: No html (all stripped, only line-breaks).</li>
* <li>NOHTML_NOBREAK: No html whatsoever, no line breaks added.
* Included for completeness.</li>
* </ul>
* </li>
* <li>space2html: (array) TODO</li>
* </ul>
*/
public function __construct($params = array())
{
parent::__construct($params);
// Use ISO-8859-1 instead of US-ASCII
if (Horde_String::lower($this->_params['charset']) == 'us-ascii') {
$this->_params['charset'] = 'iso-8859-1';
}
}
/**
* Executes any code necessary before applying the filter patterns.
*
* @param mixed $text The text before the filtering. Either a string or
* a Horde_Text_Flowed object (since 1.1.0).
*
* @return string The modified text.
*/
public function preProcess($text)
{
if ($text instanceof Horde_Text_Flowed) {
$text->setMaxLength(0);
$lines = $text->toFixedArray();
$level = 0;
$out = $txt = '';
foreach ($lines as $key => $val) {
$line = ltrim($val['text'], '>');
if (!isset($lines[$key + 1])) {
$out .= $this->preProcess(ltrim($txt) . $line);
while (--$level > 0) {
$out .= '</blockquote>';
}
} elseif ($val['level'] > $level) {
$out .= $this->preProcess(ltrim($txt));
do {
$out .= $this->_params['flowed'];
} while (++$level != $val['level']);
$txt = $line;
} elseif ($val['level'] < $level) {
$out .= $this->preProcess(ltrim($txt));
do {
$out .= '</blockquote>';
} while (--$level != $val['level']);
$txt = $line;
} else {
$txt .= "\n" . $line;
}
}
return $out;
}
if (!strlen($text)) {
return '';
}
/* Abort out on simple cases. */
if ($this->_params['parselevel'] == self::PASSTHRU) {
return $text;
}
if ($this->_params['parselevel'] == self::NOHTML_NOBREAK) {
return @htmlspecialchars($text, ENT_COMPAT, $this->_params['charset']);
}
if ($this->_params['parselevel'] < self::NOHTML) {
$filters = array();
if ($this->_params['linkurls']) {
reset($this->_params['linkurls']);
$this->_params['linkurls'][key($this->_params['linkurls'])]['encode'] = true;
$filters = $this->_params['linkurls'];
} else {
$filters['linkurls'] = array(
'encode' => true
);
}
if ($this->_params['parselevel'] < self::MICRO_LINKURL) {
if ($this->_params['emails']) {
reset($this->_params['emails']);
$this->_params['emails'][key($this->_params['emails'])]['encode'] = true;
$filters += $this->_params['emails'];
} else {
$filters['emails'] = array(
'encode' => true
);
}
}
$text = Horde_Text_Filter::filter($text, array_keys($filters), array_values($filters));
}
/* For level MICRO or NOHTML, start with htmlspecialchars(). */
$text2 = @htmlspecialchars($text, ENT_COMPAT, $this->_params['charset']);
/* Bad charset input in may result in an empty string. Or the charset
* may not be supported. Convert to UTF-8 for htmlspecialchars() and
* then convert back. If we STILL don't have any output, the input
* charset is probably incorrect. Try the popular Western charsets as
* a last resort. */
if (!strlen($text2)) {
$text2 = Horde_String::convertCharset(
@htmlspecialchars(
Horde_String::convertCharset($text, $this->_params['charset'], 'UTF-8'),
ENT_COMPAT,
'UTF-8'
),
'UTF-8',
$this->_params['charset']
);
if (!strlen($text2)) {
foreach (array('windows-1252', 'utf-8') as $val) {
$text2 = Horde_String::convertCharset(
@htmlspecialchars($text, ENT_COMPAT, $val),
$val,
$this->_params['charset']
);
if (strlen($text2)) {
break;
}
}
}
}
$text = $text2;
/* Do in-lining of http://xxx.xxx to link, xxx@xxx.xxx to email. */
if ($this->_params['parselevel'] < self::NOHTML) {
$text = Horde_Text_Filter_Linkurls::decode($text);
if ($this->_params['parselevel'] < self::MICRO_LINKURL) {
$text = Horde_Text_Filter_Emails::decode($text);
}
if ($this->_params['space2html']) {
$params = reset($this->_params['space2html']);
$driver = key($this->_params['space2html']);
} else {
$driver = 'space2html';
$params = array();
}
$text = Horde_Text_Filter::filter($text, $driver, $params);
}
/* Do the newline ---> <br /> substitution. Everybody gets this; if
* you don't want even that, just use htmlspecialchars(). */
return nl2br($text);
}
}
|