<?php

/* ========================================================================
 * $Id: done.php 7848 2020-07-08 19:31:34Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onez.cn
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
# 安装完成
# done
#请求数据
if($MODE=='$post'){
  $action=onez()->gp('action');
  if($action=='data'){
    $ac=onez()->gp('ac');
    if($ac=='install'){
      define('IS_ONEZ_DEBUG',1);
      try {
        $ptoken=onez()->gp('ptoken');
        if(defined('ONEZ_PRODUCT_PTOKEN')){
          $ptoken=ONEZ_PRODUCT_PTOKEN;
        }
        $pname=onez()->gp('pname');
        if(defined('ONEZ_PRODUCT_TITLE')){
          $pname=ONEZ_PRODUCT_TITLE;
        }
        $db=onez()->gp('db');
        $user=onez()->gp('user');
        $siteinfo=array();
        $siteinfo['ptoken']=$ptoken;
        $siteinfo['ip']=onez()->ip();
        $siteinfo['homepage']=onez()->homepage();
        $siteinfo['time']=time();
        $siteinfo['date']=date('Y-m-d H:i:s');
        $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);
        
        $datatype=onez()->gp('datatype');
        if($datatype=='new'||$datatype=='demo'){
          onez('db')->db()->query("DROP TABLE IF EXISTS {$db['prefix']}data;");
          onez('db')->db()->query("DROP TABLE IF EXISTS {$db['prefix']}device;");
          onez('db')->db()->query("DROP TABLE IF EXISTS {$db['prefix']}member;");
          onez('db')->db()->query("DROP TABLE IF EXISTS {$db['prefix']}order;");
          //创建数据表
          
          #安装数据库
          $sysFile=ONEZ_ROOT.'/cache/dbtables.php';
          if(!file_exists($sysFile)){
            file_put_contents($sysFile,file_get_contents(ONEZ_BS_APIURL.'?'.http_build_query(array(
              'action'=>'file',
              'filename'=>'cache/dbtables.php',
            ))));
          }
          $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);
            }
          }
        }
        //写入主站数据
        $rndkey=uniqid();
        $T=onez('db')->open('member')->one("grade='admin'");
        if($T){
          $item=array(
            'nickname'=>'超级管理员',
            'username'=>$user['username'],
            'grade'=>'admin',
            'rndkey'=>$rndkey,
            //'password'=>hash_hmac('md5',$user['password'],$rndkey),
            'password'=>md5($user['password']),
          );
          onez('db')->open('member')->update($item,"id='$T[userid]'");
          $userid=$T['userid'];
        }else{
          $userid=onez('db')->open('member')->insert(array(
            'nickname'=>'超级管理员',
            'username'=>$user['username'],
            'grade'=>'admin',
            'rndkey'=>$rndkey,
            //'password'=>hash_hmac('md5',$user['password'],$rndkey),
            'password'=>md5($user['password']),
            'infotime'=>time(),
            'infoip'=>onez()->ip(),
          ));
        }
        $G['userid']=$userid=intval($userid);
        #
        if($ptoken!='bs'){
          //创建站点
          $G['this']=onez('bs');
          $G['this']->init();
          $onez=array();
          $onez['name']=$pname?$pname:$ptoken;
          $item=array();
          $item['name']=$onez['name'];
          $item['summary']='';
          $item['userid']=(int)$userid;
          $onez['data']=$G['this']->data()->open('datas')->insert($item);
          
          $item=array();
          $item['name']=$onez['name'];
          $item['summary']='';
          $item['userid']=(int)$userid;
          $item['grades']=array(
            'admin'=>'超级管理员',
            'user'=>'普通用户',
          );
          $item['gradesTxt']="admin=超级管理员\nuser=普通用户";
          $onez['user']=$G['this']->data()->open('users')->insert($item);
          $onez['userid']=(int)$userid;
          $siteinfo['siteid']=$G['this']->data()->open('sites')->insert($onez);
          $G['this']->siteUpdate();
          
          
          #站点文件
          $siteFile=ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/sites/infos/'.$siteinfo['siteid'].'.php';
          if(file_exists($siteFile)){
            $G['site']=include($siteFile);
            $G['siteId']=(int)$G['site']['id'];
            include_once(onez('bs')->path.'/lib/site.onez.php');
            $Site=new onezphp_bs_site($G['site']);
            $Site->init();
          }
          if($datatype=='demo'){
            $sysFile=ONEZ_ROOT.'/cache/datas.default.db';
            $_REQUEST['siteid']=0;
            if(file_exists($sysFile)){
              $value=onez()->read($sysFile);
              $value=base64_encode($value);
              $value=onez()->strcode($value,'DECODE','onez');
              $value=json_decode($value,1);
              if($value){
                $admins=$value['admins']?$value['admins']:array();
                foreach($value['tables'] as $table=>$records){
                  foreach($records as $r){
                    unset($r['siteid']);
                    if($table=='member'){
                      $db=$G['this']->userdb()->noappid()->open($table);
                    }else{
                      $db=$G['this']->data()->noappid()->open($table);
                    }
                    $t=$db->one("id='$r[id]'");
                    if(!$t){
                      if(in_array($r['userid'],$admins)){
                        $r['userid']=(int)$admins[0];
                      }
                      if($table=='setting'){
                        $r['index12']=(int)$G['siteId'];
                      }
                      $db->insert($r);
                    }
                  }
                }
              }
            }
          }
        
          $T2=$G['this']->userdb()->open('member')->one("key1='admin'");
          if(!$T2){
            $item=array();
            $item['key1']='admin';
            $item['key2']=$user['username'];
            $item['key3']=md5($user['password']);
            $item['key4']='超级管理员';
            $item['key5']='安装向导创建';
            $item['index11']=(int)$G['this']->site['userid'];
            $item['index12']=$G['this']->siteId;
            $siteUid=$G['this']->userdb()->open('member')->insert($item);
          }else{
            $item=array();
            $item['key2']=$user['username'];
            $item['key3']=md5($user['password']);
            $G['this']->userdb()->open('member')->update($item,"id='$T2[id]'");
            $siteUid=$T2['id'];
          }
          $siteinfo['userid_bs_first']=$userid;
          $siteinfo['userid_site_first']=$siteUid;
        }
        
        $siteinfo=var_export($siteinfo,1);
        $code=<<<ONEZ
<?php
!defined('IN_ONEZ') && exit('Access Denied');
return $siteinfo;
ONEZ;
        onez()->write(ONEZ_BS_SITE_FILE,$code);
        
        $A['pagedata']['image']='http://cdn.onez.cn/icons/2020/0707/2020070713485243980001.svg';
        $A['pagedata']['summary']='产品安装成功';
        $A['pagedata']['success']=true;
        $A['pagedata']['clear']=true;
      }catch(Exception $e) {
        $A['pagedata']['image']='error';
        $A['pagedata']['summary']=$e->getMessage();
        $A['pagedata']['success']=false;
      }
      $A['pagedata']['ready']=true;
    }
  }
  return;
}
?>
<template v-if="pagedata && pagedata.ready">
<van-empty :image="pagedata.image" :description="pagedata.summary">
  <van-button round type="info" v-if="pagedata.success" @click="done" class="bottom-button">
    进入网站
  </van-button>
  <van-button type="primary" v-else @click="prev" native-type="button">返回上一步</van-button>
</van-empty>
</template>