1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Steven Bühner |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
namespace HtpasswdManager\Service; |
8
|
|
|
|
9
|
|
|
class HtpasswdService { |
10
|
|
|
private $filename; |
11
|
|
|
|
12
|
|
|
// Caching of htpasswd-file |
13
|
|
|
private $userListCache = NULL; |
14
|
|
|
private $htpasswdCache = NULL; |
15
|
|
|
|
16
|
|
|
// Static Variables |
17
|
|
|
protected static $REGULAR_USER_PASSWORD = '~^([^:]+):(.+)$~im'; |
18
|
|
|
|
19
|
6 |
|
public function __construct($htpasswd_filename) { |
|
|
|
|
20
|
6 |
|
$this->filename = $htpasswd_filename; |
21
|
6 |
|
$this->createFileIfNotExistant(); |
22
|
6 |
|
} |
23
|
|
|
|
24
|
6 |
|
private function createFileIfNotExistant() { |
25
|
6 |
|
if (false === file_exists($this->filename)) { |
26
|
1 |
|
touch($this->filename); |
27
|
1 |
|
} |
28
|
6 |
|
} |
29
|
|
|
|
30
|
5 |
|
private function getHtpasswdContent() { |
|
|
|
|
31
|
5 |
|
if ($this->htpasswdCache === NULL) { |
32
|
|
|
|
33
|
5 |
|
$this->htpasswdCache = file_get_contents($this->filename); |
34
|
5 |
|
} |
35
|
|
|
|
36
|
5 |
|
return $this->htpasswdCache; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
private function updateHtpasswdContent() { |
40
|
2 |
|
$this->htpasswdCache = NULL; |
41
|
2 |
|
$this->userListCache = NULL; |
42
|
2 |
|
$this->getUserList(); |
43
|
2 |
|
} |
44
|
|
|
|
45
|
5 |
|
public function getUserList() { |
46
|
5 |
|
if ($this->userListCache === NULL) { |
47
|
5 |
|
$result = array(); |
48
|
|
|
|
49
|
5 |
|
$content = $this->getHtpasswdContent(); |
50
|
|
|
|
51
|
5 |
|
if (preg_match_all($this::$REGULAR_USER_PASSWORD, $content, $matches, PREG_PATTERN_ORDER) !== false) { |
52
|
5 |
|
foreach ($matches [1] as $i => $user) { |
53
|
4 |
|
$result [$user] = $matches [2] [$i]; |
54
|
5 |
|
} |
55
|
5 |
|
} |
56
|
|
|
|
57
|
5 |
|
$this->userListCache = $result; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
5 |
|
return $this->userListCache; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
private function encodePassword($password) { |
64
|
1 |
|
return crypt($password, substr(str_replace('+', '.', base64_encode(pack('N4', mt_rand(), mt_rand(), mt_rand(), mt_rand()))), 0, 22)); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
private function getNewUserEncodedString($username, $password) { |
68
|
1 |
|
return $username . ':' . $this->encodePassword($password) . "\n"; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function addUser($username, $password) { |
72
|
1 |
|
$newContent = $this->getHtpasswdContent(); |
73
|
1 |
|
$newContent .= $this->getNewUserEncodedString($username, $password); |
74
|
|
|
|
75
|
1 |
|
$this->replaceHtPasswdContent($newContent); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
public function updateUser($username, $password) { |
79
|
|
|
if ($this->userExists($username)) { |
80
|
|
|
$this->deleteUser($username); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->addUser($username, $password); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function deleteUser($username) { |
87
|
1 |
|
$newContent = ''; |
88
|
1 |
|
$usernameDeleted = false; |
89
|
|
|
|
90
|
1 |
|
if (preg_match_all($this::$REGULAR_USER_PASSWORD, $this->getHtpasswdContent(), $match) > 0) { |
91
|
1 |
|
foreach ($match [1] as $i => $user) { |
92
|
1 |
|
if ($user == $username) { |
93
|
1 |
|
$usernameDeleted = true; |
94
|
1 |
|
} else { |
95
|
1 |
|
$newContent .= $match [0] [$i] . "\n"; |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
if (true === $usernameDeleted) { |
100
|
1 |
|
$this->replaceHtPasswdContent($newContent); |
101
|
1 |
|
} |
102
|
1 |
|
} |
103
|
|
|
|
104
|
1 |
|
return $usernameDeleted; |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
private function replaceHtPasswdContent($newContent) { |
108
|
2 |
|
$fp = fopen($this->filename, 'w'); |
109
|
2 |
|
fwrite($fp, $newContent); |
110
|
2 |
|
fclose($fp); |
111
|
|
|
|
112
|
2 |
|
$this->updateHtpasswdContent(); |
113
|
2 |
|
} |
114
|
|
|
|
115
|
1 |
|
public function userExists($username) { |
116
|
1 |
|
$userList = $this->getUserList(); |
117
|
|
|
|
118
|
1 |
|
if (isset ($userList [$username])) |
119
|
1 |
|
return true; |
120
|
|
|
|
121
|
1 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.