Source for file jSession.class.php

Documentation is available at jSession.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage core
  5. @author     Julien Issler
  6. @contributor
  7. @copyright  2007-2008 Julien Issler
  8. @link       http://www.jelix.org
  9. @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. @since 1.0
  11. */
  12.  
  13. /**
  14.  * session management class of the jelix core
  15.  *
  16.  * @package  jelix
  17.  * @subpackage core
  18.  * @since 1.0
  19.  */
  20. class jSession {
  21.  
  22.     protected static $_params;
  23.  
  24.     /**
  25.      * start a session
  26.      */
  27.     public static function start(){
  28.  
  29.         // do not start the session if the request is made from the command line or if sessions are disabled in configuration
  30.         if($GLOBALS['gJCoord']->request instanceof jCmdLineRequest || !$GLOBALS['gJConfig']->sessions['start']){
  31.             return false;
  32.         }
  33.  
  34.         $params $GLOBALS['gJConfig']->sessions;
  35.  
  36.         //make sure that the session cookie is only for the current application
  37.         if(!$params['shared_session'])
  38.             session_set_cookie_params $GLOBALS['gJConfig']->urlengine['basePath']);
  39.  
  40.         if(isset($params['storage'])){
  41.  
  42.             /* on debian/ubuntu (maybe others), garbage collector launch probability is set to 0
  43.                and replaced by a simple cron job which is not enough for jSession (different path, db storage, ...),
  44.                so we set it to 1 as PHP's default value */
  45.             if(!ini_get('session.gc_probability'))
  46.                 ini_set('session.gc_probability','1');
  47.  
  48.             switch($params['storage']){
  49.  
  50.                 case 'dao':
  51.                     session_set_save_handler(
  52.                         array(__CLASS__,'daoOpen'),
  53.                         array(__CLASS__,'daoClose'),
  54.                         array(__CLASS__,'daoRead'),
  55.                         array(__CLASS__,'daoWrite'),
  56.                         array(__CLASS__,'daoDestroy'),
  57.                         array(__CLASS__,'daoGarbageCollector')
  58.                     );
  59.                     self::$_params $params;
  60.                     break;
  61.  
  62.                 case 'files':
  63.                     $path str_replace(array('lib:','app:')array(LIB_PATHJELIX_APP_PATH)$params['files_path']);
  64.                     session_save_path($path);
  65.                     break;
  66.  
  67.                 default:
  68.                     break;
  69.             }
  70.  
  71.         }
  72.  
  73.         if(isset($params['name'])){
  74.             if(!preg_match('#^[a-zA-Z0-9]+$#',$params['name'])){
  75.                 // regexp check because session name can only be alpha numeric according to the php documentation
  76.                 throw new jException('jelix~errors.jsession.name.invalid');
  77.             }
  78.             session_name($params['name']);
  79.         }
  80.  
  81.         session_start();
  82.         return true;
  83.     }
  84.  
  85.     /**
  86.      * end a session
  87.      */
  88.     public static function end(){
  89.         session_write_close();
  90.         return true;
  91.     }
  92.  
  93.  
  94.     protected static function _getDao(){
  95.         if(isset(self::$_params['dao_db_profile']&& self::$_params['dao_db_profile']){
  96.             $dao jDao::get(self::$_params['dao_selector']self::$_params['dao_db_profile']);
  97.         }
  98.         else{
  99.             $dao jDao::get(self::$_params['dao_selector']);
  100.         }
  101.         return $dao;
  102.     }
  103.  
  104.     /**
  105.      * dao handler for session stored in database
  106.      */
  107.     public static function daoOpen ($save_path$session_name{
  108.         return true;
  109.     }
  110.  
  111.     /**
  112.      * dao handler for session stored in database
  113.      */
  114.     public static function daoClose({
  115.         return true;
  116.     }
  117.  
  118.     /**
  119.      * dao handler for session stored in database
  120.      */
  121.     public static function daoRead ($id{
  122.         $session self::_getDao()->get($id);
  123.  
  124.         if(!$session){
  125.             return '';
  126.         }
  127.  
  128.         return $session->data;
  129.     }
  130.  
  131.     /**
  132.      * dao handler for session stored in database
  133.      */
  134.     public static function daoWrite ($id$data{
  135.         $dao self::_getDao();
  136.  
  137.         $session $dao->get($id);
  138.         if(!$session){
  139.             $session jDao::createRecord(self::$_params['dao_selector']);
  140.             $session->id $id;
  141.             $session->data $data;
  142.             $dao->insert($session);
  143.         }
  144.         else{
  145.             $session->data $data;
  146.             $dao->update($session);
  147.         }
  148.  
  149.         return true;
  150.     }
  151.  
  152.     /**
  153.      * dao handler for session stored in database
  154.      */
  155.     public static function daoDestroy ($id{
  156.         if (isset($_COOKIE[session_name()])) {
  157.            setcookie(session_name()''time()-42000'/');
  158.         }
  159.  
  160.         self::_getDao()->delete($id);
  161.         return true;
  162.     }
  163.  
  164.     /**
  165.      * dao handler for session stored in database
  166.      */
  167.     public static function daoGarbageCollector ($maxlifetime{
  168.         $date new jDateTime();
  169.         $date->now();
  170.         $date->sub(0,0,0,0,0,$maxlifetime);
  171.         self::_getDao()->deleteExpired($date->toString(jDateTime::BD_DTFORMAT));
  172.         return true;
  173.     }
  174.  
  175. }
  176. ?>

Documentation generated on Wed, 07 Sep 2011 13:48:00 +0200 by phpDocumentor 1.4.3