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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * @version @package_version@
  16. * @license GNU GPLv3+
  17. * @author Thomas Bruederli
  18. */
  19. class http_authentication extends rcube_plugin
  20. {
  21. private $redirect_query;
  22. function init()
  23. {
  24. $this->add_hook('startup', array($this, 'startup'));
  25. $this->add_hook('authenticate', array($this, 'authenticate'));
  26. $this->add_hook('logout_after', array($this, 'logout'));
  27. $this->add_hook('login_after', array($this, 'login'));
  28. }
  29. function startup($args)
  30. {
  31. if (!empty($_SERVER['PHP_AUTH_USER'])) {
  32. $rcmail = rcmail::get_instance();
  33. $rcmail->add_shutdown_function(array('http_authentication', 'shutdown'));
  34. // handle login action
  35. if (empty($_SESSION['user_id'])) {
  36. $args['action'] = 'login';
  37. $this->redirect_query = $_SERVER['QUERY_STRING'];
  38. }
  39. // Set user password in session (see shutdown() method for more info)
  40. else if (!empty($_SESSION['user_id']) && empty($_SESSION['password'])
  41. && !empty($_SERVER['PHP_AUTH_PW'])) {
  42. $_SESSION['password'] = $rcmail->encrypt($_SERVER['PHP_AUTH_PW']);
  43. }
  44. }
  45. return $args;
  46. }
  47. function authenticate($args)
  48. {
  49. // Load plugin's config file
  50. $this->load_config();
  51. $host = rcmail::get_instance()->config->get('http_authentication_host');
  52. if (is_string($host) && trim($host) !== '' && empty($args['host']))
  53. $args['host'] = rcube_utils::idn_to_ascii(rcube_utils::parse_host($host));
  54. // Allow entering other user data in login form,
  55. // e.g. after log out (#1487953)
  56. if (!empty($args['user'])) {
  57. return $args;
  58. }
  59. if (!empty($_SERVER['PHP_AUTH_USER'])) {
  60. $args['user'] = $_SERVER['PHP_AUTH_USER'];
  61. if (!empty($_SERVER['PHP_AUTH_PW']))
  62. $args['pass'] = $_SERVER['PHP_AUTH_PW'];
  63. }
  64. $args['cookiecheck'] = false;
  65. $args['valid'] = true;
  66. return $args;
  67. }
  68. function logout($args)
  69. {
  70. // redirect to configured URL in order to clear HTTP auth credentials
  71. if (!empty($_SERVER['PHP_AUTH_USER']) && $args['user'] == $_SERVER['PHP_AUTH_USER']) {
  72. if ($url = rcmail::get_instance()->config->get('logout_url')) {
  73. header("Location: $url", true, 307);
  74. }
  75. }
  76. }
  77. function shutdown()
  78. {
  79. // There's no need to store password (even if encrypted) in session
  80. // We'll set it back on startup (#1486553)
  81. rcmail::get_instance()->session->remove('password');
  82. }
  83. function login($args)
  84. {
  85. // Redirect to the previous QUERY_STRING
  86. if($this->redirect_query){
  87. header('Location: ./?' . $this->redirect_query);
  88. exit;
  89. }
  90. return $args;
  91. }
  92. }