<?php
/**
 * 版本更新，返回json格式版本更新列表
 * version.history.json为版本历史记录文件，其中版本号须按倒序排列(最近的放在最上)
 * level: 1:只有资源更新(包括lua)  2:lib的so文件有更新(暂时无用)  3：全新安装包下载   4:调用系统下载安装包并提示删除旧包 5：调用系统下载  并提示安装  
 * um:更新版本程序（安装包下载前的更新）
 * 
 * 
 * 当level1时，不区分平台、sdk
 * 
 * 当level为3、4、5时，忽略别的，先下载安装包，除非有um=1的情况，需先下载此补丁，更新版本管理程序，再下载安装包进行安装
 * 
 * 
platform
#define CC_PLATFORM_UNKNOWN            0
#define CC_PLATFORM_IOS                1
#define CC_PLATFORM_ANDROID            2
#define CC_PLATFORM_WIN32              3
#define CC_PLATFORM_MARMALADE          4
#define CC_PLATFORM_LINUX              5
#define CC_PLATFORM_BADA               6
#define CC_PLATFORM_BLACKBERRY         7
#define CC_PLATFORM_MAC                8
#define CC_PLATFORM_NACL               9

sdk
#define SDK_PLATFORM_UC                 1    //uc
#define SDK_PLATFORM_91                 2    //91
#define SDK_PLATFORM_PP                 3    //pp助手
#define SDK_PLATFORM_DJOY               4    //当乐
#define SDK_PLATFORM_MI                 5    //小米
 */
error_reporting ( E_ERROR );

define ( 'SECRET_KEY', "&*^(ABC09" );
define("DEFAULT_LANG", 'vn');

$LANG = $_REQUEST['lang'];
$LANG = $LANG ? $LANG : DEFAULT_LANG;

$baseVersion = stripslashes ( urldecode ( $_REQUEST ['ver'] ) );
$is50m = $_REQUEST ['is50'];
$platform = intval ( $_REQUEST ['pf'] );
$sdkid = intval ( $_REQUEST ['sdk'] );
$deviceId = $_REQUEST ['deviceId'];
$pinfo = stripslashes ( urldecode ( $_REQUEST ['p50'] ) );
$vcode = $_REQUEST ['vcode'];

$pfName = in_array ( $platform, array (
                1
) ) ? 'android' : ($platform == 2 ? 'ios' : 'window');


$sdkNameList = array (
                8 => "angame",
                1 => "uc",
                2 => "91",
                3 => "pp",
                5 => "mi",
                6 => "qh",
                9 => "bd",
                11 => 'wdj',
                12 => 'lx',
                13 => 'yy' 
);
$sdkName = $sdkNameList [$sdkid];

// writeLog($realmListName, 'update');
// writeLog($pinfo . $is50m . $baseVersion . SECRET_KEY, 'update');
$md5code = md5 ( $pinfo . $is50m . $baseVersion . SECRET_KEY );
//$outputList = array ('code'=>0);
if ($vcode == $md5code) {
		$pkglist = array ();
		if (! empty ( $pinfo )) {
			$pkglist = json_decode ( $pinfo, true );
		}
		if (! empty ( $baseVersion )) {
			$pkglist ["1"] = $baseVersion;
		}
		$ver = null;
		if (isset ( $pkglist ["1"] )) {
			$ver = $pkglist ["1"];
		}
		
		$verCode = getVersionCode();
        if (!isset($verCode)) {
        	//echo json_encode ( $outputList );
        	exit ("code error");
        }
        
        $fileName = "version.$verCode.json";
        if ( ! file_exists ( $fileName )) {
        	//echo json_encode ( $outputList );
        	exit ("file not exist:".$fileName);
        }
        
        $content = file_get_contents ( $fileName );
        if ($content) {
                $allList = json_decode ( $content, true );
                if ($allList) {
                	$phpfile = "update.$LANG.php";
                	if (file_exists($phpfile)) {
	                    include_once $phpfile;
	                    $output = getVersionList($allList, $pkglist, $baseVersion, $pfName, $sdkName);
	                    if ($output){
	                    	exit($output);
	                    }else{
	                    	exit("[]");
	                    }
                	}
                }
        }
        // }
}
else{
	exit("token error:" . $md5code);
}
//echo json_encode ( $outputList );


function getVersionCode(){
	global $deviceId, $pinfo, $baseVersion, $ver;
	
	include_once ('../common/func.php');
	include_once ('../common/db.php');
	include_once ('../config/config.inc.php');
	
	$realmListName = null;
	
	$ip = get_real_ip ();
	if ($ip == '121.8.201.114' || $ip == '121.8.201.115' ) {
		$realmListName = 'ddtlocal';
	}
	
	db_open ();
	// 检查设备 ID 是否合法
	if (checkMD5 ( $deviceId )) {
		// 查找是否对设备有设置
		$info = sql_fetch_one ( "select * from sys_realmlist_config where deviceId='$deviceId' order by cid desc limit 1" );
		if ($info) {
			if ($info ['realmListName'] != '') {
				$realmListName = $info ['realmListName'];
			}
		}
	}
	
	// 检查 IP 是否合法
	if (! $realmListName && checkIP ( $ip )) {
		// 查找是否对版本号有设置
		$info = sql_fetch_one ( "select * from sys_realmlist_config where ip='$ip' order by cid desc limit 1" );
		if ($info) {
			if ($info ['realmListName'] != '' && $info ['deviceId'] == '')
				$realmListName = $info ['realmListName'];
		}
	}
	
	if (! $realmListName) {
		$info = null;
		// 检查版本号是否合法
		if (isset ( $ver ) && checkVersion ( $ver )) {
			// 查找是否对版本号有设置
			$info = sql_fetch_one ( "select * from sys_realmlist_config where version='$ver' order by cid desc limit 1" );
		} else {
			// $info = sql_fetch_one("select * from sys_realmlist_config order by cid desc limit 1");
		}
	
		if ($info && $info ['realmListName'] != '' && ($info ['deviceId'] == '' and $info ['ip'] == '0'))
			$realmListName = $info ['realmListName'];
	}
	
	// 如果没有限定，则默认采用公网正式服
	if (! $realmListName) $realmListName = 'formal';
	
	$verCode = null;
	$sql = "select verCode from client_version_control where verType='$realmListName'";
	$row = sql_fetch_one ($sql);
	if ($row) {
		return $row['verCode'];
	}
}
?>