PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/rxwandc/system/classes/kohana/session.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 462 lines | 175 code | 55 blank | 232 comment | 14 complexity | 3e126ee0122f04ec2a6c7edf96bacc4c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Base session class.
  4. *
  5. * @package Kohana
  6. * @category Session
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2012 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. abstract class Kohana_Session {
  12. /**
  13. * @var string default session adapter
  14. */
  15. public static $default = 'native';
  16. /**
  17. * @var array session instances
  18. */
  19. public static $instances = array();
  20. /**
  21. * Creates a singleton session of the given type. Some session types
  22. * (native, database) also support restarting a session by passing a
  23. * session id as the second parameter.
  24. *
  25. * $session = Session::instance();
  26. *
  27. * [!!] [Session::write] will automatically be called when the request ends.
  28. *
  29. * @param string $type type of session (native, cookie, etc)
  30. * @param string $id session identifier
  31. * @return Session
  32. * @uses Kohana::$config
  33. */
  34. public static function instance($type = NULL, $id = NULL)
  35. {
  36. if ($type === NULL)
  37. {
  38. // Use the default type
  39. $type = Session::$default;
  40. }
  41. if ( ! isset(Session::$instances[$type]))
  42. {
  43. // Load the configuration for this type
  44. $config = Kohana::$config->load('session')->get($type);
  45. // Set the session class name
  46. $class = 'Session_'.ucfirst($type);
  47. // Create a new session instance
  48. Session::$instances[$type] = $session = new $class($config, $id);
  49. // Write the session at shutdown
  50. register_shutdown_function(array($session, 'write'));
  51. }
  52. return Session::$instances[$type];
  53. }
  54. /**
  55. * @var string cookie name
  56. */
  57. protected $_name = 'session';
  58. /**
  59. * @var int cookie lifetime
  60. */
  61. protected $_lifetime = 0;
  62. /**
  63. * @var bool encrypt session data?
  64. */
  65. protected $_encrypted = FALSE;
  66. /**
  67. * @var array session data
  68. */
  69. protected $_data = array();
  70. /**
  71. * @var bool session destroyed?
  72. */
  73. protected $_destroyed = FALSE;
  74. /**
  75. * Overloads the name, lifetime, and encrypted session settings.
  76. *
  77. * [!!] Sessions can only be created using the [Session::instance] method.
  78. *
  79. * @param array $config configuration
  80. * @param string $id session id
  81. * @return void
  82. * @uses Session::read
  83. */
  84. public function __construct(array $config = NULL, $id = NULL)
  85. {
  86. if (isset($config['name']))
  87. {
  88. // Cookie name to store the session id in
  89. $this->_name = (string) $config['name'];
  90. }
  91. if (isset($config['lifetime']))
  92. {
  93. // Cookie lifetime
  94. $this->_lifetime = (int) $config['lifetime'];
  95. }
  96. if (isset($config['encrypted']))
  97. {
  98. if ($config['encrypted'] === TRUE)
  99. {
  100. // Use the default Encrypt instance
  101. $config['encrypted'] = 'default';
  102. }
  103. // Enable or disable encryption of data
  104. $this->_encrypted = $config['encrypted'];
  105. }
  106. // Load the session
  107. $this->read($id);
  108. }
  109. /**
  110. * Session object is rendered to a serialized string. If encryption is
  111. * enabled, the session will be encrypted. If not, the output string will
  112. * be encoded using [base64_encode].
  113. *
  114. * echo $session;
  115. *
  116. * @return string
  117. * @uses Encrypt::encode
  118. */
  119. public function __toString()
  120. {
  121. // Serialize the data array
  122. $data = serialize($this->_data);
  123. if ($this->_encrypted)
  124. {
  125. // Encrypt the data using the default key
  126. $data = Encrypt::instance($this->_encrypted)->encode($data);
  127. }
  128. else
  129. {
  130. // Obfuscate the data with base64 encoding
  131. $data = base64_encode($data);
  132. }
  133. return $data;
  134. }
  135. /**
  136. * Returns the current session array. The returned array can also be
  137. * assigned by reference.
  138. *
  139. * // Get a copy of the current session data
  140. * $data = $session->as_array();
  141. *
  142. * // Assign by reference for modification
  143. * $data =& $session->as_array();
  144. *
  145. * @return array
  146. */
  147. public function & as_array()
  148. {
  149. return $this->_data;
  150. }
  151. /**
  152. * Get the current session id, if the session supports it.
  153. *
  154. * $id = $session->id();
  155. *
  156. * [!!] Not all session types have ids.
  157. *
  158. * @return string
  159. * @since 3.0.8
  160. */
  161. public function id()
  162. {
  163. return NULL;
  164. }
  165. /**
  166. * Get the current session cookie name.
  167. *
  168. * $name = $session->name();
  169. *
  170. * @return string
  171. * @since 3.0.8
  172. */
  173. public function name()
  174. {
  175. return $this->_name;
  176. }
  177. /**
  178. * Get a variable from the session array.
  179. *
  180. * $foo = $session->get('foo');
  181. *
  182. * @param string $key variable name
  183. * @param mixed $default default value to return
  184. * @return mixed
  185. */
  186. public function get($key, $default = NULL)
  187. {
  188. return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
  189. }
  190. /**
  191. * Get and delete a variable from the session array.
  192. *
  193. * $bar = $session->get_once('bar');
  194. *
  195. * @param string $key variable name
  196. * @param mixed $default default value to return
  197. * @return mixed
  198. */
  199. public function get_once($key, $default = NULL)
  200. {
  201. $value = $this->get($key, $default);
  202. unset($this->_data[$key]);
  203. return $value;
  204. }
  205. /**
  206. * Set a variable in the session array.
  207. *
  208. * $session->set('foo', 'bar');
  209. *
  210. * @param string $key variable name
  211. * @param mixed $value value
  212. * @return $this
  213. */
  214. public function set($key, $value)
  215. {
  216. $this->_data[$key] = $value;
  217. return $this;
  218. }
  219. /**
  220. * Set a variable by reference.
  221. *
  222. * $session->bind('foo', $foo);
  223. *
  224. * @param string $key variable name
  225. * @param mixed $value referenced value
  226. * @return $this
  227. */
  228. public function bind($key, & $value)
  229. {
  230. $this->_data[$key] =& $value;
  231. return $this;
  232. }
  233. /**
  234. * Removes a variable in the session array.
  235. *
  236. * $session->delete('foo');
  237. *
  238. * @param string $key,... variable name
  239. * @return $this
  240. */
  241. public function delete($key)
  242. {
  243. $args = func_get_args();
  244. foreach ($args as $key)
  245. {
  246. unset($this->_data[$key]);
  247. }
  248. return $this;
  249. }
  250. /**
  251. * Loads existing session data.
  252. *
  253. * $session->read();
  254. *
  255. * @param string $id session id
  256. * @return void
  257. */
  258. public function read($id = NULL)
  259. {
  260. $data = NULL;
  261. try
  262. {
  263. if (is_string($data = $this->_read($id)))
  264. {
  265. if ($this->_encrypted)
  266. {
  267. // Decrypt the data using the default key
  268. $data = Encrypt::instance($this->_encrypted)->decode($data);
  269. }
  270. else
  271. {
  272. // Decode the base64 encoded data
  273. $data = base64_decode($data);
  274. }
  275. // Unserialize the data
  276. $data = unserialize($data);
  277. }
  278. else
  279. {
  280. // Ignore these, session is valid, likely no data though.
  281. }
  282. }
  283. catch (Exception $e)
  284. {
  285. // Error reading the session, usually
  286. // a corrupt session.
  287. throw new Session_Exception('Error reading session data.', NULL, Session_Exception::SESSION_CORRUPT);
  288. }
  289. if (is_array($data))
  290. {
  291. // Load the data locally
  292. $this->_data = $data;
  293. }
  294. }
  295. /**
  296. * Generates a new session id and returns it.
  297. *
  298. * $id = $session->regenerate();
  299. *
  300. * @return string
  301. */
  302. public function regenerate()
  303. {
  304. return $this->_regenerate();
  305. }
  306. /**
  307. * Sets the last_active timestamp and saves the session.
  308. *
  309. * $session->write();
  310. *
  311. * [!!] Any errors that occur during session writing will be logged,
  312. * but not displayed, because sessions are written after output has
  313. * been sent.
  314. *
  315. * @return boolean
  316. * @uses Kohana::$log
  317. */
  318. public function write()
  319. {
  320. if (headers_sent() OR $this->_destroyed)
  321. {
  322. // Session cannot be written when the headers are sent or when
  323. // the session has been destroyed
  324. return FALSE;
  325. }
  326. // Set the last active timestamp
  327. $this->_data['last_active'] = time();
  328. try
  329. {
  330. return $this->_write();
  331. }
  332. catch (Exception $e)
  333. {
  334. // Log & ignore all errors when a write fails
  335. Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e))->write();
  336. return FALSE;
  337. }
  338. }
  339. /**
  340. * Completely destroy the current session.
  341. *
  342. * $success = $session->destroy();
  343. *
  344. * @return boolean
  345. */
  346. public function destroy()
  347. {
  348. if ($this->_destroyed === FALSE)
  349. {
  350. if ($this->_destroyed = $this->_destroy())
  351. {
  352. // The session has been destroyed, clear all data
  353. $this->_data = array();
  354. }
  355. }
  356. return $this->_destroyed;
  357. }
  358. /**
  359. * Restart the session.
  360. *
  361. * $success = $session->restart();
  362. *
  363. * @return boolean
  364. */
  365. public function restart()
  366. {
  367. if ($this->_destroyed === FALSE)
  368. {
  369. // Wipe out the current session.
  370. $this->destroy();
  371. }
  372. // Allow the new session to be saved
  373. $this->_destroyed = FALSE;
  374. return $this->_restart();
  375. }
  376. /**
  377. * Loads the raw session data string and returns it.
  378. *
  379. * @param string $id session id
  380. * @return string
  381. */
  382. abstract protected function _read($id = NULL);
  383. /**
  384. * Generate a new session id and return it.
  385. *
  386. * @return string
  387. */
  388. abstract protected function _regenerate();
  389. /**
  390. * Writes the current session.
  391. *
  392. * @return boolean
  393. */
  394. abstract protected function _write();
  395. /**
  396. * Destroys the current session.
  397. *
  398. * @return boolean
  399. */
  400. abstract protected function _destroy();
  401. /**
  402. * Restarts the current session.
  403. *
  404. * @return boolean
  405. */
  406. abstract protected function _restart();
  407. } // End Session