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-2012 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 = & jApp::config()->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 (jApp::coord()->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 ( 0 , jApp::config()->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.     public static function isStarted() {
  94.         if (php_sapi_name() !== 'cli') {
  95.             if (version_compare(phpversion(), '5.4.0', '>=') ) {
  96.                 return (session_status() === PHP_SESSION_ACTIVE);
  97.             } else {
  98.                 return session_id() === '' ? false : true;
  99.             }
  100.         }
  101.         return false;
  102.     }
  103.  
  104.     protected static function _getDao(){
  105.         if(isset(self::$_params['dao_db_profile']) && self::$_params['dao_db_profile']){
  106.             $dao = jDao::get(self::$_params['dao_selector'], self::$_params['dao_db_profile']);
  107.         }
  108.         else{
  109.             $dao = jDao::get(self::$_params['dao_selector']);
  110.         }
  111.         return $dao;
  112.     }
  113.  
  114.     /**
  115.      * dao handler for session stored in database
  116.      */
  117.     public static function daoOpen ($save_path, $session_name) {
  118.         return true;
  119.     }
  120.  
  121.     /**
  122.      * dao handler for session stored in database
  123.      */
  124.     public static function daoClose() {
  125.         return true;
  126.     }
  127.  
  128.     /**
  129.      * dao handler for session stored in database
  130.      */
  131.     public static function daoRead ($id) {
  132.         $session = self::_getDao()->get($id);
  133.  
  134.         if(!$session){
  135.             return '';
  136.         }
  137.  
  138.         return $session->data;
  139.     }
  140.  
  141.     /**
  142.      * dao handler for session stored in database
  143.      */
  144.     public static function daoWrite ($id, $data) {
  145.         $dao = self::_getDao();
  146.  
  147.         $session = $dao->get($id);
  148.         if(!$session){
  149.             $session = jDao::createRecord(self::$_params['dao_selector']);
  150.             $session->id = $id;
  151.             $session->data = $data;
  152.             $now = date('Y-m-d H:i:s');
  153.             $session->creation = $now;
  154.             $session->access = $now;
  155.             $dao->insert($session);
  156.         }
  157.         else{
  158.             $session->data = $data;
  159.             $session->access = date('Y-m-d H:i:s');
  160.             $dao->update($session);
  161.         }
  162.  
  163.         return true;
  164.     }
  165.  
  166.     /**
  167.      * dao handler for session stored in database
  168.      */
  169.     public static function daoDestroy ($id) {
  170.         if (isset($_COOKIE[session_name()])) {
  171.            setcookie(session_name(), '', time()-42000, '/');
  172.         }
  173.  
  174.         self::_getDao()->delete($id);
  175.         return true;
  176.     }
  177.  
  178.     /**
  179.      * dao handler for session stored in database
  180.      */
  181.     public static function daoGarbageCollector ($maxlifetime) {
  182.         $date = new jDateTime();
  183.         $date->now();
  184.         $date->sub(0,0,0,0,0,$maxlifetime);
  185.         self::_getDao()->deleteExpired($date->toString(jDateTime::DB_DTFORMAT));
  186.         return true;
  187.     }
  188.  
  189. }

Documentation generated on Wed, 04 Jan 2017 22:56:48 +0100 by phpDocumentor 1.4.3