/frameworks/lithium/0.9.9/libraries/lithium/storage/session/adapter/Php.php

https://github.com/ggunlugu/ornekler · PHP · 241 lines · 119 code · 22 blank · 100 comment · 20 complexity · bb59ddb9afaaa40c61dab8afa1116483 MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\storage\session\adapter;
  9. use lithium\util\Set;
  10. use RuntimeException;
  11. use lithium\core\ConfigException;
  12. /**
  13. * A minimal adapter to interface with native PHP sessions.
  14. *
  15. * This adapter provides basic support for `write`, `read` and `delete`
  16. * session handling, as well as allowing these three methods to be filtered as
  17. * per the Lithium filtering system.
  18. *
  19. */
  20. class Php extends \lithium\core\Object {
  21. /**
  22. * Default ini settings for this session adapter.
  23. *
  24. * @var array Keys are session ini settings, but without the `session.` namespace.
  25. */
  26. protected $_defaults = array(
  27. 'session.cookie_lifetime' => '0',
  28. );
  29. /**
  30. * Class constructor.
  31. *
  32. * Takes care of setting appropriate configurations for this object.
  33. *
  34. * @param array $config Unified constructor configuration parameters. You can set
  35. * the `session.*` PHP ini settings here as key/value pairs.
  36. * @return void
  37. */
  38. public function __construct(array $config = array()) {
  39. parent::__construct($config + $this->_defaults);
  40. }
  41. /**
  42. * Initialization of the session.
  43. *
  44. * @todo Split up into an _initialize() and a _start().
  45. * @return void
  46. */
  47. protected function _init() {
  48. $config = $this->_config;
  49. unset($config['adapter'], $config['strategies'], $config['filters'], $config['init']);
  50. if (!isset($config['session.name'])) {
  51. $config['session.name'] = basename(LITHIUM_APP_PATH);
  52. }
  53. foreach ($config as $key => $value) {
  54. if (strpos($key, 'session.') === false) {
  55. continue;
  56. }
  57. if (ini_set($key, $value) === false) {
  58. throw new ConfigException("Could not initialize the session.");
  59. }
  60. }
  61. }
  62. /**
  63. * Starts the session.
  64. *
  65. * @return boolean True if session successfully started (or has already been started),
  66. * false otherwise.
  67. */
  68. protected static function _start() {
  69. if (session_id()) {
  70. return true;
  71. }
  72. if (!isset($_SESSION)) {
  73. session_cache_limiter('nocache');
  74. }
  75. return session_start();
  76. }
  77. /**
  78. * Obtain the status of the session.
  79. *
  80. * @return boolean True if $_SESSION is accessible and if a '_timestamp' key
  81. * has been set, false otherwise.
  82. */
  83. public static function isStarted() {
  84. return (boolean) session_id();
  85. }
  86. /**
  87. * Obtain the session id.
  88. *
  89. * @return mixed Session id, or null if the session has not been started.
  90. */
  91. public static function key() {
  92. return session_id() ?: null;
  93. }
  94. /**
  95. * Checks if a value has been set in the session.
  96. *
  97. * @param string $key Key of the entry to be checked.
  98. * @param array $options Options array. Not used for this adapter method.
  99. * @return boolean True if the key exists, false otherwise.
  100. */
  101. public static function check($key, array $options = array()) {
  102. if (!static::isStarted() && !static::_start()) {
  103. throw new RuntimeException("Could not start session.");
  104. }
  105. return function($self, $params, $chain) {
  106. return Set::check($_SESSION, $params['key']);
  107. };
  108. }
  109. /**
  110. * Read a value from the session.
  111. *
  112. * @param null|string $key Key of the entry to be read. If no key is passed, all
  113. * current session data is returned.
  114. * @param array $options Options array. Not used for this adapter method.
  115. * @return mixed Data in the session if successful, false otherwise.
  116. */
  117. public static function read($key = null, array $options = array()) {
  118. if (!static::isStarted() && !static::_start()) {
  119. throw new RuntimeException("Could not start session.");
  120. }
  121. return function($self, $params, $chain) {
  122. $key = $params['key'];
  123. if (!$key) {
  124. return $_SESSION;
  125. }
  126. if (strpos($key, '.') === false) {
  127. return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
  128. }
  129. $filter = function($keys, $data) use (&$filter) {
  130. $key = array_shift($keys);
  131. if (isset($data[$key])) {
  132. return (empty($keys)) ? $data[$key] : $filter($keys, $data[$key]);
  133. }
  134. };
  135. return $filter(explode('.', $key), $_SESSION);
  136. };
  137. }
  138. /**
  139. * Write a value to the session.
  140. *
  141. * @param string $key Key of the item to be stored.
  142. * @param mixed $value The value to be stored.
  143. * @param array $options Options array. Not used for this adapter method.
  144. * @return boolean True on successful write, false otherwise
  145. */
  146. public static function write($key, $value, array $options = array()) {
  147. if (!static::isStarted() && !static::_start()) {
  148. throw new RuntimeException("Could not start session.");
  149. }
  150. $class = __CLASS__;
  151. return function($self, $params, $chain) use ($class) {
  152. return $class::overwrite(
  153. $_SESSION, Set::insert($_SESSION, $params['key'], $params['value'])
  154. );
  155. };
  156. }
  157. /**
  158. * Delete value from the session
  159. *
  160. * @param string $key The key to be deleted
  161. * @param array $options Options array. Not used for this adapter method.
  162. * @return boolean True if the key no longer exists in the session, false otherwise
  163. */
  164. public static function delete($key, array $options = array()) {
  165. if (!static::isStarted() && !static::_start()) {
  166. throw new RuntimeException("Could not start session.");
  167. }
  168. $class = __CLASS__;
  169. return function($self, $params, $chain) use ($class) {
  170. $key = $params['key'];
  171. $class::overwrite($_SESSION, Set::remove($_SESSION, $key));
  172. return !Set::check($_SESSION, $key);
  173. };
  174. }
  175. /**
  176. * Clears all keys from the session.
  177. *
  178. * @param array $options Options array. Not used fro this adapter method.
  179. * @return boolean True on successful clear, false otherwise.
  180. */
  181. public function clear(array $options = array()) {
  182. if (!static::isStarted() && !static::_start()) {
  183. throw new RuntimeException("Could not start session.");
  184. }
  185. return function($self, $params, $chain) {
  186. return session_destroy();
  187. };
  188. }
  189. /**
  190. * Determines if PHP sessions are enabled.
  191. *
  192. * @return boolean True if enabled (that is, if session_id() returns a value), false otherwise.
  193. */
  194. public static function enabled() {
  195. return (boolean) session_id();
  196. }
  197. /**
  198. * Overwrites session keys and values.
  199. *
  200. * @param array $old Reference to the array that needs to be overwritten. Will usually
  201. * be `$_SESSION`.
  202. * @param array $new The data that should overwrite the keys/values in `$old`.
  203. * @return true Success
  204. */
  205. public static function overwrite(&$old, $new) {
  206. if (!empty($old)) {
  207. foreach ($old as $key => $value) {
  208. if (!isset($new[$key])) {
  209. unset($old[$key]);
  210. }
  211. }
  212. }
  213. foreach ($new as $key => $value) {
  214. $old[$key] = $value;
  215. }
  216. return true;
  217. }
  218. }
  219. ?>