| 1 | <?php |
|---|
| 2 | // |
|---|
| 3 | // FPDI - Version 1.3.1 |
|---|
| 4 | // |
|---|
| 5 | // Copyright 2004-2009 Setasign - Jan Slabon |
|---|
| 6 | // |
|---|
| 7 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 8 | // you may not use this file except in compliance with the License. |
|---|
| 9 | // You may obtain a copy of the License at |
|---|
| 10 | // |
|---|
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | // |
|---|
| 13 | // Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | // distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | // See the License for the specific language governing permissions and |
|---|
| 17 | // limitations under the License. |
|---|
| 18 | // |
|---|
| 19 | |
|---|
| 20 | if (!defined ('PDF_TYPE_NULL')) |
|---|
| 21 | define ('PDF_TYPE_NULL', 0); |
|---|
| 22 | if (!defined ('PDF_TYPE_NUMERIC')) |
|---|
| 23 | define ('PDF_TYPE_NUMERIC', 1); |
|---|
| 24 | if (!defined ('PDF_TYPE_TOKEN')) |
|---|
| 25 | define ('PDF_TYPE_TOKEN', 2); |
|---|
| 26 | if (!defined ('PDF_TYPE_HEX')) |
|---|
| 27 | define ('PDF_TYPE_HEX', 3); |
|---|
| 28 | if (!defined ('PDF_TYPE_STRING')) |
|---|
| 29 | define ('PDF_TYPE_STRING', 4); |
|---|
| 30 | if (!defined ('PDF_TYPE_DICTIONARY')) |
|---|
| 31 | define ('PDF_TYPE_DICTIONARY', 5); |
|---|
| 32 | if (!defined ('PDF_TYPE_ARRAY')) |
|---|
| 33 | define ('PDF_TYPE_ARRAY', 6); |
|---|
| 34 | if (!defined ('PDF_TYPE_OBJDEC')) |
|---|
| 35 | define ('PDF_TYPE_OBJDEC', 7); |
|---|
| 36 | if (!defined ('PDF_TYPE_OBJREF')) |
|---|
| 37 | define ('PDF_TYPE_OBJREF', 8); |
|---|
| 38 | if (!defined ('PDF_TYPE_OBJECT')) |
|---|
| 39 | define ('PDF_TYPE_OBJECT', 9); |
|---|
| 40 | if (!defined ('PDF_TYPE_STREAM')) |
|---|
| 41 | define ('PDF_TYPE_STREAM', 10); |
|---|
| 42 | if (!defined ('PDF_TYPE_BOOLEAN')) |
|---|
| 43 | define ('PDF_TYPE_BOOLEAN', 11); |
|---|
| 44 | if (!defined ('PDF_TYPE_REAL')) |
|---|
| 45 | define ('PDF_TYPE_REAL', 12); |
|---|
| 46 | |
|---|
| 47 | require_once('pdf_context.php'); |
|---|
| 48 | |
|---|
| 49 | if (!class_exists('pdf_parser')) { |
|---|
| 50 | |
|---|
| 51 | class pdf_parser { |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Filename |
|---|
| 55 | * @var string |
|---|
| 56 | */ |
|---|
| 57 | var $filename; |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * File resource |
|---|
| 61 | * @var resource |
|---|
| 62 | */ |
|---|
| 63 | var $f; |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * PDF Context |
|---|
| 67 | * @var object pdf_context-Instance |
|---|
| 68 | */ |
|---|
| 69 | var $c; |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * xref-Data |
|---|
| 73 | * @var array |
|---|
| 74 | */ |
|---|
| 75 | var $xref; |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * root-Object |
|---|
| 79 | * @var array |
|---|
| 80 | */ |
|---|
| 81 | var $root; |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * PDF version of the loaded document |
|---|
| 85 | * @var string |
|---|
| 86 | */ |
|---|
| 87 | var $pdfVersion; |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Constructor |
|---|
| 91 | * |
|---|
| 92 | * @param string $filename Source-Filename |
|---|
| 93 | */ |
|---|
| 94 | function pdf_parser($filename) { |
|---|
| 95 | $this->filename = $filename; |
|---|
| 96 | //$f = @ fopen($this->filename, 'rb'); |
|---|
| 97 | //$this->f = $f; |
|---|
| 98 | //var_dump($f); |
|---|
| 99 | $this->f = @ fopen($this->filename, 'rb'); |
|---|
| 100 | |
|---|
| 101 | if (!$this->f) |
|---|
| 102 | $this->error(sprintf('Cannot open %s !', $filename)); |
|---|
| 103 | |
|---|
| 104 | $this->getPDFVersion(); |
|---|
| 105 | |
|---|
| 106 | $this->c = new pdf_context($this->f); |
|---|
| 107 | |
|---|
| 108 | // Read xref-Data |
|---|
| 109 | $this->xref = array(); |
|---|
| 110 | $this->pdf_read_xref($this->xref, $this->pdf_find_xref()); |
|---|
| 111 | |
|---|
| 112 | // Check for Encryption |
|---|
| 113 | $this->getEncryption(); |
|---|
| 114 | |
|---|
| 115 | // Read root |
|---|
| 116 | $this->pdf_read_root(); |
|---|
| 117 | |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Close the opened file |
|---|
| 122 | */ |
|---|
| 123 | function closeFile() { |
|---|
| 124 | if (isset($this->f) && is_resource($this->f)) { |
|---|
| 125 | fclose($this->f); |
|---|
| 126 | unset($this->f); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Print Error and die |
|---|
| 132 | * |
|---|
| 133 | * @param string $msg Error-Message |
|---|
| 134 | */ |
|---|
| 135 | function error($msg) { |
|---|
| 136 | die('<b>PDF-Parser Error:</b> '.$msg); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Check Trailer for Encryption |
|---|
| 141 | */ |
|---|
| 142 | function getEncryption() { |
|---|
| 143 | if (isset($this->xref['trailer'][1]['/Encrypt'])) { |
|---|
| 144 | $this->error('File is encrypted!'); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Find/Return /Root |
|---|
| 150 | * |
|---|
| 151 | * @return array |
|---|
| 152 | */ |
|---|
| 153 | function pdf_find_root() { |
|---|
| 154 | if ($this->xref['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF) { |
|---|
| 155 | $this->error('Wrong Type of Root-Element! Must be an indirect reference'); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | return $this->xref['trailer'][1]['/Root']; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Read the /Root |
|---|
| 163 | */ |
|---|
| 164 | function pdf_read_root() { |
|---|
| 165 | // read root |
|---|
| 166 | $this->root = $this->pdf_resolve_object($this->c, $this->pdf_find_root()); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Get PDF-Version |
|---|
| 171 | * |
|---|
| 172 | * And reset the PDF Version used in FPDI if needed |
|---|
| 173 | */ |
|---|
| 174 | function getPDFVersion() { |
|---|
| 175 | fseek($this->f, 0); |
|---|
| 176 | preg_match('/\d\.\d/',fread($this->f,16),$m); |
|---|
| 177 | if (isset($m[0])) |
|---|
| 178 | $this->pdfVersion = $m[0]; |
|---|
| 179 | return $this->pdfVersion; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * Find the xref-Table |
|---|
| 184 | */ |
|---|
| 185 | function pdf_find_xref() { |
|---|
| 186 | |
|---|
| 187 | $toRead = 1500; |
|---|
| 188 | $stat = fseek ($this->f, -$toRead, SEEK_END); |
|---|
| 189 | if ($stat === -1) { |
|---|
| 190 | fseek ($this->f, 0); |
|---|
| 191 | } |
|---|
| 192 | $data = fread($this->f, $toRead); |
|---|
| 193 | |
|---|
| 194 | $pos = strlen($data) - strpos(strrev($data), strrev('startxref')); |
|---|
| 195 | |
|---|
| 196 | $data = substr($data, $pos); |
|---|
| 197 | |
|---|
| 198 | if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) { |
|---|
| 199 | $this->error('Unable to find pointer to xref table'); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | return (int) $matches[1]; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Read xref-table |
|---|
| 207 | * |
|---|
| 208 | * @param array $result Array of xref-table |
|---|
| 209 | * @param integer $offset of xref-table |
|---|
| 210 | */ |
|---|
| 211 | function pdf_read_xref(&$result, $offset) { |
|---|
| 212 | |
|---|
| 213 | fseek($this->f, $o_pos = $offset-20); // set some bytes backwards to fetch errorious docs |
|---|
| 214 | |
|---|
| 215 | $data = fread($this->f, 100); |
|---|
| 216 | |
|---|
| 217 | $xrefPos = strrpos($data, 'xref'); |
|---|
| 218 | |
|---|
| 219 | if ($xrefPos === false) { |
|---|
| 220 | fseek($this->f, $offset); |
|---|
| 221 | $c = new pdf_context($this->f); |
|---|
| 222 | $xrefStreamObjDec = $this->pdf_read_value($c); |
|---|
| 223 | |
|---|
| 224 | if (is_array($xrefStreamObjDec) && isset($xrefStreamObjDec[0]) && $xrefStreamObjDec[0] == PDF_TYPE_OBJDEC) { |
|---|
| 225 | $this->error(sprintf('This document (%s) probably uses a compression technique which is not supported by the free parser shipped with FPDI.', $this->filename)); |
|---|
| 226 | } else { |
|---|
| 227 | $this->error('Unable to find xref table.'); |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | if (!isset($result['xref_location'])) { |
|---|
| 232 | $result['xref_location'] = $o_pos+$xrefPos; |
|---|
| 233 | $result['max_object'] = 0; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | $cylces = -1; |
|---|
| 237 | $bytesPerCycle = 100; |
|---|
| 238 | |
|---|
| 239 | fseek($this->f, $o_pos = $o_pos+$xrefPos+4); // set the handle directly after the "xref"-keyword |
|---|
| 240 | $data = fread($this->f, $bytesPerCycle); |
|---|
| 241 | |
|---|
| 242 | while (($trailerPos = strpos($data, 'trailer', max($bytesPerCycle*$cylces++, 0))) === false && !feof($this->f)) { |
|---|
| 243 | $data .= fread($this->f, $bytesPerCycle); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | if ($trailerPos === false) { |
|---|
| 247 | $this->error('Trailer keyword not found after xref table'); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | $data = substr($data, 0, $trailerPos); |
|---|
| 251 | |
|---|
| 252 | // get Line-Ending |
|---|
| 253 | preg_match_all("/(\r\n|\n|\r)/", substr($data, 0, 100), $m); // check the first 100 bytes for linebreaks |
|---|
| 254 | |
|---|
| 255 | $differentLineEndings = count(array_unique($m[0])); |
|---|
| 256 | if ($differentLineEndings > 1) { |
|---|
| 257 | $lines = preg_split("/(\r\n|\n|\r)/", $data, -1, PREG_SPLIT_NO_EMPTY); |
|---|
| 258 | } else { |
|---|
| 259 | $lines = explode($m[0][1], $data); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | $data = $differentLineEndings = $m = null; |
|---|
| 263 | unset($data, $differentLineEndings, $m); |
|---|
| 264 | |
|---|
| 265 | $linesCount = count($lines); |
|---|
| 266 | |
|---|
| 267 | $start = 1; |
|---|
| 268 | |
|---|
| 269 | for ($i = 0; $i < $linesCount; $i++) { |
|---|
| 270 | $line = trim($lines[$i]); |
|---|
| 271 | if ($line) { |
|---|
| 272 | $pieces = explode(' ', $line); |
|---|
| 273 | $c = count($pieces); |
|---|
| 274 | switch($c) { |
|---|
| 275 | case 2: |
|---|
| 276 | $start = (int)$pieces[0]; |
|---|
| 277 | $end = $start+(int)$pieces[1]; |
|---|
| 278 | if ($end > $result['max_object']) |
|---|
| 279 | $result['max_object'] = $end; |
|---|
| 280 | break; |
|---|
| 281 | case 3: |
|---|
| 282 | if (!isset($result['xref'][$start])) |
|---|
| 283 | $result['xref'][$start] = array(); |
|---|
| 284 | |
|---|
| 285 | if (!array_key_exists($gen = (int) $pieces[1], $result['xref'][$start])) { |
|---|
| 286 | $result['xref'][$start][$gen] = $pieces[2] == 'n' ? (int) $pieces[0] : null; |
|---|
| 287 | } |
|---|
| 288 | $start++; |
|---|
| 289 | break; |
|---|
| 290 | default: |
|---|
| 291 | $this->error('Unexpected data in xref table'); |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | $lines = $pieces = $line = $start = $end = $gen = null; |
|---|
| 297 | unset($lines, $pieces, $line, $start, $end, $gen); |
|---|
| 298 | |
|---|
| 299 | fseek($this->f, $o_pos+$trailerPos+7); |
|---|
| 300 | |
|---|
| 301 | $c = new pdf_context($this->f); |
|---|
| 302 | $trailer = $this->pdf_read_value($c); |
|---|
| 303 | |
|---|
| 304 | $c = null; |
|---|
| 305 | unset($c); |
|---|
| 306 | |
|---|
| 307 | if (!isset($result['trailer'])) { |
|---|
| 308 | $result['trailer'] = $trailer; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | if (isset($trailer[1]['/Prev'])) { |
|---|
| 312 | $this->pdf_read_xref($result, $trailer[1]['/Prev'][1]); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | $trailer = null; |
|---|
| 316 | unset($trailer); |
|---|
| 317 | |
|---|
| 318 | return true; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | /** |
|---|
| 322 | * Reads an Value |
|---|
| 323 | * |
|---|
| 324 | * @param object $c pdf_context |
|---|
| 325 | * @param string $token a Token |
|---|
| 326 | * @return mixed |
|---|
| 327 | */ |
|---|
| 328 | function pdf_read_value(&$c, $token = null) { |
|---|
| 329 | if (is_null($token)) { |
|---|
| 330 | $token = $this->pdf_read_token($c); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | if ($token === false) { |
|---|
| 334 | return false; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | switch ($token) { |
|---|
| 338 | case '<': |
|---|
| 339 | // This is a hex string. |
|---|
| 340 | // Read the value, then the terminator |
|---|
| 341 | |
|---|
| 342 | $pos = $c->offset; |
|---|
| 343 | |
|---|
| 344 | while(1) { |
|---|
| 345 | |
|---|
| 346 | $match = strpos ($c->buffer, '>', $pos); |
|---|
| 347 | |
|---|
| 348 | // If you can't find it, try |
|---|
| 349 | // reading more data from the stream |
|---|
| 350 | |
|---|
| 351 | if ($match === false) { |
|---|
| 352 | if (!$c->increase_length()) { |
|---|
| 353 | return false; |
|---|
| 354 | } else { |
|---|
| 355 | continue; |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | $result = substr ($c->buffer, $c->offset, $match - $c->offset); |
|---|
| 360 | $c->offset = $match + 1; |
|---|
| 361 | |
|---|
| 362 | return array (PDF_TYPE_HEX, $result); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | break; |
|---|
| 366 | case '<<': |
|---|
| 367 | // This is a dictionary. |
|---|
| 368 | |
|---|
| 369 | $result = array(); |
|---|
| 370 | |
|---|
| 371 | // Recurse into this function until we reach |
|---|
| 372 | // the end of the dictionary. |
|---|
| 373 | while (($key = $this->pdf_read_token($c)) !== '>>') { |
|---|
| 374 | if ($key === false) { |
|---|
| 375 | return false; |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | if (($value = $this->pdf_read_value($c)) === false) { |
|---|
| 379 | return false; |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | // Catch missing value |
|---|
| 383 | if ($value[0] == PDF_TYPE_TOKEN && $value[1] == '>>') { |
|---|
| 384 | $result[$key] = array(PDF_TYPE_NULL); |
|---|
| 385 | break; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | $result[$key] = $value; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | return array (PDF_TYPE_DICTIONARY, $result); |
|---|
| 392 | |
|---|
| 393 | case '[': |
|---|
| 394 | // This is an array. |
|---|
| 395 | |
|---|
| 396 | $result = array(); |
|---|
| 397 | |
|---|
| 398 | // Recurse into this function until we reach |
|---|
| 399 | // the end of the array. |
|---|
| 400 | while (($token = $this->pdf_read_token($c)) !== ']') { |
|---|
| 401 | if ($token === false) { |
|---|
| 402 | return false; |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | if (($value = $this->pdf_read_value($c, $token)) === false) { |
|---|
| 406 | return false; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | $result[] = $value; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | return array (PDF_TYPE_ARRAY, $result); |
|---|
| 413 | |
|---|
| 414 | case '(' : |
|---|
| 415 | // This is a string |
|---|
| 416 | $pos = $c->offset; |
|---|
| 417 | |
|---|
| 418 | $openBrackets = 1; |
|---|
| 419 | do { |
|---|
| 420 | for (; $openBrackets != 0 && $pos < $c->length; $pos++) { |
|---|
| 421 | switch (ord($c->buffer[$pos])) { |
|---|
| 422 | case 0x28: // '(' |
|---|
| 423 | $openBrackets++; |
|---|
| 424 | break; |
|---|
| 425 | case 0x29: // ')' |
|---|
| 426 | $openBrackets--; |
|---|
| 427 | break; |
|---|
| 428 | case 0x5C: // backslash |
|---|
| 429 | $pos++; |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | } while($openBrackets != 0 && $c->increase_length()); |
|---|
| 433 | |
|---|
| 434 | $result = substr($c->buffer, $c->offset, $pos - $c->offset - 1); |
|---|
| 435 | $c->offset = $pos; |
|---|
| 436 | |
|---|
| 437 | return array (PDF_TYPE_STRING, $result); |
|---|
| 438 | |
|---|
| 439 | case 'stream': |
|---|
| 440 | $o_pos = ftell($c->file)-strlen($c->buffer); |
|---|
| 441 | $o_offset = $c->offset; |
|---|
| 442 | |
|---|
| 443 | $c->reset($startpos = $o_pos + $o_offset); |
|---|
| 444 | |
|---|
| 445 | $e = 0; // ensure line breaks in front of the stream |
|---|
| 446 | if ($c->buffer[0] == chr(10) || $c->buffer[0] == chr(13)) |
|---|
| 447 | $e++; |
|---|
| 448 | if ($c->buffer[1] == chr(10) && $c->buffer[0] != chr(10)) |
|---|
| 449 | $e++; |
|---|
| 450 | |
|---|
| 451 | if ($this->actual_obj[1][1]['/Length'][0] == PDF_TYPE_OBJREF) { |
|---|
| 452 | $tmp_c = new pdf_context($this->f); |
|---|
| 453 | $tmp_length = $this->pdf_resolve_object($tmp_c,$this->actual_obj[1][1]['/Length']); |
|---|
| 454 | $length = $tmp_length[1][1]; |
|---|
| 455 | } else { |
|---|
| 456 | $length = $this->actual_obj[1][1]['/Length'][1]; |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | if ($length > 0) { |
|---|
| 460 | $c->reset($startpos+$e,$length); |
|---|
| 461 | $v = $c->buffer; |
|---|
| 462 | } else { |
|---|
| 463 | $v = ''; |
|---|
| 464 | } |
|---|
| 465 | $c->reset($startpos+$e+$length+9); // 9 = strlen("endstream") |
|---|
| 466 | |
|---|
| 467 | return array(PDF_TYPE_STREAM, $v); |
|---|
| 468 | |
|---|
| 469 | default : |
|---|
| 470 | if (is_numeric ($token)) { |
|---|
| 471 | // A numeric token. Make sure that |
|---|
| 472 | // it is not part of something else. |
|---|
| 473 | if (($tok2 = $this->pdf_read_token ($c)) !== false) { |
|---|
| 474 | if (is_numeric ($tok2)) { |
|---|
| 475 | |
|---|
| 476 | // Two numeric tokens in a row. |
|---|
| 477 | // In this case, we're probably in |
|---|
| 478 | // front of either an object reference |
|---|
| 479 | // or an object specification. |
|---|
| 480 | // Determine the case and return the data |
|---|
| 481 | if (($tok3 = $this->pdf_read_token ($c)) !== false) { |
|---|
| 482 | switch ($tok3) { |
|---|
| 483 | case 'obj' : |
|---|
| 484 | return array (PDF_TYPE_OBJDEC, (int) $token, (int) $tok2); |
|---|
| 485 | case 'R' : |
|---|
| 486 | return array (PDF_TYPE_OBJREF, (int) $token, (int) $tok2); |
|---|
| 487 | } |
|---|
| 488 | // If we get to this point, that numeric value up |
|---|
| 489 | // there was just a numeric value. Push the extra |
|---|
| 490 | // tokens back into the stack and return the value. |
|---|
| 491 | array_push ($c->stack, $tok3); |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | array_push ($c->stack, $tok2); |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | if ($token === (string)((int)$token)) |
|---|
| 499 | return array (PDF_TYPE_NUMERIC, (int)$token); |
|---|
| 500 | else |
|---|
| 501 | return array (PDF_TYPE_REAL, (float)$token); |
|---|
| 502 | } else if ($token == 'true' || $token == 'false') { |
|---|
| 503 | return array (PDF_TYPE_BOOLEAN, $token == 'true'); |
|---|
| 504 | } else if ($token == 'null') { |
|---|
| 505 | return array (PDF_TYPE_NULL); |
|---|
| 506 | } else { |
|---|
| 507 | // Just a token. Return it. |
|---|
| 508 | return array (PDF_TYPE_TOKEN, $token); |
|---|
| 509 | } |
|---|
| 510 | } |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | /** |
|---|
| 514 | * Resolve an object |
|---|
| 515 | * |
|---|
| 516 | * @param object $c pdf_context |
|---|
| 517 | * @param array $obj_spec The object-data |
|---|
| 518 | * @param boolean $encapsulate Must set to true, cause the parsing and fpdi use this method only without this para |
|---|
| 519 | */ |
|---|
| 520 | function pdf_resolve_object(&$c, $obj_spec, $encapsulate = true) { |
|---|
| 521 | // Exit if we get invalid data |
|---|
| 522 | if (!is_array($obj_spec)) { |
|---|
| 523 | $ret = false; |
|---|
| 524 | return $ret; |
|---|
| 525 | } |
|---|
| 526 | |
|---|
| 527 | if ($obj_spec[0] == PDF_TYPE_OBJREF) { |
|---|
| 528 | |
|---|
| 529 | // This is a reference, resolve it |
|---|
| 530 | if (isset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]])) { |
|---|
| 531 | |
|---|
| 532 | // Save current file position |
|---|
| 533 | // This is needed if you want to resolve |
|---|
| 534 | // references while you're reading another object |
|---|
| 535 | // (e.g.: if you need to determine the length |
|---|
| 536 | // of a stream) |
|---|
| 537 | |
|---|
| 538 | $old_pos = ftell($c->file); |
|---|
| 539 | |
|---|
| 540 | // Reposition the file pointer and |
|---|
| 541 | // load the object header. |
|---|
| 542 | |
|---|
| 543 | $c->reset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]]); |
|---|
| 544 | |
|---|
| 545 | $header = $this->pdf_read_value($c); |
|---|
| 546 | |
|---|
| 547 | if ($header[0] != PDF_TYPE_OBJDEC || $header[1] != $obj_spec[1] || $header[2] != $obj_spec[2]) { |
|---|
| 548 | $this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location"); |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | // If we're being asked to store all the information |
|---|
| 552 | // about the object, we add the object ID and generation |
|---|
| 553 | // number for later use |
|---|
| 554 | $result = array(); |
|---|
| 555 | $this->actual_obj =& $result; |
|---|
| 556 | if ($encapsulate) { |
|---|
| 557 | $result = array ( |
|---|
| 558 | PDF_TYPE_OBJECT, |
|---|
| 559 | 'obj' => $obj_spec[1], |
|---|
| 560 | 'gen' => $obj_spec[2] |
|---|
| 561 | ); |
|---|
| 562 | } |
|---|
| 563 | |
|---|
| 564 | // Now simply read the object data until |
|---|
| 565 | // we encounter an end-of-object marker |
|---|
| 566 | while(1) { |
|---|
| 567 | $value = $this->pdf_read_value($c); |
|---|
| 568 | if ($value === false || count($result) > 4) { |
|---|
| 569 | // in this case the parser coudn't find an endobj so we break here |
|---|
| 570 | break; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | if ($value[0] == PDF_TYPE_TOKEN && $value[1] === 'endobj') { |
|---|
| 574 | break; |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | $result[] = $value; |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | $c->reset($old_pos); |
|---|
| 581 | |
|---|
| 582 | if (isset($result[2][0]) && $result[2][0] == PDF_TYPE_STREAM) { |
|---|
| 583 | $result[0] = PDF_TYPE_STREAM; |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | return $result; |
|---|
| 587 | } |
|---|
| 588 | } else { |
|---|
| 589 | return $obj_spec; |
|---|
| 590 | } |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | /** |
|---|
| 596 | * Reads a token from the file |
|---|
| 597 | * |
|---|
| 598 | * @param object $c pdf_context |
|---|
| 599 | * @return mixed |
|---|
| 600 | */ |
|---|
| 601 | function pdf_read_token(&$c) |
|---|
| 602 | { |
|---|
| 603 | // If there is a token available |
|---|
| 604 | // on the stack, pop it out and |
|---|
| 605 | // return it. |
|---|
| 606 | |
|---|
| 607 | if (count($c->stack)) { |
|---|
| 608 | return array_pop($c->stack); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | // Strip away any whitespace |
|---|
| 612 | |
|---|
| 613 | do { |
|---|
| 614 | if (!$c->ensure_content()) { |
|---|
| 615 | return false; |
|---|
| 616 | } |
|---|
| 617 | $c->offset += strspn($c->buffer, " \n\r\t", $c->offset); |
|---|
| 618 | } while ($c->offset >= $c->length - 1); |
|---|
| 619 | |
|---|
| 620 | // Get the first character in the stream |
|---|
| 621 | |
|---|
| 622 | $char = $c->buffer[$c->offset++]; |
|---|
| 623 | |
|---|
| 624 | switch ($char) { |
|---|
| 625 | |
|---|
| 626 | case '[': |
|---|
| 627 | case ']': |
|---|
| 628 | case '(': |
|---|
| 629 | case ')': |
|---|
| 630 | |
|---|
| 631 | // This is either an array or literal string |
|---|
| 632 | // delimiter, Return it |
|---|
| 633 | |
|---|
| 634 | return $char; |
|---|
| 635 | |
|---|
| 636 | case '<': |
|---|
| 637 | case '>': |
|---|
| 638 | |
|---|
| 639 | // This could either be a hex string or |
|---|
| 640 | // dictionary delimiter. Determine the |
|---|
| 641 | // appropriate case and return the token |
|---|
| 642 | |
|---|
| 643 | if ($c->buffer[$c->offset] == $char) { |
|---|
| 644 | if (!$c->ensure_content()) { |
|---|
| 645 | return false; |
|---|
| 646 | } |
|---|
| 647 | $c->offset++; |
|---|
| 648 | return $char . $char; |
|---|
| 649 | } else { |
|---|
| 650 | return $char; |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | case '%': |
|---|
| 654 | |
|---|
| 655 | // This is a comment - jump over it! |
|---|
| 656 | |
|---|
| 657 | $pos = $c->offset; |
|---|
| 658 | while(1) { |
|---|
| 659 | $match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos); |
|---|
| 660 | if ($match === 0) { |
|---|
| 661 | if (!$c->increase_length()) { |
|---|
| 662 | return false; |
|---|
| 663 | } else { |
|---|
| 664 | continue; |
|---|
| 665 | } |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | $c->offset = $m[0][1]+strlen($m[0][0]); |
|---|
| 669 | |
|---|
| 670 | return $this->pdf_read_token($c); |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | default: |
|---|
| 674 | |
|---|
| 675 | // This is "another" type of token (probably |
|---|
| 676 | // a dictionary entry or a numeric value) |
|---|
| 677 | // Find the end and return it. |
|---|
| 678 | |
|---|
| 679 | if (!$c->ensure_content()) { |
|---|
| 680 | return false; |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | while(1) { |
|---|
| 684 | |
|---|
| 685 | // Determine the length of the token |
|---|
| 686 | |
|---|
| 687 | $pos = strcspn($c->buffer, " %[]<>()\r\n\t/", $c->offset); |
|---|
| 688 | |
|---|
| 689 | if ($c->offset + $pos <= $c->length - 1) { |
|---|
| 690 | break; |
|---|
| 691 | } else { |
|---|
| 692 | // If the script reaches this point, |
|---|
| 693 | // the token may span beyond the end |
|---|
| 694 | // of the current buffer. Therefore, |
|---|
| 695 | // we increase the size of the buffer |
|---|
| 696 | // and try again--just to be safe. |
|---|
| 697 | |
|---|
| 698 | $c->increase_length(); |
|---|
| 699 | } |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | $result = substr($c->buffer, $c->offset - 1, $pos + 1); |
|---|
| 703 | |
|---|
| 704 | $c->offset += $pos; |
|---|
| 705 | return $result; |
|---|
| 706 | } |
|---|
| 707 | } |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | } |
|---|