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 string Name of the cookie set after logging in **/
18 public static $LOGGED_IN_COOKIE = 'shaarli_staySignedIn';
20 /** @var array Local reference to the global $_SESSION array */
21 protected $session = [];
23 /** @var ConfigManager Configuration Manager instance **/
24 protected $conf = null;
26 /** @var bool Whether the user should stay signed in (LONG_TIMEOUT) */
27 protected $staySignedIn = false;
32 * @param array $session The $_SESSION array (reference)
33 * @param ConfigManager $conf ConfigManager instance
35 public function __construct(& $session, $conf)
37 $this->session
= &$session;
42 * Define whether the user should stay signed in across browser sessions
44 * @param bool $staySignedIn Keep the user signed in
46 public function setStaySignedIn($staySignedIn)
48 $this->staySignedIn
= $staySignedIn;
52 * Generates a session token
54 * @return string token
56 public function generateToken()
58 $token = sha1(uniqid('', true) .'_'. mt_rand() . $this->conf
->get('credentials.salt'));
59 $this->session
['tokens'][$token] = 1;
64 * Checks the validity of a session token, and destroys it afterwards
66 * @param string $token The token to check
68 * @return bool true if the token is valid, else false
70 public function checkToken($token)
72 if (! isset($this->session
['tokens'][$token])) {
73 // the token is wrong, or has already been used
77 // destroy the token to prevent future use
78 unset($this->session
['tokens'][$token]);
83 * Validate session ID to prevent Full Path Disclosure.
86 * The session ID's format depends on the hash algorithm set in PHP settings
88 * @param string $sessionId Session ID
90 * @return true if valid, false otherwise.
92 * @see http://php.net/manual/en/function.hash-algos.php
93 * @see http://php.net/manual/en/session.configuration.php
95 public static function checkId($sessionId)
97 if (empty($sessionId)) {
105 if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
113 * Store user login information after a successful login
115 * @param string $clientIpId Client IP address identifier
117 public function storeLoginInfo($clientIpId)
119 // Generate unique random number (different than phpsessionid)
120 $this->session
['uid'] = sha1(uniqid('', true) . '_' . mt_rand());
121 $this->session
['ip'] = $clientIpId;
122 $this->session
['username'] = $this->conf
->get('credentials.login');
123 $this->extendTimeValidityBy(self
::$SHORT_TIMEOUT);
127 * Extend session validity
129 public function extendSession()
131 if ($this->staySignedIn
) {
132 return $this->extendTimeValidityBy(self
::$LONG_TIMEOUT);
134 return $this->extendTimeValidityBy(self
::$SHORT_TIMEOUT);
138 * Extend expiration time
140 * @param int $duration Expiration time extension (seconds)
142 * @return int New session expiration time
144 protected function extendTimeValidityBy($duration)
146 $expirationTime = time() +
$duration;
147 $this->session
['expires_on'] = $expirationTime;
148 return $expirationTime;
152 * Logout a user by unsetting all login information
155 * - https://secure.php.net/manual/en/function.setcookie.php
157 public function logout()
159 if (isset($this->session
)) {
160 unset($this->session
['uid']);
161 unset($this->session
['ip']);
162 unset($this->session
['expires_on']);
163 unset($this->session
['username']);
164 unset($this->session
['visibility']);
165 unset($this->session
['untaggedonly']);
170 * Check whether the session has expired
172 * @param string $clientIpId Client IP address identifier
174 * @return bool true if the session has expired, false otherwise
176 public function hasSessionExpired()
178 if (empty($this->session
['uid'])) {
181 if (time() >= $this->session
['expires_on']) {
188 * Check whether the client IP address has changed
190 * @param string $clientIpId Client IP address identifier
192 * @return bool true if the IP has changed, false if it has not, or
193 * if session protection has been disabled
195 public function hasClientIpChanged($clientIpId)
197 if ($this->conf
->get('security.session_protection_disabled') === true) {
200 if ($this->session
['ip'] == $clientIpId) {