dbscript
[ class tree: dbscript ] [ index: dbscript ] [ all elements ]

Source for file cookie.php

Documentation is available at cookie.php

  1. <?php
  2.  
  3.   /** 
  4.    * dbscript for PHP 4 & 5 - restful crud framework
  5.    * @version 0.1.2 -- 19-Feb-2007
  6.    * @author Brian Hendrickson <brian@dbscript.net>
  7.    * @link http://dbscript.net/
  8.    * @copyright Copyright 2007 Brian Hendrickson
  9.    * @package dbscript
  10.    * @license http://www.opensource.org/licenses/mit-license.php MIT License
  11.    */
  12.  
  13.   /**
  14.    * Cookie
  15.    * 
  16.    * An md5 encrypted cookie, which times out after $expiration seconds.
  17.    * After you authenticate a user, set a cookie to securely and
  18.    * transparently propagate the user's id.
  19.    * Set $cookie->key to a unique string before set() and validate().
  20.    * 
  21.    * Usage:
  22.    * <code>
  23.    * function set_cookie() {
  24.    *   $cookie = new Cookie();
  25.    *   $cookie->userid = "foobar";
  26.    *   $cookie->set();
  27.    * }
  28.    *
  29.    * function check_cookie() {
  30.    *  $cookie = new Cookie();
  31.    *  if ($cookie->validate()) {
  32.    *   print "cookie is good";
  33.    *  } else {
  34.    *   print "cookie is not good";
  35.    *  }
  36.    * }
  37.    * </code>
  38.    * 
  39.    * More info...
  40.    * {@link http://dbscript.net/cookie}
  41.    * 
  42.    * @package dbscript
  43.    * @author Brian Hendrickson <brian@dbscript.net>
  44.    * @access public
  45.    * @return object 
  46.    * @version 0.1.2
  47.    * @todo support for clients with cookies-disabled
  48.    */
  49.  
  50. class Cookie {
  51.   var $created;
  52.   var $userid;
  53.   var $version;
  54.   var $mode = 'cfb';
  55.   var $key = 'foobar';
  56.   var $cookiename = 'auth';
  57.   var $myversion = '1';
  58.   var $expiration = '1800';
  59.   var $warning = '300';
  60.   var $glue = '|';
  61.   var $validated = false;
  62.  
  63.   function Cookie({
  64.     if (array_key_exists($this->cookiename$_COOKIE)) {
  65.       $buffer $this->_unpackage($_COOKIE[$this->cookiename]);
  66.     else {
  67.       return false;
  68.     }
  69.   }
  70.  
  71.   function get_rnd_iv($iv_len){
  72.     $iv '';
  73.     while ($iv_len-- > 0{
  74.       $iv .= chr(mt_rand(0xff);
  75.     }
  76.     return $iv;
  77.   }
  78.   function md5_encrypt($plain_text$password$iv_len 16){
  79.     $plain_text .= "\x13";
  80.     $n strlen($plain_text);
  81.     if ($n 16$plain_text .= str_repeat("\0"16 ($n 16));
  82.     $i 0;
  83.     $enc_text $this->get_rnd_iv($iv_len);
  84.     $iv substr($password $enc_text0512);
  85.     while ($i $n{
  86.       $block substr($plain_text$i16pack('H*'md5($iv));
  87.       $enc_text .= $block;
  88.       $iv substr($block $iv0512$password;
  89.       $i += 16;
  90.     }
  91.     return base64_encode($enc_text);
  92.   }
  93.   function md5_decrypt($enc_text$password$iv_len 16){
  94.     $enc_text base64_decode($enc_text);
  95.     $n strlen($enc_text);
  96.     $i $iv_len;
  97.     $plain_text '';
  98.     $iv substr($password substr($enc_text0$iv_len)0512);
  99.     while ($i $n{
  100.       $block substr($enc_text$i16);
  101.       $plain_text .= $block pack('H*'md5($iv));
  102.       $iv substr($block $iv0512$password;
  103.       $i += 16;
  104.     }
  105.     return preg_replace('/\\x13\\x00*$/'''$plain_text);
  106.   }
  107.   function validate({
  108.     if (!$this->version || !$this->created || !$this->userid{
  109.       $validated false;
  110.     }
  111.     if ($this->version != $this->myversion{
  112.       $validated false;
  113.     }
  114.     if (time($this->created > $this->expiration{
  115.       // ERROR cookie expired
  116.       $validated false;
  117.     else {
  118.       $this->_reissue();
  119.       $this->set();
  120.       $validated true;
  121.     }
  122.     return $validated;
  123.   }
  124.   function _unpackage($cookie{
  125.     $buffer $this->_decrypt($cookie);
  126.     list($this->version,$this->created,$this->userid=explode($this->glue,$buffer);
  127.     if ($this->version != $this->myversion || !$this->created ||!$this->userid{
  128.     }
  129.   }
  130.   function _package({
  131.     $parts array($this->myversion,time(),$this->userid);
  132.     $cookie implode($this->glue,$parts);
  133.     return $this->_encrypt($cookie);
  134.   }
  135.   function set({
  136.     $cookie $this->_package();
  137.     setcookie($this->cookiename,$cookie);
  138.   }
  139.   function logout({
  140.     setcookie($this->cookiename,"",0);
  141.   }
  142.   function _decrypt($crypttext{
  143.     $plaintext $this->md5_decrypt($crypttext$this->key);
  144.     return $plaintext;
  145.   }
  146.   function _encrypt($plaintext{
  147.     $crypttext $this->md5_encrypt($plaintext$this->key);
  148.     return $crypttext;
  149.   }
  150.   function _reissue({
  151.     $this->created = time();
  152.   }
  153. }
  154.  
  155. ?>

Documentation generated on Mon, 19 Feb 2007 10:24:43 -0800 by phpDocumentor 1.3.1