Ver Mensaje Individual
  #3 (permalink)  
Antiguo 16/09/2010, 09:55
jota2
 
Fecha de Ingreso: diciembre-2003
Mensajes: 474
Antigüedad: 21 años
Puntos: 5
Respuesta: .htpasswd con php

Cita:
Iniciado por abimaelrc Ver Mensaje
Puedes crear el archivo con fopen, file_put_contents, touch, etc.
He encontrado este script a ver si sirve para otros que busquen lo mismo:
Código:
<?php

$AllowAddNewUser = "TRUE";
$htpasswd = "editht/.htpasswd";

if (isset($_POST['submit']))

{
function generateString($user,$pass)
{

$saltchars = array(

'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'1','2','3','4','5','6','7','8','9','0','.','/'
);
srand((double)microtime()*1000000);
$saltcount = "2";
$des_salt = '';
for ($i=0; $i<$saltcount; $i++)
{
$des_salt .= $saltchars[rand(0,count($saltchars))];
}

$encrypted = crypt($pass,$des_salt);
$return = $user . ":" . $encrypted . "\n";
return $return;
}

if ($_POST['p'] != $_POST['conf_p'])
{
echo "Passwords do not match. Killing script...<br /><br />";
die;
}

$Fp = fopen($htpasswd, "r");
$Contents = fread($Fp, filesize($htpasswd));
fclose($Fp);
$String = explode("\n", $Contents);

for ($i=0; $i<count($String); $i++)

{
$chkString = explode(":", $String[$i]);
if($chkString[0] == $_POST['u'])
$newContents = '';
{
$Str[$i] = $String[$i] . "\n\n";
$newContents = str_replace($Str[$i], "", $Contents);
}
}

$InsContents = $newContents;
if (!$newContents)
{
if (isset($_POST['AddNew']))
{
echo "Could not locate specified user so creating new account<br /><br />";
$InsContents = $Contents;
} else

{
echo "User could not be found. Killing script...<br /><br />";
die;
}
}


$insString = generateString($_POST['u'], $_POST['p']); // Generate the user:pass string
$newHtpasswd = $InsContents . $insString; // Set the new contents of the file
$Handle = fopen($htpasswd, "w");
if (!fputs($Handle, $newHtpasswd))
{
echo "Could not write new passwd file";
} else
{
echo "New passwd file written successfully";
}
fclose($Handle);
} else
{
echo "<form name=\"changePass\" action=\"$PHP_SELF\" method=\"POST\">
<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" bgcolor=\"#DDDDDD\">
<tr><td bgcolor=\"#FFFFFF\">Username:</td><td bgcolor=\"#FFFFFF\"><input type=\"text\" name=\"u\" /></td></tr>
<tr><td bgcolor=\"#FFFFFF\">New Password:</td><td bgcolor=\"#FFFFFF\"><input type=\"password\" name=\"p\" /></td
></tr>
<tr><td bgcolor=\"#FFFFFF\">Confirm New<br />Password:</td><td bgcolor=\"#FFFFFF\"><input type=\"password\" name
=\"conf_p\" /></td></tr>";

if ($AllowAddNewUser == "TRUE") // If the variable is TRUE then show the checkbox
{
echo "<tr><td bgcolor=\"#FFFFFF\">Create new user<br />if this user does<br />not exist?</td><td bgcolor=\"#
FFFFFF\"><input type=\"checkbox\" name=\"AddNew\"></td></tr>";
}
echo "<tr><td bgcolor=\"#FFFFFF\" colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"
Change Password\"></td></tr>
</table>
</form>";
}
?>