Session.php 4.3 KB

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