Source for file Encrypt.class.php

Documentation is available at Encrypt.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Session
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Encrypted Session Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Session
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Encrypted Session Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Session_Encrypt");
  34. gumbo_load ("Session_Basic");
  35. gumbo_load ("Encrypt");
  36. gumbo_load ("Decrypt");
  37.  
  38.     
  39.     /** @var Gumbo_Encrypt $_encrypt */
  40.     private $_encrypt;
  41.     /** @var Gumbo_Decrypt $_decrypt */
  42.     private $_decrypt;
  43.     
  44.     
  45.     
  46.     /**
  47.      * Constructor
  48.      * @param string $key Encryption key
  49.      * @param string $cipher Encryption Cipher
  50.      * @param string $mode Encryption Mode
  51.      */
  52.     public function __construct ($key=null$cipher=null$mode=null{
  53.         $this->setEncryptionSettings ($key$cipher$mode);
  54.     }
  55.     
  56.     
  57.     
  58.     /** MUTATOR METHODS **/
  59.     /**
  60.      * Sets Encryption/Decryption settings for the Encrypted Session
  61.      * @param string $key 
  62.      * @param string $cipher 
  63.      * @param string $mode 
  64.      * @uses Gumbo_Encrypt, Gumbo_Decrypt
  65.      */
  66.     public function setEncryptionSettings ($key=null$cipher=null$mode=null{
  67.         $this->_encrypt = new Gumbo_Encrypt (null$key$cipher$modetrue);
  68.         $this->_decrypt = new Gumbo_Decrypt (null$key$cipher$modetrue);
  69.     }
  70.     
  71.     /**
  72.      * Sets a value inside the Session array
  73.      * @param string $key 
  74.      * @param mixed $val (primitive values only)
  75.      * @throws Gumbo_Exception
  76.      */
  77.     public function set ($key$val{
  78.         try {
  79.             // verify precondition
  80.             if (!is_string ($key)) {
  81.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  82.             }
  83.             if (!is_null ($val&& !is_bool ($val&& !is_numeric ($val&& !is_string ($val)) {
  84.                 throw new Gumbo_Exception ("Invalid Argument 'val:num|str|bool' => {$val}:gettype ($val));
  85.             }
  86.             
  87.             $this->_encrypt->setData ($key);
  88.             $key $this->_encrypt->parse ();
  89.             
  90.             if (!is_null ($val)) {
  91.                 $this->_encrypt->setData ($val);
  92.                 $val $this->_encrypt->parse ();
  93.             }
  94.             
  95.             $_SESSION [$key$val;
  96.         catch (Gumbo_Exception $e{
  97.             $e->setFunction (__METHOD__);
  98.             gumbo_trigger ($e);
  99.         }
  100.     }
  101.     
  102.     
  103.     
  104.     /** ACCESSOR METHODS **/
  105.     /**
  106.      * Returns the value of the Session variable
  107.      * @param string $key 
  108.      * @return string 
  109.      * @throws Gumbo_Exception
  110.      */
  111.     public function get ($key{
  112.         try {
  113.             // verify precondition
  114.             if (!is_string ($key)) {
  115.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  116.             }
  117.             
  118.             $this->_encrypt->setData ($key);
  119.             $key $this->_encrypt->parse ();
  120.             if (!isset ($_SESSION [$key])) {
  121.                 throw new Gumbo_Exception ("Invalid Session Variable: {$key}");
  122.             }
  123.             
  124.             $this->_decrypt->setData ($_SESSION [$key]);
  125.             return $this->_decrypt->parse ();
  126.         catch (Gumbo_Exception $e{
  127.             $e->setFunction (__METHOD__);
  128.             gumbo_trigger ($e);
  129.         }
  130.         return null;
  131.     }
  132.     
  133. }
  134.  
  135. ?>