<?
@header("content-Type:text/html; charset=UTF-8");
@error_reporting(0);

//定义根目录
define('ROOT', substr(dirname(__FILE__), 0, -7));

//包含配置文件以及数据库类
require_once(ROOT."config.php");
require_once(ROOT."include/mysql.class.php");
require_once(ROOT."include/sec.php");

//保证在register_global=off的时候进行工作
$onoff=function_exists("ini_get")?ini_get("register_globals"):get_cfg_var("register_globals");

if($onff!=1)
{
	@extract($_GET,EXTR_SKIP);
	@extract($_POST,EXTR_SKIP);
	@extract($_COOKIE,EXTR_SKIP);
}
if (isset($_SESSION)) {   
  
	@array_unshift($superglobals,$_SESSION);   
  
} 

//数据库类的实例化
$mysql=new mysql($dbhost,$dbname,$dbuser,$dbpassword,$conn="conn");
$mysql->connect();
unset($dbhost,$dbname,$dbuser,$dbpassword,$conn);

//$sql="select * from userinfo";
//$mysql->query($sql);
//while($array=$mysql->fetcharray())
//{
//	echo $array["username"];
//}

//用递归函数去除转义字符的\
function stripslasharr($array)
{
	if(is_array($array))
	{
		foreach($array as $id=>$value)
		{
			$array[$id]=stripslasharr($value);
		}
	}
	
	if(is_string($array))
	{
		$array=stripslashes($array);
		
	}
	
	return $array;
}

//判断magic_quotes_gpc状态
if(@get_magic_quotes_gpc())
{
	$_GET=stripslasharr($_GET);
	$_POST=stripslasharr($_POST);
	$_COOKIE=stripslasharr($_COOKIE);
}

//得到各种标准时间
function gettime($id)
{
	if(!function_exists(date_default_timezone_set))
	{
		die("错误信息");
	}
	date_default_timezone_set("Asia/Shanghai");
	$time='';
	$weekday=array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
	switch($id)
	{
		case 1:
			$time=date("20y-m-d");
			break;
		case 2:
			$time=date("Y-m-d H:i:s");
			break;
		case 3:
			$time=date("Y-m-d H:i:s")."&nbsp;".$weekday[date("w")];
			break;
		case 4:
			$time=time();
			break;
		default:
			die("参数错误");
	}
	return $time;
}


//获得有效的ip地址

function getip()
{
	if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])   
	{                                               
        $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
       
	}                                               
	elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])     
	{                                               
        $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
           
	}                                               
	elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])       
	{                                               
        $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"]; 
               
	}                                               
	elseif (getenv("HTTP_X_FORWARDED_FOR"))         
	{                                               
        $ip = getenv("HTTP_X_FORWARDED_FOR"); 
                 
	}                                               
	elseif (getenv("HTTP_CLIENT_IP"))               
	{                                               
        $ip = getenv("HTTP_CLIENT_IP"); 
                       
	}           
                                   
	elseif (getenv("REMOTE_ADDR"))                   
	{                                               
        $ip = getenv("REMOTE_ADDR"); 
                         
	}         
                                     
	else                                             
	{                                               
        $ip = "Unknown";     
                                 
	}     
	return $ip;
}

//获取gdversion
function getgd()
{
	if(function_exists("gd_info"))
	{
		$gdarray=gd_info();
		$gd=$gdarray["GD Version"]?$gdarray["GD Version"]:"未知";
		unset($gdarray);
	}
	else
	{
		$gd="未知";
	}
	return $gd;
}

//截取字符串
function cutstring($string,$limit=20)
{
	if(function_exists("mb_substr"))
	{
		if(strlen($string)<$limit)
		{
			$string=$string."……";
		}
		else
		{
			$string=mb_substr($string,0,$limit,'utf-8');
		}
	}
	return $string;
}

//base64编码函数
function authcode($string,$operation="ENCODE")
{
	$string=$operation=="DECODE"?base64_decode($string):base64_encode($string);
	return $string;
}

//e-mail检测
function isemail($string)
{
	return strlen($string)>6 && preg_match("/^[\w\-\.]+@+[\w\-\.]+(\.\w)+$/",$string);
}

//判断是否登录

function islogin()
{
	if($_SESSION["username"]=="" || $_SESSION["password"]=="")
	{
		die("没有登录,<a href='/root/login.php'>返回登录</a>");
	}
}

//获得扩展名

function getextension($filename)
{
	$ext=pathinfo($filename);
	return $ext["extension"];
}

//上传附件函数
function upfile($source, $target) {
	// 如果一种函数上传失败，还可以用其他函数上传
	if (function_exists('move_uploaded_file') && @move_uploaded_file($source, $target)) {
		@chmod($target, 0666);
		return $target;
	} elseif (@copy($source, $target)) {
		@chmod($target, 0666);
		return $target;
	} elseif (@is_readable($source)) {
		if ($fp = @fopen($source,'rb')) {
			@flock($fp,2);
			$filedata = @fread($fp,@filesize($source));
			@fclose($fp);
		}
		if ($fp = @fopen($target, 'wb')) {
			@flock($fp, 2);
			@fwrite($fp, $filedata);
			@fclose($fp);
			@chmod ($target, 0666);
			return $target;
		} else {
			return false;
		}
	}
}


//输出错误信息

function showmsg($message,$url,$time)
{
	@header("Location:error.php?message=$message&url=$url&time=$time");
}

function redirect()
{
	echo "<script language='javascript'>window.history.go(-1)</script>";
}
?>