<?php
	include_once("check_login.php");
	$action = trim($_GET["action"]);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>清除网站文件BOM</title>
<style type="text/css"></style>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
<div class="head_menu"><a href="other_main.php">空间探测</a><span class="head_menu_on"><a href="other_bom.php">清除BOM</a></span></div>
<div class="set_main_out">
	<div class="set_main">
		<ul>
        	<li style="width:789px; text-align:center; font-weight:bold;">清除网站文件BOM</li>
            <li style="width:779px; padding:10px; height:auto; line-height:20px;">BOM 是 Byte Order Mark 的缩写。是UTF编码方案里用于标识编码的标准标记，在UTF-16里本来是FF FE，变成UTF-8就成了EF BB BF。这个标记是可选的，因为UTF8字节没有顺序，所以它可以被用来检测一个字节流是否是UTF-8编码的。微软做这种检测，但有些软件不做这种检测，而把它当作正常字符处理。<span style="color:#F00;">当有BOM存在时，网页显示或操作可能出现异常的情况，所以需要清理。</span><br /><br /><span style="color:#F00;">请把整个网站设成可写权限，清理BOM结束后再取消</span></li>
            <?php
            	if($action == "do"){
					echo " <li style=\"width:789px; text-indent:0px; height:auto; line-height:20px;\">";
					ob_start();
					print str_repeat("", 4096);
					include_once("loading.html");
					ob_flush();
					flush();
					if (isset($_GET['dir'])){
						$basedir=$_GET['dir'];
					}else{
						$basedir = '../';
					}
					$bom_count = 0;
					print str_repeat("", 4096);
					echo "文件处理开始……<br />";
					ob_flush();
					flush();
					checkdir($basedir);
					echo "处理结束：共处理文件<span style=\"color:#00F; font-size:18px; font-weight:bold;\">$bom_count</span>个";
				}
				else{
					echo " <li style=\"width:789px; text-indent:0px; text-align:center; padding-top:5px;\">";
					echo "<input type=\"button\" name=\"button\" id=\"button\" onclick=\"location.href='other_bom.php?action=do';\" value=\"  清 理  \" />";
				}
			?>
            </li>
    	</ul>
	</div>
</div>
</body>
</html>
<?php
function checkdir($basedir){
if ($dh = opendir($basedir)) {
   while (($file = readdir($dh)) !== false) {
    if ($file != '.' && $file != '..'){
     if (!is_dir($basedir."/".$file)) {
     }else{
      $dirname = $basedir."/".
$file;
      checkdir($dirname);
     }
    }
   }
closedir($dh);
}
}

function checkBOM ($filename) {
global $bom_count;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
    $rest = substr($contents, 3);
    rewrite ($filename, $rest);
	$bom_count++;
    return ("<font color=red>BOM found,automatically removed.</font><br />");
}
}

function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>