<?php
function random($length, $numeric = 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 mexit($msg=''){
	exit($msg);
}

function writetofile($file_name,$data,$method='w') {
	if($filenum=@fopen($file_name,$method)){
		flock($filenum,LOCK_EX);
		$file_data=fwrite($filenum,$data);
		fclose($filenum);
		return $file_data;
	}else{
		exit('Can not fopen please check the file or PHP.INI');
	}
}
function readfromfile($file){
	if(!@$fp = fopen($file, 'r')) {
		return false;
	} else {
		$str = @fread($fp, filesize($file));
		fclose($fp);
		return $str;
	}	
}
function head(){
	global $charset, $lang;
	
	echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" /><title>Photo Content Management System</title><style type="text/css"><!--body,td,th {color: #3A98C5;font-size: 12px;}body {margin:0px;background: url(install/images/bg.jpg) no-repeat scroll 50% 0pt;background-color: #ccebff;font-size: 12px;font-family: Tahoma;}.toptitle {font-size: 18px;font-weight: bold;color:#FFFFFF;}input {color: #3A98C5;background-color: #E3EDFD;border: 1px solid #3A98C5;}.header {background: url(install/images/header.gif);color: #FFFFFF;background-color: #3A98C5;}.tableborder {background-color: #a2c4cf;}.tdbg {background-color: #eff7ff;}.tdbg2 {background-color: #FFFFFF;}a {font-family: Tahoma;font-size: 12px;color: #3A98C5;text-decoration: none;}a:hover{font-family: Tahoma;font-size: 12px;color: #3A98C5;text-decoration: underline;}--></style></head><body><table width="820" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td height="122">&nbsp;</td></tr><tr><td height="72"><span class="toptitle">Photo Content Management System '.$lang['installguide'].'</span></td></tr>';
}


function msg($msg, $url = '',$return = 0) {
	global $lang,$msgs;

		head();
		
	$msg = $msgs[$msg] ? $msgs[$msg] : $msg;
	if($url) {
		
		$msg .= "<br /><br /><br /><a href=\"$url\">$url</a>";
		$msg .= "<script>function redirect() {window.location.replace('$url');}setTimeout('redirect();', 1250);</script>";
	} elseif($return){
		$msg .= "<br /><br /><br /><a href=\"javascript:history.go(-1);\">$lang[msg_back]</a>";
	}	
	echo '<tr><td height="40" style="color:#c3e3f9">&nbsp;</td></tr><tr><td height="20">&nbsp;</td></tr><tr><td><table width="100%" border="0" align="center" cellpadding="10" cellspacing="0" bgcolor="#FFFFFF"><tr><td><table width="80%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder"><tr class="header"><td class="header">'.$lang['tips'].'</td></tr><tr><td height="90" class="tdbg"><div align="center">'.$msg.'</div></td></tr></table></td></tr></table>';
	 foot();
}

function checkwrite($dir) {
	if(!is_dir($dir)) {
		@mkdir($dir, 0777);
	}
	if(is_dir($dir) && $fp = @fopen("$dir/test.txt", 'w')) {
		@fclose($fp);
		@unlink("$dir/test.txt");
		return true;
	}
	return false;
} 

function foot(){
	echo '</td></tr></table></body></html>';
	mexit();
}

function setconfig($string) {
	if(!get_magic_quotes_gpc()) {
		$string = str_replace('\'', '\\\'', $string);
	} else {
		$string = str_replace('\"', '"', $string);
	}
	return $string;
}
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 runquery($sql) {
	global $lang, $dbcharset, $tablepre, $db;

	$sql = str_replace("\r", "\n", str_replace(' mm_', ' '.$tablepre, $sql));
	$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);
			}

		}
	}
}
?>