Session.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Protocols\Http;
  15. use Workerman\Protocols\Http\Session\SessionHandlerInterface;
  16. /**
  17. * Class Session
  18. * @package Workerman\Protocols\Http
  19. */
  20. class Session
  21. {
  22. /**
  23. * Session andler class which implements SessionHandlerInterface.
  24. *
  25. * @var string
  26. */
  27. protected static $_handlerClass = 'Workerman\Protocols\Http\Session\FileSessionHandler';
  28. /**
  29. * Parameters of __constructor for session handler class.
  30. *
  31. * @var null
  32. */
  33. protected static $_handlerConfig = null;
  34. /**
  35. * Session name.
  36. *
  37. * @var string
  38. */
  39. public static $name = 'PHPSID';
  40. /**
  41. * Auto update timestamp.
  42. *
  43. * @var bool
  44. */
  45. public static $autoUpdateTimestamp = false;
  46. /**
  47. * Session lifetime.
  48. *
  49. * @var int
  50. */
  51. public static $lifetime = 1440;
  52. /**
  53. * Cookie lifetime.
  54. *
  55. * @var int
  56. */
  57. public static $cookieLifetime = 1440;
  58. /**
  59. * Session cookie path.
  60. *
  61. * @var string
  62. */
  63. public static $cookiePath = '/';
  64. /**
  65. * Session cookie domain.
  66. *
  67. * @var string
  68. */
  69. public static $domain = '';
  70. /**
  71. * HTTPS only cookies.
  72. *
  73. * @var bool
  74. */
  75. public static $secure = false;
  76. /**
  77. * HTTP access only.
  78. *
  79. * @var bool
  80. */
  81. public static $httpOnly = true;
  82. /**
  83. * Same-site cookies.
  84. *
  85. * @var string
  86. */
  87. public static $sameSite = '';
  88. /**
  89. * Gc probability.
  90. *
  91. * @var int[]
  92. */
  93. public static $gcProbability = [1, 1000];
  94. /**
  95. * Session handler instance.
  96. *
  97. * @var SessionHandlerInterface
  98. */
  99. protected static $_handler = null;
  100. /**
  101. * Session data.
  102. *
  103. * @var array
  104. */
  105. protected $_data = [];
  106. /**
  107. * Session changed and need to save.
  108. *
  109. * @var bool
  110. */
  111. protected $_needSave = false;
  112. /**
  113. * Session id.
  114. *
  115. * @var null
  116. */
  117. protected $_sessionId = null;
  118. /**
  119. * Session constructor.
  120. *
  121. * @param string $session_id
  122. */
  123. public function __construct($session_id)
  124. {
  125. static::checkSessionId($session_id);
  126. if (static::$_handler === null) {
  127. static::initHandler();
  128. }
  129. $this->_sessionId = $session_id;
  130. if ($data = static::$_handler->read($session_id)) {
  131. $this->_data = \unserialize($data);
  132. }
  133. }
  134. /**
  135. * Get session id.
  136. *
  137. * @return string
  138. */
  139. public function getId()
  140. {
  141. return $this->_sessionId;
  142. }
  143. /**
  144. * Get session.
  145. *
  146. * @param string $name
  147. * @param mixed|null $default
  148. * @return mixed|null
  149. */
  150. public function get($name, $default = null)
  151. {
  152. return isset($this->_data[$name]) ? $this->_data[$name] : $default;
  153. }
  154. /**
  155. * Store data in the session.
  156. *
  157. * @param string $name
  158. * @param mixed $value
  159. */
  160. public function set($name, $value)
  161. {
  162. $this->_data[$name] = $value;
  163. $this->_needSave = true;
  164. }
  165. /**
  166. * Delete an item from the session.
  167. *
  168. * @param string $name
  169. */
  170. public function delete($name)
  171. {
  172. unset($this->_data[$name]);
  173. $this->_needSave = true;
  174. }
  175. /**
  176. * Retrieve and delete an item from the session.
  177. *
  178. * @param string $name
  179. * @param mixed|null $default
  180. * @return mixed|null
  181. */
  182. public function pull($name, $default = null)
  183. {
  184. $value = $this->get($name, $default);
  185. $this->delete($name);
  186. return $value;
  187. }
  188. /**
  189. * Store data in the session.
  190. *
  191. * @param string|array $key
  192. * @param mixed|null $value
  193. */
  194. public function put($key, $value = null)
  195. {
  196. if (!\is_array($key)) {
  197. $this->set($key, $value);
  198. return;
  199. }
  200. foreach ($key as $k => $v) {
  201. $this->_data[$k] = $v;
  202. }
  203. $this->_needSave = true;
  204. }
  205. /**
  206. * Remove a piece of data from the session.
  207. *
  208. * @param string $name
  209. */
  210. public function forget($name)
  211. {
  212. if (\is_scalar($name)) {
  213. $this->delete($name);
  214. return;
  215. }
  216. if (\is_array($name)) {
  217. foreach ($name as $key) {
  218. unset($this->_data[$key]);
  219. }
  220. }
  221. $this->_needSave = true;
  222. }
  223. /**
  224. * Retrieve all the data in the session.
  225. *
  226. * @return array
  227. */
  228. public function all()
  229. {
  230. return $this->_data;
  231. }
  232. /**
  233. * Remove all data from the session.
  234. *
  235. * @return void
  236. */
  237. public function flush()
  238. {
  239. $this->_needSave = true;
  240. $this->_data = [];
  241. }
  242. /**
  243. * Determining If An Item Exists In The Session.
  244. *
  245. * @param string $name
  246. * @return bool
  247. */
  248. public function has($name)
  249. {
  250. return isset($this->_data[$name]);
  251. }
  252. /**
  253. * To determine if an item is present in the session, even if its value is null.
  254. *
  255. * @param string $name
  256. * @return bool
  257. */
  258. public function exists($name)
  259. {
  260. return \array_key_exists($name, $this->_data);
  261. }
  262. /**
  263. * Save session to store.
  264. *
  265. * @return void
  266. */
  267. public function save()
  268. {
  269. if ($this->_needSave) {
  270. if (empty($this->_data)) {
  271. static::$_handler->destroy($this->_sessionId);
  272. } else {
  273. static::$_handler->write($this->_sessionId, \serialize($this->_data));
  274. }
  275. } elseif (static::$autoUpdateTimestamp) {
  276. static::refresh();
  277. }
  278. $this->_needSave = false;
  279. }
  280. /**
  281. * Refresh session expire time.
  282. *
  283. * @return bool
  284. */
  285. public function refresh()
  286. {
  287. static::$_handler->updateTimestamp($this->getId());
  288. }
  289. /**
  290. * Init.
  291. *
  292. * @return void
  293. */
  294. public static function init()
  295. {
  296. if (($gc_probability = (int)\ini_get('session.gc_probability')) && ($gc_divisor = (int)\ini_get('session.gc_divisor'))) {
  297. static::$gcProbability = [$gc_probability, $gc_divisor];
  298. }
  299. if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
  300. self::$lifetime = (int)$gc_max_life_time;
  301. }
  302. $session_cookie_params = \session_get_cookie_params();
  303. static::$cookieLifetime = $session_cookie_params['lifetime'];
  304. static::$cookiePath = $session_cookie_params['path'];
  305. static::$domain = $session_cookie_params['domain'];
  306. static::$secure = $session_cookie_params['secure'];
  307. static::$httpOnly = $session_cookie_params['httponly'];
  308. }
  309. /**
  310. * Set session handler class.
  311. *
  312. * @param mixed|null $class_name
  313. * @param mixed|null $config
  314. * @return string
  315. */
  316. public static function handlerClass($class_name = null, $config = null)
  317. {
  318. if ($class_name) {
  319. static::$_handlerClass = $class_name;
  320. }
  321. if ($config) {
  322. static::$_handlerConfig = $config;
  323. }
  324. return static::$_handlerClass;
  325. }
  326. /**
  327. * Get cookie params.
  328. *
  329. * @return array
  330. */
  331. public static function getCookieParams()
  332. {
  333. return [
  334. 'lifetime' => static::$cookieLifetime,
  335. 'path' => static::$cookiePath,
  336. 'domain' => static::$domain,
  337. 'secure' => static::$secure,
  338. 'httponly' => static::$httpOnly,
  339. 'samesite' => static::$sameSite,
  340. ];
  341. }
  342. /**
  343. * Init handler.
  344. *
  345. * @return void
  346. */
  347. protected static function initHandler()
  348. {
  349. if (static::$_handlerConfig === null) {
  350. static::$_handler = new static::$_handlerClass();
  351. } else {
  352. static::$_handler = new static::$_handlerClass(static::$_handlerConfig);
  353. }
  354. }
  355. /**
  356. * GC sessions.
  357. *
  358. * @return void
  359. */
  360. public function gc()
  361. {
  362. static::$_handler->gc(static::$lifetime);
  363. }
  364. /**
  365. * __destruct.
  366. *
  367. * @return void
  368. */
  369. public function __destruct()
  370. {
  371. $this->save();
  372. if (\random_int(1, static::$gcProbability[1]) <= static::$gcProbability[0]) {
  373. $this->gc();
  374. }
  375. }
  376. /**
  377. * Check session id.
  378. *
  379. * @param string $session_id
  380. */
  381. protected static function checkSessionId($session_id)
  382. {
  383. if (!\preg_match('/^[a-zA-Z0-9]+$/', $session_id)) {
  384. throw new SessionException("session_id $session_id is invalid");
  385. }
  386. }
  387. }
  388. /**
  389. * Class SessionException
  390. * @package Workerman\Protocols\Http
  391. */
  392. class SessionException extends \RuntimeException
  393. {
  394. }
  395. // Init session.
  396. Session::init();