session.php 7.7 KB

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