<?php
// set this to false to disable auto-configuration and checks
function locate_htpasswd() {
$htpasswd = '';
if (is_executable('/usr/sbin/htpasswd2')) {
$htpasswd = '/usr/sbin/htpasswd2';
} else if (is_executable('/usr/bin/htpasswd')) {
$htpasswd = '/usr/bin/htpasswd';
} else {
$output2 = array();
$return2 = null;
exec('which htpasswd2', $output2, $return2);
$output = array();
$return = null;
exec('which htpasswd', $output, $return);
if (!$return2)
$htpasswd = $output2[0];
else if (!$return)
$htpasswd = $output[0];
}
if (strpos($htpasswd, 'htpasswd')) {
$output = array();
$return = null;
exec($htpasswd . ' -nbp user pass', $output, $return);
if ($return || $output[0] != 'user:pass')
$htpasswd = '';
} else {
$htpasswd = '';
}
return $htpasswd;
}
function getHtpassword($user,$pass)
{
$auto_configuration = true;
$htpasswd_binary = '';
global $alphabet_64;
$alphabet_64 =
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
if ($auto_configuration)
$htpasswd_binary = locate_htpasswd();
if (get_magic_quotes_gpc()) {
$username = stripslashes($user);
$password = stripslashes($pass);
$mode = stripslashes(sha);
} else {
$username = $user;
$password = $pass;
$mode = 'sha';
}
$valid = true;
$valid = $valid && $username;
$valid = $valid && !strpos($username, ':');
$valid = $valid && $password;
if ($valid) {
if ($htpasswd_binary) {
if (!ini_get('safe_mode')) {
$username_esc = escapeshellarg($username);
$password_esc = escapeshellarg($password);
} else {
$username_esc = $username;
$password_esc = $password;
}
$flags = '-bn';
if ($mode == 'crypt')
$flags .= 'd';
else if ($mode == 'md5')
$flags .= 'm';
else if ($mode == 'sha')
$flags .= 's';
else if ($mode == 'plain')
$flags .= 'p';
$output = array();
$return = null;
exec("$htpasswd_binary $flags $username_esc $password_esc",
$output, $return);
if ($return) {
$htpasswd_binary = '';
} else {
$htpasswd_line = $output[0];
}
}
if (!$htpasswd_binary) {
if ($mode == 'crypt' && CRYPT_STD_DES == 1)
$htpasswd_line = $username . ':'
. crypt($password, salt(2));
else if ($mode == 'md5')
$htpasswd_line = $username . ':'
. crypt_apr_md5($password);
else if ($mode == 'sha')
$htpasswd_line = $username . ':'
. '{SHA}' . base64_encode(sha1($password, true));
else if ($mode == 'plain')
$htpasswd_line = $username . ':'
. $password;
}
}
return $htpasswd_line ;
}
?>