PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Sabre/DAV/Auth/Backend/File.php

https://github.com/KOLANICH/SabreDAV
PHP | 77 lines | 23 code | 19 blank | 35 comment | 3 complexity | 6f21c077c66c1f7eb0c21bf68794625f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAV\Auth\Backend;
  3. use Sabre\DAV;
  4. /**
  5. * This is an authentication backend that uses a file to manage passwords.
  6. *
  7. * The backend file must conform to Apache's htdigest format
  8. *
  9. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  12. */
  13. class File extends 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|null $filename
  26. */
  27. public function __construct($filename=null) {
  28. if (!is_null($filename))
  29. $this->loadFile($filename);
  30. }
  31. /**
  32. * Loads an htdigest-formatted file. This method can be called multiple times if
  33. * more than 1 file is used.
  34. *
  35. * @param string $filename
  36. * @return void
  37. */
  38. public function loadFile($filename) {
  39. foreach(file($filename,FILE_IGNORE_NEW_LINES) as $line) {
  40. if (substr_count($line, ":") !== 2)
  41. throw new DAV\Exception('Malformed htdigest file. Every line should contain 2 colons');
  42. list($username,$realm,$A1) = explode(':',$line);
  43. if (!preg_match('/^[a-zA-Z0-9]{32}$/', $A1))
  44. throw new DAV\Exception('Malformed htdigest file. Invalid md5 hash');
  45. $this->users[$realm . ':' . $username] = $A1;
  46. }
  47. }
  48. /**
  49. * Returns a users' information
  50. *
  51. * @param string $realm
  52. * @param string $username
  53. * @return string
  54. */
  55. public function getDigestHash($realm, $username) {
  56. return isset($this->users[$realm . ':' . $username])?$this->users[$realm . ':' . $username]:false;
  57. }
  58. }