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

Source for file Session.php

Documentation is available at Session.php

  1. <?php
  2. /**
  3.  * User_Session
  4.  * 
  5.  * Copyright (C) 2009  Mai (Me Artificial Intelligence)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  * 
  20.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  21.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  22.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  23.  * @link http://www.launchpad.net/mai
  24.  * 
  25.  * @package Models
  26.  * @subpackage User
  27.  * 
  28.  */
  29.  
  30. /**
  31.  * User_Session
  32.  * 
  33.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  34.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  35.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  36.  * @link http://www.launchpad.net/mai
  37.  * 
  38.  * @package Models
  39.  * @subpackage User
  40.  *  
  41.  */
  42.  
  43. {
  44.     /**
  45.      * Holds the session namespace.
  46.      * @var Zend_Session_Namespace 
  47.      */
  48.     static private $_session;
  49.     
  50.     /**
  51.      * Private constructor to prevent construction.
  52.      * @return void 
  53.      */
  54.     private function __construct(}
  55.     
  56.     /**
  57.      * Sets a session using Zend_Session_Namespace.
  58.      * @param $id 
  59.      * @uses Zend_Session_Namespace
  60.      * @return void 
  61.      */
  62.     static public function set($id)
  63.     {
  64.         if(!isset($id|| !is_numeric($id))
  65.         {
  66.             throw UnexpectedValueException('$id is not set or not a integer.');
  67.         }
  68.         
  69.         self::$_session new Zend_Session_Namespace('mai-session');
  70.         
  71.         self::$_session->setExpirationSeconds(3600);
  72.         self::$_session->ip Base::getRealIp();
  73.         self::$_session->user_id $id;
  74.     }
  75.     
  76.     /**
  77.      * Validates the session.
  78.      * @return Boolean 
  79.      */
  80.     static public function isValid()
  81.     {
  82.         if(!self::$_session || !(self::$_session instanceof Zend_Session_Namespace))
  83.         {
  84.             self::$_session new Zend_Session_Namespace('mai-session');
  85.         }
  86.         
  87.         if(    isset(self::$_session->ip)
  88.             && self::$_session->ip == Base::getRealIp()
  89.             && isset(self::$_session->user_id)
  90.             && is_numeric(self::$_session->user_id))
  91.         {
  92.             return true;
  93.         }
  94.             
  95.         return false;
  96.     }
  97.  
  98.     /**
  99.      * Return the id of the user is a session excists.
  100.      * @return Int 
  101.      */
  102.     static public function returnUserId()
  103.     {
  104.         if(self::isValid())
  105.         {
  106.             return self::$_session->user_id;
  107.         }
  108.         else
  109.         {
  110.             trigger_error('Session does not excist.'E_USER_NOTICE);
  111.             return 0;
  112.         }
  113.     }
  114.     
  115.     /**
  116.      * Remove all session data.
  117.      * @return void 
  118.      */
  119.     static public function remove()
  120.     {
  121.         if(!self::$_session || !(self::$_session instanceof Zend_Session_Namespace))
  122.         {
  123.             self::$_session new Zend_Session_Namespace('mai-session');
  124.         }
  125.         
  126.         self::$_session->unsetAll();
  127.     }
  128. }

Documentation generated on Mon, 27 Jul 2009 19:51:57 +0200 by phpDocumentor 1.4.1