Advertisement
Guest User

Untitled

a guest
Jan 7th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. public class Cookie {
  4.    
  5.     private $follow_law = true;
  6.     private $success = true;
  7.     private $disabled = false;
  8.    
  9.    
  10.     //setcookie properties
  11.     private $name;
  12.     private $value;
  13.     private $expiration;
  14.     private $domain;
  15.     private $path;
  16.     private $prefix;
  17.     private $security;
  18.     private $httponly;
  19.    
  20.  
  21.     /*
  22.     private $laws = array(
  23.         'region' => ''
  24.     );
  25.    
  26.     //etc ->
  27.     private $laws = array(
  28.         'EU' => array(
  29.             'general_rule' => 'not_before_accepted' //No cookies before acception
  30.             'regional_exceptions' => array(
  31.                 'UK' => 'remove_after_not_accepted', //Start cookies allowed
  32.                 'Netherland' 'other_law'
  33.             )
  34.         ),
  35.         'USA' => '',
  36.         'China' => '',
  37.         'Russia' => ''
  38.     );
  39.    
  40.    
  41.     In order to comply with legal frameworks in different countries,
  42.     All cookies should perhaps be disabled by default for some countries.
  43.    
  44.     $this->disable() will keep the ccokies stored but not used until
  45.     $this->enable() is called.
  46.    
  47.     $this->destroy() will remove the cookie and remove the cookie object
  48.    
  49.     All the law values for each law-region can be loaded as an array with 'region' as key
  50.     and 'law' as value; See above for example.
  51.    
  52.     The point of this is to make a flexible cookie where
  53.     1. Some cookies can follow the law, others not.
  54.     2. It should be as easy as possible to modify the class when a region change it laws, it should also
  55.     3. Be possible to modify the behaviour in the objects itself.
  56.     */
  57.  
  58.  
  59.     private function set_cookie($expire = this->expire)
  60.     {
  61.         setcookie($this->prefix.$this->name,
  62.             $this->value,
  63.             $expire,
  64.             $this->path,
  65.             $this->domain,
  66.             $this->secure,
  67.             $this->httponly
  68.         ) ? null : $this->success=false;
  69.     }
  70.  
  71.  
  72.     /*
  73.     *   Public
  74.     */
  75.     public function disable()
  76.     {
  77.         $this->disable == true; //
  78.     }
  79.    
  80.    
  81.     public function enable()
  82.     {
  83.         $this->disable == false; //
  84.     }
  85.    
  86.    
  87.     public function destroy()
  88.     {
  89.         $this->set_cookie(time()-3600);
  90.         $this->__destruct();
  91.     }
  92.    
  93.    
  94.     public function set_name($name)
  95.     {
  96.         $this->name = $name;
  97.     }
  98.    
  99.    
  100.     public function set_value($value)
  101.     {
  102.         $this->value = $value;
  103.     }
  104.    
  105.    
  106.     public function set_expiration($expire)
  107.     {
  108.         $this->expire = $expire;
  109.     }
  110.    
  111.    
  112.     public function set_domain($domain)
  113.     {
  114.         $this->domain = $domain;
  115.     }
  116.    
  117.    
  118.     public function set_path($path)
  119.     {
  120.         $this->path = $path;
  121.     }
  122.    
  123.    
  124.     public function set_prefix($prefix)
  125.     {
  126.         $this->prefix = $prefix;
  127.     }
  128.    
  129.    
  130.     public function set_security($security)
  131.     {
  132.         $this->security = $security;
  133.     }
  134.    
  135.    
  136.     public function set_httponly($httponly)
  137.     {
  138.         $this->httponly = $httponly;
  139.     }
  140.    
  141.    
  142.     public get_follow_law()
  143.     {
  144.         return $this->follow_law;
  145.     }
  146.  
  147.  
  148.     public get_success()
  149.     {
  150.         return $this->success;
  151.     }
  152.  
  153.  
  154.     public get_disabled()
  155.     {
  156.         return $this->disabled;
  157.     }
  158.  
  159.  
  160.     public function get_name()
  161.     {
  162.         return $this->name;
  163.     }
  164.  
  165.  
  166.     public function get_value()
  167.     {
  168.         return $this->value;
  169.     }
  170.  
  171.  
  172.     public function get_expiration()
  173.     {
  174.         return $this->expiration;
  175.     }
  176.  
  177.  
  178.     public function get_domain()
  179.     {
  180.         return $this->
  181.     }
  182.  
  183.  
  184.     public function get_path()
  185.     {
  186.         return $this->path;
  187.     }
  188.  
  189.  
  190.     public function get_prefix()
  191.     {
  192.         return $this->prefix;
  193.     }
  194.  
  195.  
  196.     public function get_security()
  197.     {
  198.         return $this->security;
  199.     }
  200.  
  201.  
  202.     public function get_httponly()
  203.     {
  204.         return $this->httponly;
  205.     }
  206.  
  207.  
  208.     public function __construct($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
  209.     {
  210.         if (is_array($name))
  211.         {
  212.             // always leave 'name' in last place, as the loop will break otherwise, due to $$item
  213.             foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
  214.             {
  215.                 if (isset($name[$item]))
  216.                 {
  217.                     $$item = $name[$item];
  218.                 }
  219.             }
  220.         }
  221.  
  222.         if ($prefix === '' && config_item('cookie_prefix') !== '')
  223.         {
  224.             $this->prefix = config_item('cookie_prefix');
  225.         }
  226.  
  227.         if ($domain == '' && config_item('cookie_domain') != '')
  228.         {
  229.             $this->domain = config_item('cookie_domain');
  230.         }
  231.  
  232.         if ($path === '/' && config_item('cookie_path') !== '/')
  233.         {
  234.             $this->path = config_item('cookie_path');
  235.         }
  236.  
  237.         if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
  238.         {
  239.             $this->secure = config_item('cookie_secure');
  240.         }
  241.  
  242.         if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
  243.         {
  244.             $this->httponly = config_item('cookie_httponly');
  245.         }
  246.  
  247.         if ( ! is_numeric($expire))
  248.         {
  249.             $this->expire = time() - 86500; // a week
  250.         }
  251.         else
  252.         {
  253.             $this->expire = ($expire > 0) ? time() + $expire : 0;
  254.         }
  255.  
  256.         $this->setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
  257.     }
  258.  
  259.  
  260.     public function __destruct()
  261.     {
  262.         // is called by the $this-destroy() method
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement