Ticket #490: 490-jSession-start-control.diff

File 490-jSession-start-control.diff, 12.3 kB (added by Julien, 10 months ago)
  • lib/jelix-scripts/templates/var/config/defaultconfig.ini.php.tpl

    old new  
    137137 
    138138 
    139139[sessions] 
     140; You can specify the way that sessions start. 
     141; - "always" means that you have nothing to do to use sessions. This is required to use PHP's $_SESSION var directly 
     142; - "ondemand" means that the session in only started when you use jSession::set(), jSession::get(), jSession::isDefined(), jSession::delete() or jSession::destroy() 
     143; - "never" means that sessions can not be started. 
     144start = always 
     145; 
    140146; You can change the session name by setting the following parameter (only accepts alpha-numeric chars) : 
    141147; name = "mySessionName" 
    142148; Use alternative storage engines for sessions 
  • lib/jelix/core/jSession.class.php

    old new  
    2020class jSession { 
    2121 
    2222    protected static $_params; 
     23    protected static $_started=false; 
    2324 
    2425    /** 
    2526     * start a session 
    2627     */ 
    2728    public static function start(){ 
     29        if($GLOBALS['gJConfig']->sessions['start'] === 'always') 
     30            self::_realStart(); 
     31    } 
     32 
     33 
     34    /** 
     35     * end a session 
     36     */ 
     37    public static function end(){ 
     38        if(self::$_started) 
     39            session_write_close(); 
     40    } 
     41 
     42 
     43    /** 
     44     * store some value in session 
     45     * @param string $name The name of the session var 
     46     * @param mixed $value The value of the session var 
     47     */ 
     48    public static function set($name, $value){ 
     49        if(!self::$_started) 
     50            self::_realStart(); 
     51 
     52        $name = trim($name); 
     53        if($name === '') 
     54            throw new jException('jelix~errors.jsession.invalid.varname', array($name)); 
     55 
     56        $_SESSION[$name] = $value; 
     57    } 
     58 
     59 
     60    /** 
     61     * read some value from session 
     62     * @param string $name The name of the session var 
     63     * @return mixed The value of the session var 
     64     */ 
     65    public static function get($name){ 
     66        if(!self::$_started) 
     67            self::_realStart(); 
     68 
     69        $name = trim($name); 
     70        if($name === '') 
     71            throw new jException('jelix~errors.jsession.invalid.varname', array($name)); 
     72 
     73        if(!isset($_SESSION[$name])) 
     74            return NULL; 
     75 
     76        return $_SESSION[$name]; 
     77    } 
     78 
     79 
     80    /** 
     81     * check if a given var is stored in the session 
     82     * @param string $name The name of the session var to check 
     83     * @return boolean 
     84     */ 
     85    public static function isDefined($name){ 
     86        if(!self::$_started) 
     87            self::_realStart(); 
     88 
     89        $name = trim($name); 
     90        if($name === '') 
     91            throw new jException('jelix~errors.jsession.invalid.varname', array($name)); 
     92 
     93        return isset($_SESSION[$name]); 
     94    } 
     95 
     96 
     97    /** 
     98     * unset a given var is stored in the session 
     99     * @param string $name The name of the session var to unset 
     100     */ 
     101    public static function delete($name){ 
     102        if(!self::$_started) 
     103            self::_realStart(); 
     104 
     105        $name = trim($name); 
     106        if($name === '') 
     107            throw new jException('jelix~errors.jsession.invalid.varname', array($name)); 
     108 
     109        unset($_SESSION[$name]); 
     110    } 
     111 
     112 
     113    /** 
     114     * destroys the current session 
     115     */ 
     116    public static function destroy(){ 
     117        if(!self::$_started) 
     118            self::_realStart(); 
     119 
     120        $_SESSION = array(); 
     121 
     122        if (isset($_COOKIE[session_name()])) { 
     123           setcookie(session_name(), '', time()-42000, '/'); 
     124        } 
     125 
     126        session_destroy(); 
     127    } 
     128 
     129 
     130 
     131 
     132 
     133    protected static function _realStart(){ 
     134 
     135        if(self::$_started) return; 
     136 
    28137        $params = $GLOBALS['gJConfig']->sessions; 
    29138 
     139        if($params['start'] === 'never'){ 
     140            throw new jException('jelix~errors.jsession.disabled'); 
     141        } 
     142 
    30143        if(isset($params['storage'])){ 
    31144 
    32145            switch($params['storage']){ 
     
    61174            } 
    62175            session_name($params['name']); 
    63176        } 
    64          
     177 
    65178        session_start(); 
    66         return true; 
     179        self::$_started = true; 
    67180    } 
    68181 
    69     /** 
    70      * end a session 
    71      */ 
    72     public static function end(){ 
    73         session_write_close(); 
    74         return true; 
    75     } 
    76182 
    77183 
    78184    protected static function _getDao(){ 
     
    137243     * dao handler for session stored in database 
    138244     */ 
    139245    public static function daoDestroy ($id) { 
    140         if (isset($_COOKIE[session_name()])) { 
    141            setcookie(session_name(), '', time()-42000, '/'); 
    142         } 
    143  
    144246        self::_getDao()->delete($id); 
    145247        return true; 
    146248    } 
  • lib/jelix/core/defaultconfig.ini.php

    old new  
    201201 
    202202[sessions] 
    203203shared_session = off 
     204; You can specify the way that sessions start. 
     205; - "always" means that you have nothing to do to use sessions. This is required to use PHP's $_SESSION var directly 
     206; - "ondemand" means that the session in only started when you use jSession::set(), jSession::get(), jSession::isDefined(), jSession::delete() or jSession::destroy() 
     207; - "never" means that sessions can not be started. 
     208start = always 
     209; 
    204210; You can change the session name by setting the following parameter (only accepts alpha-numeric chars) : 
    205211; name = "mySessionName" 
    206212; 
  • lib/jelix/core-modules/jelix/locales/en_EN/errors.ISO-8859-15.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Session name cannot be empty and only accepts alpha-numeric chars. 
     31jsession.disabled = (91)Support of sessions is disabled in your application's configuration (parameter start = never). 
     32jsession.invalid.varname = (92)Invalid session's variable name : "%s" 
    3133 
    3234#---- actions 
    3335ad.controller.file.unknow=(100)Action %s : controller file %s doesn't exists 
  • lib/jelix/core-modules/jelix/locales/en_EN/errors.ISO-8859-1.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Session name cannot be empty and only accepts alpha-numeric chars. 
     31jsession.disabled = (91)Support of sessions is disabled in your application's configuration (parameter start = never). 
     32jsession.invalid.varname = (92)Invalid session's variable name : "%s" 
    3133 
    3234#---- actions 
    3335ad.controller.file.unknow=(100)Action %s : controller file %s doesn't exists 
  • lib/jelix/core-modules/jelix/locales/en_EN/errors.UTF-8.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Session name cannot be empty and only accepts alpha-numeric chars. 
     31jsession.disabled = (91)Support of sessions is disabled in your application's configuration (parameter start = never). 
     32jsession.invalid.varname = (92)Invalid session's variable name : "%s" 
    3133 
    3234#---- action 
    3335ad.controller.file.unknow=(100)Action %s : controller file %s doesn't exists 
  • lib/jelix/core-modules/jelix/locales/en_US/errors.ISO-8859-15.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Session name cannot be empty and only accepts alpha-numeric chars. 
     31jsession.disabled = (91)Support of sessions is disabled in your application's configuration (parameter start = never). 
     32jsession.invalid.varname = (92)Invalid session's variable name : "%s" 
    3133 
    3234#---- actions 
    3335ad.controller.file.unknow=(100)Action %s : controller file %s doesn't exists 
  • lib/jelix/core-modules/jelix/locales/en_US/errors.ISO-8859-1.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Session name cannot be empty and only accepts alpha-numeric chars. 
     31jsession.disabled = (91)Support of sessions is disabled in your application's configuration (parameter start = never). 
     32jsession.invalid.varname = (92)Invalid session's variable name : "%s" 
    3133 
    3234#---- actions 
    3335ad.controller.file.unknow=(100)Action %s : controller file %s doesn't exists 
  • lib/jelix/core-modules/jelix/locales/en_US/errors.UTF-8.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Session name cannot be empty and only accepts alpha-numeric chars. 
     31jsession.disabled = (91)Support of sessions is disabled in your application's configuration (parameter start = never). 
     32jsession.invalid.varname = (92)Invalid session's variable name : "%s" 
    3133 
    3234#---- action 
    3335ad.controller.file.unknow=(100)Action %s : controller file %s doesn't exists 
  • lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-15.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Le nom de session ne peut �e vide et ne doit contenir que des caract�s alpha-num�ques. 
     31jsession.disabled = (91)Le support des sessions est d�ctiv�ans la configuration de votre application (param�e start = never). 
     32jsession.invalid.varname = (92)Nom de la variable de session invalide : "%s" 
    3133 
    3234#---- actions 
    3335ad.controller.file.unknow=(100)Action %s : le fichier contr�r %s n'existe pas 
  • lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-1.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Le nom de session ne peut �e vide et ne doit contenir que des caract�s alpha-num�ques. 
     31jsession.disabled = (91)Le support des sessions est d�ctiv�ans la configuration de votre application (param�e start = never). 
     32jsession.invalid.varname = (92)Nom de la variable de session invalide : "%s" 
    3133 
    3234#---- actions 
    3335ad.controller.file.unknow=(100)Action %s : le fichier contr�r %s n'existe pas 
  • lib/jelix/core-modules/jelix/locales/fr_FR/errors.UTF-8.properties

    old new  
    2828 
    2929#---- jSession 
    3030jsession.name.invalid = (90)Le nom de session ne peut être vide et ne doit contenir que des caractères alpha-numériques. 
     31jsession.disabled = (91)Le support des sessions est désactivé dans la configuration de votre application (paramètre start = never). 
     32jsession.invalid.varname = (92)Nom de la variable de session invalide : "%s" 
    3133 
    3234#---- actions 
    3335ad.controller.file.unknow=(100)Action %s : le fichier contrôleur %s n'existe pas 
Download in other formats: Original Format