<?php
	/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: install.func.php 7425 2008-05-20 09:50:45Z zhaolei $
*/

function install_header() {
	global $lang;
	$charset = CHARSET;
	echo <<<END
<!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=$charset" />
<title>$lang[install_wizard]</title>
<link rel="stylesheet" href="css/setup.css" type="text/css" media="all" />
<meta content="Comsenz Inc." name="Copyright" />
<script type="text/javascript">
function showmessage(message) {
	document.getElementById('notice').value += message + '\\r\\n';
}
function checkpass() {

	var dbhost	=	document.getElementById('dbhost').vlaue;
	var dbname 	= 	document.getElementById('dbname').value;
	var dbuser	= 	document.getElementById('dbuser').value;
	var manager_name	= 	document.getElementById('manager_name').value;
	var manager_pwd		= 	document.getElementById('manager_pwd').value;
	var manager_pwd1	= 	document.getElementById('manager_pwd1').value;

	if(dbhost == '') {
		alert('$lang[dbhost_is_null]');
		return false;
	}
	if(dbname == '') {
		alert('$lang[dbname_is_null]');
		return false;
	} 
	if(dbuser == '') {
		alert('$lang[dbuser_is_null]');
		return false;
	}
	if(manager_name == '') {
		alert('$lang[manager_is_null]');
		return false;
	}
	if(manager_pwd1 == ''|| manager_pwd!=manager_pwd1) {
		alert('$lang[manager_pwdis_null]');
		return false;
	}
	document.getElementById('setupinfoform').submit();

}
</script>
</head>
<body>
<div class="container">
END;
}
function install_footer() {

	echo <<<END
				<div class="footer">&copy;2001-2008 <a href="http://www.comsenz.com/" target="_blank">Comsenz</a> Inc.</div>
			</div>
		</div>
	</body>
	</html>
END;
}
function checkfdperm($path, $isfile=0) {
	if(is_file($path)){
		$isfile=1;
	}
	if($isfile) {
		$file = $path;
		$mod = 'a';
	} else {
		$file = $path.'./install_tmptest.data';
		$mod = 'w';
	}
	if(!@$fp = fopen($file, $mod)) {
		return false;
	}
	if(!$isfile) {
		//是否可以删除
		fwrite($fp, ' ');
		fclose($fp);
		if(!@unlink($file)) {
			return false;
		}
		//检测是否可以创建子目录
		if(is_dir($path.'./install_tmpdir')) {
			if(!@rmdir($path.'./install_tmpdir')) {
				return false;
			}
		}
		if(!@mkdir($path.'./install_tmpdir')) {
			return false;
		}
		//是否可以删除
		if(!@rmdir($path.'./install_tmpdir')) {
			return false;
		}
	} else {
		fclose($fp);
	}
	return true;
}

function getgpc($k, $var='G') {
	switch($var) {
		case 'G': $var = &$_GET; break;
		case 'P': $var = &$_POST; break;
		case 'C': $var = &$_COOKIE; break;
		case 'R': $var = &$_REQUEST; break;
	}
	return isset($var[$k]) ? $var[$k] : '';
}

function runquery($sql) {
	global $lang, $dbcharset, $db, $dbcharset;
	$ret = array();
	$num = 0;
	foreach(explode(";\n", trim($sql)) as $query) {
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			@$ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
		}
		$num++;
	}
	unset($sql);

	foreach($ret as $query) {
		$query = trim($query);
		if($query) {

			if(substr($query, 0, 12) == 'CREATE TABLE') {
				$name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
				showjsmessage($lang['create_table'].' '.$name.' ... '.$lang['succeed']);
				$db->query(createtable($query, $dbcharset));

			} else {
				$db->query($query);
			}

		}
	}
}

function showjsmessage($message) {
	echo '<script type="text/javascript">showmessage(\''.addslashes($message).' \');</script>'."\r\n";
	flush();
	ob_flush();
}

function createtable($sql, $dbcharset) {
	$type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
	$type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
	return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
		(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
}
function var_to_hidden($k, $v) {
	return "<input type=\"hidden\" name=\"$k\" value=\"$v\" />";
}

function generate_key() {
	$random = randomi(32);
	$info = md5($_SERVER['SERVER_SOFTWARE'].$_SERVER['SERVER_NAME'].$_SERVER['SERVER_ADDR'].$_SERVER['SERVER_PORT'].$_SERVER['HTTP_USER_AGENT'].time());
	$return = '';
	for($i=0; $i<64; $i++) {
		$p = intval($i/2);
		$return[$i] = $i % 2 ? $random[$p] : $info[$p];
	}
	return implode('', $return);
}

function randomi($length, $numeric = 0) {
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	if($numeric) {
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	} else {
		$hash = '';
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}

function show_msg($message, $next=0) {
	global $theurl, $lang;

	$nextstr = '';
	$backstr = '';

	if(empty($next)) {
		$backstr = '<a href=\"javascript:history.go(-1);\">'.$lang['message_return'].'</a>';
	}

	print<<<END
	<table>
	<tr><td>$message</td></tr>
	<tr><td>$backstr $nextstr</td></tr>
	</table>
END;
	exit();
}

function sreadfilei($filename) {
	$content = '';
	if(function_exists('file_get_contents')) {
		@$content = file_get_contents($filename);
	} else {
		if(@$fp = fopen($filename, 'r')) {
			@$content = fread($fp, filesize($filename));
			@fclose($fp);
		}
	}
	return $content;
}

function insertconfig($s, $find, $replace) {
	if(preg_match($find, $s)) {
		$s = preg_replace($find, $replace, $s);
	} else {
		// 插入到最后一行
		$s .= "\r\n".$replace;
	}
	return $s;
}

function submitchecki($var) {
	if(!empty($_POST[$var]) && $_SERVER['REQUEST_METHOD'] == 'POST') {
		return true;
	} else {
		return false;
	}
}
?>