<?php
/**
 * 测试链接
 * http://localhost/dbmis_download/public/?from=s_48&spread=666666
 * http://localhost/dbmis_download/public/1_48.plist
 */
//报告运行时错误
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
//报告所有错误
//error_reporting(E_ALL);
//ini_set("display_errors","On");
//禁用错误日志
error_reporting(0);
//设置时区
use nochso\HtmlCompressTwig\Extension;
ini_set('date.timezone','Asia/Shanghai');
//项目根目录
define('ROOT_PATH',__DIR__.'/../');
define('TEMPLATE_CACHE',ROOT_PATH.'/runtime/cache');
include_once __DIR__.'/../lib/App.php';
//验证路径是否是请求文件
$requestUrl = $_SERVER['REQUEST_URI'];
$pInfo = pathinfo($requestUrl);

$extension = null;
if($pInfo && isset($pInfo['extension'])){
    $extension = $pInfo['extension'];
}
if($extension && $extension!='plist'){
    header('HTTP/1.1 404 Not Found');
    header("status: 404 Not Found");
    die();
}
$instance = \lib\App::getInstance();

$Msg = $instance->getDownloadParams();

function base64_img($filePath){
    if ($filePath && file_exists($filePath)){
        $ext = pathinfo($filePath)['extension'];
        return 'data:image/' . $ext . ';base64,' . base64_encode(file_get_contents($filePath));
    }
    return null;
}
function check_img($filePath){
    if (isset($filePath) && !empty($filePath)){
        if (strpos($filePath,'http')===0){
            return $filePath;
        }else{
        	return base64_img('.'.$filePath);
            //return $filePath;
        }
    }
    return null;
}
function save_user_agent() 
{
    $data = $_SERVER['HTTP_USER_AGENT'] . PHP_EOL;
    $data .= '---------------------------------------------------------------' . PHP_EOL;
    $path = './user_agent/' . date('Ym');
    if (!is_dir($path)){
        mkdir($path, 0777, true);
        chmod($path,0777);
    }
    
    $full_path = $path . '/' . date('d') . '.log';
    error_log($data, 3, $full_path);
}
save_user_agent();
/**
 * 注释每个参数的意义
 */
//ios包下载路径
//$ipaUrl     = $Msg['ipaUrl'];
//apk下载链接
//$apkUrl     = $Msg['apkUrl'];
//剪切板内容
//$clipText   = $Msg['clipText'];
//推广海图片地址
//$spreadImg  = $Msg['spreadImg'];
//包ID
//$packageId  = $Msg['packageId'];
//平台ID
//$platform   = $Msg['platform'];
if ($Msg && !empty($Msg)){

    $loader = new \Twig\Loader\FilesystemLoader('view',ROOT_PATH);

    $twig = new \Twig\Environment($loader,[
            'cache'=>TEMPLATE_CACHE,
            'autoescape'=>false,
            'auto_reload'=>true,
    ]);
    $twig->addExtension(new Extension(true));
    
    // @对接code
    $udid = $_GET['udid'];
    if(!empty($udid)){
        $Msg["autoDownload"] = 1;
    }
    $tag = $Msg["tag"];
    $Msg['udid'] = $udid;
    $Msg['app_tag'] = $tag;
  	$Msg["packageTag"] = $Msg["packageTag"] ? $Msg["packageTag"]:"a036";
    // @end对接code
    try {
        $tConfig = require_once '../template.php';
        //背景图片
        if (isset($tConfig['background_img']) && $tConfig['background_img']){
            $Msg['spreadImg'] = ($tConfig['background_img']);
        }
        //顶部悬浮窗
        if (isset($tConfig['header_img'])){
            $Msg['headerImg'] = ($tConfig['header_img']);
        }
        //底部悬浮窗
        if (isset($tConfig['footer_img'])){
            $Msg['footerImg'] = ($tConfig['footer_img']);
        }
     	$Msg['ico_download'] = ($tConfig['ico_download']);
      	$Msg['bg1'] = ($tConfig['bg1']);
        $Msg['bg2'] = ($tConfig['bg2']);
        $Msg['bg3'] = ($tConfig['bg3']);
        $Msg['dj1'] = ($tConfig['dj1']);
        $Msg['dj2'] = ($tConfig['dj2']);
        $Msg['kefuurl'] = ($tConfig['kefuurl']);
        $Msg['qiyeqian'] = ($tConfig['qiyeqian']);
        $Msg['guide_ios_install'] = ($tConfig['guide_ios_install']);
        $Msg['guide_weixin'] = ($tConfig['guide_weixin']);
        $Msg['qrShow'] = $tConfig["qrShow"];
        $Msg['ois'] = $tConfig["ois"];
        $template = "download";
        //ios与
    	if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
    		if(preg_match("/^http(s)?:\\/\\/.+/",$Msg["superIosUrl"])){
    			if($Msg["superIosUrlParams"]==1){
    				$Msg["plistUrl"] = $Msg["superIosUrl"].'?appenddata={"PACKAGETAG":"'
    				.$Msg['packageTag'].'","WEIXINTAG":"'
    				.$Msg['weixinLogin'].'","HAVE_QMDL_TAG":"'
    				.$Msg['spreadTag'].'"}';
    				var_dump($Msg["plistUrl"]);
    			}else{
    				$Msg["plistUrl"] = $Msg["superIosUrl"];
    			}
    			$template = "download";
    		}else{
    			$template = $Msg["iosTemplate"];
    		}
			
    	}else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
	    	$template = $Msg["androidTemplate"];
	    }else{
    	    $only_mobile=false;
    	    $packageConfigs = require __DIR__.'/../packages.php';
            $severName = $_SERVER["HTTP_HOST"];
            if (isset($packageConfigs[$severName])){
            	$packageConfig = $packageConfigs[$severName];
            	if(isset($packageConfig["only_mobile"])){
            		$only_mobile = $packageConfig["only_mobile"];
            	}
            }else{
            	$packageConfig = $packageConfigs["default"];
            	if(isset($packageConfig["only_mobile"])){
            	    $only_mobile = $packageConfig["only_mobile"];
            	}
            }
	        if($only_mobile){
	             $template='computer';
	        }else{
	             $template=$Msg["androidTemplate"];
	        }
	    }
	    $Msg['host'] = $_SERVER["HTTP_HOST"];
        echo $twig->render($template.'.html', $Msg);
        die();
 	
    } catch (\Exception $e) {
        header('HTTP/1.1 404 Not Found');
    }
}
//编译失败，返回404
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
