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 $session The $_SESSION array (reference)
52 * @param array $cookie The $_COOKIE array
53 * @param string $webPath Path on the server in which the cookie will be available on
54 * @param string $clientIpId Client IP address identifier
55 * @param string $token Session token
57 * @return bool true if the user session is valid, false otherwise
59 public function checkLoginState(& $session, $cookie, $webPath, $clientIpId, $token)
61 if (! $this->configManager
->exists('credentials.login')) {
62 // Shaarli is not configured yet
63 $this->isLoggedIn
= false;
67 if (isset($cookie[SessionManager
::$LOGGED_IN_COOKIE])
68 && $cookie[SessionManager
::$LOGGED_IN_COOKIE] === $token
70 $this->sessionManager
->storeLoginInfo($clientIpId);
71 $this->isLoggedIn
= true;
74 if ($this->sessionManager
->hasSessionExpired()
75 || $this->sessionManager
->hasClientIpChanged($clientIpId)
77 $this->sessionManager
->logout($webPath);
78 $this->isLoggedIn
= false;
82 $this->sessionManager
->extendSession();
86 * Return whether the user is currently logged in
88 * @return true when the user is logged in, false otherwise
90 public function isLoggedIn()
92 if ($this->openShaarli
) {
95 return $this->isLoggedIn
;
99 * Check user credentials are valid
101 * @param string $remoteIp Remote client IP address
102 * @param string $clientIpId Client IP address identifier
103 * @param string $login Username
104 * @param string $password Password
106 * @return bool true if the provided credentials are valid, false otherwise
108 public function checkCredentials($remoteIp, $clientIpId, $login, $password)
110 $hash = sha1($password . $login . $this->configManager
->get('credentials.salt'));
112 if ($login != $this->configManager
->get('credentials.login')
113 || $hash != $this->configManager
->get('credentials.hash')
116 $this->configManager
->get('resource.log'),
118 'Login failed for user ' . $login
123 $this->sessionManager
->storeLoginInfo($clientIpId);
125 $this->configManager
->get('resource.log'),
133 * Read a file containing banned IPs
135 protected function readBanFile()
137 if (! file_exists($this->banFile
)) {
140 include $this->banFile
;
144 * Write the banned IPs to a file
146 protected function writeBanFile()
148 if (! array_key_exists('IPBANS', $this->globals
)) {
153 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals
['IPBANS'], true) . ";\n?>"
158 * Handle a failed login and ban the IP after too many failed attempts
160 * @param array $server The $_SERVER array
162 public function handleFailedLogin($server)
164 $ip = $server['REMOTE_ADDR'];
165 $trusted = $this->configManager
->get('security.trusted_proxies', []);
167 if (in_array($ip, $trusted)) {
168 $ip = getIpAddressFromProxy($server, $trusted);
170 // the IP is behind a trusted forward proxy, but is not forwarded
171 // in the HTTP headers, so we do nothing
176 // increment the fail count for this IP
177 if (isset($this->globals
['IPBANS']['FAILURES'][$ip])) {
178 $this->globals
['IPBANS']['FAILURES'][$ip]++
;
180 $this->globals
['IPBANS']['FAILURES'][$ip] = 1;
183 if ($this->globals
['IPBANS']['FAILURES'][$ip] >= $this->configManager
->get('security.ban_after')) {
184 $this->globals
['IPBANS']['BANS'][$ip] = time() +
$this->configManager
->get('security.ban_duration', 1800);
186 $this->configManager
->get('resource.log'),
187 $server['REMOTE_ADDR'],
188 'IP address banned from login'
191 $this->writeBanFile();
195 * Handle a successful login
197 * @param array $server The $_SERVER array
199 public function handleSuccessfulLogin($server)
201 $ip = $server['REMOTE_ADDR'];
202 // FIXME unban when behind a trusted proxy?
204 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
205 unset($this->globals
['IPBANS']['BANS'][$ip]);
207 $this->writeBanFile();
211 * Check if the user can login from this IP
213 * @param array $server The $_SERVER array
215 * @return bool true if the user is allowed to login
217 public function canLogin($server)
219 $ip = $server['REMOTE_ADDR'];
221 if (! isset($this->globals
['IPBANS']['BANS'][$ip])) {
222 // the user is not banned
226 if ($this->globals
['IPBANS']['BANS'][$ip] > time()) {
227 // the user is still banned
231 // the ban has expired, the user can attempt to log in again
232 logm($this->configManager
->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
233 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
234 unset($this->globals
['IPBANS']['BANS'][$ip]);
236 $this->writeBanFile();