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 Laurent Jouanneau
  7. @copyright  2007-2009 Julien Issler, 2008 Laurent Jouanneau
  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.         $params $GLOBALS['gJConfig']->sessions;
  30.  
  31.         // do not start the session if the request is made from the command line or if sessions are disabled in configuration
  32.         if ($GLOBALS['gJCoord']->request instanceof jCmdLineRequest || !$params['start']{
  33.             return false;
  34.         }
  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 ($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.                 case 'dao':
  50.                     session_set_save_handler(
  51.                         array(__CLASS__,'daoOpen'),
  52.                         array(__CLASS__,'daoClose'),
  53.                         array(__CLASS__,'daoRead'),
  54.                         array(__CLASS__,'daoWrite'),
  55.                         array(__CLASS__,'daoDestroy'),
  56.                         array(__CLASS__,'daoGarbageCollector')
  57.                     );
  58.                     self::$_params $params;
  59.                     break;
  60.  
  61.                 case 'files':
  62.                     session_save_path($params['files_path']);
  63.                     break;
  64.             }
  65.         }
  66.  
  67.         if($params['name'!=''){
  68.             if(!preg_match('#^[a-zA-Z0-9]+$#',$params['name'])){
  69.                 // regexp check because session name can only be alpha numeric according to the php documentation
  70.                 throw new jException('jelix~errors.jsession.name.invalid');
  71.             }
  72.             session_name($params['name']);
  73.         }
  74.  
  75.         if(isset($params['_class_to_load'])) {
  76.             foreach($params['_class_to_load'as $file{
  77.                 require_once($file);
  78.             }
  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.             $now date('Y-m-d H:i:s');
  143.             $session->creation $now;
  144.             $session->access $now;
  145.             $dao->insert($session);
  146.         }
  147.         else{
  148.             $session->data $data;
  149.             $session->access date('Y-m-d H:i:s');
  150.             $dao->update($session);
  151.         }
  152.  
  153.         return true;
  154.     }
  155.  
  156.     /**
  157.      * dao handler for session stored in database
  158.      */
  159.     public static function daoDestroy ($id{
  160.         if (isset($_COOKIE[session_name()])) {
  161.            setcookie(session_name()''time()-42000'/');
  162.         }
  163.  
  164.         self::_getDao()->delete($id);
  165.         return true;
  166.     }
  167.  
  168.     /**
  169.      * dao handler for session stored in database
  170.      */
  171.     public static function daoGarbageCollector ($maxlifetime{
  172.         $date new jDateTime();
  173.         $date->now();
  174.         $date->sub(0,0,0,0,0,$maxlifetime);
  175.         self::_getDao()->deleteExpired($date->toString(jDateTime::DB_DTFORMAT));
  176.         return true;
  177.     }
  178.  
  179. }

Documentation generated on Wed, 24 Sep 2014 22:02:07 +0200 by phpDocumentor 1.4.3