source of /sprout/Helpers/Drivers/Session/Database.php
Copyright (C) 2017 Karmabunny Pty Ltd.
This file is a part of SproutCMS.
SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.
For more information, visit <http://getsproutcms.com>.
This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team <?php /** * Copyright (C) 2017 Karmabunny Pty Ltd. * * This file is a part of SproutCMS. * * SproutCMS is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Foundation, either * version 2 of the License, or (at your option) any later version. * * For more information, visit <http://getsproutcms.com>. * * This class was originally from Kohana 2.3.4 * Copyright 2007-2008 Kohana Team */ namespace Sprout\Helpers\Drivers\Session; use Kohana; use karmabunny\pdb\Exceptions\RowMissingException; use Sprout\Helpers\Drivers\SessionDriver; use Sprout\Helpers\Encrypt; use Sprout\Helpers\Pdb; /** * Session database driver. */ class Database implements SessionDriver { // Database settings protected $table = 'sessions'; // Encryption protected $encrypt; // Session settings protected $session_id; protected $written = FALSE; public function __construct() { // Load configuration $config = Kohana::config('session'); if ( ! empty($config['encryption'])) { // Load encryption $this->encrypt = Encrypt::instance(); } if (!empty($config['storage']['table'])) { // Set the table name Pdb::validateIdentifier($config['storage']['table']); $this->table = $config['storage']['table']; } } Kohana ::log('debug', 'Session Database Driver Initialized'); } public function open($path, $name) { return TRUE; } public function close() { return TRUE; } public function read($id) { // Load the session try { $q = "SELECT data FROM ~{$this->table} WHERE session_id = ? LIMIT 1"; $data = Pdb::q($q, [$id], 'val'); } catch (RowMissingException $ex) { // No current session return ''; } // Set the current session id return ($this->encrypt === NULL) ? base64_decode($data) : $this->encrypt->decode($data); } public function write($id, $data) { ( 'session_id' => $id, 'last_activity' => time(), 'data' => ($this->encrypt === NULL) ? base64_encode($data) : $this->encrypt->encode($data) ); { // Insert a new session $count = Pdb::insert($this->table, $data); } { // Do not update the session_id unset($data['session_id']); // Update the existing session $count = Pdb ::update($this->table, $data, array('session_id' => $id)); } else { // Update the session and id $count = Pdb ::update($this->table, $data, array('session_id' => $this->session_id)); // Set the new session id } return (bool) $count; } public function destroy($id) { // Delete the requested session Pdb ::delete($this->table, array('session_id' => $id)); // Session id is no longer valid return TRUE; } public function regenerate() { // Generate a new session id // Return new session id } public function gc($maxlifetime) { // Delete all expired sessions Pdb ::delete($this->table, [['last_activity', '<', time() - $maxlifetime]]); } } // End Session Database Driver
|