-
Notifications
You must be signed in to change notification settings - Fork 2
/
securecookie.php
executable file
·176 lines (173 loc) · 7.61 KB
/
securecookie.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
<?
/**
* PHP Class: SecureCookie
* @author Aikar <[email protected]>
* @desc Provides a Secure way of using cookies so the end user is not able to edit the cookies or even see the data it contains.
* @notes You will not want to use large ammounts of stored data on 1 object, as each object is stored in only 1 cookie on the users system
* If you use too many, the encrypted data will become too large to store in the cookie and will be corrupted or may not set at all.
* Use multiple objects if you need alot of data stored.
*
* @example:
* $C = new SecureCookie('mysecretword','SomeCookieID',time()+3600,'/','.mydomain.com');
* $C->Set('test','hello');
* echo 'value of test is: ' . $C->Get('test') . '<br />';
*
**/
class SecureCookie {
var $_CookieObject;
var $_CookieID;
var $_Expire;
var $_EncryptionPassword;
var $_Path;
var $_Domain;
var $_Secure;
/***
* Create Object.
* EncryptionPassword: (required) The password to encrypt the cookie.
* - NOTE: Changing this password after a cookie has been set will make the cookie fail to be read.
* CookieID: (required) A unique name for the cookie. This is the ACTUAL cookie name. Do not use the name of a cookie
* already in use on your website.
* expire, domain, path, secure: Standard Cookie Paramaters.
* - NOTE: This applies to all values in the object!
* You will need multiple objects for different parameters.
***/
function SecureCookie($EncryptionPassword,$CookieID,$expire=false,$path=false,$domain=false,$secure=false) {
// Store all our passed parameters.
$this->_Expire=$expire;
$this->_EncryptionPassword=$EncryptionPassword;
$this->_CookieID=$CookieID;
$this->_Path=$path;
$this->_Domain=$domain;
$this->_Secure=$secure;
// Does this cookie ID exists?
if(isset($_COOKIE[$CookieID])) {
// Decrypt it.
$obj=unserialize($this->_Decrypt($_COOKIE[$this->_CookieID],$this->_EncryptionPassword));
// The best way to see if a successful decryption, check a stored value to see if the passwords match.
// A failed decryption would corrupt it and return bad data.
if($obj['____ENCRYPTIONPASSWORD'] == md5($this->_EncryptionPassword)) {
// Its good! Lets use it.
$this->_CookieObject=$obj;
}else{
// Failed! Developer may of changed the encryption password.
// Open up with a blank object and set our verification field.
$this->_CookieObject=array('____ENCRYPTIONPASSWORD' => md5($this->_EncryptionPassword));
}
}else{
// Cookie doesn't exists, Open up with a blank object and set our verification field.
$this->_CookieObject=array('____ENCRYPTIONPASSWORD' => md5($this->_EncryptionPassword));
}
// Cleanup obj.
unset($obj);
}
// Alias: SetCookie()
function Set($name,$value) {
$this->SetCookie($name,$value);
}
// Alias: GetCookie()
function Get($name,$default = null) {
return $this->GetCookie($name,$default);
}
// Alias: DeleteCookie()
function Del($name) {
$this->DeleteCookie($name);
}
/**
* Sets the value of the cookie.
**/
function SetCookie($name,$value) {
// Check to make sure not using invalid name.
if($name != '____ENCRYPTIONPASSWORD') {
// Make a copy of our object
$obj=$this->_CookieObject;
// Be sure the encryption password is in the object for password verifcation.
$obj['____ENCRYPTIONPASSWORD'] = md5($this->_EncryptionPassword);
// Set our new value
$obj[$name]=$value;
// Restore the new data to the object
$this->_CookieObject=$obj;
// Lets reuse $obj to store our encrypted object
$obj=$this->_Encrypt(serialize($obj),$this->_EncryptionPassword);
// Set the actual cookie with our encrypted data.
setcookie($this->_CookieID,$obj,$this->_Expire,$this->_Path,$this->_Domain,$this->_Secure);
// Set the cookie global so the data is usable on this page load.
$_COOKIE[$this->_CookieID] = $obj;
// Cleanup obj.
unset($obj);
}else{
// See if your trying to intentionally break my script smile.gif Why else would you name it this!
die('INVALID COOKIE NAME. YOU MAY NOT USE "____ENCRYPTIONPASSWORD" AS YOUR COOKIE NAME');
}
}
/**
* Retrieves the specified name from the object.
**/
function GetCookie($name,$default=null) {
// Check to make sure not using invalid name.
if($name != '____ENCRYPTIONPASSWORD') {
// Make a copy of object
$obj=$this->_CookieObject;
// Return the value.
return isset($obj[$name]) ? $obj[$name] : $default;
}else{
// See if your trying to intentionally break my script smile.gif Why else would you name it this!
die('INVALID COOKIE NAME. YOU MAY NOT USE "____ENCRYPTIONPASSWORD" AS YOUR COOKIE NAME');
}
}
/**
* Deletes the specified name from the object.
**/
function DeleteCookie($name) {
// Check to make sure not using invalid name.
if($name != '____ENCRYPTIONPASSWORD') {
// Make a copy of object.
$obj=$this->_CookieObject;
// Unset the value to delete it.
unset($obj[$name]);
// Restore our new data to the object.
$this->_CookieObject=$obj;
// Lets reuse $obj to store our encrypted object
$obj=$this->_Encrypt(serialize($obj),$this->_EncryptionPassword);
// Set the actual cookie with our encrypted data.
setcookie($this->_CookieID,$obj,$this->_Expire,$this->_Path,$this->_Domain,$this->_Secure);
// Set the cookie global so the data is usable on this page load.
$_COOKIE[$this->_CookieID] = $obj;
// Cleanup obj.
unset($obj);
}else{
// See if your trying to intentionally break my script smile.gif Why else would you name it this!
die('INVALID COOKIE NAME. YOU MAY NOT USE "____ENCRYPTIONPASSWORD" AS YOUR COOKIE NAME');
}
}
// Returns the Cookie Array
function GetObject(){
// Make a copy of the object
$obj=$this->_CookieObject;
// Get Rid of our encryption password value.
unset($obj['____ENCRYPTIONPASSWORD']);
// Return the array of values.
return $obj;
}
// Standard Encryption Functions.
function _Encrypt($string,$key) {
$result = '';
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return base64_encode(gzdeflate($result,9));
}
function _Decrypt($string,$key) {
$result = '';
$string = gzinflate(base64_decode($string));
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
}