<?php
function showjsmessage($message) {
	
	echo $message."\r\n<br>";
	flush();
	ob_flush();
}
function createtable($sql) {
	
	$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', 'MEMORY')) ? $type : 'MYISAM';
	return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
	(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=utf8" :  "TYPE=$type");
}
function runquery($sql) {
	global $_G, $tablepre;
	if(!isset($sql) || empty($sql)) return;
	$sql = str_replace("\r", "\n", str_replace(' pre_',' '.$tablepre, $sql));
	$sql = str_replace("\r", "\n", str_replace(' `pre_',' `'.$tablepre, $sql));
	$ret = array();
	$num = 0;
	foreach(explode(";\n", trim($sql)) as $query) {
		$ret[$num] = '';
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			$ret[$num] .= (isset($query[0]) && $query[0] == '#') || (isset($query[1]) && isset($query[1]) && $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('CREATE TABLE '.$name.' ...成功！');
				DB::query(createtable($query));
				
			} else {
				DB::query($query);
			}
		}
	}
	
}
function addicofromappid($appid,$uid,$username){
	global $_G;
	if($app=DB::fetch_first("select * from ".DB::table('dzz_apps')." where appid='{$appid}'")){
		if($app['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $app['appico'])){
			$app['appico']=$_G['setting']['attachurl'].$app['appico'];
		}
		$icoarr=array(
							'uid'=>$uid,
							'username'=>$username,
							'oid'=>$app['appid'],
							'name'=>$app['appname'],
							'url'=>$app['appurl'],
							'img'=>$app['appico'],
							'wwidth'=>$app['width'],
							'wheight'=>$app['height'],
							'open'=>$app['open'],
							'haveflash'=>$app['haveflash'],
							'idtype'=>$app['idtype'],
							'typeid'=>$app['typeid'],
							'havetask'=>$app['havetask'],
							'isshow'=>$app['isshow'],
							'titlebuttons'=>$app['titlebuttons'],
							'type'=>'app',
							'size'=>0,
							'ext'=>'',
							'friend'=>0,
							'dateline'=>$_G['timestamp'],
							
			);
		return $icoarr['icoid']=DB::insert('dzz_icos',daddslashes($icoarr),1);
	}
	return false;
}
function getappico($url){
	global $_G;
	$value=array();
	if(!$url) return false;
	$url=trim($url);
	$imgencode=md5($url).'.png';
	$subdir=substr($imgencode,0,2);
	$target='appico/'.$subdir.'/'.$imgencode;
	if(is_file($_G['setting']['attachdir'].$target)){
		return $target;
	}else{
		return false;
	}
}
function imagetolocal($source, $dir='appimg',$target='') {
	global $_G;
	if(empty($source)) return false;
	if(!$data =file_get_contents($source)) {
		return false;
	}
	if($target=='dzz/images/default/icodefault.png' || $target=='dzz/images/default/widgetdefault.png' || preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $target)){
		$target='';
	}
	if(!$target) {
		$imageext=array('jpg','jpeg','png','gif');
		$ext=strtolower(substr(strrchr($source, '.'), 1, 10));
		if(!in_array($ext,$imageext)) $ext='jpg';
		$subdir = $subdir1 = $subdir2 = '';
		$subdir1 = date('Ym');
		$subdir2 = date('d');
		$subdir = $subdir1.'/'.$subdir2.'/';
		$target1=$_G['setting']['attachdir'].$dir.'/'.$subdir.''.date('His').''.strtolower(random(16)).'.'.$ext;
		$target=str_replace($_G['setting']['attachdir'],'',$target1);
	}else{
		$target1=$_G['setting']['attachdir'].$target;
	}
	$targetpath = dirname($target1);
	dmkdir($targetpath);
	if(file_put_contents($target1, $data)){
		if(@filesize($target1)<200) {
			@unlink($target1);
			return false;
		}
		return $target;
	}else return false;
}
?>