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.

76 lines
2.0 KiB

  1. <?php
  2. /**
  3. * This is an authentication backend that uses a file to manage passwords.
  4. *
  5. * The backend file must conform to Apache's htdigest format
  6. *
  7. * @package Sabre
  8. * @subpackage DAV
  9. * @copyright Copyright (C) 2007-2011 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_DAV_Auth_Backend_File extends Sabre_DAV_Auth_Backend_AbstractDigest {
  14. /**
  15. * List of users
  16. *
  17. * @var array
  18. */
  19. protected $users = array();
  20. /**
  21. * Creates the backend object.
  22. *
  23. * If the filename argument is passed in, it will parse out the specified file fist.
  24. *
  25. * @param string $filename
  26. * @return void
  27. */
  28. public function __construct($filename=null) {
  29. if (!is_null($filename))
  30. $this->loadFile($filename);
  31. }
  32. /**
  33. * Loads an htdigest-formatted file. This method can be called multiple times if
  34. * more than 1 file is used.
  35. *
  36. * @param string $filename
  37. * @return void
  38. */
  39. public function loadFile($filename) {
  40. foreach(file($filename,FILE_IGNORE_NEW_LINES) as $line) {
  41. if (substr_count($line, ":") !== 2)
  42. throw new Sabre_DAV_Exception('Malformed htdigest file. Every line should contain 2 colons');
  43. list($username,$realm,$A1) = explode(':',$line);
  44. if (!preg_match('/^[a-zA-Z0-9]{32}$/', $A1))
  45. throw new Sabre_DAV_Exception('Malformed htdigest file. Invalid md5 hash');
  46. $this->users[$realm . ':' . $username] = $A1;
  47. }
  48. }
  49. /**
  50. * Returns a users' information
  51. *
  52. * @param string $realm
  53. * @param string $username
  54. * @return string
  55. */
  56. public function getDigestHash($realm, $username) {
  57. return isset($this->users[$realm . ':' . $username])?$this->users[$realm . ':' . $username]:false;
  58. }
  59. }