<?php

/* ========================================================================
 * $Id: mysql.php 3501 2020-07-07 13:01:11Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onez.cn
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
# 填写数据库信息
# mysql
#请求数据
if($MODE=='$post'){
  $action=onez()->gp('action');
  if($action=='data'){
    $ac=onez()->gp('ac');
    if($ac=='check'){
      $db=$_REQUEST;
      $A['hasdata']=0;
      if(!$db['server']){
        return $this->error('数据库地址不能为空');
      }
      if(!$db['username']){
        return $this->error('数据库账号不能为空');
      }
      if(!$db['name']){
        return $this->error('数据库名称不能为空');
      }
      if(!$db['prefix']){
        return $this->error('表名前缀不能为空');
      }
      if(version_compare(PHP_VERSION, '7.0.0') == -1) {
        $link=@mysql_connect($db['server'], $db['username'], $db['password']);
        if(!$link){
          return $this->error('无法连接数据库，请检查数据库账号和密码是否正确');
        }
      	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)){
            return $this->error('数据库<code>'.$db['name'].'</code>不存在且无法创建，请检查['.$error.']');
          }
        }
    		$statement = @mysql_query("SHOW TABLES LIKE '{$db['prefix']}%';",$link);
    		if ($statement) {
          $rs=mysql_fetch_array($statement);
          if($rs){
            $A['hasdata']=1;
          }
    		}
      }else{
        $link=mysqli_connect($db['server'], $db['username'], $db['password']);
        if(!$link){
          return $this->error('无法连接数据库，请检查数据库账号和密码是否正确');
        }
      	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=mysqli_error();
          if(!mysqli_select_db($link,$db['name'])){
            return $this->error('数据库<code>'.$db['name'].'</code>不存在且无法创建，请检查['.$error.']');
          }
        }
    		$statement = mysqli_query($link,"SHOW TABLES LIKE '{$db['prefix']}%';");
    		if ($statement) {
          $rs=mysqli_fetch_array($statement);
          if($rs){
            $A['hasdata']=1;
          }
    		}
      }
      unset($db['$action']);
      unset($db['action']);
      unset($db['$file']);
      unset($db['step']);
      unset($db['ac']);
      $A['db']=$db;
      return;
    }
    $items=array();
    $A['items']=$items;
  }
  return;
}
?>
<van-form @submit="onSubmit">
  <van-field
    v-model="mydata.server"
    name="server"
    label="数据库主机"
    placeholder="数据库主机"
  />
  <van-field
    v-model="mydata.username"
    name="username"
    label="数据库用户"
    placeholder="数据库用户"
  />
  <van-field
    v-model="mydata.password"
    name="password"
    label="数据库密码"
    placeholder="数据库密码"
  />
  <van-field
    v-model="mydata.prefix"
    name="prefix"
    label="表前缀"
    placeholder="表前缀"
  />
  <van-field
    v-model="mydata.name"
    name="name"
    label="数据库名称"
    placeholder="数据库名称"
  />
  <div class="btns">
    <van-button type="primary" @click="prev" native-type="button">上一步</van-button>
    <van-button type="primary" :disabled="error>0" native-type="submit">下一步</van-button>
  </div>
</van-form>