-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
cookie.class.php
148 lines (131 loc) · 3.21 KB
/
cookie.class.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
<?php
/* Copyright (C) 2009 Regis Houssin <regis.houssin@capnetworks.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/core/class/cookie.class.php
* \ingroup core
* \brief File of class to manage cookies
*/
/**
* \class DolCookie
* \brief Class to manage cookies
*/
class DolCookie
{
var $myKey;
var $myCookie;
var $myValue;
var $myExpire;
var $myPath;
var $myDomain;
var $mySecure;
var $cookiearray;
var $cookie;
/**
* Constructor
*
* @param string $key Personnal key
*/
function __construct($key = '')
{
$this->myKey = $key;
$this->cookiearray = array();
$this->cookie = "";
$this->myCookie = "";
$this->myValue = "";
}
/**
* Encrypt en create the cookie
*
* @return void
*/
function cryptCookie()
{
if (!empty($this->myKey))
{
$valuecrypt = base64_encode($this->myValue);
$max=dol_strlen($valuecrypt)-1;
for ($f=0 ; $f <= $max; $f++)
{
$this->cookie .= intval(ord($valuecrypt[$f]))*$this->myKey."|";
}
}
else
{
$this->cookie = $this->myValue;
}
setcookie($this->myCookie, $this->cookie, $this->myExpire, $this->myPath, $this->myDomain, $this->mySecure);
}
/**
* Decrypt the cookie
*
* @return string
*/
function decryptCookie()
{
if (!empty($this->myKey))
{
$this->cookiearray = explode("|",$_COOKIE[$this->myCookie]);
$this->myValue = "" ;
$num = (count($this->cookiearray) - 2);
for ($f = 0; $f <= $num; $f++)
{
if (!empty($this->myKey)) {
$this->myValue .= strval(chr($this->cookiearray[$f]/$this->myKey));
}
}
return(base64_decode($this->myValue));
}
else
{
return($_COOKIE[$this->myCookie]);
}
}
/**
* Set and create the cookie
*
* @param string $cookie Cookie name
* @param string $value Cookie value
* @param string $expire Expiration
* @param string $path Path of cookie
* @param string $domain Domain name
* @param int $secure 0 or 1
* @return void
*/
function _setCookie($cookie, $value, $expire=0, $path="/", $domain="", $secure=0)
{
$this->myCookie = $cookie;
$this->myValue = $value;
$this->myExpire = $expire;
$this->myPath = $path;
$this->myDomain = $domain;
$this->mySecure = $secure;
//print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire;
$this->cryptCookie();
}
/**
* Get the cookie
*
* @param string $cookie Cookie name
* @return string Decrypted value
*/
function _getCookie($cookie)
{
$this->myCookie = $cookie;
$decryptValue = $this->decryptCookie();
return $decryptValue;
}
}