Session.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * The Minz_Session class handles user’s session
  4. */
  5. class Minz_Session {
  6. /**
  7. * @var bool
  8. */
  9. private static $volatile = false;
  10. /**
  11. * For mutual exclusion.
  12. * @var bool
  13. */
  14. private static $locked = false;
  15. public static function lock(): bool {
  16. if (!self::$volatile && !self::$locked) {
  17. session_start();
  18. self::$locked = true;
  19. }
  20. return self::$locked;
  21. }
  22. public static function unlock(): bool {
  23. if (!self::$volatile) {
  24. session_write_close();
  25. self::$locked = false;
  26. }
  27. return self::$locked;
  28. }
  29. /**
  30. * Initialize the session, with a name
  31. * The session name is used as the name for cookies and URLs (i.e. PHPSESSID).
  32. * It should contain only alphanumeric characters; it should be short and descriptive
  33. * If the volatile parameter is true, then no cookie and not session storage are used.
  34. * Volatile is especially useful for API calls without cookie / Web session.
  35. */
  36. public static function init(string $name, bool $volatile = false): void {
  37. self::$volatile = $volatile;
  38. if (self::$volatile) {
  39. $_SESSION = [];
  40. return;
  41. }
  42. $cookie = session_get_cookie_params();
  43. self::keepCookie($cookie['lifetime']);
  44. // start session
  45. session_name($name);
  46. //When using cookies (default value), session_stars() sends HTTP headers
  47. session_start();
  48. session_write_close();
  49. //Use cookie only the first time the session is started to avoid resending HTTP headers
  50. ini_set('session.use_cookies', '0');
  51. }
  52. /**
  53. * Allows you to retrieve a session variable
  54. * @param string $p the parameter to retrieve
  55. * @param mixed|false $default the default value if the parameter doesn't exist
  56. * @return mixed|false the value of the session variable, false if doesn't exist
  57. */
  58. public static function param(string $p, $default = false) {
  59. return $_SESSION[$p] ?? $default;
  60. }
  61. /**
  62. * Allows you to create or update a session variable
  63. * @param string $parameter the parameter to create or modify
  64. * @param mixed|false $value the value to assign, false to delete
  65. */
  66. public static function _param(string $parameter, $value = false): void {
  67. if (!self::$volatile && !self::$locked) {
  68. session_start();
  69. }
  70. if ($value === false) {
  71. unset($_SESSION[$parameter]);
  72. } else {
  73. $_SESSION[$parameter] = $value;
  74. }
  75. if (!self::$volatile && !self::$locked) {
  76. session_write_close();
  77. }
  78. }
  79. /**
  80. * @param array<string,string|bool|int|array<string>> $keyValues
  81. */
  82. public static function _params(array $keyValues): void {
  83. if (!self::$volatile && !self::$locked) {
  84. session_start();
  85. }
  86. foreach ($keyValues as $key => $value) {
  87. if ($value === false) {
  88. unset($_SESSION[$key]);
  89. } else {
  90. $_SESSION[$key] = $value;
  91. }
  92. }
  93. if (!self::$volatile && !self::$locked) {
  94. session_write_close();
  95. }
  96. }
  97. /**
  98. * Allows to delete a session
  99. * @param bool $force if false, does not clear the language parameter
  100. */
  101. public static function unset_session(bool $force = false): void {
  102. $language = self::param('language');
  103. if (!self::$volatile) {
  104. session_destroy();
  105. }
  106. $_SESSION = array();
  107. if (!$force) {
  108. self::_param('language', $language);
  109. Minz_Translate::reset($language);
  110. }
  111. }
  112. public static function getCookieDir(): string {
  113. // Get the script_name (e.g. /p/i/index.php) and keep only the path.
  114. $cookie_dir = '';
  115. if (!empty($_SERVER['HTTP_X_FORWARDED_PREFIX'])) {
  116. $cookie_dir .= rtrim($_SERVER['HTTP_X_FORWARDED_PREFIX'], '/ ');
  117. }
  118. $cookie_dir .= empty($_SERVER['REQUEST_URI']) ? '/' : $_SERVER['REQUEST_URI'];
  119. if (substr($cookie_dir, -1) !== '/') {
  120. $cookie_dir = dirname($cookie_dir) . '/';
  121. }
  122. return $cookie_dir;
  123. }
  124. /**
  125. * Specifies the lifetime of the cookies
  126. * @param int $l the lifetime
  127. */
  128. public static function keepCookie(int $l): void {
  129. session_set_cookie_params($l, self::getCookieDir(), '', Minz_Request::isHttps(), true);
  130. }
  131. /**
  132. * Regenerate a session id.
  133. * Useful to call session_set_cookie_params after session_start()
  134. */
  135. public static function regenerateID(): void {
  136. session_regenerate_id(true);
  137. }
  138. public static function deleteLongTermCookie(string $name): void {
  139. setcookie($name, '', 1, '', '', Minz_Request::isHttps(), true);
  140. }
  141. public static function setLongTermCookie(string $name, string $value, int $expire): void {
  142. setcookie($name, $value, $expire, '', '', Minz_Request::isHttps(), true);
  143. }
  144. public static function getLongTermCookie(string $name): string {
  145. return $_COOKIE[$name] ?? '';
  146. }
  147. }