<?php
/** 
 * 读取配置文件
 * @param    string    $Key    KEY值 
 */
function Config($Key) {
	include (_Amysql . 'Config.php');
	if (empty ( $Key ))
		return $Config;
	if (! array_key_exists ( $Key, $Config ))
		return null;
	return $Config [$Key];
}

/**
 * 缓存操作
 * 
 * @param string $Key
 *        	KEY值
 * @param string $Value
 *        	值
 * @param int $Time
 *        	缓存有效期（秒）
 */
function Cache($Key, $Value = '', $Time = null) {
	require_once (_Amysql . 'Cache.php');
	$Cahce = new Cache ();
	if ($Value === '' && is_null ( $Time )) {
		return $Cahce->Get ( md5 ( $Key ) );
	} else if (is_null ( $Value )) {
		$Cahce->Del ( md5 ( $Key ) );
	} else {
		if (is_null ( $Time )) {
			$Time = Config ( 'CacheDefaultTime' );
		}
		$Cahce->Set ( md5 ( $Key ), $Value, $Time );
	}
}

/**
 * 随即生成字符串
 * 
 * @param int $l
 *        	字符串长度
 */
function RandSting($l = 10) {
	$c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	srand ( ( double ) microtime () * 1000000 );
	for($i = 0; $i < $l; $i ++) {
		$rand .= $c [rand () % strlen ( $c )];
	}
	return $rand;
}

/**
 * json数据输出
 * 
 * @param object $data
 *        	json输出的数据
 */
function Json($data) {
	header ( 'Content-Type:application/json; charset=' . Config ( 'Charset' ) );
	exit ( json_encode ( $data ) );
}

/**
 * json success数据输出
 * 
 * @param object $info
 *        	json输出的数据
 */
function JsonSuccess($info) {
	$json_data ['status'] = 'success';
	$json_data ['info'] = $info;
	Json ( $json_data );
}

/**
 * json error数据输出
 * 
 * @param object $info
 *        	json输出的数据
 */
function JsonError($info) {
	$json_data ['status'] = 'error';
	$json_data ['info'] = $info;
	Json ( $json_data );
}

/**
 * 生成url地址
 * 
 * @param string $c
 *        	控制器
 * @param string $a
 *        	控制器方法
 * @param array $p
 *        	get提交参数
 */
function Url($c, $a, $p = null) {
	$sp = '';
	if (is_array ( $p )) {
		foreach ( $p as $key => $value ) {
			$sp .= sprintf ( "&%s=%s", urlencode ( $key ), urlencode ( $value ) );
		}
	}
	return sprintf ( "%s%s?%s=%s&%s=%s%s", _Http, Config ( 'index' ), Config ( 'UrlControllerName' ), $c, Config ( 'UrlActionName' ), $a, $sp );
}

/**
 * 获取客户端ip地址
 */
function GetUserIP($type = 0, $adv = false) {
	$type = $type ? 1 : 0;
	static $ip = NULL;
	if ($ip !== NULL)
		return $ip [$type];
	if ($adv) {
		if (isset ( $_SERVER ['HTTP_X_FORWARDED_FOR'] )) {
			$arr = explode ( ',', $_SERVER ['HTTP_X_FORWARDED_FOR'] );
			$pos = array_search ( 'unknown', $arr );
			if (false !== $pos)
				unset ( $arr [$pos] );
			$ip = trim ( $arr [0] );
		} elseif (isset ( $_SERVER ['HTTP_CLIENT_IP'] )) {
			$ip = $_SERVER ['HTTP_CLIENT_IP'];
		} elseif (isset ( $_SERVER ['REMOTE_ADDR'] )) {
			$ip = $_SERVER ['REMOTE_ADDR'];
		}
	} elseif (isset ( $_SERVER ['REMOTE_ADDR'] )) {
		$ip = $_SERVER ['REMOTE_ADDR'];
	}
	// IP地址合法验证
	$long = sprintf ( "%u", ip2long ( $ip ) );
	$ip = $long ? array (
			$ip,
			$long 
	) : array (
			'0.0.0.0',
			0 
	);
	return $ip [$type];
}
/**
 * 设置SESSION有效时间
 */
function start_session($expire = 0) {
	if ($expire == 0) {
		$expire = ini_get ( 'session.gc_maxlifetime' );
	} else {
		ini_set ( 'session.gc_maxlifetime', $expire );
	}
	
	if (empty ( $_COOKIE ['PHPSESSID'] )) {
		session_set_cookie_params ( $expire );
		session_start ();
	} else {
		session_start ();
		setcookie ( 'PHPSESSID', session_id (), time () + $expire );
	}
}
function mailsender($mailer, $verify, $type) {
	include (_Amysql . 'mail.class.php');
	// 邮箱线程**********************************************************
	$smtp = new smtp ( Config ( 'smtp' ), Config ( 'port' ), true, Config ( 'user' ), Config ( 'pass' ) );
	switch ($type) {
		case 'status' :
			$smtpemailto = $mailer; // 发送给谁
			$mailsubject = "minecraft账号验证通知"; // 邮件主题
			$mailbody = "你已成功注册 请妥善保管<br>验证码:{$verify}<br>请及时验证邮箱";
			$mailtype = "HTML"; // 邮件格式（HTML/TXT）,TXT为文本邮件
			$smtp->debug = Config ( 'debug' ); // 是否显示发送的调试信息
			if ($smtp->sendmail ( $smtpemailto, Config ( 'sender' ), $mailsubject, $mailbody, $mailtype ))
				return true;
			return false;
			break;
		case 'forget' :
			$smtpemailto = $mailer; // 发送给谁
			$mailsubject = "minecraft账号验证通知"; // 邮件主题
			$mailbody = "你正在操作找回密码<br>验证码:{$verify}<br>请及时验证邮箱";
			$mailtype = "HTML"; // 邮件格式（HTML/TXT）,TXT为文本邮件
			$smtp->debug = Config ( 'debug' ); // 是否显示发送的调试信息
			if ($smtp->sendmail ( $smtpemailto, Config ( 'sender' ), $mailsubject, $mailbody, $mailtype ))
				return true;
			return false;
			break;
		case 'forge' :
			$smtpemailto = $mailer; // 发送给谁
			$mailsubject = "minecraft账号通知"; // 邮件主题
			$mailbody = "你的密码已重置 新密码是:$verify <br>收到密码后请尽快登陆后台修改密码";
			$mailtype = "HTML"; // 邮件格式（HTML/TXT）,TXT为文本邮件
			$smtp->debug = Config ( 'debug' ); // 是否显示发送的调试信息
			if ($smtp->sendmail ( $smtpemailto, Config ( 'sender' ), $mailsubject, $mailbody, $mailtype ))
				return true;
			return false;
			break;
	}
}
function load($tpl) {
	include (_View . $tpl . ".php");
}