/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

  1. <?php
  2. /**
  3. * Colony
  4. * Copyright (c) Army of Bees (www.armyofbees.com)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * @category Colony
  25. * @package Bee
  26. * @copyright Copyright (c) Army of Bees (www.armyofbees.com)
  27. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  28. */
  29. /**
  30. * @see Bee_Session_Abstract
  31. */
  32. require_once 'Bee/Session/Abstract.php';
  33. /**
  34. * @see Bee_Db_Abstract
  35. */
  36. require_once 'Bee/Db/Abstract.php';
  37. /**
  38. * @see Bee_Input
  39. */
  40. require_once 'Bee/Input.php';
  41. /**
  42. * Session backend using the built-in PHP session handlers
  43. *
  44. * @category Colony
  45. * @package Bee
  46. * @copyright Copyright (c) Army of Bees (www.armyofbees.com)
  47. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  48. */
  49. class Bee_Session_Native
  50. {
  51. /**
  52. * Database object
  53. * @var Bee_Db_Abstract
  54. */
  55. private $_db = null;
  56. /**
  57. * Constructor for the class. Just sets some values for other functions.
  58. *
  59. * @param array $config The configuration to use for connection
  60. */
  61. public function __construct( &$config = array() )
  62. {
  63. // Verify that configuration is in an array.
  64. if( !is_array( $config ) )
  65. throw new Bee_Session_Native_Exception('Configuration must be in an array');
  66. // Verify that a db object is passed
  67. if( !isset( $config['db'] ) )
  68. throw new Bee_Session_Native_Exception('Configuration is missing db');
  69. if( !($config['db'] instanceof Bee_Db_Abstract) )
  70. throw new Bee_Session_Native_Exception('Configuration is not Bee_Db_Abstract');
  71. $this->_db =& $config['db'];
  72. $this->timeout = $config['session_timeout'];
  73. $this->domain = $config['session_domain'];
  74. $this->path = $config['session_path'];
  75. }
  76. /**
  77. * Loads the session from the database
  78. *
  79. * @return void
  80. */
  81. private function _loadSession()
  82. {
  83. // Check if we've got an existing session stored
  84. $input =& Bee_Input::filterInput();
  85. if ( !array_key_exists( 'session', $input ) )
  86. {
  87. $this->newSession();
  88. }
  89. else
  90. {
  91. session_start();
  92. $this->_id = $input['session'];
  93. if ( $_SESSION['session_id'] == $this->_id )
  94. {
  95. $this->_data = $_SESSION['data'];
  96. $this->_time = $_SESSION['time'];
  97. setcookie( 'session', $this->_id, time() + $this->timeout, $this->path, $this->domain, FALSE, TRUE );
  98. }
  99. else
  100. {
  101. $this->newSession();
  102. }
  103. }
  104. }
  105. /**
  106. * Creates a new empty session
  107. *
  108. * @return void
  109. */
  110. public function newSession()
  111. {
  112. $this->_id = sha1( uniqid( microtime() ) );
  113. $this->_time = time();
  114. $this->_data = array();
  115. setcookie( 'session', $this->_id, time() + $this->timeout, $this->path, $this->domain, FALSE, TRUE );
  116. session_start();
  117. $_SESSION['session_id'] = $this->_id;
  118. $_SESSION['data'] = array();
  119. $_SESSION['time'] = time();
  120. }
  121. /**
  122. * Saves the session into the database
  123. *
  124. * @param array $data The data to be stored into the session
  125. * @return void
  126. */
  127. public function saveSession( &$data )
  128. {
  129. $this->_data = $data;
  130. session_start();
  131. $_SESSION['session_id'] = $this->_id;
  132. $_SESSION['data'] = $data;
  133. $_SESSION['time'] = time();
  134. }
  135. /**
  136. * Gets the stored data from the session
  137. *
  138. * @return array
  139. */
  140. public function getData()
  141. {
  142. $this->_loadSession();
  143. return $this->_data;
  144. }
  145. /**
  146. * Gets the stored time from the session
  147. *
  148. * @return int
  149. */
  150. public function getTime()
  151. {
  152. $this->_loadSession();
  153. return $this->_time;
  154. }
  155. /**
  156. * Returns the string representation of the object
  157. *
  158. * @return string
  159. */
  160. public function __toString() {
  161. //$this->_loadSession();
  162. $out = "";
  163. $out .= "<pre>";
  164. $out .= "Session Object\n";
  165. $out .= "{\n";
  166. $out .= "\t[session_id] => ".$this->_id."\n";
  167. $out .= "\t[time] => ".date( DATE_RFC822, $this->_time )."\n";
  168. $out .= "\t[data] => ".print_r( $this->_data, true )."\n";
  169. $out .= "}\n";
  170. $out .= "</pre>";
  171. return $out;
  172. }
  173. }
  174. class Bee_Session_Native_Exception extends Bee_Session_Abstract_Exception
  175. {}