/usr/share/php/Horde/Compress/Tnef/Rtf.php is in php-horde-compress 2.2.1-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 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 | <?php
/**
* Copyright 2002-2017 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 J Rubinsky <mrubinsk@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Compress
*/
/**
* Object to parse RTF data encapsulated in a TNEF file.
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @category Horde
* @copyright 2002-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Compress
*/
class Horde_Compress_Tnef_Rtf extends Horde_Compress_Tnef_Object
{
const UNCOMPRESSED = 0x414c454d;
const COMPRESSED = 0x75465a4c;
/**
* RTF content.
*
* @var string
*/
protected $_content = '';
/**
* Size of RTF content.
*
* @var integer
*/
protected $_size = 0;
/**
* MIME type.
*
* @var string
*/
public $type = 'application/rtf';
public function __construct($logger, $data)
{
parent::__construct($logger, $data);
$this->_decode();
}
public function __get($property)
{
if ($property == 'content') {
return $this->_content;
}
throw new InvalidArgumentException('Invalid property access.');
}
/**
* Output the data for this object in an array.
*
* @return array
* - type: (string) The MIME type of the content.
* - subtype: (string) The MIME subtype.
* - name: (string) The filename.
* - stream: (string) The file data.
*/
public function toArray()
{
return array(
'type' => 'application',
'subtype' => 'rtf',
'name' => 'Untitled.rtf',
'stream' => $this->_content
);
}
/**
* Obtain a good-enough-for-our-needs plain text representation of
* the RTF document.
*
* @return string The plaintext.
*/
public function toPlain()
{
return $this->_rtf2text($this->_content);
}
protected function _decode()
{
$c_size = $this->_geti($this->_data, 32);
$this->_size = $this->_geti($this->_data, 32);
$magic = $this->_geti($this->_data, 32);
$crc = $this->_geti($this->_data, 32);
$this->_logger->debug(sprintf(
'TNEF: compressed size: %s, size: %s, magic: %s, CRC: %s',
$c_size, $this->_size, $magic, $crc)
);
switch ($magic) {
case self::COMPRESSED:
$this->_decompress();
break;
case self::UNCOMPRESSED:
$this->_content = $this->_data;
break;
default:
$this->_logger->notice('TNEF: Unknown RTF compression.');
}
}
/**
* Decompress compressed RTF. Logic taken and adapted from NasMail RTF
* plugin.
*
* @return string
*/
protected function _decompress()
{
$uncomp = '';
$in = $out = $flags = $flag_count = 0;
$preload = "{\\rtf1\\ansi\\mac\\deff0\\deftab720{\\fonttbl;}{\\f0\\fnil \\froman \\fswiss \\fmodern \\fscript \\fdecor MS Sans SerifSymbolArialTimes New RomanCourier{\\colortbl\\red0\\green0\\blue0\n\r\\par \\pard\\plain\\f0\\fs20\\b\\i\\u\\tab\\tx";
$length_preload = strlen($preload);
for ($cnt = 0; $cnt < $length_preload; $cnt++) {
$uncomp .= $preload{$cnt};
++$out;
}
while ($out < ($this->_size + $length_preload)) {
if (($flag_count++ % 8) == 0) {
$flags = ord($this->_data{$in++});
} else {
$flags = $flags >> 1;
}
if (($flags & 1) != 0) {
$offset = ord($this->_data{$in++});
$length = ord($this->_data{$in++});
$offset = ($offset << 4) | ($length >> 4);
$length = ($length & 0xF) + 2;
$offset = ((int)($out / 4096)) * 4096 + $offset;
if ($offset >= $out) {
$offset -= 4096;
}
$end = $offset + $length;
while ($offset < $end) {
$uncomp.= $uncomp[$offset++];
++$out;
}
} else {
$uncomp .= $this->_data{$in++};
++$out;
}
}
$this->_content = substr_replace($uncomp, "", 0, $length_preload);
}
/**
* Parse RTF data and return the best plaintext representation we can.
* Adapted from:
* http://webcheatsheet.com/php/reading_the_clean_text_from_rtf.php
*
* @param string $text The RTF text.
*
* @return string The plaintext.
*/
protected function _rtf2text($text)
{
$document = '';
$stack = array();
$j = -1;
// Read the data character-by- character…
for ($i = 0, $len = strlen($text); $i < $len; $i++) {
$c = $text[$i];
switch ($c) {
case '\\':
// Key Word
$nextCharacter = $text[$i + 1];
// If it is another backslash or nonbreaking space or hyphen,
// then the character is plain text and add it to the output stream.
if ($nextCharacter == '\\' && $this->_rtfIsPlain($stack[$j])) {
$document .= '\\';
} elseif ($nextCharacter == '~' && $this->_rtfIsPlain($stack[$j])) {
$document .= ' ';
} elseif ($nextCharacter == '_' && $this->_rtfIsPlain($stack[$j])) {
$document .= '-';
} elseif ($nextCharacter == '*') {
// Add to the stack.
$stack[$j]['*'] = true;
} elseif ($nextCharacter == "'") {
// If it is a single quote, read next two characters that
// are the hexadecimal notation of a character we should add
// to the output stream.
$hex = substr($text, $i + 2, 2);
if ($this->_rtfIsPlain($stack[$j])) {
$document .= html_entity_decode('&#' . hexdec($hex) .';');
}
//Shift the pointer.
$i += 2;
} elseif ($nextCharacter >= 'a' && $nextCharacter <= 'z'
|| $nextCharacter >= 'A' && $nextCharacter <= 'Z') {
// Since, we’ve found the alphabetic character, the next
// characters are control words and, possibly, some digit
// parameter.
$word = '';
$param = null;
// Start reading characters after the backslash.
for ($k = $i + 1, $m = 0; $k < strlen($text); $k++, $m++) {
$nextCharacter = $text[$k];
// If the current character is a letter and there were
// no digits before it, then we’re still reading the
// control word. If there were digits, we should stop
// since we reach the end of the control word.
if ($nextCharacter >= 'a' && $nextCharacter <= 'z'
|| $nextCharacter >= 'A' && $nextCharacter <= 'Z') {
if (!empty($param)) {
break;
}
$word .= $nextCharacter;
} elseif ($nextCharacter >= '0' && $nextCharacter <= '9') {
// If it is a digit, store the parameter.
$param .= $nextCharacter;
} elseif ($nextCharacter == '-') {
// Since minus sign may occur only before a digit
// parameter, check whether $param is empty.
// Otherwise, we reach the end of the control word.
if (!empty($param)) {
break;
}
$param .= $nextCharacter;
} else {
break;
}
}
// Shift the pointer on the number of read characters.
$i += $m - 1;
// Start analyzing.We are interested mostly in control words
$toText = '';
switch (Horde_String::lower($word)) {
// If the control word is "u", then its parameter is
// the decimal notation of the Unicode character that
// should be added to the output stream. We need to
// check whether the stack contains \ucN control word.
// If it does, we should remove the N characters from
// the output stream.
case 'u':
$toText .= html_entity_decode('&#x' . dechex($param) .';');
$ucDelta = @$stack[$j]['uc'];
if ($ucDelta > 0) {
$i += $ucDelta;
}
break;
case 'par':
case 'page':
case 'column':
case 'line':
case 'lbr':
$toText .= "\n";
break;
case 'emspace':
case 'enspace':
case 'qmspace':
$toText .= ' ';
break;
case 'tab':
$toText .= "\t";
break;
case 'chdate':
$toText .= date('m.d.Y');
break;
case 'chdpl':
$toText .= date('l, j F Y');
break;
case 'chdpa':
$toText .= date('D, j M Y');
break;
case 'chtime':
$toText .= date('H:i:s');
break;
case 'emdash':
$toText .= html_entity_decode('—');
break;
case 'endash':
$toText .= html_entity_decode('–');
break;
case 'bullet':
$toText .= html_entity_decode('•');
break;
case 'lquote':
$toText .= html_entity_decode('‘');
break;
case 'rquote':
$toText .= html_entity_decode('’');
break;
case 'ldblquote':
$toText .= html_entity_decode('«');
break;
case 'rdblquote':
$toText .= html_entity_decode('»');
break;
default:
$stack[$j][Horde_String::lower($word)] = empty($param) ? true : $param;
break;
}
// Add data to the output stream if required.
if ($this->_rtfIsPlain($stack[$j])) {
$document .= $toText;
}
}
$i++;
break;
case '{':
// New subgroup starts, add new stack element and write the data
// from previous stack element to it.
if (!empty($stack[$j])) {
array_push($stack, $stack[$j++]);
} else {
$j++;
}
break;
case '}':
array_pop($stack);
$j--;
break;
case '\0':
case '\r':
case '\f':
case '\n':
// Junk
break;
default:
// Add other data to the output stream if required.
if (!empty($stack[$j]) && $this->_rtfIsPlain($stack[$j])) {
$document .= $c;
}
break;
}
}
return $document;
}
protected function _rtfIsPlain($s)
{
$notPlain = array('*', 'fonttbl', 'colortbl', 'datastore', 'themedata', 'stylesheet');
for ($i = 0; $i < count($notPlain); $i++) {
if (!empty($s[$notPlain[$i]])) {
return false;
}
}
return true;
}
}
|