| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Provides a session with a client to work with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * PHP Version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @category  Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package   Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author    Hans-Joachim Piepereit <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2013 cSphere Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license   http://opensource.org/licenses/bsd-license Simplified BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @link      http://www.csphere.eu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace csphere\core\session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Provides a session with a client to work with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @category  Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @package   Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author    Hans-Joachim Piepereit <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @copyright 2013 cSphere Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @license   http://opensource.org/licenses/bsd-license Simplified BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @link      http://www.csphere.eu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  **/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | class Session | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Creates a new session object | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return \csphere\core\session\Session | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // Check for an active session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $status = session_status(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $active = ($status == PHP_SESSION_ACTIVE) ? true : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // If there is no active session create one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (empty($active)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * Cares about session settings and starts a new one | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private function _start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // Set some session settings, httponly is set by cookie_params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ini_set('session.use_trans_sid', 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ini_set('session.use_cookies', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ini_set('session.use_only_cookies', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // Change session name and cookie params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $request = \csphere\core\http\Request::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $dns = ($request['dns'] == 'localhost') ? '' : $request['dns']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         //ToDo: Fix Session Management | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         //$salt = mcrypt_create_iv(40, MCRYPT_DEV_URANDOM); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         //$name = 'csphere_' . md5($request['dns'] . $request['dirname'] . $salt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         //session_name($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $secure = ($request['protocol'] == 'https') ? true : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         session_set_cookie_params(0, $request['dirname'], $dns, $secure, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Start a new session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         session_start(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * Clears all session data | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @return boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function destroy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // Empty all session contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         session_unset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         session_destroy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // Disable future changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         session_write_close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // Get a new id to kill the old one for sure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         session_regenerate_id(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return true; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * Retrieves the data stored in a session | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @param string $name Name of the session key to retrieve | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function get($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $string = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if (isset($_SESSION[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $string = $_SESSION[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $string; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * Stores session data | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * @param string $name  Name of the session key to set | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * @param string $value The value of the session key | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * @return boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function set($name, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $_SESSION[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return true; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  | 
            
                        
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.