LoginManager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Shaarli;
  3. /**
  4. * User login management
  5. */
  6. class LoginManager
  7. {
  8. protected $globals = [];
  9. protected $configManager = null;
  10. protected $banFile = '';
  11. /**
  12. * Constructor
  13. *
  14. * @param array $globals The $GLOBALS array (reference)
  15. * @param ConfigManager $configManager Configuration Manager instance.
  16. */
  17. public function __construct(& $globals, $configManager)
  18. {
  19. $this->globals = &$globals;
  20. $this->configManager = $configManager;
  21. $this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php');
  22. $this->readBanFile();
  23. }
  24. /**
  25. * Read a file containing banned IPs
  26. */
  27. protected function readBanFile()
  28. {
  29. if (! file_exists($this->banFile)) {
  30. return;
  31. }
  32. include $this->banFile;
  33. }
  34. /**
  35. * Write the banned IPs to a file
  36. */
  37. protected function writeBanFile()
  38. {
  39. if (! array_key_exists('IPBANS', $this->globals)) {
  40. return;
  41. }
  42. file_put_contents(
  43. $this->banFile,
  44. "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals['IPBANS'], true) . ";\n?>"
  45. );
  46. }
  47. /**
  48. * Handle a failed login and ban the IP after too many failed attempts
  49. *
  50. * @param array $server The $_SERVER array
  51. */
  52. public function handleFailedLogin($server)
  53. {
  54. $ip = $server['REMOTE_ADDR'];
  55. $trusted = $this->configManager->get('security.trusted_proxies', []);
  56. if (in_array($ip, $trusted)) {
  57. $ip = getIpAddressFromProxy($server, $trusted);
  58. if (! $ip) {
  59. // the IP is behind a trusted forward proxy, but is not forwarded
  60. // in the HTTP headers, so we do nothing
  61. return;
  62. }
  63. }
  64. // increment the fail count for this IP
  65. if (isset($this->globals['IPBANS']['FAILURES'][$ip])) {
  66. $this->globals['IPBANS']['FAILURES'][$ip]++;
  67. } else {
  68. $this->globals['IPBANS']['FAILURES'][$ip] = 1;
  69. }
  70. if ($this->globals['IPBANS']['FAILURES'][$ip] >= $this->configManager->get('security.ban_after')) {
  71. $this->globals['IPBANS']['BANS'][$ip] = time() + $this->configManager->get('security.ban_duration', 1800);
  72. logm(
  73. $this->configManager->get('resource.log'),
  74. $server['REMOTE_ADDR'],
  75. 'IP address banned from login'
  76. );
  77. }
  78. $this->writeBanFile();
  79. }
  80. /**
  81. * Handle a successful login
  82. *
  83. * @param array $server The $_SERVER array
  84. */
  85. public function handleSuccessfulLogin($server)
  86. {
  87. $ip = $server['REMOTE_ADDR'];
  88. // FIXME unban when behind a trusted proxy?
  89. unset($this->globals['IPBANS']['FAILURES'][$ip]);
  90. unset($this->globals['IPBANS']['BANS'][$ip]);
  91. $this->writeBanFile();
  92. }
  93. /**
  94. * Check if the user can login from this IP
  95. *
  96. * @param array $server The $_SERVER array
  97. *
  98. * @return bool true if the user is allowed to login
  99. */
  100. public function canLogin($server)
  101. {
  102. $ip = $server['REMOTE_ADDR'];
  103. if (! isset($this->globals['IPBANS']['BANS'][$ip])) {
  104. // the user is not banned
  105. return true;
  106. }
  107. if ($this->globals['IPBANS']['BANS'][$ip] > time()) {
  108. // the user is still banned
  109. return false;
  110. }
  111. // the ban has expired, the user can attempt to log in again
  112. logm($this->configManager->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.');
  113. unset($this->globals['IPBANS']['FAILURES'][$ip]);
  114. unset($this->globals['IPBANS']['BANS'][$ip]);
  115. $this->writeBanFile();
  116. return true;
  117. }
  118. }