/usr/share/php/Horde/Text/Flowed.php is in php-horde-text-flowed 2.0.3-3ubuntu1.
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | <?php
/**
* The Text_Flowed:: class provides common methods for manipulating text
* using the encoding described in RFC 3676 ('flowed' text).
*
* This class is based on the Text::Flowed perl module (Version 0.14) found
* in the CPAN perl repository. This module is released under the Perl
* license, which is compatible with the LGPL.
*
* Copyright 2002-2003 Philip Mak
* Copyright 2004-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 Michael Slusarz <slusarz@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Text_Flowed
*/
class Horde_Text_Flowed
{
/**
* The maximum length that a line is allowed to be (unless faced with
* with a word that is unreasonably long). This class will re-wrap a
* line if it exceeds this length.
*
* @var integer
*/
protected $_maxlength = 78;
/**
* When this class wraps a line, the newly created lines will be split
* at this length.
*
* @var integer
*/
protected $_optlength = 72;
/**
* The text to be formatted.
*
* @var string
*/
protected $_text;
/**
* The cached output of the formatting.
*
* @var array
*/
protected $_output = array();
/**
* The format of the data in $_output.
*
* @var string
*/
protected $_formattype = null;
/**
* The character set of the text.
*
* @var string
*/
protected $_charset;
/**
* Convert text using DelSp?
*
* @var boolean
*/
protected $_delsp = false;
/**
* Constructor.
*
* @param string $text The text to process.
* @param string $charset The character set of $text.
*/
public function __construct($text, $charset = 'UTF-8')
{
$this->_text = $text;
$this->_charset = $charset;
}
/**
* Set the maximum length of a line of text.
*
* @param integer $max A new value for $_maxlength.
*/
public function setMaxLength($max)
{
$this->_maxlength = $max;
}
/**
* Set the optimal length of a line of text.
*
* @param integer $max A new value for $_optlength.
*/
public function setOptLength($opt)
{
$this->_optlength = $opt;
}
/**
* Set whether to format text using DelSp.
*
* @param boolean $delsp Use DelSp?
*/
public function setDelSp($delsp)
{
$this->_delsp = (bool)$delsp;
}
/**
* Reformats the input string, where the string is 'format=flowed' plain
* text as described in RFC 2646.
*
* @param boolean $quote Add level of quoting to each line?
*
* @return string The text converted to RFC 2646 'fixed' format.
*/
public function toFixed($quote = false)
{
$txt = '';
$this->_reformat(false, $quote);
reset($this->_output);
$lines = count($this->_output) - 1;
foreach ($this->_output as $no => $line) {
$txt .= $line['text'] . (($lines == $no) ? '' : "\n");
}
return $txt;
}
/**
* Reformats the input string, and returns the output in an array format
* with quote level information.
*
* @param boolean $quote Add level of quoting to each line?
*
* @return array An array of arrays with the following elements:
* <pre>
* 'level' - The quote level of the current line.
* 'text' - The text for the current line.
* </pre>
*/
public function toFixedArray($quote = false)
{
$this->_reformat(false, $quote);
return $this->_output;
}
/**
* Reformats the input string, where the string is 'format=fixed' plain
* text as described in RFC 2646.
*
* @param boolean $quote Add level of quoting to each line?
* @param array $opts Additional options:
* <pre>
* 'nowrap' - (boolean) If true, does not wrap unquoted lines.
* DEFAULT: false
* </pre>
*
* @return string The text converted to RFC 2646 'flowed' format.
*/
public function toFlowed($quote = false, array $opts = array())
{
$txt = '';
$this->_reformat(true, $quote, empty($opts['nowrap']));
reset($this->_output);
foreach ($this->_output as $line) {
$txt .= $line['text'] . "\n";
}
return $txt;
}
/**
* Reformats the input string, where the string is 'format=flowed' plain
* text as described in RFC 2646.
*
* @param boolean $toflowed Convert to flowed?
* @param boolean $quote Add level of quoting to each line?
* @param boolean $wrap Wrap unquoted lines?
*/
protected function _reformat($toflowed, $quote, $wrap = true)
{
$format_type = implode('|', array($toflowed, $quote));
if ($format_type == $this->_formattype) {
return;
}
$this->_output = array();
$this->_formattype = $format_type;
/* Set variables used in regexps. */
$delsp = ($toflowed && $this->_delsp) ? 1 : 0;
$opt = $this->_optlength - 1 - $delsp;
/* Process message line by line. */
$text = preg_split("/\r?\n/", $this->_text);
$text_count = count($text) - 1;
$skip = 0;
reset($text);
foreach ($text as $no => $line) {
if ($skip) {
--$skip;
continue;
}
/* Per RFC 2646 [4.3], the 'Usenet Signature Convention' line
* (DASH DASH SP) is not considered flowed. Watch for this when
* dealing with potentially flowed lines. */
/* The next three steps come from RFC 2646 [4.2]. */
/* STEP 1: Determine quote level for line. */
if (($num_quotes = $this->_numquotes($line))) {
$line = substr($line, $num_quotes);
}
/* Only combine lines if we are converting to flowed or if the
* current line is quoted. */
if (!$toflowed || $num_quotes) {
/* STEP 2: Remove space stuffing from line. */
$line = $this->_unstuff($line);
/* STEP 3: Should we interpret this line as flowed?
* While line is flowed (not empty and there is a space
* at the end of the line), and there is a next line, and the
* next line has the same quote depth, add to the current
* line. A line is not flowed if it is a signature line. */
if ($line != '-- ') {
while (!empty($line) &&
(substr($line, -1) == ' ') &&
($text_count != $no) &&
($this->_numquotes($text[$no + 1]) == $num_quotes)) {
/* If DelSp is yes and this is flowed input, we need to
* remove the trailing space. */
if (!$toflowed && $this->_delsp) {
$line = substr($line, 0, -1);
}
$line .= $this->_unstuff(substr($text[++$no], $num_quotes));
++$skip;
}
}
}
/* Ensure line is fixed, since we already joined all flowed
* lines. Remove all trailing ' ' from the line. */
if ($line != '-- ') {
$line = rtrim($line);
}
/* Increment quote depth if we're quoting. */
if ($quote) {
$num_quotes++;
}
/* The quote prefix for the line. */
$quotestr = str_repeat('>', $num_quotes);
if (empty($line)) {
/* Line is empty. */
$this->_output[] = array('text' => $quotestr, 'level' => $num_quotes);
} elseif ((!$wrap && !$num_quotes) ||
empty($this->_maxlength) ||
((Horde_String::length($line, $this->_charset) + $num_quotes) <= $this->_maxlength)) {
/* Line does not require rewrapping. */
$this->_output[] = array('text' => $quotestr . $this->_stuff($line, $num_quotes, $toflowed), 'level' => $num_quotes);
} else {
$min = $num_quotes + 1;
/* Rewrap this paragraph. */
while ($line) {
/* Stuff and re-quote the line. */
$line = $quotestr . $this->_stuff($line, $num_quotes, $toflowed);
$line_length = Horde_String::length($line, $this->_charset);
if ($line_length <= $this->_optlength) {
/* Remaining section of line is short enough. */
$this->_output[] = array('text' => $line, 'level' => $num_quotes);
break;
} else {
$regex = array();
if ($min <= $opt) {
$regex[] = '^(.{' . $min . ',' . $opt . '}) (.*)';
}
if ($min <= $this->_maxlength) {
$regex[] = '^(.{' . $min . ',' . $this->_maxlength . '}) (.*)';
}
$regex[] = '^(.{' . $min . ',})? (.*)';
if ($m = Horde_String::regexMatch($line, $regex, $this->_charset)) {
/* We need to wrap text at a certain number of
* *characters*, not a certain number of *bytes*;
* thus the need for a multibyte capable regex.
* If a multibyte regex isn't available, we are
* stuck with preg_match() (the function will
* still work - are just left with shorter rows
* than expected if multibyte characters exist in
* the row).
*
* 1. Try to find a string as long as _optlength.
* 2. Try to find a string as long as _maxlength.
* 3. Take the first word. */
if (empty($m[1])) {
$m[1] = $m[2];
$m[2] = '';
}
$this->_output[] = array('text' => $m[1] . ' ' . (($delsp) ? ' ' : ''), 'level' => $num_quotes);
$line = $m[2];
} elseif ($line_length > 998) {
/* One excessively long word left on line. Be
* absolutely sure it does not exceed 998
* characters in length or else we must
* truncate. */
$this->_output[] = array('text' => Horde_String::substr($line, 0, 998, $this->_charset), 'level' => $num_quotes);
$line = Horde_String::substr($line, 998, null, $this->_charset);
} else {
$this->_output[] = array('text' => $line, 'level' => $num_quotes);
break;
}
}
}
}
}
}
/**
* Returns the number of leading '>' characters in the text input.
* '>' characters are defined by RFC 2646 to indicate a quoted line.
*
* @param string $text The text to analyze.
*
* @return integer The number of leading quote characters.
*/
protected function _numquotes($text)
{
return strspn($text, '>');
}
/**
* Space-stuffs if it starts with ' ' or '>' or 'From ', or if
* quote depth is non-zero (for aesthetic reasons so that there is a
* space after the '>').
*
* @param string $text The text to stuff.
* @param string $num_quotes The quote-level of this line.
* @param boolean $toflowed Are we converting to flowed text?
*
* @return string The stuffed text.
*/
protected function _stuff($text, $num_quotes, $toflowed)
{
return ($toflowed && ($num_quotes || preg_match("/^(?: |>|From |From$)/", $text)))
? ' ' . $text
: $text;
}
/**
* Unstuffs a space stuffed line.
*
* @param string $text The text to unstuff.
*
* @return string The unstuffed text.
*/
protected function _unstuff($text)
{
return (!empty($text) && ($text[0] == ' '))
? substr($text, 1)
: $text;
}
}
|