<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Index extends CI_Controller
{


    function __construct()
    {
        parent::__construct();
        $this->load->model('fastapi_model');
    }

    public function index()
    {
        $this->permission->is_admin(); //需要登陆
        $data = array();
        $this->load->view('welcome');
    }


    public function query_encrypt(){
        $sql = $this->input->get_post('sql_text');//sql语句
        $database = $this->input->get_post('database');//数据库
        if (!empty($sql) && !empty($database)) {
            $sql=base64_decode($sql);
            $database=base64_decode($database);
            return $this->query($sql,$database);
        }else{
            echo json_encode(array('result' => 'no', 'data' => '参数为空'));
        }
    }

    public function query_post()
    {
        $sql = $this->input->post('sql_text');//sql语句
        $database = $this->input->post('database');//数据库
        return $this->query($sql,$database);
    }

    public function query($sql,$database)
    {
        if ($this->input->ip_address() !== '116.8.4.34' && $this->input->ip_address() !== '180.140.114.225' && $this->input->ip_address() !== '127.0.0.1') { //不是公司的网络则不能访问
            //echo $this->input->ip_address();
            Header("HTTP/1.1 403 Forbidden");
            return false;
        }
        if (!empty($sql) && !empty($database)) {
            $result = $this->fastapi_model->get_query($database, $sql);
            if (!empty($result)) {
                echo json_encode(array('result' => 'ok', 'data' => $result));
            } else {
                echo json_encode(array('result' => 'no', 'data' => '查询不到数据'));
            }
        } else {
            echo json_encode(array('result' => 'no', 'data' => '参数为空'));
        }
    }

}
