Source for file User.class.php

Documentation is available at User.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.  * User 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 User Session Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Session_User");
  34. gumbo_load ("Session_Encrypt");
  35. gumbo_load ("Date");
  36.  
  37.     
  38.     /** @var string $_user_key SESSION key name for User data */
  39.     private $_user_key = "user";
  40.     /** @var int|string$_user_id User ID */
  41.     private $_user_id;
  42.     /** @var boolean $_user_loaded if user was successfully loaded */
  43.     private $_user_loaded;
  44.     
  45.     
  46.     
  47.     /**
  48.      * Constructor
  49.      * @param string $user_key SESSION [key] name for User data
  50.      */
  51.     public function __construct ($user_key=null{
  52.         if (!is_null ($user_key)) $this->setUserKey ($user_key)}
  53.     }
  54.     
  55.     /** ACTION METHODS **/
  56.     /**
  57.      * Loads the User SESSION data
  58.      * The User Session Encrypted String is saved in the following format:
  59.      * session_id||user_id||agent||ip_address||date
  60.      * @postcondition setUser ()
  61.      * @postcondition _setUserLoaded ([true|false])
  62.      * @throws Gumbo_Exception
  63.      * @uses Gumbo_Date
  64.      */
  65.     public function loadUser ({
  66.         try {
  67.             // verify precondition
  68.             $key $this->getUserKey ();
  69.             if (isset ($this->$key&& !$this->getUser ()) {
  70.                 $data explode ("||"$this->get ($key));
  71.                 if (count ($data!= 5{
  72.                     throw new Gumbo_Exception ("Invalid User Session Data: {$this->get ($key)}");
  73.                 }
  74.                 
  75.                 $id null;
  76.                 $user null;
  77.                 $agent null;
  78.                 $ip null;
  79.                 $date null;
  80.                 
  81.                 foreach ($data as $key=>$val{
  82.                     switch ($key{
  83.                         case $id $valbreak// Session ID
  84.                         case $user $valbreak// User ID
  85.                         case $agent $valbreak// $_SERVER ['HTTP_USER_AGENT']
  86.                         case $ip long2ip ($val)break// IP Address $_SERVER ['REMOTE_ADDR']
  87.                         case $date new Gumbo_Date ($val)break// Logged In or Saved Session
  88.                     }
  89.                 }
  90.                 
  91.                 // check that Session ID is equal
  92.                 if ($id !== $this->getId ()) {
  93.                     throw new Gumbo_Exception ("User Session ID Invalid: {$this->getId ()} != {$id}");
  94.                 }
  95.                 
  96.                 // check that User Agents match
  97.                 if ($agent !== $_SERVER ['HTTP_USER_AGENT']{
  98.                     throw new Gumbo_Exception ("User Agent Invalid: {$_SERVER ['HTTP_USER_AGENT']} != {$agent}");
  99.                 }
  100.                 
  101.                 // check that the Session hasn't expired
  102.                 if (abs ($date->diffSeconds (new Gumbo_Date ())) >= $this->getLifetime ()) {
  103.                     throw new Gumbo_Exception ("Session has Expired: {$date->get ()}");
  104.                 }
  105.                 
  106.                 // check the IP Address, or IP Range is accurate
  107.                 if ($ip !== $_SERVER ['REMOTE_ADDR']{
  108.                     // check the IP range
  109.                     foreach (explode ("."$ipas $key=>$val{
  110.                         if ($key == 2break}
  111.                         $new_ip $val ".";
  112.                     }
  113.                     if (substr ($_SERVER ['REMOTE_ADDR']0strlen ($new_ip)) !== $new_ip{
  114.                         throw new Gumbo_Exception ("Session IP Address Invalid: {$_SERVER ['REMOTE_ADDR']} != {$ip}");
  115.                     }
  116.                 }
  117.                 
  118.                 $this->setUser ($user);
  119.             }
  120.             
  121.             $this->_saveUser ();
  122.                 
  123.         catch (Gumbo_Exception $e{
  124.             $e->setFunction (__METHOD__);
  125.             gumbo_trigger ($e);
  126.             $this->killUser ();
  127.         }
  128.     }
  129.     
  130.     /**
  131.      * Saves the User Session
  132.      * The User Session Encrypted String is saved in the following format:
  133.      * session_id||user_id||agent||ip_address||date
  134.      * @precondition getUser()
  135.      * @throws Gumbo_Exception
  136.      * @uses Gumbo_Date
  137.      */
  138.     private function _saveUser ({
  139.         try {
  140.             // verify precondition
  141.             if (!$this->getUser ()) {
  142.                 throw new Gumbo_Exception ("Session User ID Undefined");
  143.             }
  144.             
  145.             $date new Gumbo_Date ();
  146.             $this->$key $this->getId ("||" $this->getUser ("||" $_SERVER ['HTTP_USER_AGENT'"||" $_SERVER ['REMOTE_ADDR'"||" $date->get ();
  147.         catch (Gumbo_Exception $e{
  148.             $e->setFunction (__METHOD__);
  149.             gumbo_trigger ($e);
  150.         }
  151.     }
  152.     
  153.     
  154.     
  155.     /** MUTATOR METHODS **/
  156.     /**
  157.      * Sets the User Key
  158.      * @param string $key 
  159.      * @throws Gumbo_Exception
  160.      */
  161.     public function setUserKey ($key{
  162.         try {
  163.             // verify precondition
  164.             if (!is_string ($key)) {
  165.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  166.             }
  167.             
  168.             $this->_user_key = $key;
  169.         catch (Gumbo_Exception $e{
  170.             $e->setFunction (__METHOD__);
  171.             gumbo_trigger ($e);
  172.         }
  173.     }
  174.     
  175.     /**
  176.      * Sets the User ID
  177.      * @param int|string$userID 
  178.      * @throws Gumbo_Exception
  179.      */
  180.     public function setUser ($userID{
  181.         try {
  182.             // verify precondition
  183.             if (!is_string ($userID&& !is_numeric ($userID)) {
  184.                 throw new Gumbo_Exception_InvalidArgument ("userID"$userID"string|int");
  185.             }
  186.             if (is_numeric ($userID)) $userID = (int) $userID}
  187.             
  188.             $this->_user_id = $userID;
  189.         catch (Gumbo_Exception $e{
  190.             $e->setFunction (__METHOD__);
  191.             gumbo_trigger ($e);
  192.         }
  193.     }
  194.     
  195.     /**
  196.      * Sets if the User was properly loaded
  197.      * @param bool $val 
  198.      * @throws Gumbo_Exception
  199.      */
  200.     private function _setUserLoaded ($val{
  201.         try {
  202.             // verify precondition
  203.             if (!is_bool ($val)) {
  204.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  205.             }
  206.             
  207.             $this->_user_loaded = $val;
  208.         catch (Gumbo_Exception $e{
  209.             $e->setFunction (__METHOD__);
  210.             gumbo_trigger ($e);
  211.         }
  212.     }
  213.     
  214.     
  215.     
  216.     /** ACCESSOR METHODS **/
  217.     /**
  218.      * Returns the User Key
  219.      * @return string 
  220.      */
  221.     public function getUserKey ({
  222.         return $this->_user_key;
  223.     }
  224.     
  225.     /**
  226.      * Returns the User ID
  227.      * @return string|int
  228.      */
  229.     public function getUser ({
  230.         return $this->_user_id;
  231.     }
  232.     
  233.     /**
  234.      * Returns if the User has been loaded
  235.      * @return bool 
  236.      */
  237.     public function isUserLoaded ({
  238.         return $this->_user_loaded;
  239.     }
  240.     
  241. }
  242.  
  243. ?>