Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require('fpdf.php');
- //=== AlphaPDF class ==
- //-- extends this class from FPDF class
- class AlphaPDF extends FPDF
- {
- var $extgstates;
- function AlphaPDF($orientation='P', $unit='mm', $format='A4')
- {
- parent::FPDF($orientation, $unit, $format);
- $this->extgstates = array();
- }
- function SetAlpha($alpha, $bm='Normal')
- {
- $gs = $this->AddExtGState(array('ca'=>$alpha, 'CA'=>$alpha, 'BM'=>'/'.$bm));
- $this->SetExtGState($gs);
- }
- function AddExtGState($parms)
- {
- $n = count($this->extgstates)+1;
- $this->extgstates[$n]['parms'] = $parms;
- return $n;
- }
- function SetExtGState($gs)
- {
- $this->_out(sprintf('/GS%d gs', $gs));
- }
- function _enddoc()
- {
- if(!empty($this->extgstates) && $this->PDFVersion<'1.4')
- $this->PDFVersion='1.4';
- parent::_enddoc();
- }
- function _putextgstates()
- {
- for ($i = 1; $i <= count($this->extgstates); $i++)
- {
- $this->_newobj();
- $this->extgstates[$i]['n'] = $this->n;
- $this->_out('<</Type /ExtGState');
- foreach ($this->extgstates[$i]['parms'] as $k=>$v)
- $this->_out('/'.$k.' '.$v);
- $this->_out('>>');
- $this->_out('endobj');
- }
- }
- function _putresourcedict()
- {
- parent::_putresourcedict();
- $this->_out('/ExtGState <<');
- foreach($this->extgstates as $k=>$extgstate)
- $this->_out('/GS'.$k.' '.$extgstate['n'].' 0 R');
- $this->_out('>>');
- }
- function _putresources()
- {
- $this->_putextgstates();
- parent::_putresources();
- }
- }
- if(function_exists('mcrypt_encrypt'))
- {
- function RC4($key, $data)
- {
- return mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $data, MCRYPT_MODE_STREAM, '');
- }
- }
- else
- {
- function RC4($key, $data)
- {
- static $last_key, $last_state;
- if($key != $last_key)
- {
- $k = str_repeat($key, 256/strlen($key)+1);
- $state = range(0, 255);
- $j = 0;
- for ($i=0; $i<256; $i++){
- $t = $state[$i];
- $j = ($j + $t + ord($k[$i])) % 256;
- $state[$i] = $state[$j];
- $state[$j] = $t;
- }
- $last_key = $key;
- $last_state = $state;
- }
- else
- $state = $last_state;
- $len = strlen($data);
- $a = 0;
- $b = 0;
- $out = '';
- for ($i=0; $i<$len; $i++){
- $a = ($a+1) % 256;
- $t = $state[$a];
- $b = ($b+$t) % 256;
- $state[$a] = $state[$b];
- $state[$b] = $t;
- $k = $state[($state[$a]+$state[$b]) % 256];
- $out .= chr(ord($data[$i]) ^ $k);
- }
- return $out;
- }
- }
- //==== FPDF_Protection class ===
- // do this trick:
- // -- extends this class from AlphaPDF class not from FPDF class
- class FPDF_Protection extends AlphaPDF
- {
- var $encrypted = false;
- var $Uvalue;
- var $Ovalue;
- var $Pvalue;
- var $enc_obj_id;
- function SetProtection($permissions=array(), $user_pass='', $owner_pass=null)
- {
- $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32 );
- $protection = 192;
- foreach($permissions as $permission)
- {
- if (!isset($options[$permission]))
- $this->Error('Incorrect permission: '.$permission);
- $protection += $options[$permission];
- }
- if ($owner_pass === null)
- $owner_pass = uniqid(rand());
- $this->encrypted = true;
- $this->padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08".
- "\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
- $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
- }
- function _putstream($s)
- {
- if ($this->encrypted) {
- $s = RC4($this->_objectkey($this->n), $s);
- }
- parent::_putstream($s);
- }
- function _textstring($s)
- {
- if ($this->encrypted) {
- $s = RC4($this->_objectkey($this->n), $s);
- }
- return parent::_textstring($s);
- }
- function _objectkey($n)
- {
- return substr($this->_md5_16($this->encryption_key.pack('VXxx',$n)),0,10);
- }
- function _putresources()
- {
- parent::_putresources();
- if ($this->encrypted) {
- $this->_newobj();
- $this->enc_obj_id = $this->n;
- $this->_out('<<');
- $this->_putencryption();
- $this->_out('>>');
- $this->_out('endobj');
- }
- }
- function _putencryption()
- {
- $this->_out('/Filter /Standard');
- $this->_out('/V 1');
- $this->_out('/R 2');
- $this->_out('/O ('.$this->_escape($this->Ovalue).')');
- $this->_out('/U ('.$this->_escape($this->Uvalue).')');
- $this->_out('/P '.$this->Pvalue);
- }
- function _puttrailer()
- {
- parent::_puttrailer();
- if ($this->encrypted) {
- $this->_out('/Encrypt '.$this->enc_obj_id.' 0 R');
- $this->_out('/ID [()()]');
- }
- }
- function _md5_16($string)
- {
- return pack('H*',md5($string));
- }
- function _Ovalue($user_pass, $owner_pass)
- {
- $tmp = $this->_md5_16($owner_pass);
- $owner_RC4_key = substr($tmp,0,5);
- return RC4($owner_RC4_key, $user_pass);
- }
- function _Uvalue()
- {
- return RC4($this->encryption_key, $this->padding);
- }
- function _generateencryptionkey($user_pass, $owner_pass, $protection)
- {
- $user_pass = substr($user_pass.$this->padding,0,32);
- $owner_pass = substr($owner_pass.$this->padding,0,32);
- $this->Ovalue = $this->_Ovalue($user_pass,$owner_pass);
- $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
- $this->encryption_key = substr($tmp,0,5);
- $this->Uvalue = $this->_Uvalue();
- $this->Pvalue = -(($protection^255)+1);
- }
- }
- //-- create new object from FPDF_Protection class
- $pdf = new FPDF_Protection();
- $pdf->AddPage();
- $pdf->SetLineWidth(1.5);
- $pdf->SetFillColor(255,0,0);
- $pdf->Rect(10,10,40,40,'DF');
- $pdf->SetAlpha(0.5);
- $pdf->SetFillColor(0,255,0);
- $pdf->Rect(20,20,40,40,'DF');
- $pdf->Image('lena.jpg',30,30,40);
- $pdf->SetAlpha(1);
- $pdf->SetFont('Arial', '', 12);
- $pdf->Text(46,68,'Lena');
- $pdf->SetProtection(array('print'));
- $pdf->AddPage();
- $pdf->SetFont('Arial');
- $pdf->Write(10,'You can print me but not copy my text.');
- $pdf->Output();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement