module.misc.pdf.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.misc.pdf.php //
  11. // module for analyzing PDF files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_pdf extends getid3_handler
  19. {
  20. /** misc.pdf
  21. * return full details of PDF Cross-Reference Table (XREF)
  22. *
  23. * @var bool
  24. */
  25. public $returnXREF = false;
  26. /**
  27. * @return bool
  28. */
  29. public function Analyze() {
  30. $info = &$this->getid3->info;
  31. $this->fseek(0);
  32. if (preg_match('#^%PDF-([0-9\\.]+)$#', rtrim($this->fgets()), $matches)) {
  33. $info['pdf']['header']['version'] = floatval($matches[1]);
  34. $info['fileformat'] = 'pdf';
  35. // the PDF Cross-Reference Table (XREF) is located near the end of the file
  36. // the starting offset is specified in the penultimate section, on the two lines just before "%%EOF"
  37. // the first line is "startxref", the second line is the byte offset of the XREF.
  38. // We know the length of "%%EOF" and "startxref", but the offset could be 2-10 bytes,
  39. // and we're not sure if the line ends are one or two bytes, so we might find "startxref" as little as 18(?) bytes
  40. // from EOF, but it could 30 bytes, so we start 40 bytes back just to be safe and do a search for the data we want.
  41. $this->fseek(-40, SEEK_END);
  42. if (preg_match('#[\r\n]startxref[ \r\n]+([0-9]+)[ \r\n]+#', $this->fread(40), $matches)) {
  43. $info['pdf']['trailer']['startxref'] = intval($matches[1]);
  44. $this->parseXREF($info['pdf']['trailer']['startxref']);
  45. if (!empty($info['pdf']['xref']['offset'])) {
  46. while (!$this->feof() && (max(array_keys($info['pdf']['xref']['offset'])) > $info['pdf']['xref']['count'])) {
  47. // suspect that there may be another XREF entry somewhere in the file, brute-force scan for it
  48. /*
  49. // starting at last known entry of main XREF table
  50. $this->fseek(max($info['pdf']['xref']['offset']));
  51. */
  52. // starting at the beginning of the file
  53. $this->fseek(0);
  54. while (!$this->feof()) {
  55. $XREFoffset = $this->ftell();
  56. if (rtrim($this->fgets()) == 'xref') {
  57. if (empty($info['pdf']['xref']['xref_offsets']) || !in_array($XREFoffset, $info['pdf']['xref']['xref_offsets'])) {
  58. $this->parseXREF($XREFoffset);
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. asort($info['pdf']['xref']['offset']);
  65. $maxObjLengths = array();
  66. $prevOffset = 0;
  67. $prevObjNum = 0;
  68. foreach ($info['pdf']['xref']['offset'] as $objectNumber => $offset) {
  69. // walk through all listed offsets to calculate the maximum possible length for each known object
  70. if ($prevObjNum) {
  71. $maxObjLengths[$prevObjNum] = $offset - $prevOffset;
  72. }
  73. $prevOffset = $offset;
  74. $prevObjNum = $objectNumber;
  75. }
  76. ksort($maxObjLengths);
  77. foreach ($info['pdf']['xref']['offset'] as $objectNumber => $offset) {
  78. if ($info['pdf']['xref']['entry'][$objectNumber] == 'f') {
  79. // "free" object means "deleted", ignore
  80. continue;
  81. }
  82. if (!empty($maxObjLengths[$objectNumber]) && ($maxObjLengths[$objectNumber] < $this->getid3->option_fread_buffer_size)) {
  83. // ignore object that are zero-size or >32kB, they are unlikely to contain information we're interested in
  84. $this->fseek($offset);
  85. $objBlob = $this->fread($maxObjLengths[$objectNumber]);
  86. if (preg_match('#^'.$objectNumber.'[\\x00 \\r\\n\\t]*([0-9]+)[\\x00 \\r\\n\\t]*obj[\\x00 \\r\\n\\t]*(.*)(endobj)?[\\x00 \\r\\n\\t]*$#s', $objBlob, $matches)) {
  87. list($dummy, $generation, $objectData) = $matches;
  88. if (preg_match('#^<<[\r\n\s]*(/Type|/Pages|/Parent [0-9]+ [0-9]+ [A-Z]|/Count [0-9]+|/Kids *\\[[0-9A-Z ]+\\]|[\r\n\s])+[\r\n\s]*>>#', $objectData, $matches)) {
  89. if (preg_match('#/Count ([0-9]+)#', $objectData, $matches)) {
  90. $info['pdf']['pages'] = (int) $matches[1];
  91. break; // for now this is the only data we're looking for in the PDF not need to loop through every object in the file (and a large PDF may contain MANY objects). And it MAY be possible that there are other objects elsewhere in the file that define additional (or removed?) pages
  92. }
  93. }
  94. } else {
  95. $this->error('Unexpected structure "'.substr($objBlob, 0, 100).'" at offset '.$offset);
  96. break;
  97. }
  98. }
  99. }
  100. if (!$this->returnXREF) {
  101. unset($info['pdf']['xref']['offset'], $info['pdf']['xref']['generation'], $info['pdf']['xref']['entry'], $info['pdf']['xref']['xref_offsets']);
  102. }
  103. } else {
  104. $this->error('Did not find "xref" at offset '.$info['pdf']['trailer']['startxref']);
  105. }
  106. } else {
  107. $this->error('Did not find "startxref" in the last 40 bytes of the PDF');
  108. }
  109. $this->warning('PDF parsing incomplete in this version of getID3() ['.$this->getid3->version().']');
  110. return true;
  111. }
  112. $this->error('Did not find "%PDF" at the beginning of the PDF');
  113. return false;
  114. }
  115. /**
  116. * @return bool
  117. */
  118. private function parseXREF($XREFoffset) {
  119. $info = &$this->getid3->info;
  120. $this->fseek($XREFoffset);
  121. if (rtrim($this->fgets()) == 'xref') {
  122. $info['pdf']['xref']['xref_offsets'][$XREFoffset] = $XREFoffset;
  123. list($firstObjectNumber, $XREFcount) = explode(' ', rtrim($this->fgets()));
  124. $firstObjectNumber = (int) $firstObjectNumber;
  125. $XREFcount = (int) $XREFcount;
  126. $info['pdf']['xref']['count'] = $XREFcount + (!empty($info['pdf']['xref']['count']) ? $info['pdf']['xref']['count'] : 0);
  127. for ($i = 0; $i < $XREFcount; $i++) {
  128. $line = rtrim($this->fgets());
  129. if (preg_match('#^([0-9]+) ([0-9]+) ([nf])$#', $line, $matches)) {
  130. $info['pdf']['xref']['offset'][($firstObjectNumber + $i)] = (int) $matches[1];
  131. $info['pdf']['xref']['generation'][($firstObjectNumber + $i)] = (int) $matches[2];
  132. $info['pdf']['xref']['entry'][($firstObjectNumber + $i)] = $matches[3];
  133. } else {
  134. $this->error('failed to parse XREF entry #'.$i.' in XREF table at offset '.$XREFoffset);
  135. return false;
  136. }
  137. }
  138. sort($info['pdf']['xref']['xref_offsets']);
  139. return true;
  140. }
  141. $this->warning('failed to find expected XREF structure at offset '.$XREFoffset);
  142. return false;
  143. }
  144. }