Changeset 16 for trunk

Show
Ignore:
Timestamp:
06/08/04 22:10:06 (5 years ago)
Author:
gerd
Message:

changed some security-features
added examples

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/autopackage.php

    r14 r16  
    1414 */ 
    1515 
    16 $baseVersion = '1.0.0'; 
     16$baseVersion = '1.0.1'; 
    1717 
    1818/** 
     
    5050$package = new PEAR_PackageFileManager(); 
    5151 
    52 $result = $package->setOptions(array( 
     52$result = $package->setOptions( array( 
    5353    'package'           => 'patSession', 
    5454    'summary'           => 'Session management package.', 
     
    6868                                 'tests' => 'test', 
    6969                                 ) 
    70     )); 
     70    ) ); 
    7171 
    72 if (PEAR::isError($result)) { 
     72if (PEAR::isError($result))  
     73
    7374    echo $result->getMessage(); 
    7475    die(); 
    7576} 
    7677 
    77 $package->addMaintainer('gerd', 'lead', 'Gerd Schaufelberger', 'gerd@php-tools.net'); 
     78$package->addMaintainer('gerd', 'lead', 'gERD Schaufelberger', 'gerd@php-tools.net'); 
    7879 
    7980$package->addDependency('patError', '', 'has', 'pkg', false); 
     
    8283$result = $package->writePackageFile(); 
    8384 
    84 if (PEAR::isError($result)) { 
     85if (PEAR::isError($result))  
     86
    8587    echo $result->getMessage(); 
    8688    die(); 
  • trunk/docs/readme.txt

    r2 r16  
    1010 * @link http://www.php-tools.net 
    1111 * @todo more detailed information 
    12  * @todo               write a lot :-) 
     12 * @todo write a lot :-) 
    1313 */ 
    1414 
     
    2020WHAT IS PATSESSION? 
    2121=================== 
    22  
    23 hm? Guess what it is :-) 
     22patSession is an abstraction layer for session-management. Therefore patSession can be used  
     23if you need session support. patSession comes with some with some security-features for  
     24session-fixiation. Also the patSession storage-containers are based on drivers. Currently  
     25only the native-driver is implemented (it used the native php session support). 
    2426 
    2527Sorry, no text here... 
  • trunk/examples/example_basic_first.php

    r9 r16  
    2323        // most easy way to create a session object 
    2424        $sess   =&      patSession::singleton( 'ham' ); 
    25          
     25 
    2626        // store a value  
    2727        echo '<br/>Store some values into session...<br/>'; 
     
    4141        echo 'Query-string <i>' . $queryString . '</i> from this session.<br />'; 
    4242        echo 'This <a href="' . $_SERVER['PHP_SELF'] . '?' . $queryString . '" title="'. $_SERVER['PHP_SELF'] . '?' . $queryString .'">URL</a> contains the query-string<br />'; 
     43         
    4344?> 
  • trunk/examples/example_secure_expire.php

    r10 r16  
    2929        $sess   =&      patSession::singleton( 'ham', 'Native', $options ); 
    3030         
     31        // recieve query string from session 
    3132        $queryString    =       $sess->getQueryString(); 
     33         
    3234        echo 'This session will expire within one minute idle time.<br><br>'; 
    3335        echo 'Just wait one minute until you follow '; 
  • trunk/patSession/Storage.php

    r15 r16  
    2828         
    2929   /** 
    30         * internal session name 
    31         * @var  string $_name 
    32         */ 
    33         var     $_name  =       null; 
    34          
    35    /** 
    36         * internal session id 
    37         * @var  string $_id  
    38         */ 
    39         var     $_id    =       null; 
    40          
    41    /** 
    4230        * maximum age of unused session  
    4331        * @var  string $_expire minutes  
    4432        */ 
    4533        var     $_expire        =       null; 
    46          
    47    /** 
    48         * cookie behaviour options 
    49         * Decide either to use cookies, force the usage or deny it or let  
    50         * the value of php-ini-file decide how if cookies should be used. 
    51         * @var  string $_cookie named behaviour for cookie-management 
    52         */ 
    53         var     $_cookie        =       'ini'; 
    5434         
    5535   /** 
     
    10585        * recieve name of this session 
    10686        * 
    107         * @final 
     87        * @abstract 
    10888        * @access private 
    10989        * @return string $name session name 
     
    11191    function getName() 
    11292    { 
    113                 return $this->_name
     93                return null
    11494    } 
    11595 
     
    11797        * recieve id of this session 
    11898        * 
    119         * @final 
     99        * @abstract 
    120100        * @access private 
    121101        * @return string $id session id 
     
    123103    function getId() 
    124104    { 
    125                 if( $this->_state === 'destroyed' ) 
    126                 { 
    127                         patErrorManager::raiseNotice( 'patSession:Storage:' . PATSESSION_ERROR_NOT_ACTIVE, 
    128                                                 'Session is not active - nothing to clear', 
    129                                                 'The session was destroyed before.' 
    130                                          ); 
    131                         return null; 
    132                 } 
    133                  
    134                 return $this->_id; 
     105                return null; 
    135106    } 
    136107         
     
    209180        * @access private 
    210181        * @return boolean $result true on success 
    211         * @todo implement browser fixiation 
    212182        * @todo add network-mask feature for ip-check 
    213183        * @todo add allowed referer for session-transfers 
    214184        */ 
    215         function _checkSecurity() 
    216         { 
     185        function _checkSecurity( $rewoke = false ) 
     186        { 
     187                // allow to rewoke a session 
     188                if( $rewoke ) 
     189                { 
     190                        $this->_state   =       'active'; 
     191                        $this->set( '_patSession_atime', null ); 
     192                        $this->set( '_patSession_clientAddr', null ); 
     193                        $this->set( '_patSession_clientBrowser', null ); 
     194                } 
     195                 
    217196                $atime  =       $this->get( '_patSession_atime' ); 
    218197                 
     
    243222                } 
    244223                // else check referer 
    245                 else if( in_array( 'fix_referer', $this->_security ) ) 
     224                else if( !empty( $this->_allowedReferer ) && in_array( 'fix_referer', $this->_security ) ) 
    246225                { 
    247226                        // referer must match server 
     
    252231                        if( $ref !== $_SERVER['SERVER_NAME'] ) 
    253232                        { 
    254                                 if( empty( $this->_allowedReferer ) ) 
     233                                // check allowed referers 
     234                                $found  =       false; 
     235                                foreach( $this->_allowedReferer as $allowed ) 
    255236                                { 
    256                                         $this->_state   =       'fix_referer_failed'; 
     237                                        if( preg_match( $allowed, $ref ) ) 
     238                                        { 
     239                                                $found  =       true; 
     240                                                break; 
     241                                        } 
     242                                } 
     243                                 
     244                                if( !$found ) 
     245                                { 
     246                                        $this->_state   =       'referer_not_allowed'; 
    257247                                        return false; 
    258                                 } 
    259                                 // check allowed referers 
    260                                 else 
    261                                 { 
    262                                         $found  =       false; 
    263                                         foreach( $this->_allowedReferer as $allowed ) 
    264                                         { 
    265                                                 if( preg_match( $allowed, $ref ) ) 
    266                                                 { 
    267                                                         $found  =       true; 
    268                                                         break; 
    269                                                 } 
    270                                         } 
    271                                          
    272                                         if( !$found ) 
    273                                         { 
    274                                                 $this->_state   =       'referer_not_allowed'; 
    275                                                 return false; 
    276                                         } 
    277248                                } 
    278249                        } 
     
    311282                } 
    312283                 
     284                return true; 
    313285        } 
    314286         
     
    343315    function _setOptions( &$options ) 
    344316    { 
    345                 if( isset( $options['name'] ) ) 
    346                 { 
    347                         $this->_name    =       $options['name']; 
    348                 } 
    349                  
    350                 // set id 
    351                 if( isset( $options['id'] ) ) 
    352                 { 
    353                         $this->_id      =       $options['id']; 
    354                 } 
    355                  
    356317                // set expire time 
    357318                if( isset( $options['expire'] ) ) 
     
    396357                } 
    397358                 
    398                 // select cookie-mode 
    399                 $cookieOptions  =       array( 'ini', 'only', 'allow', 'deny' ); 
    400                 if( isset( $options['cookie'] ) && in_array( $options['cookie'], $cookieOptions ) ) 
    401                 { 
    402                         $this->_cookie  =       $options['cookie']; 
    403                          
    404                         // get cookie behavour from php.ini 
    405                         if( $this->_cookie === 'ini' ) 
    406                         { 
    407                                 $this->_cookie  =       'deny'; 
    408                          
    409                                 // cookies are allowed 
    410                                 if( ini_get( 'session.use_cookies' ) ) 
    411                                 { 
    412                                         $this->_cookie  =       'allow'; 
    413                                         if( ini_get( 'session.use_only_cookies' ) ) 
    414                                         { 
    415                                                 $this->_cookie  =       'only'; 
    416                                         } 
    417                                 } 
    418                         } 
    419                          
    420                 } 
    421                  
    422359                return true; 
    423360    } 
  • trunk/patSession/Storage/Native.php

    r15 r16  
    3434   /** 
    3535        * id string 
    36         * @var  string $_sessId  
    37         */ 
    38         var     $_sessId      =       null; 
     36        * @var  string $_id  
     37        */ 
     38        var     $_id  =       null; 
    3939         
    4040   /** 
     
    5656        function        __construct( $id = 'session', $options = array() ) 
    5757        { 
    58                 $this->_sessId        =       $id; 
     58                $this->_id    =       $id; 
    5959                $this->_setOptions( $options ); 
    60  
    61                 // use options 
    62                 if( $this->_name !== null  ) 
    63                 { 
    64                         session_name( $this->_name ); 
    65                 } 
    66                  
    67                 if( $this->_id !== null  ) 
    68                 { 
    69                         session_id( $this->_id ); 
    70                 } 
    71                  
    72                 // set cookie options 
    73                 switch( $this->_cookie ) 
    74                 { 
    75                         case 'deny'; 
    76                                 if( ini_get( 'session.use_cookies' ) ) 
    77                                 { 
    78                                         ini_set( 'session.use_cookies', 0 ); 
    79                                 } 
    80                                 break; 
    81                          
    82                         case 'allow'; 
    83                                 if( !ini_get( 'session.use_cookies' ) ) 
    84                                 { 
    85                                         ini_set( 'session.use_cookies', 1 ); 
    86                                 } 
    87                                 break; 
    88                          
    89                         case 'only'; 
    90                                 if( !ini_get( 'session.use_cookies' ) ) 
    91                                 { 
    92                                         ini_set( 'session.use_cookies', 1 ); 
    93                                 } 
    94                                  
    95                                 if( !ini_get( 'session.use_only_cookies' ) ) 
    96                                 { 
    97                                         ini_set( 'session.use_only_cookies', 1 ); 
    98                                 } 
    99                                 break; 
    100                 } 
    101                  
    10260                 
    10361                //  start session if not startet 
     
    10765                } 
    10866 
    109                 $this->_name    =       session_name(); 
    110                 $this->_id              =       session_id(); 
    111                  
    11267                // init session-array            
    113                 if( !isset( $_SESSION[$this->_sessId] ) ) 
    114                 { 
    115                         $_SESSION[$this->_sessId]     =       array(); 
    116                 } 
    117                  
    118                 $this->_sess    =&      $_SESSION[$this->_sessId]; 
     68                if( !isset( $_SESSION[$this->_id] ) ) 
     69                { 
     70                        $_SESSION[$this->_id] =       array(); 
     71                } 
     72                 
     73                $this->_sess    =&      $_SESSION[$this->_id]; 
    11974                $this->_state   =       'active'; 
    12075                 
     
    12782 
    12883   /** 
     84        * recieve name of this session 
     85        * 
     86        * @access private 
     87        * @return string $name session name 
     88        */ 
     89    function getName() 
     90    { 
     91                return session_name(); 
     92    } 
     93 
     94   /** 
     95        * recieve id of this session 
     96        * 
     97        * @access private 
     98        * @return string $id session id 
     99        */ 
     100    function getId() 
     101    { 
     102                if( $this->_state === 'destroyed' ) 
     103                { 
     104                        patErrorManager::raiseNotice( 'patSession_' . PATSESSION_ERROR_NOT_ACTIVE, 
     105                                                'Session is not active - nothing to clear', 
     106                                                'The session was destroyed before.' 
     107                                         ); 
     108                        return null; 
     109                } 
     110                 
     111                return session_id(); 
     112    } 
     113         
     114   /** 
    129115        * save data into session 
    130116        *  
     
    138124                if( $this->_state !== 'active' ) 
    139125                { 
    140                         return patErrorManager::raiseError( 'patSession:Storage:Native:' . PATSESSION_ERROR_NOT_ACTIVE, 
     126                        return patErrorManager::raiseError( 'patSession_' . PATSESSION_ERROR_NOT_ACTIVE, 
    141127                                                'Cannot set value because session not active.', 
    142128                                                'Either the session was destroyed, has expired or locked by security' 
     
    159145                if( $this->_state !== 'active' ) 
    160146                { 
    161                                 return patErrorManager::raiseWarning( 'patSession:Storage:Native:' . PATSESSION_ERROR_NOT_ACTIVE, 
     147                                return patErrorManager::raiseWarning( 'patSession_' . PATSESSION_ERROR_NOT_ACTIVE, 
    162148                                                'Cannot recieve value because session not active.', 
    163149                                                'Either the session was destroyed, has expired or locked because of security reasons.' 
     
    184170                if( $this->_state !== 'active' ) 
    185171                { 
    186                         patErrorManager::raiseNotice( 'patSession:Storage:Native:' . PATSESSION_ERROR_NOT_ACTIVE, 
     172                        patErrorManager::raiseNotice( 'patSession_' . PATSESSION_ERROR_NOT_ACTIVE, 
    187173                                                'Session is not active - nothing to clear', 
    188174                                                'Either the session was destroyed, has expired or locked because of security reasons.' 
     
    211197                if( $this->_state === 'destroyed' ) 
    212198                { 
    213                         patErrorManager::raiseNotice( 'patSession:Storage:Native:' . PATSESSION_ERROR_NOT_ACTIVE, 
     199                        patErrorManager::raiseNotice( 'patSession_' . PATSESSION_ERROR_NOT_ACTIVE, 
    214200                                                'Session is not active.', 
    215201                                                'The session was destroyed before.' 
     
    237223                if( $this->_state !== 'active' ) 
    238224                { 
    239                         return patErrorManager::raiseError( 'patSession:Storage:Native:' . PATSESSION_ERROR_NOT_ACTIVE, 
     225                        if( !$this->_checkSecurity( true ) ) 
     226                        { 
     227                                return patErrorManager::raiseNotice( 'patSession_' . PATSESSION_ERROR_NOT_ACTIVE, 
    240228                                                'Session is not active.', 
    241                                                 'Either the session was destroyed, has expired or locked because of security reasons.' 
    242                                          ); 
     229                                                'Either the session was destroyed, has expired or locked because of security reasons: '. $this->_state.'.' 
     230                                         ); 
     231                        } 
    243232                } 
    244233                 
     
    254243                $cookie =       session_get_cookie_params(); 
    255244                 
    256                 $this->_id    =       $this->_createId( strlen( session_id() ) ); 
     245                $id   =       $this->_createId( strlen( session_id() ) ); 
    257246                 
    258247                // kill session and restart it with new id 
    259248                session_destroy(); 
    260                 session_id( $this->_id ); 
     249                session_id( $id ); 
    261250                session_start(); 
    262251                 
    263252                // restore values        
    264253                $_SESSION               =       $values; 
    265                 $this->_sess    =&      $_SESSION[$this->_sessId]; 
     254                $this->_sess    =&      $_SESSION[$this->_id]; 
    266255 
    267256                // restore config                
     
    271260                return true; 
    272261    } 
     262 
     263   /** 
     264        * set additional session options 
     265        * 
     266        * @access private 
     267        * @param array $options list of parameter 
     268        * @return boolean $result true on success 
     269        */ 
     270    function _setOptions( &$options ) 
     271    { 
     272                // set name 
     273                if( isset( $options['name'] ) ) 
     274                { 
     275                        session_name( $options['name'] ); 
     276                } 
     277                 
     278                // set id 
     279                if( isset( $options['id'] ) ) 
     280                { 
     281                        session_id( $options['id'] ); 
     282                } 
     283                 
     284                return parent::_setOptions( $options ); 
     285    } 
    273286} 
    274287?>