| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class Session_Core { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | private static $instance; |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | protected static $protect = array('session_id', 'user_agent', 'last_activity', 'ip_address', 'total_hits', '_kf_flash_'); |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | protected static $config; |
|---|
| 22 | protected static $driver; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | protected static $flash; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | protected $input; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | public static function instance() |
|---|
| 34 | { |
|---|
| 35 | if (self::$instance == NULL) |
|---|
| 36 | { |
|---|
| 37 | |
|---|
| 38 | new Session; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | return self::$instance; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | public function __construct() |
|---|
| 48 | { |
|---|
| 49 | $this->input = Input::instance(); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | if (self::$instance === NULL) |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | self::$config = Kohana::config('session'); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | self::$protect = array_combine(self::$protect, self::$protect); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | ini_set('session.gc_probability', (int) self::$config['gc_probability']); |
|---|
| 62 | ini_set('session.gc_divisor', 100); |
|---|
| 63 | ini_set('session.gc_maxlifetime', (self::$config['expiration'] == 0) ? 86400 : self::$config['expiration']); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | $this->create(); |
|---|
| 67 | |
|---|
| 68 | if (self::$config['regenerate'] > 0 AND ($_SESSION['total_hits'] % self::$config['regenerate']) === 0) |
|---|
| 69 | { |
|---|
| 70 | |
|---|
| 71 | $this->regenerate(); |
|---|
| 72 | } |
|---|
| 73 | else |
|---|
| 74 | { |
|---|
| 75 | |
|---|
| 76 | cookie::set(self::$config['name'], $_SESSION['session_id'], self::$config['expiration']); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | Event::add('system.send_headers', array($this, 'write_close')); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | register_shutdown_function(array($this, 'write_close')); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | self::$instance = $this; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | Kohana::log('debug', 'Session Library initialized'); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | public function id() |
|---|
| 99 | { |
|---|
| 100 | return $_SESSION['session_id']; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | public function create($vars = NULL) |
|---|
| 110 | { |
|---|
| 111 | |
|---|
| 112 | $this->destroy(); |
|---|
| 113 | |
|---|
| 114 | if (self::$config['driver'] !== 'native') |
|---|
| 115 | { |
|---|
| 116 | |
|---|
| 117 | $driver = 'Session_'.ucfirst(self::$config['driver']).'_Driver'; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | if ( ! Kohana::auto_load($driver)) |
|---|
| 121 | throw new Kohana_Exception('core.driver_not_found', self::$config['driver'], get_class($this)); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | self::$driver = new $driver(); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if ( ! (self::$driver instanceof Session_Driver)) |
|---|
| 128 | throw new Kohana_Exception('core.driver_implements', self::$config['driver'], get_class($this), 'Session_Driver'); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | session_set_save_handler |
|---|
| 132 | ( |
|---|
| 133 | array(self::$driver, 'open'), |
|---|
| 134 | array(self::$driver, 'close'), |
|---|
| 135 | array(self::$driver, 'read'), |
|---|
| 136 | array(self::$driver, 'write'), |
|---|
| 137 | array(self::$driver, 'destroy'), |
|---|
| 138 | array(self::$driver, 'gc') |
|---|
| 139 | ); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | if ( ! preg_match('~^(?=.*[a-z])[a-z0-9_]++$~iD', self::$config['name'])) |
|---|
| 144 | throw new Kohana_Exception('session.invalid_session_name', self::$config['name']); |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | session_name(self::$config['name']); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | session_set_cookie_params |
|---|
| 151 | ( |
|---|
| 152 | self::$config['expiration'], |
|---|
| 153 | Kohana::config('cookie.path'), |
|---|
| 154 | Kohana::config('cookie.domain'), |
|---|
| 155 | Kohana::config('cookie.secure'), |
|---|
| 156 | Kohana::config('cookie.httponly') |
|---|
| 157 | ); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | session_start(); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | $_SESSION['session_id'] = session_id(); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | if ( ! isset($_SESSION['_kf_flash_'])) |
|---|
| 167 | { |
|---|
| 168 | $_SESSION['total_hits'] = 0; |
|---|
| 169 | $_SESSION['_kf_flash_'] = array(); |
|---|
| 170 | |
|---|
| 171 | $_SESSION['user_agent'] = Kohana::$user_agent; |
|---|
| 172 | $_SESSION['ip_address'] = $this->input->ip_address(); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | self::$flash =& $_SESSION['_kf_flash_']; |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | $_SESSION['total_hits'] += 1; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | if ($_SESSION['total_hits'] > 1) |
|---|
| 183 | { |
|---|
| 184 | |
|---|
| 185 | foreach (self::$config['validate'] as $valid) |
|---|
| 186 | { |
|---|
| 187 | switch ($valid) |
|---|
| 188 | { |
|---|
| 189 | |
|---|
| 190 | case 'user_agent': |
|---|
| 191 | if ($_SESSION[$valid] !== Kohana::$user_agent) |
|---|
| 192 | return $this->create(); |
|---|
| 193 | break; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | case 'ip_address': |
|---|
| 197 | if ($_SESSION[$valid] !== $this->input->$valid()) |
|---|
| 198 | return $this->create(); |
|---|
| 199 | break; |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | case 'expiration': |
|---|
| 203 | if (time() - $_SESSION['last_activity'] > ini_get('session.gc_maxlifetime')) |
|---|
| 204 | return $this->create(); |
|---|
| 205 | break; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | $this->expire_flash(); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | $_SESSION['last_activity'] = time(); |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | self::set($vars); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | public function regenerate() |
|---|
| 226 | { |
|---|
| 227 | if (self::$config['driver'] === 'native') |
|---|
| 228 | { |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | session_regenerate_id(TRUE); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | $_SESSION['session_id'] = session_id(); |
|---|
| 235 | } |
|---|
| 236 | else |
|---|
| 237 | { |
|---|
| 238 | |
|---|
| 239 | $_SESSION['session_id'] = self::$driver->regenerate(); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | $name = session_name(); |
|---|
| 244 | |
|---|
| 245 | if (isset($_COOKIE[$name])) |
|---|
| 246 | { |
|---|
| 247 | |
|---|
| 248 | $_COOKIE[$name] = $_SESSION['session_id']; |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | public function destroy() |
|---|
| 258 | { |
|---|
| 259 | if (session_id() !== '') |
|---|
| 260 | { |
|---|
| 261 | |
|---|
| 262 | $name = session_name(); |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | session_destroy(); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | $_SESSION = array(); |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | cookie::delete($name); |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | public function write_close() |
|---|
| 281 | { |
|---|
| 282 | static $run; |
|---|
| 283 | |
|---|
| 284 | if ($run === NULL) |
|---|
| 285 | { |
|---|
| 286 | $run = TRUE; |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | Event::run('system.session_write'); |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | $this->expire_flash(); |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | session_write_close(); |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | public function set($keys, $val = FALSE) |
|---|
| 307 | { |
|---|
| 308 | if (empty($keys)) |
|---|
| 309 | return FALSE; |
|---|
| 310 | |
|---|
| 311 | if ( ! is_array($keys)) |
|---|
| 312 | { |
|---|
| 313 | $keys = array($keys => $val); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | foreach ($keys as $key => $val) |
|---|
| 317 | { |
|---|
| 318 | if (isset(self::$protect[$key])) |
|---|
| 319 | continue; |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | $_SESSION[$key] = $val; |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | public function set_flash($keys, $val = FALSE) |
|---|
| 334 | { |
|---|
| 335 | if (empty($keys)) |
|---|
| 336 | return FALSE; |
|---|
| 337 | |
|---|
| 338 | if ( ! is_array($keys)) |
|---|
| 339 | { |
|---|
| 340 | $keys = array($keys => $val); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | foreach ($keys as $key => $val) |
|---|
| 344 | { |
|---|
| 345 | if ($key == FALSE) |
|---|
| 346 | continue; |
|---|
| 347 | |
|---|
| 348 | self::$flash[$key] = 'new'; |
|---|
| 349 | self::set($key, $val); |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | public function keep_flash($keys = NULL) |
|---|
| 360 | { |
|---|
| 361 | $keys = ($keys === NULL) ? array_keys(self::$flash) : func_get_args(); |
|---|
| 362 | |
|---|
| 363 | foreach ($keys as $key) |
|---|
| 364 | { |
|---|
| 365 | if (isset(self::$flash[$key])) |
|---|
| 366 | { |
|---|
| 367 | self::$flash[$key] = 'new'; |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | public function expire_flash() |
|---|
| 378 | { |
|---|
| 379 | static $run; |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | if ($run === TRUE) |
|---|
| 383 | return; |
|---|
| 384 | |
|---|
| 385 | if ( ! empty(self::$flash)) |
|---|
| 386 | { |
|---|
| 387 | foreach (self::$flash as $key => $state) |
|---|
| 388 | { |
|---|
| 389 | if ($state === 'old') |
|---|
| 390 | { |
|---|
| 391 | |
|---|
| 392 | unset(self::$flash[$key], $_SESSION[$key]); |
|---|
| 393 | } |
|---|
| 394 | else |
|---|
| 395 | { |
|---|
| 396 | |
|---|
| 397 | self::$flash[$key] = 'old'; |
|---|
| 398 | } |
|---|
| 399 | } |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | $run = TRUE; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | public function get($key = FALSE, $default = FALSE) |
|---|
| 414 | { |
|---|
| 415 | if (empty($key)) |
|---|
| 416 | return $_SESSION; |
|---|
| 417 | |
|---|
| 418 | $result = isset($_SESSION[$key]) ? $_SESSION[$key] : Kohana::key_string($_SESSION, $key); |
|---|
| 419 | |
|---|
| 420 | return ($result === NULL) ? $default : $result; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | public function get_once($key, $default = FALSE) |
|---|
| 431 | { |
|---|
| 432 | $return = self::get($key, $default); |
|---|
| 433 | self::delete($key); |
|---|
| 434 | |
|---|
| 435 | return $return; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | public function delete($keys) |
|---|
| 445 | { |
|---|
| 446 | $args = func_get_args(); |
|---|
| 447 | |
|---|
| 448 | foreach ($args as $key) |
|---|
| 449 | { |
|---|
| 450 | if (isset(self::$protect[$key])) |
|---|
| 451 | continue; |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | unset($_SESSION[$key]); |
|---|
| 455 | } |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | } |
|---|