/classes/session.php

https://github.com/michael-lefebvre/core · PHP · 364 lines · 135 code · 56 blank · 173 comment · 8 complexity · f112808590aa521e811ca7293e14fdf8 MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Session Class
  15. *
  16. * @package Fuel
  17. * @category Core
  18. * @author Harro "WanWizard" Verton
  19. * @link http://fuelphp.com/docs/classes/session.html
  20. */
  21. class Session
  22. {
  23. /**
  24. * loaded session driver instance
  25. */
  26. protected static $_instance = null;
  27. /**
  28. * array of loaded instances
  29. */
  30. protected static $_instances = array();
  31. /**
  32. * array of global config defaults
  33. */
  34. protected static $_defaults = array(
  35. 'driver' => 'cookie',
  36. 'match_ip' => false,
  37. 'match_ua' => true,
  38. 'cookie_domain' => '',
  39. 'cookie_path' => '/',
  40. 'cookie_http_only' => null,
  41. 'expire_on_close' => false,
  42. 'expiration_time' => 7200,
  43. 'rotation_time' => 300,
  44. 'flash_id' => 'flash',
  45. 'flash_auto_expire' => true,
  46. 'post_cookie_name' => ''
  47. );
  48. // --------------------------------------------------------------------
  49. /**
  50. * Initialize by loading config & starting default session
  51. */
  52. public static function _init()
  53. {
  54. \Config::load('session', true);
  55. if (\Config::get('session.auto_initialize', true))
  56. {
  57. static::instance();
  58. }
  59. }
  60. // --------------------------------------------------------------------
  61. /**
  62. * This method is deprecated...use forge() instead.
  63. *
  64. * @deprecated until 1.2
  65. */
  66. public static function factory($custom = array())
  67. {
  68. logger(\Fuel::L_WARNING, 'This method is deprecated. Please use a forge() instead.', __METHOD__);
  69. return static::forge($custom);
  70. }
  71. /**
  72. * Factory
  73. *
  74. * Produces fully configured session driver instances
  75. *
  76. * @param array|string full driver config or just driver type
  77. */
  78. public static function forge($custom = array())
  79. {
  80. $config = \Config::get('session', array());
  81. // When a string was passed it's just the driver type
  82. if ( ! empty($custom) and ! is_array($custom))
  83. {
  84. $custom = array('driver' => $custom);
  85. }
  86. $config = array_merge(static::$_defaults, $config, $custom);
  87. if (empty($config['driver']))
  88. {
  89. throw new \Session_Exception('No session driver given or no default session driver set.');
  90. }
  91. // determine the driver to load
  92. $class = '\\Session_'.ucfirst($config['driver']);
  93. $driver = new $class($config);
  94. // get the driver's cookie name
  95. $cookie = $driver->get_config('cookie_name');
  96. // do we already have a driver instance for this cookie?
  97. if (isset(static::$_instances[$cookie]))
  98. {
  99. // if so, they must be using the same driver class!
  100. $class_instance = 'Fuel\\Core\\'.$class;
  101. if (static::$_instances[$cookie] instanceof $class_instance)
  102. {
  103. throw new \FuelException('You can not instantiate two different sessions using the same cookie name "'.$cookie.'"');
  104. }
  105. }
  106. else
  107. {
  108. // register a shutdown event to update the session
  109. \Event::register('shutdown', array($driver, 'write'));
  110. // init the session
  111. $driver->init();
  112. $driver->read();
  113. // store this instance
  114. static::$_instances[$cookie] =& $driver;
  115. }
  116. return static::$_instances[$cookie];
  117. }
  118. // --------------------------------------------------------------------
  119. /**
  120. * class constructor
  121. *
  122. * @param void
  123. * @access private
  124. * @return void
  125. */
  126. final private function __construct() {}
  127. // --------------------------------------------------------------------
  128. /**
  129. * create or return the driver instance
  130. *
  131. * @param void
  132. * @access public
  133. * @return Session_Driver object
  134. */
  135. public static function instance($instance = null)
  136. {
  137. if ($instance !== null)
  138. {
  139. if ( ! array_key_exists($instance, static::$_instances))
  140. {
  141. return false;
  142. }
  143. return static::$_instances[$instance];
  144. }
  145. if (static::$_instance === null)
  146. {
  147. static::$_instance = static::forge();
  148. }
  149. return static::$_instance;
  150. }
  151. // --------------------------------------------------------------------
  152. /**
  153. * set session variables
  154. *
  155. * @param string|array name of the variable to set or array of values, array(name => value)
  156. * @param mixed value
  157. * @access public
  158. * @return void
  159. */
  160. public static function set($name, $value = null)
  161. {
  162. return static::instance()->set($name, $value);
  163. }
  164. // --------------------------------------------------------------------
  165. /**
  166. * get session variables
  167. *
  168. * @access public
  169. * @param string name of the variable to get
  170. * @param mixed default value to return if the variable does not exist
  171. * @return mixed
  172. */
  173. public static function get($name = null, $default = null)
  174. {
  175. return static::instance()->get($name, $default);
  176. }
  177. // --------------------------------------------------------------------
  178. /**
  179. * delete a session variable
  180. *
  181. * @param string name of the variable to delete
  182. * @param mixed value
  183. * @access public
  184. * @return void
  185. */
  186. public static function delete($name)
  187. {
  188. return static::instance()->delete($name);
  189. }
  190. // --------------------------------------------------------------------
  191. /**
  192. * get session key variables
  193. *
  194. * @access public
  195. * @param string name of the variable to get, default is 'session_id'
  196. * @return mixed
  197. */
  198. public static function key($name = 'session_id')
  199. {
  200. return static::instance()->key($name);
  201. }
  202. // --------------------------------------------------------------------
  203. /**
  204. * set session flash variables
  205. *
  206. * @param string name of the variable to set
  207. * @param mixed value
  208. * @access public
  209. * @return void
  210. */
  211. public static function set_flash($name, $value = null)
  212. {
  213. return static::instance()->set_flash($name, $value);
  214. }
  215. // --------------------------------------------------------------------
  216. /**
  217. * get session flash variables
  218. *
  219. * @access public
  220. * @param string name of the variable to get
  221. * @param mixed default value to return if the variable does not exist
  222. * @return mixed
  223. */
  224. public static function get_flash($name = null, $default = null)
  225. {
  226. return static::instance()->get_flash($name, $default);
  227. }
  228. // --------------------------------------------------------------------
  229. /**
  230. * keep session flash variables
  231. *
  232. * @access public
  233. * @param string name of the variable to keep
  234. * @return void
  235. */
  236. public static function keep_flash($name = null)
  237. {
  238. return static::instance()->keep_flash($name);
  239. }
  240. // --------------------------------------------------------------------
  241. /**
  242. * delete session flash variables
  243. *
  244. * @param string name of the variable to delete
  245. * @param mixed value
  246. * @access public
  247. * @return void
  248. */
  249. public static function delete_flash($name = null)
  250. {
  251. return static::instance()->delete_flash($name);
  252. }
  253. // --------------------------------------------------------------------
  254. /**
  255. * create a new session
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. public static function create()
  261. {
  262. return static::instance()->create();
  263. }
  264. // --------------------------------------------------------------------
  265. /**
  266. * read the session
  267. *
  268. * @access public
  269. * @return void
  270. */
  271. public static function read()
  272. {
  273. return static::instance()->read();
  274. }
  275. // --------------------------------------------------------------------
  276. /**
  277. * write the session
  278. *
  279. * @access public
  280. * @return void
  281. */
  282. public static function write()
  283. {
  284. return static::instance()->write();
  285. }
  286. // --------------------------------------------------------------------
  287. /**
  288. * rotate the session id
  289. *
  290. * @access public
  291. * @return void
  292. */
  293. public static function rotate()
  294. {
  295. return static::instance()->rotate();
  296. }
  297. // --------------------------------------------------------------------
  298. /**
  299. * destroy the current session
  300. *
  301. * @access public
  302. * @return void
  303. */
  304. public static function destroy()
  305. {
  306. return static::instance()->destroy();
  307. }
  308. }