htpasswd.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * htpasswd tools for Apache Basic Auth.
  4. * Uses crypt only!
  5. *
  6. */
  7. class htpasswd {
  8. var $fp;
  9. var $filename;
  10. const HTPASSWD_NAME = ".htpasswd";
  11. const HTACCESS_NAME = ".htaccess";
  12. function htpasswd($configpath) {
  13. $path = realpath($configpath);
  14. $htaccessfile = $path . "/" . self::HTACCESS_NAME;
  15. $htpasswdfile = $path . "/" . self::HTPASSWD_NAME;
  16. if (!file_exists($htaccessfile)) {
  17. $bdfp = fopen($htaccessfile, 'w');
  18. $htaccess_content = "AuthType Basic\nAuthName \"Password Protected Area\"\nAuthUserFile \"" . $htpasswdfile . "\"\nRequire valid-user";
  19. fwrite($bdfp,$htaccess_content);
  20. }
  21. if (!file_exists($htpasswdfile)) {
  22. @$this->fp = fopen ( $htpasswdfile, 'w+' );
  23. } else {
  24. @$this->fp = fopen ( $htpasswdfile, 'r+' ) or die ( 'Invalid file name' );
  25. }
  26. $this->filename = $htpasswdfile;
  27. }
  28. function user_exists($username) {
  29. rewind ( $this->fp );
  30. while ( ! feof ( $this->fp ) && trim ( $lusername = array_shift ( explode ( ":", $line = rtrim ( fgets ( $this->fp ) ) ) ) ) ) {
  31. if ($lusername == $username)
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. function get_users() {
  37. rewind ( $this->fp );
  38. $users = array();
  39. $i = 0;
  40. while ( ! feof ( $this->fp ) && trim ( $lusername = array_shift ( explode ( ":", $line = rtrim ( fgets ( $this->fp ) ) ) ) ) ) {
  41. $users[$i] = $lusername;
  42. $i++;
  43. }
  44. return $users;
  45. }
  46. function user_add($username, $password) {
  47. if ($this->user_exists ( $username ))
  48. return false;
  49. fseek ( $this->fp, 0, SEEK_END );
  50. fwrite ( $this->fp, $username . ':' . self::htcrypt($password) . "\n" );
  51. return true;
  52. }
  53. /**
  54. * Login check
  55. * first 2 characters of hash is the salt.
  56. * @param user $username
  57. * @param pass $password
  58. * @return boolean true if password is correct!
  59. */
  60. function user_check($username, $password) {
  61. rewind ( $this->fp );
  62. while ( ! feof ( $this->fp ) && $userpass = explode ( ":", $line = rtrim ( fgets ( $this->fp ) ) ) ) {
  63. $lusername = trim($userpass[0]);
  64. $hash = $userpass[1];
  65. if ($lusername == $username) {
  66. return (self::check_password_hash($password, $hash));
  67. }
  68. }
  69. return false;
  70. }
  71. static function check_password_hash($password, $hash) {
  72. $salt = substr($hash,0,2);
  73. if (crypt($password,$salt)==$hash) {
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. static function htcrypt($password) {
  80. return crypt ( $password, substr ( str_replace ( '+', '.', base64_encode ( pack ( 'N4', mt_rand (), mt_rand (), mt_rand (), mt_rand () ) ) ), 0, 22 ) );
  81. }
  82. function user_delete($username) {
  83. $data = '';
  84. rewind ( $this->fp );
  85. while ( ! feof ( $this->fp ) && trim ( $lusername = array_shift ( explode ( ":", $line = rtrim ( fgets ( $this->fp ) ) ) ) ) ) {
  86. if (! trim ( $line ))
  87. break;
  88. if ($lusername != $username)
  89. $data .= $line . "\n";
  90. }
  91. $this->fp = fopen ( $this->filename, 'w' );
  92. fwrite ( $this->fp, rtrim ( $data ) . (trim ( $data ) ? "\n" : '') );
  93. fclose ( $this->fp );
  94. $this->fp = fopen ( $this->filename, 'r+' );
  95. return true;
  96. }
  97. function user_update($username, $password) {
  98. rewind ( $this->fp );
  99. while ( ! feof ( $this->fp ) && trim ( $lusername = array_shift ( explode ( ":", $line = rtrim ( fgets ( $this->fp ) ) ) ) ) ) {
  100. if ($lusername == $username) {
  101. fseek ( $this->fp, (- 15 - strlen ( $username )), SEEK_CUR );
  102. fwrite ( $this->fp, $username . ':' . self::htcrypt($password) . "\n" );
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. }
  109. ?>