Source for file Basic.class.php

Documentation is available at Basic.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.  * 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 Session Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Session");
  34.  
  35. class Gumbo_Session_Basic implements Gumbo_Interface_Session {
  36.     
  37.     /** @var int $_lifetime Session lifetime (in seconds) */
  38.     private $_lifetime = 0;
  39.     /** @var string $_path Session path */
  40.     private $_path;
  41.     /** @var string $_domain Session domain */
  42.     private $_domain;
  43.     /** @var bool $_secure Session secure only */
  44.     private $_secure = false;
  45.     
  46.     
  47.     
  48.     /**
  49.      * Returns a SESSION value
  50.      * @param string $key 
  51.      * @return mix 
  52.      */
  53.     public function __get ($key{
  54.         return $this->get ($key);
  55.     }
  56.     
  57.     /**
  58.      * Sets the SESSION value
  59.      * @param string $key 
  60.      * @param mix $val 
  61.      */
  62.     public function __set ($key$val{
  63.         $this->set ($key$val);
  64.     }
  65.     
  66.     /**
  67.      * Returns if a SESSION variable exists
  68.      * @param string $key 
  69.      * @return bool 
  70.      * @throws Gumbo_Exception
  71.      */
  72.     public function __isset ($key{
  73.         try {
  74.             // verify precondition
  75.             if (!is_string ($key)) {
  76.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  77.             }
  78.             
  79.             return isset ($_SESSION [$key]);
  80.         catch (Gumbo_Exception $e{
  81.             $e->setFunction (__METHOD__);
  82.             gumbo_trigger ($e);
  83.         }
  84.         return false;
  85.     }
  86.     
  87.     /**
  88.      * Unsets a SESSION variable
  89.      * @param string $key 
  90.      * @throws Gumbo_Exception
  91.      */
  92.     public function __unset ($key{
  93.         try {
  94.             // verify precondition
  95.             if (!is_string ($key)) {
  96.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  97.             }
  98.             if (!isset ($_SESSION [$key])) {
  99.                 throw new Gumbo_Exception ("Key Undefined: {$key}");
  100.             }
  101.             
  102.             unset ($_SESSION [$key]);
  103.         catch (Gumbo_Exception $e{
  104.             $e->setFunction (__METHOD__);
  105.             gumbo_trigger ($e);
  106.         }
  107.     }
  108.     
  109.     
  110.     
  111.     /** ACTION METHODS **/
  112.     /**
  113.      * Starts the Session
  114.      * @return bool 
  115.      */
  116.     public function start ({
  117.         session_set_cookie_params ($this->getLifetime ()$this->getPath ()$this->getDomain ()$this->isSecure ());
  118.         return session_start ();
  119.     }
  120.     
  121.     /**
  122.      * Stops the Session by removing all Session data
  123.      */
  124.     public function stop ({
  125.         $_SESSION array ();
  126.         if (isset ($_COOKIE [$this->getName ()])) {
  127.             setcookie ($this->getName ()''time()-42000'/');
  128.         }
  129.         session_destroy ();
  130.     }
  131.     
  132.     /**
  133.      * Sets the Session settings
  134.      * @param int $lifetime 
  135.      * @param string $path 
  136.      * @param string $domain 
  137.      * @param bool $secure 
  138.      */
  139.     public function saveSettings ($lifetime$path=null$domain=null$secure=false{
  140.         $this->setLifetime ($lifetime);
  141.         if (!is_null ($path)) $this->setPath ($path)}
  142.         if (!is_null ($domain)) $this->setDomain ($domain)}
  143.         $this->setSecure ($secure);
  144.     }
  145.     
  146.     
  147.     
  148.     /** MUTATOR METHODS **/
  149.     /**
  150.      * Sets a value inside the Session array
  151.      * @param string $key 
  152.      * @param mix $val 
  153.      * @throws Gumbo_Exception
  154.      */
  155.     public function set ($key$val{
  156.         try {
  157.             // verify precondition
  158.             if (!is_string ($key)) {
  159.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  160.             }
  161.             
  162.             $_SESSION [$key$val;
  163.         catch (Gumbo_Exception $e{
  164.             $e->setFunction (__METHOD__);
  165.             gumbo_trigger ($e);
  166.         }
  167.     }
  168.     
  169.     /**
  170.      * Sets the Session Lifetime
  171.      * @param int $secs 
  172.      * @throws Gumbo_Exception
  173.      */
  174.     public function setLifetime ($secs{
  175.         try {
  176.             // verify precondition
  177.             if (!is_numeric ($secs)) {
  178.                 throw new Gumbo_Exception ("Invalid Argument 'secs:int' => {$secs}:gettype ($secs));
  179.             }
  180.             
  181.             $this->_lifetime = (int) $secs;
  182.         catch (Gumbo_Exception $e{
  183.             $e->setFunction (__METHOD__);
  184.             gumbo_trigger ($e);
  185.         }
  186.     }
  187.     
  188.     /**
  189.      * Sets the Session path
  190.      * @param string $path 
  191.      * @throws Gumbo_Exception
  192.      */
  193.     public function setPath ($path{
  194.         try {
  195.             // verify precondition
  196.             if (!is_string ($path)) {
  197.                 throw new Gumbo_Exception ("Invalid Argument 'path:str' => {$path}:gettype ($path));
  198.             }
  199.             
  200.             $this->_path = $path;
  201.         catch (Gumbo_Exception $e{
  202.             $e->setFunction (__METHOD__);
  203.             gumbo_trigger ($e);
  204.         }
  205.     }
  206.     
  207.     /**
  208.      * Sets the Session domain
  209.      * @param string $domain 
  210.      * @throws Gumbo_Exception
  211.      */
  212.     public function setDomain ($domain{
  213.         try {
  214.             // verify precondition
  215.             if (!is_string ($domain)) {
  216.                 throw new Gumbo_Exception ("Invalid Argument 'domain:str' => {$domain}:gettype ($domain));
  217.             }
  218.             
  219.             $this->_domain = $domain;
  220.         catch (Gumbo_Exception $e{
  221.             $e->setFunction (__METHOD__);
  222.             gumbo_trigger ($e);
  223.         }
  224.     }
  225.     
  226.     /**
  227.      * Sets if the Session should be secure
  228.      * @param bool $val 
  229.      * @throws Gumbo_Exception
  230.      */
  231.     public function setSecure ($val{
  232.         try {
  233.             // verify precondition
  234.             if (!is_bool ($val)) {
  235.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  236.             }
  237.             
  238.             $this->_secure = $val;
  239.         catch (Gumbo_Exception $e{
  240.             $e->setFunction (__METHOD__);
  241.             gumbo_trigger ($e);
  242.         }
  243.     }
  244.     
  245.     
  246.     
  247.     /** ACCESSOR METHODS **/
  248.     /**
  249.      * Returns the Session ID
  250.      * @return string 
  251.      */
  252.     public function getId ({
  253.         return session_id ();
  254.     }
  255.     
  256.     /**
  257.      * Returns the Session Name
  258.      * @return string 
  259.      */
  260.     public function getName ({
  261.         return session_name ();
  262.     }
  263.     
  264.     /**
  265.      * Returns the value of the Session variable
  266.      * @param string $key 
  267.      * @return string 
  268.      * @throws Gumbo_Exception
  269.      */
  270.     public function get ($key{
  271.         try {
  272.             // verify precondition
  273.             if (!is_string ($key)) {
  274.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  275.             }
  276.             if (!isset ($_SESSION [$key])) {
  277.                 throw new Gumbo_Exception ("Invalid Session Variable: {$key}");
  278.             }
  279.             
  280.             return $_SESSION [$key];
  281.         catch (Gumbo_Exception $e{
  282.             $e->setFunction (__METHOD__);
  283.             gumbo_trigger ($e);
  284.         }
  285.         return null;
  286.     }
  287.     
  288.     /**
  289.      * Returns the SESSION array
  290.      * @return array 
  291.      */
  292.     public function getAll ({
  293.         return $_SESSION;
  294.     }
  295.     
  296.     /**
  297.      * Returns the Session lifetime
  298.      * @return int 
  299.      */
  300.     public function getLifetime ({
  301.         return $this->_lifetime;
  302.     }
  303.     
  304.     /**
  305.      * Returns the Session path
  306.      * @return string 
  307.      */
  308.     public function getPath ({
  309.         return $this->_path;
  310.     }
  311.     
  312.     /**
  313.      * Returns the Session domain
  314.      * @return string 
  315.      */
  316.     public function getDomain ({
  317.         return $this->_domain;
  318.     }
  319.     
  320.     /**
  321.      * Returns if the Session is secure
  322.      * @return bool 
  323.      */
  324.     public function isSecure ({
  325.         return $this->_secure;
  326.     }
  327.     
  328. }
  329.  
  330. ?>