]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LoginManager.php
5 * User login management
9 protected $globals = [];
10 protected $configManager = null;
11 protected $banFile = '';
16 * @param array $globals The $GLOBALS array (reference)
17 * @param ConfigManager $configManager Configuration Manager instance.
19 public function __construct(& $globals, $configManager)
21 $this->globals
= &$globals;
22 $this->configManager
= $configManager;
23 $this->banFile
= $this->configManager
->get('resource.ban_file', 'data/ipbans.php');
28 * Read a file containing banned IPs
30 protected function readBanFile()
32 if (! file_exists($this->banFile
)) {
35 include $this->banFile
;
39 * Write the banned IPs to a file
41 protected function writeBanFile()
43 if (! array_key_exists('IPBANS', $this->globals
)) {
48 "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals
['IPBANS'], true) . ";\n?>"
53 * Handle a failed login and ban the IP after too many failed attempts
55 * @param array $server The $_SERVER array
57 public function handleFailedLogin($server)
59 $ip = $server['REMOTE_ADDR'];
60 $trusted = $this->configManager
->get('security.trusted_proxies', []);
62 if (in_array($ip, $trusted)) {
63 $ip = getIpAddressFromProxy($server, $trusted);
65 // the IP is behind a trusted forward proxy, but is not forwarded
66 // in the HTTP headers, so we do nothing
71 // increment the fail count for this IP
72 if (isset($this->globals
['IPBANS']['FAILURES'][$ip])) {
73 $this->globals
['IPBANS']['FAILURES'][$ip]++
;
75 $this->globals
['IPBANS']['FAILURES'][$ip] = 1;
78 if ($this->globals
['IPBANS']['FAILURES'][$ip] >= $this->configManager
->get('security.ban_after')) {
79 $this->globals
['IPBANS']['BANS'][$ip] = time() +
$this->configManager
->get('security.ban_duration', 1800);
81 $this->configManager
->get('resource.log'),
82 $server['REMOTE_ADDR'],
83 'IP address banned from login'
86 $this->writeBanFile();
90 * Handle a successful login
92 * @param array $server The $_SERVER array
94 public function handleSuccessfulLogin($server)
96 $ip = $server['REMOTE_ADDR'];
97 // FIXME unban when behind a trusted proxy?
99 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
100 unset($this->globals
['IPBANS']['BANS'][$ip]);
102 $this->writeBanFile();
106 * Check if the user can login from this IP
108 * @param array $server The $_SERVER array
110 * @return bool true if the user is allowed to login
112 public function canLogin($server)
114 $ip = $server['REMOTE_ADDR'];
116 if (! isset($this->globals
['IPBANS']['BANS'][$ip])) {
117 // the user is not banned
121 if ($this->globals
['IPBANS']['BANS'][$ip] > time()) {
122 // the user is still banned
126 // the ban has expired, the user can attempt to log in again
127 logm($this->configManager
->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
128 unset($this->globals
['IPBANS']['FAILURES'][$ip]);
129 unset($this->globals
['IPBANS']['BANS'][$ip]);
131 $this->writeBanFile();