Advertisement
stijn1989

Zend Auth Storage

Jan 9th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.56 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Slaat de data(1) encrypted op in een cookie en een sessie.
  4.  *
  5.  *      (1) customer_username_password
  6.  *
  7.  * Bij het ophalen van de data wordt alles decrypted terug gegeven.
  8.  *
  9.  * @category    AC
  10.  * @package     AC_Auth
  11.  * @author      Stijn Leenknegt
  12.  */
  13. class AC_Auth_Storage implements Zend_Auth_Storage_Interface
  14. {
  15.    
  16.    
  17.     /**
  18.      * Standaard session namespace.
  19.      */
  20.     const SESSION_NACESPACE = 'AC_Auth';
  21.    
  22.     /**
  23.      * Standaard session naam.
  24.      */
  25.     const SESSION_MEMBER    = 'storage';
  26.    
  27.     /**
  28.      * Standaard cookie naam.
  29.      */
  30.     const COOKIE_NACE       = 'AC_Auth';
  31.    
  32.     /**
  33.      * Standaard duurtijd van de cookie van 1 jaar.
  34.      */
  35.     const COOKIE_LIFETIME   = 31536000;
  36.    
  37.    
  38.     /**
  39.      * Session namespace.
  40.      *
  41.      * @var string
  42.      */
  43.     private $_sessionNamespace;
  44.    
  45.     /**
  46.      * Session member naam.
  47.      *
  48.      * @var string
  49.      */
  50.     private $_sessionMember;
  51.    
  52.     /**
  53.      * Cookie naam.
  54.      *
  55.      * @var string
  56.      */
  57.     private $_cookieName;
  58.    
  59.     /**
  60.      * Cookie duurtijd.
  61.      *
  62.      * @var integer
  63.      */
  64.     private $_cookieLifetime;
  65.    
  66.     /**
  67.      * Session object.
  68.      *
  69.      * @var Zend_Session_Namespace
  70.      */
  71.     private $_session;
  72.    
  73.    
  74.     /**
  75.      * Initialiseert het standaard Agro Manager storage object.
  76.      *
  77.      * @param string $sessionNamespace
  78.      * @param string $sessionMember
  79.      * @param string $cookieName
  80.      * @param integer $cookieLifetime
  81.      */
  82.     public function __construct($sessionNamespace = self::SESSION_NACESPACE, $sessionMember = self::SESSION_MEMBER, $cookieName = self::COOKIE_NACE, $cookieLifetime = self::COOKIE_LIFETIME)
  83.     {
  84.         $this->_sessionNamespace = $sessionNamespace;
  85.         $this->_sessionMember = $sessionMember;
  86.         $this->_cookieName = $cookieName;
  87.         $this->_cookieLifetime = $cookieLifetime;
  88.         $this->_session = new Zend_Session_Namespace($this->_sessionNamespace);
  89.     }
  90.    
  91.    
  92.     /**
  93.      * Geeft de waarde van de cookie terug.
  94.      * Deze waarde is niet decrypted.
  95.      *
  96.      * @return string
  97.      */
  98.     private function getCookie()
  99.     {
  100.         if (isset($_COOKIE[$this->_cookieName])) {
  101.             return $_COOKIE[$this->_cookieName];
  102.         }
  103.        
  104.         return "";
  105.     }
  106.    
  107.    
  108.     /**
  109.      * Stelt de cookie in. De data parameter moet encrypted zijn.
  110.      *
  111.      * @param string $data
  112.      */
  113.     private function setCookie($data)
  114.     {
  115.         setcookie($this->_cookieName, $data, time() + $this->_cookieLifetime);
  116.     }
  117.    
  118.    
  119.     /**
  120.      * Deletes the authentication cookie.
  121.      */
  122.     private function deleteCookie()
  123.     {
  124.         setcookie($this->_cookieName, '', time() - $this->_cookieLifetime);
  125.     }
  126.    
  127.    
  128.     /**
  129.      * Defined by Zend_Auth_Storage_Interface
  130.      *
  131.      * @return boolean
  132.      */
  133.     public function isEmpty()
  134.     {
  135.         if(isset($this->_session->{$this->_sessionMember})) {
  136.             return false;
  137.         } else {
  138.             $cookie = $this->getCookie();
  139.             if (!empty($cookie)) {
  140.                 return false;
  141.             }
  142.         }
  143.        
  144.         return true;
  145.     }
  146.    
  147.    
  148.     /**
  149.      * Defined by Zend_Auth_Storage_Interface
  150.      *
  151.      * @return mixed
  152.      */
  153.     public function read()
  154.     {
  155.         $contents = $this->_session->{$this->_sessionMember};
  156.        
  157.         if(empty($contents)) {
  158.             $contents = $this->getCookie();
  159.         }
  160.        
  161.         if(!empty($contents)) {
  162.             $contents = AC_Encrypt::decrypt($contents);
  163.         }
  164.        
  165.         return $contents;
  166.     }
  167.    
  168.    
  169.     /**
  170.      * Defined by Zend_Auth_Storage_Interface
  171.      *
  172.      * @param  mixed $contents
  173.      * @return void
  174.      */
  175.     public function write($contents)
  176.     {
  177.         $contents = AC_Encrypt::encrypt($contents);
  178.         $this->_session->{$this->_sessionMember} = $contents;
  179.         $this->setCookie($contents);
  180.     }
  181.    
  182.    
  183.     /**
  184.      * Defined by Zend_Auth_Storage_Interface
  185.      *
  186.      * @return void
  187.      */
  188.     public function clear()
  189.     {
  190.         unset($this->_session->{$this->_sessionMember});
  191.         $this->deleteCookie();
  192.     }
  193.    
  194.    
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement