<?php
function dbinsert($table,$type) 
{
	$dbin=mysql_query("INSERT INTO `$table` $type");
	return $dbin;
}
function dbquery($table,$type) 
{
	$dbq=mysql_query("SELECT * FROM `$table` $type");
	while($rdb=mysql_fetch_array($dbq))
	{
		$rdbq[]=$rdb;
	}
	return $rdbq;
}
function dbquerysun($table,$type) 
{
	$dbqsun=mysql_query("SELECT count(id) FROM `$table` $type");
	$rdbsun=mysql_fetch_array($dbqsun);
	return $rdbsun;
}
function dbdel($table,$type) 
{
	$dbdel=mysql_query("delete FROM `$table` where $type");
	return $rdel;
}
function queryall($table,$type) 
{
	$sql=mysql_query("SELECT * FROM `$table` $type");
	$row=mysql_fetch_array($sql);
	return $row;
}


function upalldt($table,$type) 
{
	$dbup=mysql_query("UPDATE `$table` SET $type");
	return $dbup;
}
function msglayer($str,$num) 
{
	$dbin="<script>parent.layer.msg('$str',{shade: 0.3,shift:$num});</script>";
	return $dbin;
}
function msglayerurl($str,$num,$url) 
{
	$dbin="<script>parent.layer.msg('$str',{shade: 0.3,shift:$num});setTimeout(function(){top.location.href='$url'},3000);</script>";
	return $dbin;
}
function writefile($fname,$str)
{
	$fp=fopen($fname,"w");
	fputs($fp,$str);
	fclose($fp);
}


function ubo($sql_str) 
{

		return $sql_str;

}
function random($length) 
{
	$hash = '';
	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
	$max = strlen($chars) - 1;
	mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $length; $i++) 
	{
		$hash .= $chars[mt_rand(0, $max)];
	}
	return $hash;
}

function getIP()
{
	static $realip;
	if (isset($_SERVER))
	{
		if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
		{
			$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		else if (isset($_SERVER["HTTP_CLIENT_IP"])) 
		{
			$realip = $_SERVER["HTTP_CLIENT_IP"];
		}
		else 
		{
			$realip = $_SERVER["REMOTE_ADDR"];
		}
	}
	else 
	{
		if (getenv("HTTP_X_FORWARDED_FOR"))
		{
			$realip = getenv("HTTP_X_FORWARDED_FOR");
		}
		else if (getenv("HTTP_CLIENT_IP")) 
		{
			$realip = getenv("HTTP_CLIENT_IP");
		}
		else 
		{
			$realip = getenv("REMOTE_ADDR");
		}
	}
	$realip = explode(',',$realip);
	return $realip[0];
}
function get_ip_info($ip = '') 
{
	if (empty($ip)) 
	{
		$ip = get_ip();
	}
	//$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
	if (empty($res)) 
	{
		return false;
	}
	$lengthMatches = array();
	preg_match('#\{.+?\}#', $res, $lengthMatches);
	if (!isset($lengthMatches[0])) 
	{
		return false;
	}
	$length = json_decode($lengthMatches[0], true);
	if (isset($length['ret']) && $length['ret'] == 1) 
	{
		$length['ip'] = $ip;
		unset($length['ret']);
	}
	else 
	{
		return false;
	}
	return $length;
}
function randColor()
{
	$colors = array();
	for($i = 0;$i<6;$i++)
	{
		$colors[] = dechex(rand(0,15));
	}
	return implode('',$colors);
}


function del_dir($dir)
{
	if(is_dir($dir))
	{
		foreach(scandir($dir) as $row)
		{
			if($row == '.' || $row == '..')
			{
				continue;
			}
			$path = $dir .'/'. $row;
			if(filetype($path) == 'dir')
			{
				del_dir($path);
			}
			else
			{
				unlink($path);
			}
		}
		rmdir($dir);
	}
	else
	{
		return false;
	}
}
function rarray_rand($xuy)
{
	return mt_rand( 0, count( $xuy ) - 1 );
}
function varray_rand($xuy) 
{
	return $xuy[rarray_rand($xuy)];
}

function getFileExt($filename)
{
	return strtolower(strrchr($filename, '.'));
}
function checkVideoType($filename){
	$arr = array(
        ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",".gif",
        ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid");
	return in_array(getFileExt($filename), $arr);
}
?>