<?php
header('Content-type: text/html; charset=gbk');

if(is_dir('.svn')) exit('哥,你懂的');

if(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
	
set_time_limit(0);

if(is_file('./data/charsetize.php')){
	$FILES = include './data/charsetize.php';
}else{
	$FILES = array();
}


function _convert($dir, $from, $to, $reconvert = false){
	global $FILES;
	static $filter = array('.svn' => 1, '_svn' => 1, 'svn' => 1, 'ckeditor' => 1, 'ck_tools' => 1, 'doc' => 1, 'note' => 1, 'attachment' => 1, 'coreseek' => 1, '#export' => 1);
	
	$handle = opendir($dir);
	while(false !== ($item = readdir($handle))){
		if($item == '.' || $item == '..' || isset($filter[$item]) || preg_match('/^\.\/install\//', $dir)) continue;
		
		//#.php
		$cond = $reconvert ? isset($FILES[$dir . $item .'/#.php']) : !isset($FILES[$dir . $item .'/#.php']);
		if(is_dir($dir . $item) && is_file($dir . $item .'/#.php') && $cond){
			_convert_file($from, $to, $dir . $item .'/#.php', $reconvert);
		}
		
		//language
		$cond = $reconvert ? isset($FILES[$dir . $item]) : !isset($FILES[$dir . $item]);
		if(preg_match('/^\.\/lang\//', $dir . $item) && $item != '#.php' && is_file($dir . $item) && $cond){
			_convert_file($from, $to, $dir . $item, $reconvert);
		}
		
		//js and template
		if(is_file($dir . $item) && preg_match('/\.(js|html|css)/i', $item) && $cond){
			_convert_file($from, $to, $dir . $item, $reconvert);
		}
		
		//recursive
		if(is_dir($dir . $item)){
			echo '<b>'. $dir . $item .'</b><br />';
			_convert($dir . $item .'/', $from, $to, $reconvert);
		}
	}
	
	
}

function _convert_file($from_char, $to_char, $file, $reconvert = false){
	global $FILES;
	$c = file_get_contents($file);
	
	$c = _encode($from_char, $to_char, $c);
	
	$fp = fopen($file, 'w');
	fputs($fp, $c);
	fclose($fp);
	
	if($reconvert){
		unset($FILES[$file]);
	}else{
		$FILES[$file] = 1;
	}
	echo $file .'<br />';
}

function _encode($from, $to, $string){
	$from = strtoupper($from);
	$to = strtoupper($to);
	if($from == $to) return $string;
	
	if(function_exists('mb_convert_encoding')){
		if(!is_array($string)) return mb_convert_encoding($string, $to, $from);
		
		foreach($string as $k => $v)
			$string[$k] = _encode($from, $to, $v);
		return $string;
	}else if(function_exists('iconv')){
		if(!is_array($string)) return iconv($from, $to .'//IGNORE', $string);
		
		foreach($string as $k => $v)
			$string[$k] = _encode($from, $to, $v);
		return $string;
	}else{
		exit('no function for convert');
	}
}

function _file($f, $c){
	$fp = fopen($f, 'w');
	fputs($fp, $c);
	fclose($fp);
}

















echo '<html>
<body>
<div style="height: 400px; overflow: auto;">';

if($_POST['op'] == 'imutf8ize'){
	
	$char = 'gbk';
	$char2 = 'gbk';
	
	_convert('./', 'utf-8', 'gbk', true);
	_convert_file('utf-8', 'gbk', './#.php', true);
	
	_file('./data/charsetize.php', "<?php\r\nreturn ". var_export($FILES, true) .';');
	
}else{
	
	$char = 'utf8';
	$char2 = 'utf-8';

	_convert('./', 'gbk', 'utf-8');
	_convert_file('gbk', 'utf-8', './#.php');
	
	_file('./data/charsetize.php', "<?php\r\nreturn ". var_export($FILES, true) .';');
}
echo '</div>
<script type="text/javascript">
if(window.opener){
	var doc = window.opener.document;
	doc.getElementById("charset").value = "'. $char .'";
	doc.getElementById("p_charset").value = "'. $char2 .'";
}
alert("完成");
</script>
</body>
</html>';



















}else{

header('Content-type: text/html; charset=gbk');
?>
<!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>
<title>PHP168 sharp 核心安装程序</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head>

<body>
检查iconv扩展: <?php echo function_exists('iconv') ? '存在' : '不存在';?><br />
检查mbstring扩展: <?php echo function_exists('mb_convert_encoding') ? '存在' : '不存在';?><br />
需要以上两种扩展之一才能UTF-8化,你要使网站(反)UTF-8化吗?(UTF-8化仅会对未UTF-8化的文件进行UTF-8,己转换过的文件不再进行转换)<br />
提示:上传新系统,模块时可以执行UTF-8化,如果网站没有进行过UTF-8化操作的时候不要直接运行反UTF-8化操作<br />
用完之后记得删除本脚本,本程序还处于测试阶段！
<form method="post">
<input type="hidden" name="op" value="utf8ize" />
<input type="submit" value="UTF-8化" onclick="this.form.op.value='utf8ize';" />
<input type="submit" value="反UTF-8化" onclick="this.form.op.value='imutf8ize';" />
</form>
</body>
</html>
<?php
}
?>