4 use Shaarli\Config\ConfigManager
;
7 * User login management
11 /** @var array A reference to the $_GLOBALS array */
12 protected $globals = [];
14 /** @var ConfigManager Configuration Manager instance **/
15 protected $configManager = null;
17 /** @var SessionManager Session Manager instance **/
18 protected $sessionManager = null;
20 /** @var string Path to the file containing IP bans */
21 protected $banFile = '';
23 /** @var bool Whether the user is logged in **/
24 protected $isLoggedIn = false;
26 /** @var bool Whether the Shaarli instance is open to public edition **/
27 protected $openShaarli = false;
32 * @param array $globals The $GLOBALS array (reference)
33 * @param ConfigManager $configManager Configuration Manager instance
34 * @param SessionManager $sessionManager SessionManager instance
36 public function __construct(& $globals, $configManager, $sessionManager)
38 $this->globals
= &$globals;
39 $this->configManager
= $configManager;
40 $this->sessionManager
= $sessionManager;
41 $this->banFile
= $this->configManager
->get('resource.ban_file', 'data/ipbans.php');
43 if ($this->configManager
->get('security.open_shaarli')) {
44 $this->openShaarli
= true;
49 * Check user session state and validity (expiration)
51 * @param array $server The $_SERVER array
52 * @param array $session The $_SESSION array (reference)
53 * @param array $cookie The $_COOKIE array
54 * @param string $webPath Path on the server in which the cookie will be available on
55 * @param string $token Session token
57 * @return bool true if the user session is valid, false otherwise
59 public function checkLoginState($server, & $session, $cookie, $webPath, $token)
61 if (! $this->configManager
->exists('credentials.login')) {
62 // Shaarli is not configured yet
63 $this->isLoggedIn
= false;
67 $clientIpId = client_ip_id($server);
69 if (isset($cookie[SessionManager
::$LOGGED_IN_COOKIE])
70 && $cookie[SessionManager
::$LOGGED_IN_COOKIE] === $token
72 $this->sessionManager
->storeLoginInfo($clientIpId);
73 $this->isLoggedIn
= true;
76 if ($this->sessionManager
->hasSessionExpired()
77 || $this->sessionManager
->hasClientIpChanged($clientIpId)
79 $this->sessionManager
->logout($webPath);
80 $this->isLoggedIn
= false;
84 $this->sessionManager
->extendSession();
88 * Return whether the user is currently logged in
90 * @return true when the user is logged in, false otherwise
92 public function isLoggedIn()
94 if ($this->openShaarli
) {
97 return $this->isLoggedIn
;
101 * Check user credentials are valid
103 * @param array $server The $_SERVER array
104 * @param string $login Username
105 * @param string $password Password
107 * @return bool true if the provided credentials are valid, false otherwise
109 public function checkCredentials($server, $login, $password)
111 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
113 if ($login != $this->configManager
->get('credentials.login')
114 || $hash != $this->configManager
->get('credentials.hash')
117 $this->configManager
->get('resource.log'),
118 $server['REMOTE_ADDR'],
119 'Login failed for user ' . $login
124 $clientIpId = client_ip_id($server);
125 $this->sessionManager
->storeLoginInfo($clientIpId);
127 $this->configManager
->get('resource.log'),
128 $server['REMOTE_ADDR'],
135 * Read a file containing banned IPs
137 protected function readBanFile()
139 if (! file_exists($this->banFile
)) {
142 include $this->banFile
;
146 * Write the banned IPs to a file
148 protected function writeBanFile()
150 if (! array_key_exists('IPBANS', $this->globals
)) {
155 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals
['IPBANS'], true) . ";\n?>"
160 * Handle a failed login and ban the IP after too many failed attempts
162 * @param array $server The $_SERVER array
164 public function handleFailedLogin($server)
166 $ip = $server['REMOTE_ADDR'];
167 $trusted = $this->configManager
->get('security.trusted_proxies', []);
169 if (in_array($ip, $trusted)) {
170 $ip = getIpAddressFromProxy($server, $trusted);
172 // the IP is behind a trusted forward proxy, but is not forwarded
173 // in the HTTP headers, so we do nothing
178 // increment the fail count for this IP
179 if (isset($this->globals
['IPBANS']['FAILURES'][$ip])) {
180 $this->globals
['IPBANS']['FAILURES'][$ip]++
;
182 $this->globals
['IPBANS']['FAILURES'][$ip] = 1;
185 if ($this->globals
['IPBANS']['FAILURES'][$ip] >= $this->configManager
->get('security.ban_after')) {
186 $this->globals
['IPBANS']['BANS'][$ip] = time() +
$this->configManager
->get('security.ban_duration', 1800);
188 $this->configManager
->get('resource.log'),
189 $server['REMOTE_ADDR'],
190 'IP address banned from login'
193 $this->writeBanFile();
197 * Handle a successful login
199 * @param array $server The $_SERVER array
201 public function handleSuccessfulLogin($server)
203 $ip = $server['REMOTE_ADDR'];
204 // FIXME unban when behind a trusted proxy?
206 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
207 unset($this->globals
['IPBANS']['BANS'][$ip]);
209 $this->writeBanFile();
213 * Check if the user can login from this IP
215 * @param array $server The $_SERVER array
217 * @return bool true if the user is allowed to login
219 public function canLogin($server)
221 $ip = $server['REMOTE_ADDR'];
223 if (! isset($this->globals
['IPBANS']['BANS'][$ip])) {
224 // the user is not banned
228 if ($this->globals
['IPBANS']['BANS'][$ip] > time()) {
229 // the user is still banned
233 // the ban has expired, the user can attempt to log in again
234 logm($this->configManager
->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
235 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
236 unset($this->globals
['IPBANS']['BANS'][$ip]);
238 $this->writeBanFile();