dolibarr/htdocs/core/class/cookie.class.php

147 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2012-12-30 15:13:49 +01:00
/* 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
2011-08-01 01:45:11 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
2010-06-08 01:52:43 +02:00
* \file htdocs/core/class/cookie.class.php
2010-04-21 20:40:18 +02:00
* \ingroup core
* \brief File of class to manage cookies
*/
2010-07-21 13:57:52 +02:00
/**
* \class DolCookie
* \brief Class to manage cookies
*/
2010-04-21 20:40:18 +02:00
class DolCookie
{
var $myKey;
var $myCookie;
var $myValue;
var $myExpire;
var $myPath;
var $myDomain;
var $mySsecure;
var $cookiearray;
var $cookie;
/**
2011-09-11 20:35:38 +02:00
* Constructor
*
2011-09-12 19:08:02 +02:00
* @param string $key Personnal key
2010-04-21 20:40:18 +02:00
*/
function __construct($key = '')
2010-04-21 20:40:18 +02:00
{
$this->myKey = $key;
$this->cookiearray = array();
$this->cookie = "";
$this->myCookie = "";
$this->myValue = "";
}
/**
2011-09-21 15:05:32 +02:00
* Encrypt en create the cookie
*
* @return void
2010-04-21 20:40:18 +02:00
*/
function cryptCookie()
{
if (!empty($this->myKey))
{
$valuecrypt = base64_encode($this->myValue);
2011-09-21 15:05:32 +02:00
$max=dol_strlen($valuecrypt)-1;
for ($f=0 ; $f <= $max; $f++)
2010-04-21 20:40:18 +02:00
{
$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);
}
/**
2011-09-21 15:05:32 +02:00
* Decrypt the cookie
*
* @return void
2010-04-21 20:40:18 +02:00
*/
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++)
2010-04-21 20:40:18 +02:00
{
$this->myValue .= strval(chr($this->cookiearray[$f]/$this->myKey));
}
return(base64_decode($this->myValue));
2010-04-21 20:40:18 +02:00
}
else
{
return($_COOKIE[$this->myCookie]);
}
}
/**
2011-09-21 15:05:32 +02:00
* Set and create the cookie
2011-09-12 19:08:02 +02:00
*
2011-09-21 15:05:32 +02:00
* @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
2011-09-21 15:05:32 +02:00
* @param int $secure 0 or 1
* @return void
2010-04-21 20:40:18 +02:00
*/
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;
2010-09-26 11:53:42 +02:00
$this->mySecure = $secure;
2010-04-21 20:40:18 +02:00
//print 'key='.$this->myKey.' name='.$this->myCookie.' value='.$this->myValue.' expire='.$this->myExpire;
$this->cryptCookie();
}
/**
2011-09-12 19:08:02 +02:00
* Get the cookie
*
* @param string $cookie Cookie name
* @return string Decrypted value
2010-04-21 20:40:18 +02:00
*/
function _getCookie($cookie)
{
$this->myCookie = $cookie;
$decryptValue = $this->decryptCookie();
return $decryptValue;
}
}
?>