You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_authentication.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * HTTP Basic Authentication
  4. *
  5. * Make use of an existing HTTP authentication and perform login with the existing user credentials
  6. *
  7. * Configuration:
  8. * // redirect the client to this URL after logout. This page is then responsible to clear HTTP auth
  9. * $config['logout_url'] = 'http://server.tld/logout.html';
  10. *
  11. * See logout.html (in this directory) for an example how HTTP auth can be cleared.
  12. *
  13. * For other configuration options, see config.inc.php.dist!
  14. *
  15. * @license GNU GPLv3+
  16. * @author Thomas Bruederli
  17. */
  18. class http_authentication extends rcube_plugin
  19. {
  20. private $redirect_query;
  21. function init()
  22. {
  23. $this->add_hook('startup', array($this, 'startup'));
  24. $this->add_hook('authenticate', array($this, 'authenticate'));
  25. $this->add_hook('logout_after', array($this, 'logout'));
  26. $this->add_hook('login_after', array($this, 'login'));
  27. }
  28. function startup($args)
  29. {
  30. if (!empty($_SERVER['PHP_AUTH_USER'])) {
  31. $rcmail = rcmail::get_instance();
  32. $rcmail->add_shutdown_function(array('http_authentication', 'shutdown'));
  33. // handle login action
  34. if (empty($_SESSION['user_id'])) {
  35. $args['action'] = 'login';
  36. $this->redirect_query = $_SERVER['QUERY_STRING'];
  37. }
  38. // Set user password in session (see shutdown() method for more info)
  39. else if (!empty($_SESSION['user_id']) && empty($_SESSION['password'])
  40. && !empty($_SERVER['PHP_AUTH_PW'])) {
  41. $_SESSION['password'] = $rcmail->encrypt($_SERVER['PHP_AUTH_PW']);
  42. }
  43. }
  44. return $args;
  45. }
  46. function authenticate($args)
  47. {
  48. // Load plugin's config file
  49. $this->load_config();
  50. $host = rcmail::get_instance()->config->get('http_authentication_host');
  51. if (is_string($host) && trim($host) !== '' && empty($args['host']))
  52. $args['host'] = rcube_utils::idn_to_ascii(rcube_utils::parse_host($host));
  53. // Allow entering other user data in login form,
  54. // e.g. after log out (#1487953)
  55. if (!empty($args['user'])) {
  56. return $args;
  57. }
  58. if (!empty($_SERVER['PHP_AUTH_USER'])) {
  59. $args['user'] = $_SERVER['PHP_AUTH_USER'];
  60. if (!empty($_SERVER['PHP_AUTH_PW']))
  61. $args['pass'] = $_SERVER['PHP_AUTH_PW'];
  62. }
  63. $args['cookiecheck'] = false;
  64. $args['valid'] = true;
  65. return $args;
  66. }
  67. function logout($args)
  68. {
  69. // redirect to configured URL in order to clear HTTP auth credentials
  70. if (!empty($_SERVER['PHP_AUTH_USER']) && $args['user'] == $_SERVER['PHP_AUTH_USER']) {
  71. if ($url = rcmail::get_instance()->config->get('logout_url')) {
  72. header("Location: $url", true, 307);
  73. }
  74. }
  75. }
  76. function shutdown()
  77. {
  78. // There's no need to store password (even if encrypted) in session
  79. // We'll set it back on startup (#1486553)
  80. rcmail::get_instance()->session->remove('password');
  81. }
  82. function login($args)
  83. {
  84. // Redirect to the previous QUERY_STRING
  85. if($this->redirect_query){
  86. header('Location: ./?' . $this->redirect_query);
  87. exit;
  88. }
  89. return $args;
  90. }
  91. }