Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * The class for the access spark
  5. */
  6. class Access
  7. {
  8. /* The list of URLs to include or exclude from prompt */
  9. private $_forceList = array();
  10.  
  11. /**
  12. * Contructor. Prompt the user for their credentials
  13. * if configured to do so.
  14. */
  15. public function __construct()
  16. {
  17. $this->_forceList = config_item('access_force_list');
  18.  
  19. if(config_item('access_site_protection_enabled'))
  20. {
  21. $this->prompt();
  22. }
  23. }
  24.  
  25. /**
  26. * Prompt the user for their credentials. Issues a basic HTTP Auth
  27. * challenge.
  28. */
  29. public function prompt()
  30. {
  31. $force_list = $this->_forceList;
  32.  
  33. if($force_list && !$this->shouldPrompt())
  34. return;
  35.  
  36. $user = @$_SERVER['PHP_AUTH_USER'];
  37. $pass = @$_SERVER['PHP_AUTH_PW'];
  38.  
  39. $logins = config_item('access_logins');
  40. $realm = config_item('access_realm');
  41.  
  42. if(!array_key_exists($user, $logins) || $logins[$user] != $pass)
  43. {
  44. $delay = config_item('access_delay');
  45.  
  46. # Is a delay configured for failed logins?
  47. if($delay) sleep($delay);
  48.  
  49. header('HTTP/1.0 401 Unauthorized');
  50. header('WWW-Authenticate: Basic realm="'.$realm.'"');
  51. die("<h1>Unauthorized</h1>\n");
  52. }
  53. }
  54.  
  55. /**
  56. * Should the user see an auth prompt?
  57. * @return bool
  58. */
  59. public function shouldPrompt()
  60. {
  61. $url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  62. $in = FALSE;
  63.  
  64. foreach($this->_forceList as $pattern)
  65. {
  66. if(preg_match("#$pattern#", $url))
  67. {
  68. $in = TRUE;
  69. break;
  70. }
  71. }
  72.  
  73. # If the list is an INCLUDE list and the url is in it, prompt.
  74. # If the list is an EXCLUDE list and the url isn't in it, prompt.
  75. return (config_item('access_force_list_include') === $in);
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement