Session.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 8EA+/uBGYPSnOrrVYH+ZcSO0hheeDEEXbE7r4V/0Uyw=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2016 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.7
  11. */
  12. /**
  13. * sessions handler class
  14. */
  15. namespace Cube;
  16. use Cube\Controller\Request;
  17. class Session
  18. {
  19. const COOKIE_DAYS = 30;
  20. /**
  21. *
  22. * session variables namespace/prefix
  23. *
  24. * @var string
  25. */
  26. protected $_namespace = null;
  27. /**
  28. *
  29. * cookie secret string, used for encrypting cookie values
  30. *
  31. * @var string
  32. */
  33. protected $_secret;
  34. /**
  35. *
  36. * class constructor
  37. *
  38. * @param array $options options array set for initializing the object
  39. */
  40. public function __construct($options = array())
  41. {
  42. $this->_namespace = isset($options['namespace']) ? $options['namespace'] : null;
  43. $this->_secret = isset($options['secret']) ? $options['secret'] : null;
  44. $this->start();
  45. }
  46. /**
  47. *
  48. * get the namespace of the session object
  49. *
  50. * @return string
  51. */
  52. public function getNamespace()
  53. {
  54. return $this->_namespace;
  55. }
  56. /**
  57. *
  58. * set the namespace of the session object
  59. *
  60. * @param string $namespace
  61. * @return \Cube\Session
  62. */
  63. public function setNamespace($namespace)
  64. {
  65. $this->_namespace = (string)$namespace;
  66. return $this;
  67. }
  68. /**
  69. *
  70. * get cookie secret string
  71. *
  72. * @return string
  73. */
  74. public function getSecret()
  75. {
  76. return $this->_secret;
  77. }
  78. /**
  79. *
  80. * set cookie secret string
  81. *
  82. * @param string $secret
  83. * @return \Cube\Session
  84. */
  85. public function setSecret($secret)
  86. {
  87. $this->_secret = (string)$secret;
  88. return $this;
  89. }
  90. /**
  91. *
  92. * get the selected session variable
  93. *
  94. * @param string $name
  95. * @return mixed|null return variable or null if variable is not set
  96. */
  97. public function get($name)
  98. {
  99. if (isset($_SESSION[$this->_namespace][$name])) {
  100. return $_SESSION[$this->_namespace][$name];
  101. }
  102. return null;
  103. }
  104. /**
  105. *
  106. * set a new session variable
  107. *
  108. * @param string $name
  109. * @param mixed $value
  110. * @return \Cube\Session
  111. */
  112. public function set($name, $value)
  113. {
  114. $_SESSION[$this->_namespace][$name] = $value;
  115. return $this;
  116. }
  117. /**
  118. *
  119. * get magic method, proxy to $this->get($name)
  120. *
  121. * @param string $name
  122. * @return string|null
  123. */
  124. public function __get($name)
  125. {
  126. return $this->get($name);
  127. }
  128. /**
  129. *
  130. * set magic method, proxy for $this->set($name, $value) method
  131. *
  132. * @param string $name
  133. * @param string $value
  134. */
  135. public function __set($name, $value)
  136. {
  137. $this->set($name, $value);
  138. }
  139. /**
  140. *
  141. * delete a session variable
  142. *
  143. * @param string $name
  144. * @return \Cube\Session
  145. */
  146. public function unregister($name)
  147. {
  148. if (isset($_SESSION[$this->_namespace][$name])) {
  149. unset($_SESSION[$this->_namespace][$name]);
  150. }
  151. return $this;
  152. }
  153. /**
  154. *
  155. * get the readable value of a cookie or null if the cookie doesnt exist
  156. *
  157. * @param string $key
  158. * @return string|null
  159. */
  160. public function getCookie($key)
  161. {
  162. $key = $this->getCookieKey($key);
  163. if (isset($_COOKIE[$key])) {
  164. $crypt = new Crypt();
  165. $crypt->setKey($this->_secret);
  166. return $crypt->decrypt($_COOKIE[$key]);
  167. }
  168. return null;
  169. }
  170. /**
  171. *
  172. * create a cookie
  173. * 1.7 - save cookie as superglobal too, so that it can be used immediately
  174. *
  175. * @param string $key cookie name
  176. * @param string $value cookie value
  177. * @param string $path cookie path
  178. * @return \Cube\Session
  179. */
  180. public function setCookie($key, $value, $path = null)
  181. {
  182. $key = $this->getCookieKey($key);
  183. $value = $this->setCookieValue($value);
  184. $expirationDate = time() + self::COOKIE_DAYS * 24 * 60 * 60;
  185. if ($path == null) {
  186. $request = new Request();
  187. $path = $request->getBaseUrl(true);
  188. }
  189. setcookie($key, $value, $expirationDate, $path);
  190. $_COOKIE[$key] = $value;
  191. return $this;
  192. }
  193. /**
  194. *
  195. * return the set name of a cookie variable (namespace + name)
  196. *
  197. * @param string $key
  198. * @return string
  199. */
  200. public function getCookieKey($key)
  201. {
  202. return $this->_namespace . $key;
  203. }
  204. /**
  205. *
  206. * set the cookie value
  207. *
  208. * @param string $value
  209. * @return string
  210. */
  211. public function setCookieValue($value)
  212. {
  213. $crypt = new Crypt();
  214. $crypt->setKey($this->_secret);
  215. return $crypt->encrypt($value);
  216. }
  217. /**
  218. *
  219. * delete a cookie
  220. *
  221. * @param string $name cookie name
  222. * @param string $path cookie path
  223. * @return \Cube\Session
  224. */
  225. public function unsetCookie($name, $path = null)
  226. {
  227. if ($path === null) {
  228. $request = new Request();
  229. $path = $request->getBaseUrl();
  230. }
  231. setcookie($this->getCookieKey($name), null, -1, $path);
  232. return $this;
  233. }
  234. /**
  235. *
  236. * start session
  237. */
  238. public function start()
  239. {
  240. if (session_id() == '') {
  241. session_start();
  242. }
  243. }
  244. /**
  245. *
  246. * destroy the current session
  247. */
  248. public function destroy()
  249. {
  250. session_destroy();
  251. }
  252. }