SessionManager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Shaarli\Security;
  3. use Shaarli\Config\ConfigManager;
  4. /**
  5. * Manages the server-side session
  6. */
  7. class SessionManager
  8. {
  9. /** @var int Session expiration timeout, in seconds */
  10. public static $SHORT_TIMEOUT = 3600; // 1 hour
  11. /** @var int Session expiration timeout, in seconds */
  12. public static $LONG_TIMEOUT = 31536000; // 1 year
  13. /** @var array Local reference to the global $_SESSION array */
  14. protected $session = [];
  15. /** @var ConfigManager Configuration Manager instance **/
  16. protected $conf = null;
  17. /** @var bool Whether the user should stay signed in (LONG_TIMEOUT) */
  18. protected $staySignedIn = false;
  19. /**
  20. * Constructor
  21. *
  22. * @param array $session The $_SESSION array (reference)
  23. * @param ConfigManager $conf ConfigManager instance
  24. */
  25. public function __construct(& $session, $conf)
  26. {
  27. $this->session = &$session;
  28. $this->conf = $conf;
  29. }
  30. /**
  31. * Define whether the user should stay signed in across browser sessions
  32. *
  33. * @param bool $staySignedIn Keep the user signed in
  34. */
  35. public function setStaySignedIn($staySignedIn)
  36. {
  37. $this->staySignedIn = $staySignedIn;
  38. }
  39. /**
  40. * Generates a session token
  41. *
  42. * @return string token
  43. */
  44. public function generateToken()
  45. {
  46. $token = sha1(uniqid('', true) .'_'. mt_rand() . $this->conf->get('credentials.salt'));
  47. $this->session['tokens'][$token] = 1;
  48. return $token;
  49. }
  50. /**
  51. * Checks the validity of a session token, and destroys it afterwards
  52. *
  53. * @param string $token The token to check
  54. *
  55. * @return bool true if the token is valid, else false
  56. */
  57. public function checkToken($token)
  58. {
  59. if (! isset($this->session['tokens'][$token])) {
  60. // the token is wrong, or has already been used
  61. return false;
  62. }
  63. // destroy the token to prevent future use
  64. unset($this->session['tokens'][$token]);
  65. return true;
  66. }
  67. /**
  68. * Validate session ID to prevent Full Path Disclosure.
  69. *
  70. * See #298.
  71. * The session ID's format depends on the hash algorithm set in PHP settings
  72. *
  73. * @param string $sessionId Session ID
  74. *
  75. * @return true if valid, false otherwise.
  76. *
  77. * @see http://php.net/manual/en/function.hash-algos.php
  78. * @see http://php.net/manual/en/session.configuration.php
  79. */
  80. public static function checkId($sessionId)
  81. {
  82. if (empty($sessionId)) {
  83. return false;
  84. }
  85. if (!$sessionId) {
  86. return false;
  87. }
  88. if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Store user login information after a successful login
  95. *
  96. * @param string $clientIpId Client IP address identifier
  97. */
  98. public function storeLoginInfo($clientIpId)
  99. {
  100. $this->session['ip'] = $clientIpId;
  101. $this->session['username'] = $this->conf->get('credentials.login');
  102. $this->extendTimeValidityBy(self::$SHORT_TIMEOUT);
  103. }
  104. /**
  105. * Extend session validity
  106. */
  107. public function extendSession()
  108. {
  109. if ($this->staySignedIn) {
  110. return $this->extendTimeValidityBy(self::$LONG_TIMEOUT);
  111. }
  112. return $this->extendTimeValidityBy(self::$SHORT_TIMEOUT);
  113. }
  114. /**
  115. * Extend expiration time
  116. *
  117. * @param int $duration Expiration time extension (seconds)
  118. *
  119. * @return int New session expiration time
  120. */
  121. protected function extendTimeValidityBy($duration)
  122. {
  123. $expirationTime = time() + $duration;
  124. $this->session['expires_on'] = $expirationTime;
  125. return $expirationTime;
  126. }
  127. /**
  128. * Logout a user by unsetting all login information
  129. *
  130. * See:
  131. * - https://secure.php.net/manual/en/function.setcookie.php
  132. */
  133. public function logout()
  134. {
  135. if (isset($this->session)) {
  136. unset($this->session['ip']);
  137. unset($this->session['expires_on']);
  138. unset($this->session['username']);
  139. unset($this->session['visibility']);
  140. unset($this->session['untaggedonly']);
  141. }
  142. }
  143. /**
  144. * Check whether the session has expired
  145. *
  146. * @param string $clientIpId Client IP address identifier
  147. *
  148. * @return bool true if the session has expired, false otherwise
  149. */
  150. public function hasSessionExpired()
  151. {
  152. if (empty($this->session['expires_on'])) {
  153. return true;
  154. }
  155. if (time() >= $this->session['expires_on']) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. /**
  161. * Check whether the client IP address has changed
  162. *
  163. * @param string $clientIpId Client IP address identifier
  164. *
  165. * @return bool true if the IP has changed, false if it has not, or
  166. * if session protection has been disabled
  167. */
  168. public function hasClientIpChanged($clientIpId)
  169. {
  170. if ($this->conf->get('security.session_protection_disabled') === true) {
  171. return false;
  172. }
  173. if (isset($this->session['ip']) && $this->session['ip'] === $clientIpId) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. }