Beschreibung: Admintool zur Passwort und Userverwaltung von .htaccess und .htpasswd (like commandline and Strings as return parameters).
Funktionalität: Selbsterklärend durch String rückgabe
erstellt .htaccess und .htpasswd dateien (wenn der Zugriff erlaubt ist)
managen von benutzern und passwörtern (erstellen / löschen)
Wird im Ordner ausgeführt und erstellt die dateien, danach kann man sich anmelden und weitere user über dieses tool hinzufügen.
File: htadmintool.php
Source:
PHP-Code:
<pre>
<?php
session_start();
$SESSION_INDEX = "status";
$SESSION_VALUE = "administrator";
// $_SESSION['$SESSION_INDEX'] = $SESSION_VALUE;
$default_user = "default"; // MODIFY IT between the "" ! e.g. administrator
$default_pass = "default"; // MODIFY IT between the "" ! e.g. myadminpass
if($_SESSION['$SESSION_INDEX'] == $SESSION_VALUE){
if($_SERVER['QUERY_STRING'] != ""){
if(!is_file(".htaccess")){
echo "Notice: .htaccess file not found ! But i try to create it ...\r\n";
$acc_file = fopen(".htaccess", "w");
echo "Notice: .htaccess file created, now i try to write needed data ...\r\n";
fwrite($acc_file, "AuthUserFile ".str_replace("\\", "/", dirname(__FILE__))."/.htpasswd\r\n");
fwrite($acc_file, "AuthName \"<<< Passwortgeschuetzter Bereich >>>\"\r\n");
fwrite($acc_file, "AuthType Basic\r\n");
fwrite($acc_file, "require valid-user");
fclose($acc_file);
echo "Done: all data written to .htaccess file !\r\n";
} else {
echo "Notice: .htaccess file found here !\r\n";
}
if(!is_file(".htpasswd")){
echo "Notice: .htpasswd file not found ! But i try to create it ...\r\n";
fclose(fopen(".htpasswd", "w"));
echo "Notice: .htpasswd file created, i try to create the user ...\r\n";
$file = fopen(".htpasswd", "r");
if(filesize(".htpasswd") > 0){
$cont = fread($file, filesize(".htpasswd"));
} else {
$cont = " ";
}
fclose($file);
if(!preg_match("/".$default_user."/i", $cont)){
echo "Notice: Creating the default user !\r\n";
$file = fopen(".htpasswd", "a");
if(preg_match("/windows/i", $_ENV['OS'])){
fwrite($file, $default_user.":".$default_pass."\r\n");
} else {
fwrite($file, $default_user.":".crypt($default_pass)."\r\n");
}
echo "Notice: the defaultuser was created with the defaultpassword !\r\n";
fclose($file);
}
if($username != ""){
if(preg_match("/:/i", $username) || preg_match("/:/i", $password)){
echo "Warning: Username or Password have a : in it - user not created !\r\n";
} else {
echo "Message: Username should be ok, check if it in the .htpasswd file ...\r\n";
if(preg_match("/".$username.":/i", $cont)){
echo "Warning: Username already exist in the .htpasswd file !\r\n";
} else {
if($password != ""){
$file = fopen(".htpasswd", "a");
if(preg_match("/windows/i", $_ENV['OS'])){
fwrite($file, $username.":".$password."\r\n");
} else {
fwrite($file, $username.":".crypt($password)."\r\n");
}
echo "Done: User ".$username." was successfully created !\r\n";
fclose($file);
} else {
echo "Warning: No password was set for user: ".$username."\r\n";
}
}
}
} else {
echo "Warning: No username was set - so no user has been added !\r\n";
}
} else {
echo "Notice: .htpasswd file found here ! I try to create the user ...\r\n";
$file = fopen(".htpasswd", "r");
if(filesize(".htpasswd") > 0){
$cont = fread($file, filesize(".htpasswd"));
} else {
$cont = " ";
}
fclose($file);
if(!preg_match("/".$default_user."/i", $cont)){
echo "Notice: Creating the default user !\r\n";
$file = fopen(".htpasswd", "a");
if(preg_match("/windows/i", $_ENV['OS'])){
fwrite($file, $default_user.":".$default_pass."\r\n");
} else {
fwrite($file, $default_user.":".crypt($default_pass)."\r\n");
}
echo "Notice: the defaultuser was created with the defaultpassword !\r\n";
fclose($file);
}
if($username != ""){
if(preg_match("/:/i", $username) || preg_match("/:/i", $password)){
echo "Warning: Username or Password have a : in it - user not created !\r\n";
} else {
echo "Message: Username should be ok, check if it in the .htpasswd file ...\r\n";
if(preg_match("/".$username.":/i", $cont)){
echo "Warning: Username already exist in the .htpasswd file !\r\n";
} else {
if($password != ""){
$file = fopen(".htpasswd", "a");
if(preg_match("/windows/i", $_ENV['OS'])){
fwrite($file, $username.":".$password."\r\n");
} else {
fwrite($file, $username.":".crypt($password)."\r\n");
}
echo "Done: User ".$username." was successfully created !\r\n";
fclose($file);
} else {
echo "Warning: No password was set for user: ".$username."\r\n";
}
}
}
} else {
echo "Warning: No username was set - so no user has added !\r\n";
}
}
echo "Script finished !";
} else {
$file = split("/", $_SERVER['PHP_SELF']);
$file = $file[sizeof($file) - 1];
echo "USAGE: ".$file."?action=ACTION&username=USERNAME&password=PASSWORD\r\n";
echo " ACTION = create | delete - returns ErrorString if USERNAME already exists or ErrorString if USERNAME doesnt exists !\r\n";
echo " USERNAME = username\r\n";
echo " PASSWORD = password (only needed at the create function)\r\n";
echo "\r\n";
echo "Examples:\r\nCreate a new user named 'james' with the password 'pretty':\r\n";
echo $file."?action=create&username=james&password=pretty\r\n\r\n";
echo "Delete a existing user named 'james':\r\n";
echo $file."?action=delete&username=james\r\n\r\n";
echo "NOTICE: USERNAME and PASSWORD cannot have : in it !";
if($default_user == "default" || $default_pass == "default"){
echo "\r\nWARNING: You should change the DEFAULT_USER from line 11 and/or DEFAULT_PASS from line 12 in this file !";
}
}
} else {
$file = split("/", $_SERVER['PHP_SELF']);
$file = $file[sizeof($file) - 1];
echo "You must have a Valid \$_SESSION['\$SESSION_INDEX'] entry with the Value \"\$SESSION_VALUE\" to have access to this file !\r\n";
echo "Please change: \$SESSION_INDEX in line 6 and \$SESSION_VALUE in line 7 to your specialized values in this file !\r\n\r\n";
echo "To disable the needed SESSION remove the // from line 9 = // \$_SESSION['\$SESSION_INDEX'] = $SESSION_VALUE;";
}
session_destroy();
?>