Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- public class Cookie {
- private $follow_law = true;
- private $success = true;
- private $disabled = false;
- //setcookie properties
- private $name;
- private $value;
- private $expiration;
- private $domain;
- private $path;
- private $prefix;
- private $security;
- private $httponly;
- /*
- private $laws = array(
- 'region' => ''
- );
- //etc ->
- private $laws = array(
- 'EU' => array(
- 'general_rule' => 'not_before_accepted' //No cookies before acception
- 'regional_exceptions' => array(
- 'UK' => 'remove_after_not_accepted', //Start cookies allowed
- 'Netherland' 'other_law'
- )
- ),
- 'USA' => '',
- 'China' => '',
- 'Russia' => ''
- );
- In order to comply with legal frameworks in different countries,
- All cookies should perhaps be disabled by default for some countries.
- $this->disable() will keep the ccokies stored but not used until
- $this->enable() is called.
- $this->destroy() will remove the cookie and remove the cookie object
- All the law values for each law-region can be loaded as an array with 'region' as key
- and 'law' as value; See above for example.
- The point of this is to make a flexible cookie where
- 1. Some cookies can follow the law, others not.
- 2. It should be as easy as possible to modify the class when a region change it laws, it should also
- 3. Be possible to modify the behaviour in the objects itself.
- */
- private function set_cookie($expire = this->expire)
- {
- setcookie($this->prefix.$this->name,
- $this->value,
- $expire,
- $this->path,
- $this->domain,
- $this->secure,
- $this->httponly
- ) ? null : $this->success=false;
- }
- /*
- * Public
- */
- public function disable()
- {
- $this->disable == true; //
- }
- public function enable()
- {
- $this->disable == false; //
- }
- public function destroy()
- {
- $this->set_cookie(time()-3600);
- $this->__destruct();
- }
- public function set_name($name)
- {
- $this->name = $name;
- }
- public function set_value($value)
- {
- $this->value = $value;
- }
- public function set_expiration($expire)
- {
- $this->expire = $expire;
- }
- public function set_domain($domain)
- {
- $this->domain = $domain;
- }
- public function set_path($path)
- {
- $this->path = $path;
- }
- public function set_prefix($prefix)
- {
- $this->prefix = $prefix;
- }
- public function set_security($security)
- {
- $this->security = $security;
- }
- public function set_httponly($httponly)
- {
- $this->httponly = $httponly;
- }
- public get_follow_law()
- {
- return $this->follow_law;
- }
- public get_success()
- {
- return $this->success;
- }
- public get_disabled()
- {
- return $this->disabled;
- }
- public function get_name()
- {
- return $this->name;
- }
- public function get_value()
- {
- return $this->value;
- }
- public function get_expiration()
- {
- return $this->expiration;
- }
- public function get_domain()
- {
- return $this->
- }
- public function get_path()
- {
- return $this->path;
- }
- public function get_prefix()
- {
- return $this->prefix;
- }
- public function get_security()
- {
- return $this->security;
- }
- public function get_httponly()
- {
- return $this->httponly;
- }
- public function __construct($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
- {
- if (is_array($name))
- {
- // always leave 'name' in last place, as the loop will break otherwise, due to $$item
- foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
- {
- if (isset($name[$item]))
- {
- $$item = $name[$item];
- }
- }
- }
- if ($prefix === '' && config_item('cookie_prefix') !== '')
- {
- $this->prefix = config_item('cookie_prefix');
- }
- if ($domain == '' && config_item('cookie_domain') != '')
- {
- $this->domain = config_item('cookie_domain');
- }
- if ($path === '/' && config_item('cookie_path') !== '/')
- {
- $this->path = config_item('cookie_path');
- }
- if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
- {
- $this->secure = config_item('cookie_secure');
- }
- if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
- {
- $this->httponly = config_item('cookie_httponly');
- }
- if ( ! is_numeric($expire))
- {
- $this->expire = time() - 86500; // a week
- }
- else
- {
- $this->expire = ($expire > 0) ? time() + $expire : 0;
- }
- $this->setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
- }
- public function __destruct()
- {
- // is called by the $this-destroy() method
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement