-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchhtpasswd.php
56 lines (50 loc) · 1.58 KB
/
chhtpasswd.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/*******************************************************************
* php script/page to manipulate htpasswd file
*
* Written by: Jacob Isreal, Isreal Consulting LLC (www.icllc.cc)
*
* Last updated: 09-01-2024
********************************************************************/
function editHtpasswordRow(string $user, string $pass): string
{
$file = '.htpasswd';
if ($pass === '') {
$newRow = '';
$action = 'Delete';
} else {
$newRow = PHP_EOL . $user . ':' . password_hash($pass, PASSWORD_DEFAULT);
$action = 'Update';
}
$content = preg_replace(
'/\R?^' . preg_quote($user, '/') . ':.*/mu',
$newRow,
file_get_contents($file),
1,
$count
);
if (!$count && $newRow) {
$content .= PHP_EOL . $newRow;
$action = 'Insert';
}
file_put_contents($file, ltrim($content));
return $action;
}
echo '<h1>htpasswd Tool</h1>Enter username with blank password to delete user.<br><br>';
if (isset($_POST['user'], $_POST['pass'])) {
printf(
'<h3>%s of %s was successful</h3>',
editHtpasswordRow($_POST['user'], $_POST['pass']),
htmlspecialchars($_POST['user'])
);
echo '<h4>Fetched .htpasswd content:</h4><pre>' . file_get_contents('.htpasswd') . '</pre>';
} else {
echo '<h4>Fetched .htpasswd content:</h4><pre>' . file_get_contents('.htpasswd') . '</pre>';
}
?>
<form method="post">
User: <input type="text" name="user"><br>
Pass:<input type="text" name="pass"><br>
<input type="submit" value="Submit">
</form>