-
Notifications
You must be signed in to change notification settings - Fork 43
/
protection.php
executable file
·195 lines (178 loc) · 5.67 KB
/
protection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
require('invoice.php');
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;
}
}
class FPDF_Protection extends PDF_Invoice
{
var $encrypted = false; //whether document is protected
var $Uvalue; //U entry in pdf document
var $Ovalue; //O entry in pdf document
var $Pvalue; //P entry in pdf document
var $enc_obj_id; //encryption object id
/**
* Function to set permissions as well as user and owner passwords
*
* - permissions is an array with values taken from the following list:
* copy, print, modify, annot-forms
* If a value is present it means that the permission is granted
* - If a user password is set, user will be prompted before document is opened
* - If an owner password is set, document can be opened in privilege mode with no
* restriction if that password is entered
*/
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);
}
/****************************************************************************
* *
* Private methods *
* *
****************************************************************************/
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);
}
/**
* Compute key depending on object number where the encrypted data is stored
*/
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 [()()]');
}
}
/**
* Get MD5 as binary string
*/
function _md5_16($string)
{
return pack('H*',md5($string));
}
/**
* Compute O value
*/
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);
}
/**
* Compute U value
*/
function _Uvalue()
{
return RC4($this->encryption_key, $this->padding);
}
/**
* Compute encryption key
*/
function _generateencryptionkey($user_pass, $owner_pass, $protection)
{
// Pad passwords
$user_pass = substr($user_pass.$this->padding,0,32);
$owner_pass = substr($owner_pass.$this->padding,0,32);
// Compute O value
$this->Ovalue = $this->_Ovalue($user_pass,$owner_pass);
// Compute encyption key
$tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
$this->encryption_key = substr($tmp,0,5);
// Compute U value
$this->Uvalue = $this->_Uvalue();
// Compute P value
$this->Pvalue = -(($protection^255)+1);
}
}
?>