2 namespace Shaarli\Security
;
4 use Shaarli\Config\ConfigManager
;
7 * Manages the server-side session
11 /** @var int Session expiration timeout, in seconds */
12 public static $SHORT_TIMEOUT = 3600; // 1 hour
14 /** @var int Session expiration timeout, in seconds */
15 public static $LONG_TIMEOUT = 31536000; // 1 year
17 /** @var array Local reference to the global $_SESSION array */
18 protected $session = [];
20 /** @var ConfigManager Configuration Manager instance **/
21 protected $conf = null;
23 /** @var bool Whether the user should stay signed in (LONG_TIMEOUT) */
24 protected $staySignedIn = false;
29 * @param array $session The $_SESSION array (reference)
30 * @param ConfigManager $conf ConfigManager instance
32 public function __construct(& $session, $conf)
34 $this->session
= &$session;
39 * Define whether the user should stay signed in across browser sessions
41 * @param bool $staySignedIn Keep the user signed in
43 public function setStaySignedIn($staySignedIn)
45 $this->staySignedIn
= $staySignedIn;
49 * Generates a session token
51 * @return string token
53 public function generateToken()
55 $token = sha1(uniqid('', true) .'_'. mt_rand() . $this->conf
->get('credentials.salt'));
56 $this->session
['tokens'][$token] = 1;
61 * Checks the validity of a session token, and destroys it afterwards
63 * @param string $token The token to check
65 * @return bool true if the token is valid, else false
67 public function checkToken($token)
69 if (! isset($this->session
['tokens'][$token])) {
70 // the token is wrong, or has already been used
74 // destroy the token to prevent future use
75 unset($this->session
['tokens'][$token]);
80 * Validate session ID to prevent Full Path Disclosure.
83 * The session ID's format depends on the hash algorithm set in PHP settings
85 * @param string $sessionId Session ID
87 * @return true if valid, false otherwise.
89 * @see http://php.net/manual/en/function.hash-algos.php
90 * @see http://php.net/manual/en/session.configuration.php
92 public static function checkId($sessionId)
94 if (empty($sessionId)) {
102 if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
110 * Store user login information after a successful login
112 * @param string $clientIpId Client IP address identifier
114 public function storeLoginInfo($clientIpId)
116 $this->session
['ip'] = $clientIpId;
117 $this->session
['username'] = $this->conf
->get('credentials.login');
118 $this->extendTimeValidityBy(self
::$SHORT_TIMEOUT);
122 * Extend session validity
124 public function extendSession()
126 if ($this->staySignedIn
) {
127 return $this->extendTimeValidityBy(self
::$LONG_TIMEOUT);
129 return $this->extendTimeValidityBy(self
::$SHORT_TIMEOUT);
133 * Extend expiration time
135 * @param int $duration Expiration time extension (seconds)
137 * @return int New session expiration time
139 protected function extendTimeValidityBy($duration)
141 $expirationTime = time() +
$duration;
142 $this->session
['expires_on'] = $expirationTime;
143 return $expirationTime;
147 * Logout a user by unsetting all login information
150 * - https://secure.php.net/manual/en/function.setcookie.php
152 public function logout()
154 if (isset($this->session
)) {
155 unset($this->session
['ip']);
156 unset($this->session
['expires_on']);
157 unset($this->session
['username']);
158 unset($this->session
['visibility']);
159 unset($this->session
['untaggedonly']);
164 * Check whether the session has expired
166 * @param string $clientIpId Client IP address identifier
168 * @return bool true if the session has expired, false otherwise
170 public function hasSessionExpired()
172 if (empty($this->session
['expires_on'])) {
175 if (time() >= $this->session
['expires_on']) {
182 * Check whether the client IP address has changed
184 * @param string $clientIpId Client IP address identifier
186 * @return bool true if the IP has changed, false if it has not, or
187 * if session protection has been disabled
189 public function hasClientIpChanged($clientIpId)
191 if ($this->conf
->get('security.session_protection_disabled') === true) {
194 if (isset($this->session
['ip']) && $this->session
['ip'] === $clientIpId) {