BasicAuth.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * HTTP Basic Authentication handler
  4. *
  5. * Use this class for easy http authentication setup
  6. *
  7. * @package Sabre
  8. * @subpackage HTTP
  9. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  10. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  11. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  12. */
  13. class Sabre_HTTP_BasicAuth extends Sabre_HTTP_AbstractAuth {
  14. /**
  15. * Returns the supplied username and password.
  16. *
  17. * The returned array has two values:
  18. * * 0 - username
  19. * * 1 - password
  20. *
  21. * If nothing was supplied, 'false' will be returned
  22. *
  23. * @return mixed
  24. */
  25. public function getUserPass() {
  26. // Apache and mod_php
  27. if (($user = $this->httpRequest->getRawServerValue('PHP_AUTH_USER')) && ($pass = $this->httpRequest->getRawServerValue('PHP_AUTH_PW'))) {
  28. return array($user,$pass);
  29. }
  30. // Most other webservers
  31. $auth = $this->httpRequest->getHeader('Authorization');
  32. // Apache could prefix environment variables with REDIRECT_ when urls
  33. // are passed through mod_rewrite
  34. if (!$auth) {
  35. $auth = $this->httpRequest->getRawServerValue('REDIRECT_HTTP_AUTHORIZATION');
  36. }
  37. if (!$auth) return false;
  38. if (strpos(strtolower($auth),'basic')!==0) return false;
  39. return explode(':', base64_decode(substr($auth, 6)));
  40. }
  41. /**
  42. * Returns an HTTP 401 header, forcing login
  43. *
  44. * This should be called when username and password are incorrect, or not supplied at all
  45. *
  46. * @return void
  47. */
  48. public function requireLogin() {
  49. $this->httpResponse->setHeader('WWW-Authenticate','Basic realm="' . $this->realm . '"');
  50. $this->httpResponse->sendStatus(401);
  51. }
  52. }