<?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" );

$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", 3=>"91", 5=>"mi");
// $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 ();
if ($vcode == $md5code) {
        include_once ('../common/func.php');
        include_once ('../common/db.php');
        include_once ('../config/config.inc.php');

        $realmListName = null;
        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 = get_real_ip ();
        // 检查 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'];
                }
        }

        $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"];
        }

        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';

        if ($ip == '121.8.201.114' || $ip == '121.8.201.115' ) {
                $realmListName = 'ddtlocal';
        }

        // if (isset($platform) && isset($sdkid)) {
        $fileName = "version.$realmListName.json";
        if (! file_exists ( $fileName )) {
                echo json_encode ( $outputList );
                exit ();
        }
        $content = file_get_contents ( $fileName );
        if ($content) {
                $allList = json_decode ( $content, true );
                if ($allList) {
                        foreach ( $pkglist as $k => $v ) {
                                $jumpFlag = false;
                                if ($k == "1" && empty ( $baseVersion ))
                                        continue;
                                $oldVer = $v;
                                $oldVerNum = version2Num ( $oldVer );

                                $historyList = $allList [$k];

                                // 版本历史记录文件，版本号须为倒序排列
                                $updateUrlList = array ();
                                $installObj = null;
                                $umObj = null;
                                foreach ( $historyList as $verObj ) {
                                        if ($verObj && isset ( $verObj [$pfName] )) {
                                            $verObj = $verObj [$pfName];
                                        }elseif ($verObj['default']){
                                                $verObj = $verObj ['default'];
                                        }

                                        $newVerNum = version2Num ( $verObj ["version"] );

                                        if ($newVerNum <= $oldVerNum) {
                                                break;
                                        }

                                        $curLevel = $verObj ["level"];
                                        if (is_array ( $verObj ['url'] )) {
                                                $len = count ( $verObj ['url'] );
                                                $verObj ['url'] = $len > 1 ? $verObj ['url'] [mt_rand ( 0, $len - 1 )] : ($len == 1 ? $verObj ['url'] [0] : "");
                                        }

                                        if ($curLevel == 2 || empty ( $verObj ['url'] )){
                                                $updateUrlList = array ();
                                        }
                                        if ($curLevel == 3 || $curLevel == 4 || $curLevel == 5) {
                                                $installObj = $verObj;
                                                continue;
                                        }

                                        if ($installObj && isset($verObj['um']) && $verObj['um'] == 1){
                                                $umObj = $verObj;
                                                continue;
                                        }

                                        array_push ( $updateUrlList, $verObj );
                                        if (empty ( $verObj ['url'] )) {
                                                $jumpFlag = true;
                                                break;
                                        }
                                }

                                if ($installObj){
                                        $updateUrlList = array();
                                        if ($umObj) {
                                                array_push ( $updateUrlList, $umObj );
                                        }else{
                                                array_push ( $updateUrlList, $installObj );
                                        }
                                        $jumpFlag = true;
                                }

                                // 改为顺序排列
                                if (count ( $updateUrlList ) > 0) {
                                        $updateUrlList = array_reverse ( $updateUrlList );
                                        // level=3只下载安装包
                                        if ($jumpFlag) {
                                                $outputList = array ();
                                                $outputList [$k] = $updateUrlList;
                                                break;
                                        } else {
                                                $outputList [$k] = $updateUrlList;
                                        }
                                }
                        }
                        if (count ( $outputList ) > 0) {
                                echo stripslashes ( json_encode ( $outputList ) );
                                exit ();
                        }
                }
        }
        // }
}
echo json_encode ( $outputList );
?>