4 use Shaarli\Config\ConfigManager
;
7 * Manages the server-side session
11 /** @var int Session expiration timeout, in seconds */
12 public static $INACTIVITY_TIMEOUT = 3600;
14 /** @var string Name of the cookie set after logging in **/
15 public static $LOGGED_IN_COOKIE = 'shaarli_staySignedIn';
17 /** @var array Local reference to the global $_SESSION array */
18 protected $session = [];
20 /** @var ConfigManager Configuration Manager instance **/
21 protected $conf = null;
26 * @param array $session The $_SESSION array (reference)
27 * @param ConfigManager $conf ConfigManager instance
29 public function __construct(& $session, $conf)
31 $this->session
= &$session;
36 * Generates a session token
38 * @return string token
40 public function generateToken()
42 $token = sha1(uniqid('', true) .'_'. mt_rand() . $this->conf
->get('credentials.salt'));
43 $this->session
['tokens'][$token] = 1;
48 * Checks the validity of a session token, and destroys it afterwards
50 * @param string $token The token to check
52 * @return bool true if the token is valid, else false
54 public function checkToken($token)
56 if (! isset($this->session
['tokens'][$token])) {
57 // the token is wrong, or has already been used
61 // destroy the token to prevent future use
62 unset($this->session
['tokens'][$token]);
67 * Validate session ID to prevent Full Path Disclosure.
70 * The session ID's format depends on the hash algorithm set in PHP settings
72 * @param string $sessionId Session ID
74 * @return true if valid, false otherwise.
76 * @see http://php.net/manual/en/function.hash-algos.php
77 * @see http://php.net/manual/en/session.configuration.php
79 public static function checkId($sessionId)
81 if (empty($sessionId)) {
89 if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
97 * Store user login information after a successful login
99 * @param string $clientIpId Client IP address identifier
101 public function storeLoginInfo($clientIpId)
103 // Generate unique random number (different than phpsessionid)
104 $this->session
['uid'] = sha1(uniqid('', true) . '_' . mt_rand());
105 $this->session
['ip'] = $clientIpId;
106 $this->session
['username'] = $this->conf
->get('credentials.login');
107 $this->session
['expires_on'] = time() + self
::$INACTIVITY_TIMEOUT;
111 * Extend session validity
113 public function extendSession()
115 if (! empty($this->session
['longlastingsession'])) {
116 // "Stay signed in" is enabled
117 $this->session
['expires_on'] = time() +
$this->session
['longlastingsession'];
120 $this->session
['expires_on'] = time() + self
::$INACTIVITY_TIMEOUT;
124 * Logout a user by unsetting all login information
127 * - https://secure.php.net/manual/en/function.setcookie.php
129 * @param string $webPath path on the server in which the cookie will be available on
131 public function logout($webPath)
133 if (isset($this->session
)) {
134 unset($this->session
['uid']);
135 unset($this->session
['ip']);
136 unset($this->session
['username']);
137 unset($this->session
['visibility']);
138 unset($this->session
['untaggedonly']);
140 setcookie(self
::$LOGGED_IN_COOKIE, 'false', 0, $webPath);
144 * Check whether the session has expired
146 * @param string $clientIpId Client IP address identifier
148 * @return bool true if the session has expired, false otherwise
150 public function hasSessionExpired()
152 if (empty($this->session
['uid'])) {
155 if (time() >= $this->session
['expires_on']) {
162 * Check whether the client IP address has changed
164 * @param string $clientIpId Client IP address identifier
166 * @return bool true if the IP has changed, false if it has not, or
167 * if session protection has been disabled
169 public function hasClientIpChanged($clientIpId)
171 if ($this->conf
->get('security.session_protection_disabled') === true) {
174 if ($this->session
['ip'] == $clientIpId) {