<?php
error_reporting(E_ALL ^ E_NOTICE);
@set_time_limit(0);

ob_start();
define('ONEZ_ROOT', str_replace("\\",'/', dirname(__FILE__)));
define('APP_STORE_URL', 'http://www.onez.cn');
define('APP_STORE_API', 'http://www.onez.cn/api/usersite.v3.php');
define('ONEZ_PRODUCT_SUBJECT', '佳蓝应用打包平台');
define('ONEZ_PRODUCT_SUMMARY', '');
define('ONEZ_PRODUCT_SHORTNAME', '打包平台');
define('ONEZ_PRODUCT_PTOKEN', 'build.www');
define('ONEZ_VERSION', 'v3.0');
define('ONEZ_PKEY', '');
define('IS_ONEZ_DEBUG', '');
if($_GET['res']) {
	$res = $_GET['res'];
	$reses = tpl_resources();
	if(array_key_exists($res, $reses)) {
		if($res == 'css') {
			header('content-type:text/css');
		} else {
			header('content-type:image/png');
		}
		echo base64_decode($reses[$res]);
		exit();
	}
  
}

$actions = array('license', 'env', 'onez', 'db', 'finish');
$action = $_COOKIE['action'];
$action = in_array($action, $actions) ? $action : 'license';
$ispost = strtolower($_SERVER['REQUEST_METHOD']) == 'post';
if(file_exists(ONEZ_ROOT . '/cache/install.lock') && $action != 'finish') {
	header('location: ./index.php');
	exit;
}
header('content-type: text/html; charset=utf-8');
if($action == 'license') {
	if($ispost) {
		setcookie('action', 'env');
		header('location: ?refresh');
		exit;
	}
	tpl_install_license();
}
if($action == 'env') {
	if($ispost) {
		setcookie('action', $_POST['do'] == 'continue' ? 'db' : 'license');
		header('location: ?refresh');
		exit;
	}
	$ret = array();
	$ret['server']['os']['value'] = php_uname();
	if(PHP_SHLIB_SUFFIX == 'dll') {
		$ret['server']['os']['remark'] = '建议使用 Linux 系统以提升程序性能';
		$ret['server']['os']['class'] = 'warning';
	}
	$ret['server']['sapi']['value'] = $_SERVER['SERVER_SOFTWARE'];
	if(PHP_SAPI == 'isapi') {
		$ret['server']['sapi']['remark'] = '建议使用 Apache 或 Nginx 以提升程序性能';
		$ret['server']['sapi']['class'] = 'warning';
	}
	$ret['server']['php']['value'] = PHP_VERSION;
	$ret['server']['dir']['value'] = ONEZ_ROOT;
	if(function_exists('disk_free_space')) {
		$ret['server']['disk']['value'] = floor(disk_free_space(ONEZ_ROOT) / (1024*1024)).'M';
	} else {
		$ret['server']['disk']['value'] = 'unknow';
	}
	$ret['server']['upload']['value'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknow';

	$ret['php']['version']['value'] = PHP_VERSION;
	$ret['php']['version']['class'] = 'success';
	if(version_compare(PHP_VERSION, '5.4.0') == -1) {
		$ret['php']['version']['class'] = 'danger';
		$ret['php']['version']['failed'] = true;
		$ret['php']['version']['remark'] = 'PHP版本必须为 5.4.0 以上. ';
	}

	$ret['php']['fopen']['ok'] = @ini_get('allow_url_fopen') && function_exists('fsockopen');
	if($ret['php']['fopen']['ok']) {
		$ret['php']['fopen']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
	} else {
		$ret['php']['fopen']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
	}

	$ret['php']['curl']['ok'] = extension_loaded('curl') && function_exists('curl_init');
	if($ret['php']['curl']['ok']) {
		$ret['php']['curl']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['php']['curl']['class'] = 'success';
	} else {
		$ret['php']['curl']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['php']['curl']['class'] = 'danger';
		$ret['php']['curl']['remark'] = '您的PHP环境不支持cURL, 也不支持 allow_url_fopen, 系统无法正常运行.';
		$ret['php']['curl']['failed'] = true;
	}

	$ret['php']['ssl']['ok'] = extension_loaded('openssl');
	$ret['php']['ssl']['ok'] = 1;
	if($ret['php']['ssl']['ok']) {
		$ret['php']['ssl']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['php']['ssl']['class'] = 'success';
	} else {
		$ret['php']['ssl']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['php']['ssl']['class'] = 'danger';
		$ret['php']['ssl']['failed'] = true;
		$ret['php']['ssl']['remark'] = '没有启用OpenSSL, 将无法访问云端接口, 系统无法正常运行.';
	}

	$ret['php']['gd']['ok'] = extension_loaded('gd');
	if($ret['php']['gd']['ok']) {
		$ret['php']['gd']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['php']['gd']['class'] = 'success';
	} else {
		$ret['php']['gd']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['php']['gd']['class'] = 'danger';
		$ret['php']['gd']['failed'] = true;
		$ret['php']['gd']['remark'] = '没有启用GD, 将无法正常上传和压缩图片, 系统无法正常运行. ';
	}

	$ret['php']['dom']['ok'] = class_exists('DOMDocument');
	if($ret['php']['dom']['ok']) {
		$ret['php']['dom']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['php']['dom']['class'] = 'success';
	} else {
		$ret['php']['dom']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['php']['dom']['class'] = 'danger';
		$ret['php']['dom']['failed'] = true;
		$ret['php']['dom']['remark'] = '没有启用DOMDocument, 将无法正常安装使用模块, 系统无法正常运行. ';
	}


	$ret['php']['asp_tags']['ok'] = ini_get('asp_tags');
	if(empty($ret['php']['asp_tags']['ok']) || strtolower($ret['php']['asp_tags']['ok']) == 'off') {
		$ret['php']['asp_tags']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['php']['asp_tags']['class'] = 'success';
	} else {
		$ret['php']['asp_tags']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['php']['asp_tags']['class'] = 'danger';
		$ret['php']['asp_tags']['failed'] = true;
		$ret['php']['asp_tags']['remark'] = '请禁用可以使用ASP 风格的标志，配置php.ini中asp_tags = Off';
	}

	$ret['write']['root']['ok'] = local_writeable(ONEZ_ROOT . '/');
	if($ret['write']['root']['ok']) {
		$ret['write']['root']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['write']['root']['class'] = 'success';
	} else {
		$ret['write']['root']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['write']['root']['class'] = 'danger';
		$ret['write']['root']['failed'] = true;
		$ret['write']['root']['remark'] = '本地目录无法写入, 将无法使用自动更新功能, 系统无法正常运行.  ';
	}
	$ret['write']['cache']['ok'] = local_writeable(ONEZ_ROOT . '/cache');
	if($ret['write']['cache']['ok']) {
		$ret['write']['cache']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['write']['cache']['class'] = 'success';
	} else {
		$ret['write']['cache']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['write']['cache']['class'] = 'danger';
		$ret['write']['cache']['failed'] = true;
		$ret['write']['cache']['remark'] = 'cache目录无法写入, 将无法写入配置文件, 系统无法正常安装. ';
	}
	$ret['write']['plugins']['ok'] = local_writeable(ONEZ_ROOT . '/plugins');
	if($ret['write']['plugins']['ok']) {
		$ret['write']['plugins']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
		$ret['write']['plugins']['class'] = 'success';
	} else {
		$ret['write']['plugins']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
		$ret['write']['plugins']['class'] = 'danger';
		$ret['write']['plugins']['failed'] = true;
		$ret['write']['plugins']['remark'] = 'plugins目录无法写入, 将无法写入配置文件, 系统无法正常安装. ';
	}

	$ret['continue'] = true;
	foreach($ret['php'] as $opt) {
		if($opt['failed']) {
			$ret['continue'] = false;
			break;
		}
	}
	if($ret['write']['failed']) {
		$ret['continue'] = false;
	}
	tpl_install_env($ret);
}
if($action == 'onez') {
	setcookie('action', 'db');
	header('location: ?refresh');
  return;
  if(defined('ONEZ_PKEY') && ONEZ_PKEY!=''){
		setcookie('action', 'db');
		header('location: ?refresh');
		exit();
  }
	$userkey = $_GET['userkey'];
  if($userkey){
    setcookie('userkey', $userkey);
		setcookie('action', 'onez');
		header('location: ?refresh');
		exit();
  }
	$userkey = $_COOKIE['userkey'];
	if(!$ispost) {
    if($userkey){
      $ispost=1;
      $_POST['do']='continue';
      $_POST['onez']=array(
        'type'=>'check',
        'userkey'=>$userkey,
      );
    }
  }
	if($ispost) {
		if($_POST['do'] != 'continue') {
			setcookie('action', 'env');
			header('location: ?refresh');
			exit();
		}
    
    $onez=$_POST['onez'];
    $post=array(
      'action'=>'product',
      'data'=>base64_encode(json_encode($onez)),
    );
  	$ch = curl_init();
  	curl_setopt($ch, CURLOPT_URL, APP_STORE_API);
  	curl_setopt($ch, CURLOPT_POST, 1);
  	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  	$content = curl_exec($ch);
  	curl_close($ch);
    $json=json_decode($content,1);
    if($json['error']){
      tpl_install_onez($json['error']);
    }
    if($json['extra']){
      mkdirs(dirname(ONEZ_ROOT.'/cache'));
      file_put_contents(ONEZ_ROOT.'/cache/install.extra',$json['extra']);
      if(!file_exists(ONEZ_ROOT.'/cache/install.extra')){
        tpl_install_db('请确认你的安装程序目录有写入权限. 多次安装失败, 请访问论坛获取解决方案！');
      }
    }
    if(isset($json['userkey'])){
      setcookie('userkey', $json['userkey']);
    }
    if($json['action']){
			setcookie('action', $json['action']);
			header('location: ?refresh');
			exit();
    }
    if($json['content']){
      tpl_install_onez('',base64_decode($json['content']));
    }
    
    
		setcookie('action', 'db');
		header('location: ?refresh');
		exit();
	}
  tpl_install_onez('');
}
if($action == 'db') {
  $G['hasdata']=0;
	if($ispost) {
		if($_POST['do'] != 'continue') {
			setcookie('action', 'onez');
			header('location: ?refresh');
			exit();
		}
    if(!file_exists(dirname(__FILE__).'/lib/onezphp.php')){
      $post=array(
        'action'=>'download',
      );
    }else{
      $post=array(
        'action'=>'download2',
      );
    }
    $post['userkey']=$_COOKIE['userkey'];
    if(file_exists(ONEZ_ROOT.'/cache/install.extra')){
      $post['extra']=file_get_contents(ONEZ_ROOT.'/cache/install.extra');
    }
  	$ch = curl_init();
  	curl_setopt($ch, CURLOPT_URL, APP_STORE_API);
  	curl_setopt($ch, CURLOPT_POST, 1);
  	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  	$content = curl_exec($ch);
  	curl_close($ch);
    $json=json_decode($content,1);
    if($json['error']){
      tpl_install_db($json['error']);
    }
    if(!file_exists(dirname(__FILE__).'/lib/onezphp.php')){
      if(!$json['files']['lib/onezphp.php']){
        tpl_install_db('获取安装信息失败，可能是由于网络不稳定，请重试。');
      }
    }
    foreach($json['files'] as $file=>$data){
      $data=base64_decode($data);
      mkdirs(dirname(ONEZ_ROOT.'/'.$file));
      file_put_contents(ONEZ_ROOT.'/'.$file,$data);
      if(!file_exists(ONEZ_ROOT.'/'.$file)){
        tpl_install_db('请确认你的安装程序目录有写入权限. 多次安装失败, 请访问论坛获取解决方案！');
      }
    }
      
      
    include_once(dirname(__FILE__).'/lib/onezphp.php');
    $db=$_POST['db'];
    $user=$_POST['user'];
    if(version_compare(PHP_VERSION, '7.0.0') == -1) {
      $link=@mysql_connect($db['server'], $db['username'], $db['password']);
      if(!$link){
        tpl_install_db('无法连接数据库，请检查数据库账号和密码是否正确');
      }
    	if(!@mysql_select_db($db['name'], $link)){
        @mysql_query('CREATE DATABASE IF NOT EXISTS `'.$db['name'].'` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;', $link);
        $error=mysql_error();
        if(!@mysql_select_db($db['name'], $link)){
          tpl_install_db('数据库<code>'.$db['name'].'</code>不存在且无法创建，请检查['.$error.']');
        }
      }
  		$statement = @mysql_query("SHOW TABLES LIKE '{$db['prefix']}%';",$link);
  		if ($statement) {
        $rs=mysql_fetch_array($statement);
        if($rs){
          if($_POST['clear']){
            @mysql_query("DROP TABLE IF EXISTS {$db['prefix']}data;",$link);
            @mysql_query("DROP TABLE IF EXISTS {$db['prefix']}device;",$link);
            @mysql_query("DROP TABLE IF EXISTS {$db['prefix']}member;",$link);
            @mysql_query("DROP TABLE IF EXISTS {$db['prefix']}order;",$link);
          }else{
            $G['hasdata']=1;
            tpl_install_db('');
          }
        }
  		}
    }else{
      $link=mysqli_connect($db['server'], $db['username'], $db['password']);
      if(!$link){
        tpl_install_db('无法连接数据库，请检查数据库账号和密码是否正确');
      }
    	if(!@mysqli_select_db($link,$db['name'])){
        mysqli_query($link,'CREATE DATABASE IF NOT EXISTS `'.$db['name'].'` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;');
        $error=mysql_error();
        if(!mysqli_select_db($link,$db['name'])){
          tpl_install_db('数据库<code>'.$db['name'].'</code>不存在且无法创建，请检查['.$error.']');
        }
      }
  		$statement = mysqli_query($link,"SHOW TABLES LIKE '{$db['prefix']}%';");
  		if ($statement) {
        $rs=mysqli_fetch_array($statement);
        if($rs){
          if($_POST['clear']){
            mysqli_query($link,"DROP TABLE IF EXISTS {$db['prefix']}data;");
            mysqli_query($link,"DROP TABLE IF EXISTS {$db['prefix']}device;");
            mysqli_query($link,"DROP TABLE IF EXISTS {$db['prefix']}member;");
            mysqli_query($link,"DROP TABLE IF EXISTS {$db['prefix']}order;");
          }else{
            $G['hasdata']=1;
            tpl_install_db('');
          }
        }
  		}
    }
    $code=<<<ONEZ
<?php
!defined('IN_ONEZ') && exit('Access Denied');
return array(
  'dbhost'=>'$db[server]',
  'dbuser'=>'$db[username]',
  'dbpass'=>'$db[password]',
  'dbname'=>'$db[name]',
  'dbcharset'=>'utf8',
  'tablepre'=>'$db[prefix]',
  'pconnect'=>'1',
);
ONEZ;
    onez()->write(ONEZ_ROOT.'/config/db.default.php',$code);
    #安装数据库
    $sysFile=ONEZ_ROOT.'/cache/dbtables.php';
    if(file_exists($sysFile)){
      $dbtables=include($sysFile);
      if($dbtables){
        foreach($dbtables as $tablename=>$table){
          $sql=onez('db')->create_mysql($tablename,$table['idname'],$table['fields']);
          onez('db')->db()->query($sql);
        }
      }
    }
    #默认数据
    $sysFile=ONEZ_ROOT.'/cache/datas.default.php';
    if(file_exists($sysFile)){
      $value=onez()->read($sysFile);
      if($value){
        $value=substr($value,strpos($value,'?>')+2);
        $value=base64_decode($value);
        $value=unserialize($value);
        if($value){
          foreach($value as $table=>$datas){
            if(is_numeric($table)){
              $datas['namespace']='site.'.ONEZ_PRODUCT_PTOKEN;
              $datas['tablename']=$datas['tablename'];
              $datas['appid']='0';
              $datas['siteid']='0';
              onez('db')->open('data')->insert($datas);
            }else{
              foreach($datas as $rs){
                if(!empty($rs)){
                  $rs['namespace']='site.'.ONEZ_PRODUCT_PTOKEN;
                  $rs['appid']='0';
                  $rs['siteid']='0';
                  onez('db')->open($table)->insert($rs);
                }
              }
            }
          }
        }
      }
    }
    $G['this']=onez(ONEZ_PRODUCT_PTOKEN);
    #写入管理账号
    $rndkey=uniqid();
    $user=array(
      'nickname'=>'超级管理员',
      'username'=>$user['username'],
      'grade'=>'admin',
      'rndkey'=>$rndkey,
      'password'=>md5($user['password']),
      'infotime'=>time(),
      'infoip'=>onez()->ip(),
    );
    $user['key1']='admin';
    $user['key2']=$user['username'];
    $user['key3']=$user['password'];
    $userid=$G['this']->data()->open('member')->insert($user);
    onez('cache')->cookie(str_replace('.','_',ONEZ_PRODUCT_PTOKEN),"$userid\t$user[username]\t".uniqid(),0);
    
    $siteHashFile=ONEZ_CACHE_PATH.'/site.hash.php';
    $siteHash=onez()->post('http://www.onez.cn/api/build.hash.php','action=fetch&t='.time());
    onez()->write($siteHashFile,$siteHash);
    
    $pkey='';
    $json=array();
    if(file_exists(ONEZ_ROOT.'/cache/install.extra')){
      $extra=file_get_contents(ONEZ_ROOT.'/cache/install.extra');
      $json=json_decode(base64_decode($extra),1);
      if(!empty($json['pkey'])){
        $pkey=$json['pkey'];
      }
    }
    
    if(file_exists(ONEZ_ROOT.'/cache/install.extra')){
      @unlink(ONEZ_ROOT.'/cache/install.extra');
    }
		touch(ONEZ_ROOT . '/cache/install.lock');
		setcookie('action', 'finish');
		setcookie('userkey', '');
		header('location: ?refresh');
		exit();
	}
  tpl_install_db('');
}
if($action == 'finish') {
	setcookie('action', '', -10);
	tpl_install_finish();
}

function mkdirs($dir){
  if(!is_dir($dir)){
    mkdirs(dirname($dir));
    mkdir($dir,0777);
  }
  return ;
}
function local_writeable($dir) {
	$writeable = 0;
	if(!is_dir($dir)) {
		@mkdir($dir, 0777);
	}
	if(is_dir($dir)) {
		if($fp = fopen("$dir/test.txt", 'w')) {
			fclose($fp);
			unlink("$dir/test.txt");
			$writeable = 1;
		} else {
			$writeable = 0;
		}
	}
	return $writeable;
}



function tpl_frame() {
	global $action, $actions;
	$action = $_COOKIE['action'];
	$step = array_search($action, $actions);
	$steps = array();
	for($i = 0; $i <= $step; $i++) {
		if($i == $step) {
			$steps[$i] = ' list-group-item-info';
		} else {
			$steps[$i] = ' list-group-item-success';
		}
	}
	$progress = $step * 20 + 20;
	$content = ob_get_contents();
	ob_clean();
  $title='安装系统 - '.ONEZ_PRODUCT_SUBJECT.' - '.ONEZ_PRODUCT_SUMMARY;
	$tpl = <<<EOF
<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>$title</title>
		<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css">
		<style>
			html,body{font-size:13px;font-family:"Microsoft YaHei UI", "微软雅黑", "宋体";}
			.pager li.previous a{margin-right:10px;}
			.header a{color:#FFF;}
			.header a:hover{color:#428bca;}
			.footer{padding:10px;}
			.footer a,.footer{color:#eee;font-size:14px;line-height:25px;}
		</style>
		<!--[if lt IE 9]>
		  <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
		  <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
		<![endif]-->
		<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
		<script src="http://cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
	</head>
	<body style="background-color:#28b0e4;">
		<div class="container">
			<div class="header" style="margin:15px auto;">
				<ul class="nav nav-pills pull-right" role="tablist">
					<li role="presentation" class="active"><a href="javascript:;">安装系统</a></li>
					<li role="presentation"><a href="http://www.onez.cn" target="_blank">官方网站</a></li>
					<li role="presentation"><a href="http://www.onez.cn/onez/YmJzfC9pbmRleC5waHA=/mod/bbs.html" target="_blank">论坛交流</a></li>
				</ul>
				<img src="?res=logo" height="60" />
			</div>
			<div class="row well" style="margin:auto 0;">
				<div class="col-xs-3">
					<div class="progress" title="安装进度">
						<div class="progress-bar progress-bar-info progress-bar-striped active" role="progressbar" aria-valuenow="{$progress}" aria-valuemin="0" aria-valuemax="100" style="width: {$progress}%;">
							{$progress}%
						</div>
					</div>
					<div class="panel panel-default">
						<div class="panel-heading">
							安装步骤
						</div>
						<ul class="list-group">
							<a href="javascript:;" class="list-group-item{$steps[0]}"><span class="glyphicon glyphicon-copyright-mark"></span> &nbsp; 许可协议</a>
							<a href="javascript:;" class="list-group-item{$steps[1]}"><span class="glyphicon glyphicon-eye-open"></span> &nbsp; 环境监测</a>
							<a href="javascript:;" class="list-group-item{$steps[2]}"><span class="glyphicon glyphicon-globe"></span> &nbsp; 选择产品</a>
							<a href="javascript:;" class="list-group-item{$steps[3]}"><span class="glyphicon glyphicon-cog"></span> &nbsp; 参数配置</a>
							<a href="javascript:;" class="list-group-item{$steps[4]}"><span class="glyphicon glyphicon-ok"></span> &nbsp; 成功</a>
						</ul>
					</div>
				</div>
				<div class="col-xs-9">
					{$content}
				</div>
			</div>
			<div class="footer" style="margin:15px auto;">
				<div class="text-center">
					版权所有 <a href="http://www.onez.cn" target="_blank"><b>佳蓝科技</b></a> v3.0 &copy; 2020 <a href="http://www.onez.cn" target="_blank">www.onez.cn</a>
				</div>
			</div>
		</div>
	</body>
</html>
EOF;
	echo trim($tpl);
}

function tpl_install_license() {
  $ONEZ_PRODUCT_SUBJECT=ONEZ_PRODUCT_SUBJECT;
  $ONEZ_PRODUCT_SHORTNAME=ONEZ_PRODUCT_SHORTNAME;
	echo <<<EOF
		<div class="panel panel-default">
			<div class="panel-heading">阅读许可协议</div>
			<div class="panel-body" style="overflow-y:scroll;max-height:400px;line-height:20px;">
				<h3>版权所有 (c)2017，佳蓝科技保留所有权利。 </h3>
				<p>
					感谢您选择{$ONEZ_PRODUCT_SHORTNAME} - {$ONEZ_PRODUCT_SUBJECT}（以下简称{$ONEZ_PRODUCT_SHORTNAME}，{$ONEZ_PRODUCT_SHORTNAME}基于 PHP + MySQL的技术开发，全部源码开放。 <br />
					为了使你正确并合法的使用本软件，请你在使用前务必阅读清楚下面的协议条款：
				</p>
				<p>
					<strong>一、本授权协议适用且仅适用于{$ONEZ_PRODUCT_SHORTNAME}任何版本，佳蓝科技官方对本授权协议的最终解释权。</strong>
				</p>
				<p>
					<strong>二、协议许可的权利 </strong>
					<ol>
						<li>您可以在完全遵守本最终用户授权协议的基础上，将本软件应用于非商业用途，而不必支付软件版权授权费用。</li>
						<li>您可以在协议规定的约束和限制范围内修改{$ONEZ_PRODUCT_SHORTNAME}源代码或界面风格以适应您的网站要求。</li>
						<li>您拥有使用本软件构建的网站全部内容所有权，并独立承担与这些内容的相关法律义务。</li>
						<li>获得商业授权之后，您可以将本软件应用于商业用途，同时依据所购买的授权类型中确定的技术支持内容，自购买时刻起，在技术支持期限内拥有通过指定的方式获得指定范围内的技术支持服务。商业授权用户享有反映和提出意见的权力，相关意见将被作为首要考虑，但没有一定被采纳的承诺或保证。</li>
					</ol>
				</p>
				<p>
					<strong>三、协议规定的约束和限制 </strong>
					<ol>
						<li>未获商业授权之前，不得将本软件用于商业用途（包括但不限于企业网站、经营性网站、以营利为目的或实现盈利的网站）。</li>
						<li>未经官方许可，不得对本软件或与之关联的商业授权进行出租、出售、抵押或发放子许可证。</li>
						<li>未经官方许可，禁止在{$ONEZ_PRODUCT_SHORTNAME}的整体或任何部分基础上以发展任何派生版本、修改版本或第三方版本用于重新分发。</li>
						<li>您承诺，应遵守所有中华人民共和国法律法规和国际上有关计算机、互联网和电子邮件的协议、规定、程序和惯例，其使用的所有功能应用于合法用途。如您将此功能用于侵犯国家、社会、他人的合法权益以及其他违法用途的，所产生的全部法律责任由您承担，与佳蓝科技无关；如佳蓝科技因此被追责的，您应承担由此给佳蓝科技造成的一切责任、损失及其他不利后果。以下为{$ONEZ_PRODUCT_SHORTNAME}禁止您进行的一些活动的示例，包括但不限于：</li>
						<li><pre>违反国家规定的政治宣传和/或新闻信息；
涉及国家秘密和/或安全的信息；
封建迷信和/或淫秽、色情、下流的信息或教唆犯罪的信息；
博彩有奖、赌博游戏；
“私服”、“外挂”等非法互联网出版活动；
违反国家民族和宗教政策的信息；妨碍互联网运行安全的信息；
侵害他人合法权益的信息和/或其他有损于社会秩序、社会治安、公共道德的信息或内容；
其他违反法律法规、部门规章或国家政策的内容</pre></li>
						<li>如果您未能遵守本协议的条款，您的授权将被终止，所被许可的权利将被收回，并承担相应法律责任。</li>
					</ol>
				</p>
				<p>
					<strong>四、有限担保和免责声明 </strong>
					<ol>
						<li>本软件及所附带的文件是作为不提供任何明确的或隐含的赔偿或担保的形式提供的。</li>
						<li>用户出于自愿而使用本软件，您必须了解使用本软件的风险，在尚未购买产品技术服务之前，我们不承诺对免费用户提供任何形式的技术支持、使用担保，也不承担任何因使用本软件而产生问题的相关责任。</li>
						<li>电子文本形式的授权协议如同双方书面签署的协议一样，具有完全的和等同的法律效力。您一旦开始确认本协议并安装{$ONEZ_PRODUCT_SHORTNAME}，即被视为完全理解并接受本协议的各项条款，在享有上述条款授予的权力的同时，受到相关的约束和限制。协议许可范围以外的行为，将直接违反本授权协议并构成侵权，我们有权随时终止授权，责令停止损害，并保留追究相关责任的权力。</li>
						<li>如果本软件带有其它软件的整合API示范例子包，这些文件版权不属于本软件官方，并且这些文件是没经过授权发布的，请参考相关软件的使用许可合法的使用。</li>
					</ol>
				</p>
			</div>
		</div>
		<form class="form-inline" role="form" method="post">
			<ul class="pager">
				<li class="pull-left" style="display:block;padding:5px 10px 5px 0;">
					<div class="checkbox">
						<label>
							<input type="checkbox"> 我已经阅读并同意此协议
						</label>
					</div>
				</li>
				<li class="previous"><a href="javascript:;" onclick="if(jQuery(':checkbox:checked').length == 1){jQuery('form')[0].submit();}else{alert('您必须同意软件许可协议才能安装！')};">继续 <span class="glyphicon glyphicon-chevron-right"></span></a></li>
			</ul>
		</form>
EOF;
	tpl_frame();
}

function tpl_install_env($ret = array()) {
	if(empty($ret['continue'])) {
		$continue = '<li class="previous disabled"><a href="javascript:;">请先解决环境问题后继续</a></li>';
	} else {
		$continue = '<li class="previous"><a href="javascript:;" onclick="$(\'#do\').val(\'continue\');$(\'form\')[0].submit();">继续 <span class="glyphicon glyphicon-chevron-right"></span></a></li>';
	}
	echo <<<EOF
		<div class="panel panel-default">
			<div class="panel-heading">服务器信息</div>
			<table class="table table-striped">
				<tr>
					<th style="width:150px;">参数</th>
					<th>值</th>
					<th></th>
				</tr>
				<tr class="{$ret['server']['os']['class']}">
					<td>服务器操作系统</td>
					<td>{$ret['server']['os']['value']}</td>
					<td>{$ret['server']['os']['remark']}</td>
				</tr>
				<tr class="{$ret['server']['sapi']['class']}">
					<td>Web服务器环境</td>
					<td>{$ret['server']['sapi']['value']}</td>
					<td>{$ret['server']['sapi']['remark']}</td>
				</tr>
				<tr class="{$ret['server']['php']['class']}">
					<td>PHP版本</td>
					<td>{$ret['server']['php']['value']}</td>
					<td>{$ret['server']['php']['remark']}</td>
				</tr>
				<tr class="{$ret['server']['dir']['class']}">
					<td>程序安装目录</td>
					<td>{$ret['server']['dir']['value']}</td>
					<td>{$ret['server']['dir']['remark']}</td>
				</tr>
				<tr class="{$ret['server']['disk']['class']}">
					<td>磁盘空间</td>
					<td>{$ret['server']['disk']['value']}</td>
					<td>{$ret['server']['disk']['remark']}</td>
				</tr>
				<tr class="{$ret['server']['upload']['class']}">
					<td>上传限制</td>
					<td>{$ret['server']['upload']['value']}</td>
					<td>{$ret['server']['upload']['remark']}</td>
				</tr>
			</table>
		</div>

		<div class="alert alert-info">PHP环境要求必须满足下列所有条件，否则系统或系统部份功能将无法使用。</div>
		<div class="panel panel-default">
			<div class="panel-heading">PHP环境要求</div>
			<table class="table table-striped">
				<tr>
					<th style="width:150px;">选项</th>
					<th style="width:180px;">要求</th>
					<th style="width:50px;">状态</th>
					<th>说明及帮助</th>
				</tr>
				<tr class="{$ret['php']['version']['class']}">
					<td>PHP版本</td>
					<td>5.4或者5.4以上</td>
					<td>{$ret['php']['version']['value']}</td>
					<td>{$ret['php']['version']['remark']}</td>
				</tr>
				<tr class="{$ret['php']['curl']['class']}">
					<td>cURL</td>
					<td>支持</td>
					<td>{$ret['php']['curl']['value']}</td>
					<td>{$ret['php']['curl']['remark']}</td>
				</tr>
				<tr class="{$ret['php']['ssl']['class']}">
					<td>openSSL</td>
					<td>支持</td>
					<td>{$ret['php']['ssl']['value']}</td>
					<td>{$ret['php']['ssl']['remark']}</td>
				</tr>
				<tr class="{$ret['php']['gd']['class']}">
					<td>GD2</td>
					<td>支持</td>
					<td>{$ret['php']['gd']['value']}</td>
					<td>{$ret['php']['gd']['remark']}</td>
				</tr>
				<tr class="{$ret['php']['asp_tags']['class']}">
					<td>asp_tags</td>
					<td>关闭</td>
					<td>{$ret['php']['asp_tags']['value']}</td>
					<td>{$ret['php']['asp_tags']['remark']}</td>
				</tr>
			</table>
		</div>

		<div class="alert alert-info">系统要求{$ONEZ_PRODUCT_SHORTNAME}整个安装目录必须可写, 才能使用{$ONEZ_PRODUCT_SHORTNAME}所有功能。</div>
		<div class="panel panel-default">
			<div class="panel-heading">目录权限监测</div>
			<table class="table table-striped">
				<tr>
					<th style="width:150px;">目录</th>
					<th style="width:180px;">要求</th>
					<th style="width:50px;">状态</th>
					<th>说明及帮助</th>
				</tr>
				<tr class="{$ret['write']['root']['class']}">
					<td>/</td>
					<td>整目录可写</td>
					<td>{$ret['write']['root']['value']}</td>
					<td>{$ret['write']['root']['remark']}</td>
				</tr>
				<tr class="{$ret['write']['cache']['class']}">
					<td>/cache</td>
					<td>cache目录可写</td>
					<td>{$ret['write']['cache']['value']}</td>
					<td>{$ret['write']['cache']['remark']}</td>
				</tr>
				<tr class="{$ret['write']['plugins']['class']}">
					<td>/plugins</td>
					<td>plugins目录可写</td>
					<td>{$ret['write']['plugins']['value']}</td>
					<td>{$ret['write']['plugins']['remark']}</td>
				</tr>
			</table>
		</div>
		<form class="form-inline" role="form" method="post">
			<input type="hidden" name="do" id="do" />
			<ul class="pager">
				<li class="previous"><a href="javascript:;" onclick="$('#do').val('back');$('form')[0].submit();"><span class="glyphicon glyphicon-chevron-left"></span> 返回</a></li>
				{$continue}
			</ul>
		</form>
EOF;
	tpl_frame();
}

function tpl_install_db($error = '') {
  global $G;
	if(!empty($error)) {
		$message = '<div class="alert alert-danger">发生错误: ' . $error . '</div>';
	}
	$insTypes = array();
	if(file_exists(ONEZ_ROOT . '/index.php') && is_dir(ONEZ_ROOT . '/app') && is_dir(ONEZ_ROOT . '/web')) {
		$insTypes['local'] = ' checked="checked"';
	} else {
		$insTypes['remote'] = ' checked="checked"';
	}
	if (!empty($_POST['type'])) {
		$insTypes = array();
		$insTypes[$_POST['type']] = ' checked="checked"';
	}
	$disabled = empty($insTypes['local']) ? ' disabled="disabled"' : '';
  $db=$_POST['db'];
  $user=$_POST['user'];
  $extra=$_POST['extra'];
  empty($db['server']) && $db['server']='localhost';
  empty($db['username']) && $db['username']='root';
  empty($db['prefix']) && $db['prefix']='onez_';
  empty($db['name']) && $db['name']=str_replace('.','_',ONEZ_PRODUCT_PTOKEN);
  
  if($G['hasdata']){
    $error=<<<ONEZ
				<div class="form-group">
					<label class="col-sm-2 control-label"></label>
					<div class="col-sm-8">
						<input type="checkbox" name="clear" value="1" id="input-clear" />
            <label for="input-clear" style="color:red">
              您的数据库不为空，请重新建立数据库或是清空该数据库或更改表前缀！
              
              如选中此项，将会自动删除已有数据，请慎重操作
            </label>
					</div>
				</div>
ONEZ;
  }
	echo <<<EOF
	{$message}
	<form class="form-horizontal" method="post" role="form">
		<div class="panel panel-default">
			<div class="panel-heading">数据库选项</div>
			<div class="panel-body">
				<div class="form-group">
					<label class="col-sm-2 control-label">数据库主机</label>
					<div class="col-sm-4">
						<input class="form-control" type="text" name="db[server]" value="$db[server]">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">数据库用户</label>
					<div class="col-sm-4">
						<input class="form-control" type="text" name="db[username]" value="$db[username]">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">数据库密码</label>
					<div class="col-sm-4">
						<input class="form-control" type="text" name="db[password]" value="$db[password]">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">表前缀</label>
					<div class="col-sm-4">
						<input class="form-control" type="text" name="db[prefix]" value="$db[prefix]">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">数据库名称</label>
					<div class="col-sm-4">
						<input class="form-control" type="text" name="db[name]" value="$db[name]">
					</div>
				</div>
        $error
			</div>
		</div>
		<div class="panel panel-default">
			<div class="panel-heading">管理选项</div>
			<div class="panel-body">
				<div class="form-group">
					<label class="col-sm-2 control-label">管理员账号</label>
					<div class="col-sm-4">
						<input class="form-control" type="username" name="user[username]" value="$user[username]">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">管理员密码</label>
					<div class="col-sm-4">
						<input class="form-control" type="password" name="user[password]" value="$user[password]">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">确认密码</label>
					<div class="col-sm-4">
						<input class="form-control" type="password" name="user[password]" value="$user[password]">
					</div>
				</div>
			</div>
		</div>
		<input type="hidden" name="do" id="do" />
		<input type="hidden" name="extra" value="{$extra}" />
		<ul class="pager">
			<li class="previous"><a href="javascript:;" onclick="$('#do').val('back');$('form')[0].submit();"><span class="glyphicon glyphicon-chevron-left"></span> 返回</a></li>
			<li class="previous"><a href="javascript:;" onclick="$('#do').val('continue');$('form')[0].submit();">继续 <span class="glyphicon glyphicon-chevron-right"></span></a></li>
			<li style="color:red;padding-top:5px;display:inline-block">(点击继续安装时会读取云端插件，可能会出现短时间的卡顿，请耐心等待！)</li>
		</ul>
	</form>
	<script>
		var lock = false;
		function check(obj) {
			if(lock) {
				return;
			}
			$('.form-control').parent().parent().removeClass('has-error');
			var error = false;
			$('.form-control').each(function(){
				if($(this).val() == '') {
					$(this).parent().parent().addClass('has-error');
					this.focus();
					error = true;
				}
			});
			if(error) {
				alert('请检查未填项');
				return false;
			}
			if($(':password').eq(0).val() != $(':password').eq(1).val()) {
				$(':password').parent().parent().addClass('has-error');
				alert('确认密码不正确.');
				return false;
			}
			lock = true;
			$(obj).parent().addClass('disabled');
			$(obj).html('正在执行安装');
			return true;
		}
	</script>
EOF;
	tpl_frame();
  exit();
}
function tpl_install_onez($error = '',$content='') {
	if(!empty($error)) {
		$message = '<div class="alert alert-danger">发生错误: ' . $error . '</div>';
	}
  if($content){
    echo $content;
  	tpl_frame();
    exit();
  }
  
  
  $onez=$_POST['onez'];
  $time=time();
	echo <<<EOF
	{$message}
  <div class="alert alert-info">登录成功后可以自动注册您的站点，如已购买过产品，可以选择后快速安装</div>
	<form class="form-horizontal" method="post" role="form">
		<div class="panel panel-default">
			<div class="panel-heading">登录佳蓝账号</div>
			<div class="panel-body">
				<div class="form-group">
					<label class="col-sm-2 control-label">佳蓝账号</label>
					<div class="col-sm-4">
						<input class="form-control" type="text" name="onez[username]" value="$onez[username]">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">佳蓝密码</label>
					<div class="col-sm-4">
						<input class="form-control" type="password" name="onez[password]" value="$onez[password]">
					</div>
				</div>
			</div>
		</div>
		<input type="hidden" name="do" id="do" />
		<input type="hidden" name="onez[type]" value="login" />
		<ul class="pager">
			<li class="previous"><a href="javascript:;" onclick="$('#do').val('back');$('form')[0].submit();"><span class="glyphicon glyphicon-chevron-left"></span> 返回</a></li>
			<li class="previous"><a href="javascript:;" onclick="$('#do').val('continue');$('form')[0].submit();">继续 <span class="glyphicon glyphicon-chevron-right"></span></a></li>
			<li class="previous"><a href="http://www.onez.cn/onez/YmJzfC91c2VyL3JlZy5waHA=/mod/bbs.html" target="_blank">免费注册佳蓝账号 <span class="glyphicon glyphicon-fire"></span></a></li>
		</ul>
	</form>
	<script>
		var lock = false;
		function check(obj) {
			if(lock) {
				return;
			}
			$('.form-control').parent().parent().removeClass('has-error');
			var error = false;
			$('.form-control').each(function(){
				if($(this).val() == '') {
					$(this).parent().parent().addClass('has-error');
					this.focus();
					error = true;
				}
			});
			if(error) {
				alert('请检查未填项');
				return false;
			}
			lock = true;
			$(obj).parent().addClass('disabled');
			$(obj).html('正在执行安装');
			return true;
		}
	</script>
EOF;
	tpl_frame();
  exit();
}

function tpl_install_finish() {
  global $G;
  include_once(dirname(__FILE__).'/lib/onezphp.php');
	$modules = get_store_module();
	$themes = get_store_theme();
  $_REQUEST['mod']='/admin/index.php';
  $regurl=onez('onez')->href('/onez/cloud/profile.php');
  if(!$modules && !$themes){
    $classname='hide';
  }
  $ONEZ_PRODUCT_SUBJECT=ONEZ_PRODUCT_SUBJECT;
	echo <<<EOF
	<div class="page-header"><h3>安装完成</h3></div>
	<div class="alert alert-success">
		恭喜您!已成功安装“{$ONEZ_PRODUCT_SUBJECT}”系统，您现在可以: <a target="_blank" class="btn btn-success" href="./index.php">访问网站首页</a>
	</div>
	<div class="form-group $classname">
		<h5><strong>应用商城</strong></h5>
		<span class="help-block">应用商城特意为您推荐了一批优秀模块、主题，赶紧来安装几个吧！</span>
		<table class="table table-bordered">
			<tbody>
				{$modules}
				{$themes}
			</tbody>
		</table>
	</div>
EOF;
	tpl_frame();
}


function get_store_module() {
	$response = onez()->post(APP_STORE_API, http_build_query(array('action' => 'module')));
  $response=json_decode($response,1);
  if(empty($response) || empty($response['content'])){
    return;
  }
	$response = $response['content'];

	$modules = '';
  if(!$response['message']){
    return '';
  }
	foreach ($response['message'] as $key => $module) {
		if ($key % 3 < 1) {
			$modules .= '</tr><tr>';
		}
		$module['detail_link'] = APP_STORE_URL . trim($module['detail_link'], '.');
		$modules .= '<td>';
		$modules .= '<div class="col-sm-4">';
		$modules .= '<a href="' . $module['detail_link'] . '" title="查看详情" target="_blank">';
		$modules .= '<img src="' . $module['logo']. '"' . ' width="50" height="50" ' . $module['title'] . '" /></a>';
		$modules .= '</div>';
		$modules .= '<div class="col-sm-8">';
		$modules .= '<p><a href="' . $module['detail_link'] .'" title="查看详情" target="_blank">' . $module['title'] . '</a></p>';
		$modules .= '<p>安装量：<span class="text-danger">' . $module['purchases'] . '</span></p>';
		$modules .= '</div>';
		$modules .= '</td>';
	}
	$modules = substr($modules, 5) . '</tr>';

	return $modules;
}

function get_store_theme() {
	$response = onez()->post(APP_STORE_API, http_build_query(array('action' => 'theme')));
  $response=json_decode($response,1);
  if(empty($response) || empty($response['content'])){
    return;
  }
	$response = $response['content'];

	$modules = '';
  if(!$response['message']){
    return '';
  }

	$themes = '<tr><td colspan="' . count($response['message']) . '">';
	$themes .= '<div class="form-group">';
	foreach ($response['message'] as $key => $theme) {
		$theme['detail_link'] = APP_STORE_URL . trim($theme['detail_link'], '.');
		$themes .= '<div class="col-sm-2" style="padding-left: 7px;margin-right: 25px;">';
		$themes .= '<a href="' . $theme['detail_link'] .'" title="查看详情" target="_blank" /><img src="' . $theme['logo']. '" /></a>';
		$themes .= '<p></p><p class="text-right">';
		$themes .= '<a href="' . $theme['detail_link']. '" title="查看详情" target="_blank">'  . $theme['title'] . '</a></p>';
		$themes .= '</div>';
	}
	$themes .= '</div>';

	return $themes;
}

function tpl_resources() {
	$res = array(
		'logo' => ' ',
	);
	return $res;
}