| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Manage PHP sessions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright Caffeina srl - 2015 - http://caffeina.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Session { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     use Module, Events; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * Start session handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	static public function start($name=null){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		if (isset($_SESSION)) return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$ln = static::name($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     // Obfuscate IDs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ini_set('session.hash_function', 'whirlpool'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		session_cache_limiter('must-revalidate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		@session_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     static::trigger("start", $name?:$ln); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |    * Get/Set Session's cookie params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |    * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |    * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |    * @param array $args Array of cookie's parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |    * @see http://php.net/manual/en/function.session-set-cookie-params.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |    * @example $args = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |    *            "lifetime"  => 600, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    *            "path"      => "/", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |    *            "domain"    => ".caffeina.com", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |    *            "secure"    => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |    *            "httponly"  => false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |    *          ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |    * @return array The cookie's parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |    * @see http://php.net/manual/en/function.session-get-cookie-params.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |   static public function cookieParams($args = []) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     if (empty($args)) return session_get_cookie_params(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     $args = array_merge(session_get_cookie_params(), $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     session_set_cookie_params($args["lifetime"], $args["path"], $args["domain"], $args["secure"], $args["httponly"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     return $args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * Get/Set Session name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @param string $key The session name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @return string The session value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	static public function name($name=null){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		return $name ? session_name($name) : session_name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * Get a session variable reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param mixed $key The variable name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return mixed The variable value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	static public function get($key,$default=null){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 if (($active = static::active()) && isset($_SESSION[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			return $_SESSION[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 } else if ($active) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 	return $_SESSION[$key] = (is_callable($default)?call_user_func($default):$default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                  	return (is_callable($default)?call_user_func($default):$default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * Set a session variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * @param mixed $key The variable name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @param mixed $value The variable value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	static public function set($key,$value=null){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		static::start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		if($value==null && is_array($key)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			foreach($key as $k=>$v) $_SESSION[$k]=$v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			$_SESSION[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * Delete a session variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @param mixed $key The variable name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	static public function delete($key){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		static::start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		unset($_SESSION[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * Delete all session variables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	static public function clear(){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		static::start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		session_unset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		session_destroy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     static::trigger("end"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * Check if session is active | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	static public function active(){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		return session_status() == PHP_SESSION_ACTIVE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * Check if a session variable exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * @static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * @param mixed $key The variable name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  | 	static public function exists($key){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  | 		static::start(); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  | 		return isset($_SESSION[$key]); | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * Return a read-only accessor to session variables for in-view use. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * @return SessionReadOnly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	static public function readOnly(){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		return new SessionReadOnly; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | }  /* End of class */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  * Read-only Session accessor class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | class SessionReadOnly { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 * Get a session variable reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	 * @param mixed $key The variable name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	 * @return mixed The variable value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	public function get($key){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		return Session::get($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	public function __get($key){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 		return Session::get($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	public function name(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		return Session::name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	 * Check if a session variable exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	 * @param mixed $key The variable name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 	public function exists($key){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 		return Session::exists($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 	public function __isset($key){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 		return Session::exists($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 208 |  |  | }  /* End of class */ | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |  |