PDFEncryptor.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | This source file is subject to version 3.0 of the PHP license,       |
00007 // | that is bundled with this package in the file LICENSE, and is        |
00008 // | available at through the world-wide-web at                           |
00009 // | http://www.php.net/license/3_0.txt                                   |
00010 // | If you did not receive a copy of the PHP license and are unable to   |
00011 // | obtain it through the world-wide-web, please send a note to          |
00012 // | license@php.net so we can mail you a copy immediately.               |
00013 // +----------------------------------------------------------------------+
00014 // | Authors: Jason Rust <jrust@rustyparts.com>                           |
00015 // +----------------------------------------------------------------------+
00016 
00017 // }}}
00018 // {{{ includes
00019 
00020 require_once 'PEAR.php';
00021 
00022 // }}}
00023 // {{{ PDFEncryptor class
00024 
00038 // }}}
00039 class PDFEncryptor {
00040     // {{{ properties
00041 
00046     var $pdfFile = '';
00047 
00052     var $tmpDir = '/tmp';
00053 
00058     var $javaPath = '/usr/bin/java';
00059 
00064     var $iTextPath;
00065 
00070     var $encryptPdfPath;
00071 
00076     var $allowPrinting = false;
00077 
00082     var $allowModifyContents = false;
00083 
00088     var $allowCopy = false;
00089 
00094     var $allowModifyAnnotations = false;
00095 
00100     var $allowScreenReaders = false;
00101 
00106     var $allowAssembly = false;
00107 
00112     var $allowFillIn = false;
00113 
00118     var $allowDegradedPrinting = false;
00119 
00124     var $encryptionStrength = 128;
00125 
00130     var $userPassword = '';
00131 
00136     var $ownerPassword = '';
00137 
00142     var $subject;
00143 
00148     var $title;
00149 
00154     var $author;
00155 
00160     var $creator = 'PDFEncryptor';
00161 
00166     var $keywords;
00167 
00168     // }}}
00169     // {{{ constructor
00170 
00179     function PDFEncryptor($in_pdfFile)
00180     {
00181         $this->pdfFile = $in_pdfFile;
00182         $this->iTextPath = dirname(__FILE__) . '/lib/itext.jar';
00183         $this->encryptPdfPath = dirname(__FILE__) . '/lib/encrypt_pdf.java';
00184     }
00185 
00186     // }}}
00187     // {{{ setPdfFile()
00188 
00197     function setPdfFile($in_value)
00198     {
00199         $this->pdfFile = $in_value;
00200     }
00201 
00202     // }}}
00203     // {{{ setJavaPath()
00204 
00213     function setJavaPath($in_value)
00214     {
00215         $this->javaPath = $in_value;
00216     }
00217 
00218     // }}}
00219     // {{{ setITextPath()
00220 
00229     function setITextPath($in_value)
00230     {
00231         $this->iTextPath = $in_value;
00232     }
00233 
00234     // }}}
00235     // {{{ setEncryptPdfPath()
00236 
00245     function setEncryptPdfPath($in_value)
00246     {
00247         $this->encryptPdfPath = $in_value;
00248     }
00249 
00250     // }}}
00251     // {{{ setAllowPrinting()
00252 
00261     function setAllowPrinting($in_value)
00262     {
00263         $this->allowPrinting = $in_value;
00264     }
00265 
00266     // }}}
00267     // {{{ setAllowModifyContents()
00268 
00277     function setAllowModifyContents($in_value)
00278     {
00279         $this->allowModifyContents = $in_value;
00280     }
00281 
00282     // }}}
00283     // {{{ setAllowCopy()
00284 
00293     function setAllowCopy($in_value)
00294     {
00295         $this->allowCopy = $in_value;
00296     }
00297 
00298     // }}}
00299     // {{{ setAllowModifyAnnotations()
00300 
00309     function setAllowModifyAnnotations($in_value)
00310     {
00311         $this->allowModifyAnnotations = $in_value;
00312     }
00313 
00314     // }}}
00315     // {{{ setAllowScreenReaders()
00316 
00325     function setAllowScreenReaders($in_value)
00326     {
00327         $this->allowScreenReaders = $in_value;
00328     }
00329 
00330     // }}}
00331     // {{{ setAllowAssembly()
00332 
00341     function setAllowAssembly($in_value)
00342     {
00343         $this->allowAssembly = $in_value;
00344     }
00345 
00346     // }}}
00347     // {{{ setAllowFillIn()
00348 
00357     function setAllowFillIn($in_value)
00358     {
00359         $this->allowFillIn = $in_value;
00360     }
00361 
00362     // }}}
00363     // {{{ setAllowDegradedPrinting()
00364 
00373     function setAllowDegradedPrinting($in_value)
00374     {
00375         $this->allowDegradedPrinting = $in_value;
00376     }
00377 
00378     // }}}
00379     // {{{ setEncryptionStrength()
00380 
00389     function setEncryptionStrength($in_value)
00390     {
00391         if ($in_value == 40) {
00392             $this->encryptionStrength = $in_value;
00393         }
00394         else {
00395             $this->encryptionStrength = 128;
00396         }
00397     }
00398 
00399     // }}}
00400     // {{{ setUserPassword()
00401 
00410     function setUserPassword($in_value)
00411     {
00412         $this->userPassword = $in_value;
00413     }
00414 
00415     // }}}
00416     // {{{ setOwnerPassword()
00417 
00426     function setOwnerPassword($in_value)
00427     {
00428         $this->ownerPassword = $in_value;
00429     }
00430 
00431     // }}}
00432     // {{{ setSubject()
00433 
00442     function setSubject($in_value)
00443     {
00444         $this->subject = $in_value;
00445     }
00446 
00447     // }}}
00448     // {{{ setTitle()
00449 
00458     function setTitle($in_value)
00459     {
00460         $this->title = $in_value;
00461     }
00462 
00463     // }}}
00464     // {{{ setAuthor()
00465 
00474     function setAuthor($in_value)
00475     {
00476         $this->author = $in_value;
00477     }
00478 
00479     // }}}
00480     // {{{ setCreator()
00481 
00490     function setCreator($in_value)
00491     {
00492         $this->creator = $in_value;
00493     }
00494 
00495     // }}}
00496     // {{{ setKeywords()
00497 
00506     function setKeywords($in_value)
00507     {
00508         $this->keywords = $in_value;
00509     }
00510 
00511     // }}}
00512     // {{{ setTmpDir()
00513 
00522     function setTmpDir($in_path) {
00523         $this->tmpDir = $in_path;
00524     }
00525 
00526     // }}}
00527     // {{{ encrypt()
00528 
00535     function encrypt()
00536     {
00537         // make sure pdf file exists
00538         if (!file_exists($this->pdfFile)) {
00539             return new PDFEncryptorException("Error: The PDF file does not exist: $this->pdfFile");
00540         }
00541 
00542         // make sure iText jar file exists
00543         if (!file_exists($this->iTextPath)) {
00544             return new PDFEncryptorException("Error: The iText jar file does not exist: $this->iTextPath.  You can download it at http://www.lowagie.com/iText");
00545         }
00546 
00547         // make sure encryptPdf file exists
00548         if (!file_exists($this->encryptPdfPath)) {
00549             return new PDFEncryptorException("Error: The encrypt_pdf java class does not exist: $this->encryptPdfPath.");
00550         }
00551 
00552         // make sure the java binary exists 
00553         if (!@is_executable($this->javaPath)) {
00554             return new PDFEncryptorException("Error: java [$this->javaPath] is not executable.  You can download it at http://java.sun.com");
00555         }
00556 
00557         // this can take a while with large files
00558         set_time_limit(60);
00559 
00560         // Copy file so it can be worked on
00561         $tmp_file = tempnam($this->tmpDir, 'PDF-');
00562         copy($this->pdfFile, $tmp_file);
00563         $s_encryption = $this->_boolToInt($this->allowPrinting);
00564         $s_encryption .= $this->_boolToInt($this->allowModifyContents);
00565         $s_encryption .= $this->_boolToInt($this->allowCopy);
00566         $s_encryption .= $this->_boolToInt($this->allowModifyAnnotations);
00567         $s_encryption .= $this->_boolToInt($this->allowFillIn);
00568         $s_encryption .= $this->_boolToInt($this->allowScreenReaders);
00569         $s_encryption .= $this->_boolToInt($this->allowAssembly);
00570         $s_encryption .= $this->_boolToInt($this->allowDegradedPrinting);
00571 
00572         $s_properties = $this->_getProperty('Subject', $this->subject);
00573         $s_properties .= $this->_getProperty('Title', $this->title );
00574         $s_properties .= $this->_getProperty('Author', $this->author);
00575         $s_properties .= $this->_getProperty('Creator', $this->creator);
00576         $s_properties .= $this->_getProperty('Keywords', $this->keywords);
00577 
00578         // execute argument
00579         $s_arg = $this->javaPath . ' -cp ' . $this->iTextPath . ':' . dirname($this->encryptPdfPath) . 
00580                  ' encrypt_pdf ' .  $tmp_file . ' ' .  $this->pdfFile . ' "' . 
00581                  addcslashes($this->userPassword, '"') .  '" "' .  addcslashes($this->ownerPassword, '"') . 
00582                  '" ' . $s_encryption . ' ' .  $this->encryptionStrength .  ' ' . $s_properties;
00583 
00584         exec($s_arg);
00585         unlink($tmp_file);
00586         return true;
00587     }
00588 
00589     // }}}
00590     // {{{ _boolToInt()
00591 
00600     function _boolToInt($in_bool)
00601     {
00602         return $in_bool ? 1 : 0;
00603     }
00604 
00605     // }}}
00606     // {{{ _getProperty()
00607 
00617     function _getProperty($in_key, $in_value)
00618     {
00619         if (!is_null($in_value)) {
00620             return $in_key . ' "' . addcslashes($in_value, '"') . '" ';
00621         }
00622         else {
00623             return '';
00624         }
00625     }
00626 
00627     // }}}
00628 }
00629 
00630 // {{{ PDFEncryptorException 
00631 
00632 class PDFEncryptorException extends PEAR_Error {
00633     var $classname             = 'PDFEncryptor';
00634     var $error_message_prepend = 'Error: ';
00635 
00636     function PDFEncryptor($message)
00637     {
00638         $this->PEAR_Error($message);
00639     }
00640 }
00641 
00642 // }}}
00643 // {{{ is_executable()
00644 
00645 if (!function_exists('is_executable')) {
00655     function is_executable($in_filename)
00656     {
00657         return file_exists($in_filename);
00658     }
00659 }
00660 
00661 // }}}

Generated on Sat Feb 11 08:43:10 2006 for HTML_ToPDF by  doxygen 1.4.3-20050530