|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- <?php
-
-
-
- class pdf_parser
- {
-
-
- const TYPE_NULL = 0;
-
-
-
- const TYPE_NUMERIC = 1;
-
-
-
- const TYPE_TOKEN = 2;
-
-
-
- const TYPE_HEX = 3;
-
-
-
- const TYPE_STRING = 4;
-
-
-
- const TYPE_DICTIONARY = 5;
-
-
-
- const TYPE_ARRAY = 6;
-
-
-
- const TYPE_OBJDEC = 7;
-
-
-
- const TYPE_OBJREF = 8;
-
-
-
- const TYPE_OBJECT = 9;
-
-
-
- const TYPE_STREAM = 10;
-
-
-
- const TYPE_BOOLEAN = 11;
-
-
-
- const TYPE_REAL = 12;
-
-
-
- static public $searchForStartxrefLength = 5500;
-
-
-
- public $filename;
-
-
-
- protected $_f;
-
-
-
- protected $_c;
-
-
-
- protected $_xref;
-
-
-
- protected $_root;
-
-
-
- protected $_pdfVersion;
-
-
-
- protected $_readPlain = true;
-
-
-
- protected $_currentObj;
-
-
-
- public function __construct($filename)
- {
- $this->filename = $filename;
-
- $this->_f = @fopen($this->filename, 'rb');
-
- if (!$this->_f) {
- throw new InvalidArgumentException(sprintf('Cannot open %s !', $filename));
- }
-
- $this->getPdfVersion();
-
- if (!class_exists('pdf_context')) {
- require_once('pdf_context.php');
- }
- $this->_c = new pdf_context($this->_f);
-
-
- $this->_xref = array();
- $this->_readXref($this->_xref, $this->_findXref());
-
-
- $this->getEncryption();
-
-
- $this->_readRoot();
- }
-
-
-
- public function __destruct()
- {
- $this->closeFile();
- }
-
-
-
- public function closeFile()
- {
- if (isset($this->_f) && is_resource($this->_f)) {
- fclose($this->_f);
- unset($this->_f);
- }
- }
-
-
-
- public function getEncryption()
- {
- if (isset($this->_xref['trailer'][1]['/Encrypt'])) {
- throw new Exception('File is encrypted!');
- }
- }
-
-
-
- public function getPdfVersion()
- {
- if ($this->_pdfVersion === null) {
- fseek($this->_f, 0);
- preg_match('/\d\.\d/', fread($this->_f, 16), $m);
- if (isset($m[0]))
- $this->_pdfVersion = $m[0];
- }
-
- return $this->_pdfVersion;
- }
-
-
-
- protected function _readRoot()
- {
- if ($this->_xref['trailer'][1]['/Root'][0] != self::TYPE_OBJREF) {
- throw new Exception('Wrong Type of Root-Element! Must be an indirect reference');
- }
-
- $this->_root = $this->resolveObject($this->_xref['trailer'][1]['/Root']);
- }
-
-
-
- protected function _findXref()
- {
- $toRead = self::$searchForStartxrefLength;
-
- $stat = fseek($this->_f, -$toRead, SEEK_END);
- if ($stat === -1) {
- fseek($this->_f, 0);
- }
-
- $data = fread($this->_f, $toRead);
-
- $keywordPos = strpos(strrev($data), strrev('startxref'));
- if (false === $keywordPos) {
- $keywordPos = strpos(strrev($data), strrev('startref'));
- }
-
- if (false === $keywordPos) {
- throw new Exception('Unable to find "startxref" keyword.');
- }
-
- $pos = strlen($data) - $keywordPos;
- $data = substr($data, $pos);
-
- if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) {
- throw new Exception('Unable to find pointer to xref table.');
- }
-
- return (int) $matches[1];
- }
-
-
-
- protected function _readXref(&$result, $offset)
- {
- $tempPos = $offset - min(20, $offset);
- fseek($this->_f, $tempPos);
-
- $data = fread($this->_f, 100);
-
- $xrefPos = strrpos($data, 'xref');
-
- if ($xrefPos === false) {
- $this->_c->reset($offset);
- $xrefStreamObjDec = $this->_readValue($this->_c);
-
- if (is_array($xrefStreamObjDec) && isset($xrefStreamObjDec[0]) && $xrefStreamObjDec[0] == self::TYPE_OBJDEC) {
- throw new Exception(
- sprintf(
- 'This document (%s) probably uses a compression technique which is not supported by the ' .
- 'free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)',
- $this->filename
- )
- );
- } else {
- throw new Exception('Unable to find xref table.');
- }
- }
-
- if (!isset($result['xrefLocation'])) {
- $result['xrefLocation'] = $tempPos + $xrefPos;
- $result['maxObject'] = 0;
- }
-
- $cycles = -1;
- $bytesPerCycle = 100;
-
- fseek($this->_f, $tempPos = $tempPos + $xrefPos + 4);
- $data = fread($this->_f, $bytesPerCycle);
-
- while (($trailerPos = strpos($data, 'trailer', max($bytesPerCycle * $cycles++, 0))) === false && !feof($this->_f)) {
- $data .= fread($this->_f, $bytesPerCycle);
- }
-
- if ($trailerPos === false) {
- throw new Exception('Trailer keyword not found after xref table');
- }
-
- $data = ltrim(substr($data, 0, $trailerPos));
-
-
- $found = preg_match_all("/(\r\n|\n|\r)/", substr($data, 0, 100), $m);
- if ($found === 0) {
- throw new Exception('Xref table seems to be corrupted.');
- }
- $differentLineEndings = count(array_unique($m[0]));
- if ($differentLineEndings > 1) {
- $lines = preg_split("/(\r\n|\n|\r)/", $data, -1, PREG_SPLIT_NO_EMPTY);
- } else {
- $lines = explode($m[0][0], $data);
- }
-
- $data = $differentLineEndings = $m = null;
- unset($data, $differentLineEndings, $m);
-
- $linesCount = count($lines);
-
- $start = 1;
-
- for ($i = 0; $i < $linesCount; $i++) {
- $line = trim($lines[$i]);
- if ($line) {
- $pieces = explode(' ', $line);
- $c = count($pieces);
- switch($c) {
- case 2:
- $start = (int)$pieces[0];
- $end = $start + (int)$pieces[1];
- if ($end > $result['maxObject'])
- $result['maxObject'] = $end;
- break;
- case 3:
- if (!isset($result['xref'][$start]))
- $result['xref'][$start] = array();
-
- if (!array_key_exists($gen = (int) $pieces[1], $result['xref'][$start])) {
- $result['xref'][$start][$gen] = $pieces[2] == 'n' ? (int) $pieces[0] : null;
- }
- $start++;
- break;
- default:
- throw new Exception('Unexpected data in xref table');
- }
- }
- }
-
- $lines = $pieces = $line = $start = $end = $gen = null;
- unset($lines, $pieces, $line, $start, $end, $gen);
-
- $this->_c->reset($tempPos + $trailerPos + 7);
- $trailer = $this->_readValue($this->_c);
-
- if (!isset($result['trailer'])) {
- $result['trailer'] = $trailer;
- }
-
- if (isset($trailer[1]['/Prev'])) {
- $this->_readXref($result, $trailer[1]['/Prev'][1]);
- }
-
- $trailer = null;
- unset($trailer);
-
- return true;
- }
-
-
-
- protected function _readValue(&$c, $token = null)
- {
- if (is_null($token)) {
- $token = $this->_readToken($c);
- }
-
- if ($token === false) {
- return false;
- }
-
- switch ($token) {
- case '<':
-
-
-
- $pos = $c->offset;
-
- while(1) {
-
- $match = strpos($c->buffer, '>', $pos);
-
-
-
-
- if ($match === false) {
- if (!$c->increaseLength()) {
- return false;
- } else {
- continue;
- }
- }
-
- $result = substr($c->buffer, $c->offset, $match - $c->offset);
- $c->offset = $match + 1;
-
- return array (self::TYPE_HEX, $result);
- }
- break;
-
- case '<<':
-
-
- $result = array();
-
-
-
- while (($key = $this->_readToken($c)) !== '>>') {
- if ($key === false) {
- return false;
- }
-
- if (($value = $this->_readValue($c)) === false) {
- return false;
- }
-
-
- if ($value[0] == self::TYPE_TOKEN && $value[1] == '>>') {
- $result[$key] = array(self::TYPE_NULL);
- break;
- }
-
- $result[$key] = $value;
- }
-
- return array (self::TYPE_DICTIONARY, $result);
-
- case '[':
-
-
- $result = array();
-
-
-
- while (($token = $this->_readToken($c)) !== ']') {
- if ($token === false) {
- return false;
- }
-
- if (($value = $this->_readValue($c, $token)) === false) {
- return false;
- }
-
- $result[] = $value;
- }
-
- return array (self::TYPE_ARRAY, $result);
-
- case '(':
-
- $pos = $c->offset;
-
- $openBrackets = 1;
- do {
- for (; $openBrackets != 0 && $pos < $c->length; $pos++) {
- switch (ord($c->buffer[$pos])) {
- case 0x28:
- $openBrackets++;
- break;
- case 0x29:
- $openBrackets--;
- break;
- case 0x5C:
- $pos++;
- }
- }
- } while($openBrackets != 0 && $c->increaseLength());
-
- $result = substr($c->buffer, $c->offset, $pos - $c->offset - 1);
- $c->offset = $pos;
-
- return array (self::TYPE_STRING, $result);
-
- case 'stream':
- $tempPos = $c->getPos() - strlen($c->buffer);
- $tempOffset = $c->offset;
-
- $c->reset($startPos = $tempPos + $tempOffset);
-
-
- while ($c->buffer[0] !== chr(10) && $c->buffer[0] !== chr(13)) {
- $c->reset(++$startPos);
- if ($c->ensureContent() === false) {
- throw new Exception(
- 'Unable to parse stream data. No newline followed the stream keyword.'
- );
- }
- }
-
- $e = 0;
- if ($c->buffer[0] == chr(10) || $c->buffer[0] == chr(13))
- $e++;
- if ($c->buffer[1] == chr(10) && $c->buffer[0] != chr(10))
- $e++;
-
- if ($this->_currentObj[1][1]['/Length'][0] == self::TYPE_OBJREF) {
- $tmpLength = $this->resolveObject($this->_currentObj[1][1]['/Length']);
- $length = $tmpLength[1][1];
- } else {
- $length = $this->_currentObj[1][1]['/Length'][1];
- }
-
- if ($length > 0) {
- $c->reset($startPos + $e, $length);
- $v = $c->buffer;
- } else {
- $v = '';
- }
-
- $c->reset($startPos + $e + $length);
- $endstream = $this->_readToken($c);
-
- if ($endstream != 'endstream') {
- $c->reset($startPos + $e + $length + 9);
-
-
- }
-
- return array(self::TYPE_STREAM, $v);
-
- default:
- if (is_numeric($token)) {
-
-
- if (($tok2 = $this->_readToken($c)) !== false) {
- if (is_numeric($tok2)) {
-
-
-
-
-
-
- if (($tok3 = $this->_readToken($c)) !== false) {
- switch ($tok3) {
- case 'obj':
- return array(self::TYPE_OBJDEC, (int)$token, (int)$tok2);
- case 'R':
- return array(self::TYPE_OBJREF, (int)$token, (int)$tok2);
- }
-
-
-
- array_push($c->stack, $tok3);
- }
- }
-
- array_push($c->stack, $tok2);
- }
-
- if ($token === (string)((int)$token))
- return array(self::TYPE_NUMERIC, (int)$token);
- else
- return array(self::TYPE_REAL, (float)$token);
- } else if ($token == 'true' || $token == 'false') {
- return array(self::TYPE_BOOLEAN, $token == 'true');
- } else if ($token == 'null') {
- return array(self::TYPE_NULL);
- } else {
-
- return array(self::TYPE_TOKEN, $token);
- }
- }
- }
-
-
-
- public function resolveObject($objSpec)
- {
- $c = $this->_c;
-
-
- if (!is_array($objSpec)) {
- return false;
- }
-
- if ($objSpec[0] == self::TYPE_OBJREF) {
-
-
- if (isset($this->_xref['xref'][$objSpec[1]][$objSpec[2]])) {
-
-
-
-
-
-
-
- $oldPos = $c->getPos();
-
-
-
-
- $c->reset($this->_xref['xref'][$objSpec[1]][$objSpec[2]]);
-
- $header = $this->_readValue($c);
-
- if ($header[0] != self::TYPE_OBJDEC || $header[1] != $objSpec[1] || $header[2] != $objSpec[2]) {
- $toSearchFor = $objSpec[1] . ' ' . $objSpec[2] . ' obj';
- if (preg_match('/' . $toSearchFor . '/', $c->buffer)) {
- $c->offset = strpos($c->buffer, $toSearchFor) + strlen($toSearchFor);
-
- $c->stack = array();
- } else {
- throw new Exception(
- sprintf("Unable to find object (%s, %s) at expected location.", $objSpec[1], $objSpec[2])
- );
- }
- }
-
-
-
-
- $result = array (
- self::TYPE_OBJECT,
- 'obj' => $objSpec[1],
- 'gen' => $objSpec[2]
- );
-
- $this->_currentObj =& $result;
-
-
-
- while (true) {
- $value = $this->_readValue($c);
- if ($value === false || count($result) > 4) {
-
- break;
- }
-
- if ($value[0] == self::TYPE_TOKEN && $value[1] === 'endobj') {
- break;
- }
-
- $result[] = $value;
- }
-
- $c->reset($oldPos);
-
- if (isset($result[2][0]) && $result[2][0] == self::TYPE_STREAM) {
- $result[0] = self::TYPE_STREAM;
- }
-
- } else {
- throw new Exception(
- sprintf("Unable to find object (%s, %s) at expected location.", $objSpec[1], $objSpec[2])
- );
- }
-
- return $result;
- } else {
- return $objSpec;
- }
- }
-
-
-
- protected function _readToken($c)
- {
-
-
-
-
- if (count($c->stack)) {
- return array_pop($c->stack);
- }
-
-
-
- do {
- if (!$c->ensureContent()) {
- return false;
- }
- $c->offset += strspn($c->buffer, "\x20\x0A\x0C\x0D\x09\x00", $c->offset);
- } while ($c->offset >= $c->length - 1);
-
-
-
- $char = $c->buffer[$c->offset++];
-
- switch ($char) {
-
- case '[':
- case ']':
- case '(':
- case ')':
-
-
-
-
- return $char;
-
- case '<':
- case '>':
-
-
-
-
-
- if ($c->buffer[$c->offset] == $char) {
- if (!$c->ensureContent()) {
- return false;
- }
- $c->offset++;
- return $char . $char;
- } else {
- return $char;
- }
-
- case '%':
-
-
-
- $pos = $c->offset;
- while(1) {
- $match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos);
- if ($match === 0) {
- if (!$c->increaseLength()) {
- return false;
- } else {
- continue;
- }
- }
-
- $c->offset = $m[0][1] + strlen($m[0][0]);
-
- return $this->_readToken($c);
- }
-
- default:
-
-
-
-
-
- if (!$c->ensureContent()) {
- return false;
- }
-
- while(1) {
-
-
-
- $pos = strcspn($c->buffer, "\x20%[]<>()/\x0A\x0C\x0D\x09\x00", $c->offset);
-
- if ($c->offset + $pos <= $c->length - 1) {
- break;
- } else {
-
-
-
-
-
-
- $c->increaseLength();
- }
- }
-
- $result = substr($c->buffer, $c->offset - 1, $pos + 1);
-
- $c->offset += $pos;
-
- return $result;
- }
- }
-
-
-
- protected function _unFilterStream($obj)
- {
- $filters = array();
-
- if (isset($obj[1][1]['/Filter'])) {
- $filter = $obj[1][1]['/Filter'];
-
- if ($filter[0] == pdf_parser::TYPE_OBJREF) {
- $tmpFilter = $this->resolveObject($filter);
- $filter = $tmpFilter[1];
- }
-
- if ($filter[0] == pdf_parser::TYPE_TOKEN) {
- $filters[] = $filter;
- } else if ($filter[0] == pdf_parser::TYPE_ARRAY) {
- $filters = $filter[1];
- }
- }
-
- $stream = $obj[2][1];
-
- foreach ($filters AS $filter) {
- switch ($filter[1]) {
- case '/FlateDecode':
- case '/Fl':
- if (function_exists('gzuncompress')) {
- $oStream = $stream;
- $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
- } else {
- throw new Exception(
- sprintf('To handle %s filter, please compile php with zlib support.', $filter[1])
- );
- }
-
- if ($stream === false) {
- $tries = 0;
- while ($tries < 8 && ($stream === false || strlen($stream) < strlen($oStream))) {
- $oStream = substr($oStream, 1);
- $stream = @gzinflate($oStream);
- $tries++;
- }
-
- if ($stream === false) {
- throw new Exception('Error while decompressing stream.');
- }
- }
- break;
- case '/LZWDecode':
- if (!class_exists('FilterLZW')) {
- require_once('filters/FilterLZW.php');
- }
- $decoder = new FilterLZW();
- $stream = $decoder->decode($stream);
- break;
- case '/ASCII85Decode':
- if (!class_exists('FilterASCII85')) {
- require_once('filters/FilterASCII85.php');
- }
- $decoder = new FilterASCII85();
- $stream = $decoder->decode($stream);
- break;
- case '/ASCIIHexDecode':
- if (!class_exists('FilterASCIIHexDecode')) {
- require_once('filters/FilterASCIIHexDecode.php');
- }
- $decoder = new FilterASCIIHexDecode();
- $stream = $decoder->decode($stream);
- break;
- case null:
- break;
- default:
- throw new Exception(sprintf('Unsupported Filter: %s', $filter[1]));
- }
- }
-
- return $stream;
- }
- }
|