/lib/Bee/Session/Native.php
https://github.com/timdorr/hubugz · PHP · 206 lines · 87 code · 31 blank · 88 comment · 6 complexity · 85e2a4572025bad4ce78ec1c2f1b22f7 MD5 · raw file
- <?php
- /**
- * Colony
- * Copyright (c) Army of Bees (www.armyofbees.com)
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @category Colony
- * @package Bee
- * @copyright Copyright (c) Army of Bees (www.armyofbees.com)
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
-
- /**
- * @see Bee_Session_Abstract
- */
- require_once 'Bee/Session/Abstract.php';
- /**
- * @see Bee_Db_Abstract
- */
- require_once 'Bee/Db/Abstract.php';
- /**
- * @see Bee_Input
- */
- require_once 'Bee/Input.php';
- /**
- * Session backend using the built-in PHP session handlers
- *
- * @category Colony
- * @package Bee
- * @copyright Copyright (c) Army of Bees (www.armyofbees.com)
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- class Bee_Session_Native
- {
- /**
- * Database object
- * @var Bee_Db_Abstract
- */
- private $_db = null;
- /**
- * Constructor for the class. Just sets some values for other functions.
- *
- * @param array $config The configuration to use for connection
- */
- public function __construct( &$config = array() )
- {
- // Verify that configuration is in an array.
- if( !is_array( $config ) )
- throw new Bee_Session_Native_Exception('Configuration must be in an array');
- // Verify that a db object is passed
- if( !isset( $config['db'] ) )
- throw new Bee_Session_Native_Exception('Configuration is missing db');
- if( !($config['db'] instanceof Bee_Db_Abstract) )
- throw new Bee_Session_Native_Exception('Configuration is not Bee_Db_Abstract');
-
- $this->_db =& $config['db'];
-
- $this->timeout = $config['session_timeout'];
- $this->domain = $config['session_domain'];
- $this->path = $config['session_path'];
- }
-
- /**
- * Loads the session from the database
- *
- * @return void
- */
- private function _loadSession()
- {
- // Check if we've got an existing session stored
- $input =& Bee_Input::filterInput();
- if ( !array_key_exists( 'session', $input ) )
- {
- $this->newSession();
- }
- else
- {
-
- session_start();
- $this->_id = $input['session'];
- if ( $_SESSION['session_id'] == $this->_id )
- {
- $this->_data = $_SESSION['data'];
- $this->_time = $_SESSION['time'];
-
- setcookie( 'session', $this->_id, time() + $this->timeout, $this->path, $this->domain, FALSE, TRUE );
- }
- else
- {
- $this->newSession();
- }
- }
- }
- /**
- * Creates a new empty session
- *
- * @return void
- */
- public function newSession()
- {
- $this->_id = sha1( uniqid( microtime() ) );
- $this->_time = time();
- $this->_data = array();
- setcookie( 'session', $this->_id, time() + $this->timeout, $this->path, $this->domain, FALSE, TRUE );
-
- session_start();
- $_SESSION['session_id'] = $this->_id;
- $_SESSION['data'] = array();
- $_SESSION['time'] = time();
- }
- /**
- * Saves the session into the database
- *
- * @param array $data The data to be stored into the session
- * @return void
- */
- public function saveSession( &$data )
- {
-
- $this->_data = $data;
-
- session_start();
- $_SESSION['session_id'] = $this->_id;
- $_SESSION['data'] = $data;
- $_SESSION['time'] = time();
- }
-
- /**
- * Gets the stored data from the session
- *
- * @return array
- */
- public function getData()
- {
- $this->_loadSession();
- return $this->_data;
- }
- /**
- * Gets the stored time from the session
- *
- * @return int
- */
- public function getTime()
- {
- $this->_loadSession();
- return $this->_time;
- }
- /**
- * Returns the string representation of the object
- *
- * @return string
- */
- public function __toString() {
- //$this->_loadSession();
- $out = "";
- $out .= "<pre>";
- $out .= "Session Object\n";
- $out .= "{\n";
- $out .= "\t[session_id] => ".$this->_id."\n";
- $out .= "\t[time] => ".date( DATE_RFC822, $this->_time )."\n";
- $out .= "\t[data] => ".print_r( $this->_data, true )."\n";
- $out .= "}\n";
- $out .= "</pre>";
- return $out;
- }
- }
- class Bee_Session_Native_Exception extends Bee_Session_Abstract_Exception
- {}