selimppc

pdfsearch.php

Jan 18th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. <?
  2.  
  3. /**********************************************************************
  4. **
  5. ** A class to search text in pdf documents.
  6. ** Not pretending to be useful other than that.
  7. ** But it can easily be extended to a full featured pdf document
  8. ** parser by anyone who chooses so.
  9. **
  10. ** Author: Rene Kluwen / Chimit Software <[email protected]>
  11. **
  12. ** License: Public Domain
  13. ** Warranty: None
  14. **
  15. ***********************************************************************/
  16.  
  17. class pdf_search {
  18.  
  19.         // Just one private variable.
  20.         // It holds the document.
  21.         var $_buffer;
  22.  
  23.         // Constructor. Takes the pdf document as only parameter
  24.         function pdf_search($buffer) {
  25.                 $this->_buffer = $buffer;
  26.         }
  27.  
  28.         // This function returns the next line from the document.
  29.         // If a stream follows, it is deflated into readable text.
  30.         function nextline() {
  31.                 $pos = strpos($this->_buffer, "\r");
  32.                 if ($pos === false) {
  33.                         return false;
  34.                 }
  35.                 $line = substr($this->_buffer, 0, $pos);
  36.                 $this->_buffer = substr($this->_buffer, $pos + 1);
  37.                 if ($line == "stream") {
  38.                         $endpos = strpos($this->_buffer, "endstream");
  39.                         $stream = substr($this->_buffer, 1, $endpos - 1);
  40.                         $stream = @gzuncompress($stream);
  41.                         $this->_buffer = $stream . substr($this->_buffer, $endpos + 9);
  42.                 }
  43.                 return $line;
  44.         }
  45.  
  46.         // This function returns the next line in the document that is printable text.
  47.         // We need it so we can search in just that portion.
  48.         function textline() {
  49.                 $line = $this->nextline();
  50.                 if ($line === false) {
  51.                         return false;
  52.                 }
  53.                 if (preg_match("/[^\\\\]\\((.+)[^\\\\]\\)/", $line, $match)) {
  54.                         $line = preg_replace("/\\\\(\d+)/e", "chr(0\\1);", $match[1]);
  55.                         return stripslashes($line);
  56.                 }
  57.                 return $this->textline();
  58.         }
  59.  
  60.         // This function returns true or false, indicating whether the document contains
  61.         // the text that is passed in $str.
  62.         function textfound($str) {
  63.                 while (($line = $this->textline()) !== false) {
  64.                         if (preg_match("/$str/i", $line) != 0) {
  65.                                 return true;
  66.                         }
  67.                 }
  68.                 return false;
  69.         }
  70. }
  71.  
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment