dolibarr  7.0.0-beta
cookie.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2009-2015 Regis Houssin <regis.houssin@capnetworks.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
32 class DolCookie
33 {
34  private $_myKey;
35  private $_iv;
36 
37  var $myCookie;
38  var $myValue;
39  var $myExpire;
40  var $myPath;
41  var $myDomain;
42  var $mySecure;
43  var $cookie;
44 
51  function __construct($key = '')
52  {
53  $this->_myKey = hash('sha256', $key, TRUE);
54  $this->_iv = md5(md5($this->_myKey));
55  $this->cookie = "";
56  $this->myCookie = "";
57  $this->myValue = "";
58  }
59 
60 
66  private function _cryptCookie()
67  {
68  if (!empty($this->_myKey) && !empty($this->_iv))
69  {
70  $valuecrypt = base64_encode($this->myValue);
71  $this->cookie = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->_myKey, $valuecrypt, MCRYPT_MODE_CBC, $this->_iv));
72  }
73  else
74  {
75  $this->cookie = $this->myValue;
76  }
77 
78  setcookie($this->myCookie, $this->cookie, $this->myExpire, $this->myPath, $this->myDomain, $this->mySecure);
79  }
80 
86  private function _decryptCookie()
87  {
88  if (!empty($this->_myKey) && !empty($this->_iv))
89  {
90  $this->cookie = $_COOKIE[$this->myCookie];
91  $this->myValue = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->_myKey, base64_decode($this->cookie), MCRYPT_MODE_CBC, $this->_iv));
92 
93  return(base64_decode($this->myValue));
94  }
95  else
96  {
97  return($_COOKIE[$this->myCookie]);
98  }
99  }
100 
112  public function setCookie($cookie, $value, $expire=0, $path="/", $domain="", $secure=0)
113  {
114  $this->myCookie = $cookie;
115  $this->myValue = $value;
116  $this->myExpire = $expire;
117  $this->myPath = $path;
118  $this->myDomain = $domain;
119  $this->mySecure = $secure;
120 
121  //print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire;
122 
123  $this->_cryptCookie();
124  }
125 
132  public function getCookie($cookie)
133  {
134  $this->myCookie = $cookie;
135 
136  $decryptValue = $this->_decryptCookie();
137 
138  return $decryptValue;
139  }
140 
141 }
142