source-class-Com.Tecnick.Pdf.Encrypt.Output

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Output.php
  4:  *
  5:  * @since       2008-01-02
  6:  * @category    Library
  7:  * @package     PdfEncrypt
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-encrypt
 12:  *
 13:  * This file is part of tc-lib-pdf-encrypt software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Pdf\Encrypt;
 17: 
 18: /**
 19:  * Com\Tecnick\Pdf\Encrypt\Output
 20:  *
 21:  * PHP class for output encrypt PDF object
 22:  *
 23:  * @since       2008-01-02
 24:  * @category    Library
 25:  * @package     PdfEncrypt
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-pdf-encrypt
 30:  */
 31: abstract class Output
 32: {
 33:     /**
 34:      * Get the PDF encryption block
 35:      *
 36:      * @param int $pon Current PDF object number
 37:      *
 38:      * return string
 39:      */
 40:     public function getPdfEncryptionObj(&$pon)
 41:     {
 42:         $this->setMissingValues();
 43:         $this->encryptdata['objid'] = ++$pon;
 44:         $out = $this->encryptdata['objid'].' 0 obj'."\n"
 45:             .'<<'."\n"
 46:             .'/Filter /'.$this->encryptdata['Filter']."\n";
 47:         if (!empty($this->encryptdata['SubFilter'])) {
 48:             $out .= '/SubFilter /'.$this->encryptdata['SubFilter']."\n";
 49:         }
 50:         // V is a code specifying the algorithm to be used in encrypting and decrypting the document
 51:         $out .= '/V '.$this->encryptdata['V']."\n";
 52:         // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
 53:         $out .= '/Length '.$this->encryptdata['Length']."\n";
 54:         if ($this->encryptdata['V'] >= 4) {
 55:             $out .= $this->getCryptFilter();
 56:             // The name of the crypt filter that shall be used by default when decrypting streams.
 57:             $out .= '/StmF /'.$this->encryptdata['StmF']."\n";
 58:             // The name of the crypt filter that shall be used when decrypting all strings in the document.
 59:             $out .= '/StrF /'.$this->encryptdata['StrF']."\n";
 60:             /*
 61:             if (!empty($this->encryptdata['EFF'])) {
 62:                 // The name of the crypt filter that shall be used when encrypting embedded file streams
 63:                 // that do not have their own crypt filter specifier.
 64:                 $out .= ' /EFF /'.$this->encryptdata['EFF'];
 65:             }
 66:             */
 67:         }
 68:         $out .= $this->getAdditionalEncDic()
 69:             .'>>'."\n"
 70:             .'endobj'."\n";
 71:         return $out;
 72:     }
 73: 
 74:     /**
 75:      * Get Crypt Filter section
 76:      *
 77:      * A dictionary whose keys shall be crypt filter names
 78:      * and whose values shall be the corresponding crypt filter dictionaries.
 79:      *
 80:      * @return string
 81:      */
 82:     protected function getCryptFilter()
 83:     {
 84:         $out = '/CF <<'."\n"
 85:             .'/'.$this->encryptdata['StmF'].' <<'."\n"
 86:             .'/Type /CryptFilter'."\n"
 87:             .'/CFM /'.$this->encryptdata['CF']['CFM']."\n";  // The method used
 88:         if ($this->encryptdata['pubkey']) {
 89:             $out .= '/Recipients [';
 90:             foreach ($this->encryptdata['Recipients'] as $rec) {
 91:                 $out .= ' <'.$rec.'>';
 92:             }
 93:             $out .= ' ]'."\n"
 94:                 .'/EncryptMetadata '.$this->getBooleanString($this->encryptdata['CF']['EncryptMetadata'])."\n";
 95:         }
 96:         // The event to be used to trigger the authorization
 97:         // that is required to access encryption keys used by this filter.
 98:         $out .= '/AuthEvent /'.$this->encryptdata['CF']['AuthEvent']."\n";
 99:         if (!empty($this->encryptdata['CF']['Length'])) {
100:             // The bit length of the encryption key.
101:             $out .= '/Length '.$this->encryptdata['CF']['Length']."\n";
102:         }
103:         $out .= '>>'."\n"
104:             .'>>'."\n";
105:         return $out;
106:     }
107: 
108:     /**
109:      * get additional encryption dictionary entries for the standard security handler
110:      *
111:      * @return string
112:      */
113:     protected function getAdditionalEncDic()
114:     {
115:         $out = '';
116:         if ($this->encryptdata['pubkey']) {
117:             if (($this->encryptdata['V'] < 4) && !empty($this->encryptdata['Recipients'])) {
118:                 $out .= ' /Recipients [';
119:                 foreach ($this->encryptdata['Recipients'] as $rec) {
120:                     $out .= ' <'.$rec.'>';
121:                 }
122:                 $out .= ' ]'."\n";
123:             }
124:         } else {
125:             $out .= '/R ';
126:             if ($this->encryptdata['V'] == 5) { // AES-256
127:                 $out .= '5'."\n"
128:                     .'/OE ('.$this->escapeString($this->encryptdata['OE']).')'."\n"
129:                     .'/UE ('.$this->escapeString($this->encryptdata['UE']).')'."\n"
130:                     .'/Perms ('.$this->escapeString($this->encryptdata['perms']).')'."\n";
131:             } elseif ($this->encryptdata['V'] == 4) { // AES-128
132:                 $out .= '4'."\n";
133:             } elseif ($this->encryptdata['V'] < 2) { // RC-40
134:                 $out .= '2'."\n";
135:             } else { // RC-128
136:                 $out .= '3'."\n";
137:             }
138:             $out .= '/O ('.$this->escapeString($this->encryptdata['O']).')'."\n"
139:                 .'/U ('.$this->escapeString($this->encryptdata['U']).')'."\n"
140:                 .'/P '.$this->encryptdata['P']."\n"
141:                 .'/EncryptMetadata '.$this->getBooleanString($this->encryptdata['EncryptMetadata'])."\n";
142:         }
143:         return $out;
144:     }
145: 
146:     /**
147:      * Return a string representation of a boolean value
148:      *
149:      * @param bool $value Value to convert
150:      *
151:      * @return string
152:      */
153:     protected function getBooleanString($value)
154:     {
155:         return ($value ? 'true' : 'false');
156:     }
157: 
158:     /**
159:      * Set missing values
160:      */
161:     protected function setMissingValues()
162:     {
163:         if (!isset($this->encryptdata['EncryptMetadata'])) {
164:             $this->encryptdata['EncryptMetadata'] = true;
165:         }
166:         if (!empty($this->encryptdata['CF'])) {
167:             if (!isset($this->encryptdata['CF']['EncryptMetadata'])) {
168:                 $this->encryptdata['CF']['EncryptMetadata'] = true;
169:             }
170:         }
171:     }
172: }
173: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy