<?php

/* ========================================================================
 * $Id: index.php 2731 2020-07-07 14:07:55Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onez.cn
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
header('Access-Control-Allow-Origin:*');  
header('Access-Control-Allow-Methods:POST,OPTIONS');  
header('Access-Control-Allow-Headers:x-requested-with,content-type');
header('Access-Control-Allow-Credentials:true');
$bsFile=ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/config.php';
$G['config']=array();
if(file_exists($bsFile)){
  $G['config']=include($bsFile);
}
$_role=onez()->gp('_role');
$_update=(int)onez()->gp('_update');
if($_role!='develop'){
  if(defined('ONEZ_BS_SITE_FILE')){
    $siteInfo=include(ONEZ_BS_SITE_FILE);
    if($siteInfo['siteid']){
      $siteId=$siteInfo['siteid'];
      $siteFile=ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/sites/infos/'.$siteId.'.php';
      if(file_exists($siteFile)){
        if($_update){
          @unlink(ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/sites/infos/'.$siteId.'.build');
        }
        $G['site']=include($siteFile);
        if($siteInfo['ptoken']){
          $G['site']['lockPlugin']=$siteInfo['ptoken'];
        }
        $G['siteid']=(int)$G['site']['id'];
        $G['this']->loadSite($G['site']);
        exit();
      }
    }
  }
  #站点判断
  $host=$_SERVER['HTTP_HOST'];
  $domainFile=ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/sites/domains.php';
  if(file_exists($domainFile)){
    $domains=include($domainFile);
    if($domains[$host]){
      $siteId=$domains[$host];
      $siteFile=ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/sites/infos/'.$siteId.'.php';
      if(file_exists($siteFile)){
        if($_update){
          @unlink(ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/sites/infos/'.$siteId.'.build');
        }
        $G['site']=include($siteFile);
        $G['siteid']=(int)$G['site']['id'];
        $G['this']->loadSite($G['site']);
        exit();
      }
    }
  }
}


$action=onez()->gp('action');
if($action=='logout'){#退出
  onez('cache')->cookie($G['this']->cname,'del');
  onez()->ok('操作成功','reload');
}
#需要登录
$G['this']->login();
$G['title']='佳蓝智能开发';
$userFile=ONEZ_CACHE_PATH.'/plugins/'.$G['this']->token.'/sites/users/'.$G['userid'].'.php';
if(file_exists($userFile)){
  $G['bs']=include($userFile);
  $G['bs']['sitename'] && $G['title']=$G['bs']['sitename'];
}
$siteId=(int)$_GET['siteId'];
if($siteId){
  $G['site']=$G['this']->site($siteId);
}
onez('debug')->showerror();
onez('vue')->init(__FILE__);
onez('vue')->set('tpl','AdminLTE')->tpl();
onez('vue')->addPath($G['this']->path.'/www');
$buildFile=ONEZ_CACHE_PATH.'/plugins/bs/system.build';
if($_update|| !file_exists($buildFile)){
  onez('vue')->build();
  @touch($buildFile);
}
#onez('vue')->build();#调试阶段，每次都更新
onez('vue')->pc('index');