<?php

/**
 * Smart Report Maker
 * Version 9.0.0
 * Author : Webuccino
 * All copyrights are preserved to Webuccino
 * URL : http://mysqlreports.com/
 *
 */
//handle super globals
define("DIRECTACESS", 1);
require_once "../request.php";

if (online_demo_mode === 1) {
    $response           = array();
    $response["result"] = "success";

    $response = json_encode($response);
    echo $response;
    exit();
}

$_GET = array();
//the password could include dangrous special characters

$_POST   = clean_input_array($_POST);
$_ENV    = array();
$_FILES  = array();
$_COOKIE = array();

if (!isset($_POST["token"]) || empty($_POST["token"]) || $_POST["token"] != $_SESSION[Session_wizard_key]["request_token_wizard"]) {
    $response                 = array();
    $response["result"]       = "error";
    $response["errorMessage"] = "Not allowed for security reasons!";
    $response                 = json_encode($response);
    echo $response;
    exit();
}

require_once "sessionCleaner.php";
$is_form_valid = 1;
$page_errors   = "";

// set connection in session

//Case: Connect

//******************************************************************************************8
if (isset($_POST["connection"])) {

    unsetAllSession();

    $connection_name = isset($_POST["connection"]) ? $_POST["connection"] : "";

    if (empty($connection_name) || $connection_name === '') {
        $page_errors .= "* Please select a connection.";
        $is_form_valid = 0;
    }

    if ($is_form_valid === 1) {

        if ($connection_name != default_connection_name) {
            $parameters = $srm_dashboard->get_connection_details(trim($connection_name));
        } else {
            $parameters = $srm_dashboard->get_default_connection_details();
        }

        $DB_HOST      = isset($parameters["server"]) ? $parameters["server"] : "";
        $DB_USER      = isset($parameters["user"]) ? $parameters["user"] : "";
        $DB_PASSWORD  = isset($parameters["password"]) ? $parameters["password"] : "";
        $DB_NAME      = isset($parameters["db"]) ? $parameters["db"] : "";
        $DB_CHARSET   = isset($parameters["charset"]) ? $parameters["charset"] : "";
        $DB_COLLATION = isset($parameters["collation"]) ? $parameters["collation"] : "";
        $DB_PORT      = isset($parameters["port"]) ? $parameters["port"] : "";

        $dbHandler = new DatabaseHandler($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);

        list($is_connection_failed, $connection_error) = $dbHandler->is_connection_failed();

        // if(!empty($connection_error)){
        $page_errors .= "# MySQL Connection Error: " . $connection_error;

// }
        if (!$dbHandler || $is_connection_failed) {
            if (!empty($page_errors)) {
                $page_errors .= "<br>";
            }

            // debug mode
            $page_errors .= "# Result: Unable to connect to your MySQL server.<br/>";

// production mode
            // $page_errors .= "# Result: Unable to connect to your MySQL server.";

            $response                 = array();
            $response["result"]       = "error";
            $response["errorMessage"] = $page_errors;
            $dbHandler->close_connection();
            $response = json_encode($response);
            echo $response;
            exit();
        } else {
            // save data in the sessions
            $_SESSION[Session_report_Settings_key]["connection_name"] = $connection_name;

            if (Session_Save_Parameters == 1) {
                $_SESSION[Session_report_Settings_key]["host"]      = $DB_HOST;
                $_SESSION[Session_report_Settings_key]["user"]      = $DB_USER;
                $_SESSION[Session_report_Settings_key]["pass"]      = $DB_PASSWORD;
                $_SESSION[Session_report_Settings_key]["collation"] = $DB_COLLATION;
                $_SESSION[Session_report_Settings_key]["charset"]   = $DB_CHARSET;
                $_SESSION[Session_report_Settings_key]["port"]      = $DB_PORT;
            }

            $_SESSION[Session_report_Settings_key]["db"] = $DB_NAME;

            $_SESSION[Session_wizard_key]["validate_key"] = md5("srm_f92024_report_settings_valid_1010");

            $dbHandler->close_connection();
        }

        $response           = array();
        $response["result"] = "success";
        defined('BASEPATH') or define('BASEPATH', 1);
        require_once "../../Reports9/shared/helpers/Model/safeValue.php";

//$TemplateManager = new  TemplateManager("../../Reports9/",$user_name, $db);

//getting all templates
        //$arr = $TemplateManager->get_all_templates();

        $response = json_encode($response);
        echo $response;
    } else {
        $response                 = array();
        $response["result"]       = "error";
        $response["errorMessage"] = $page_errors;
        $response                 = json_encode($response);
        echo $response;
    }

    exit();
}

// set database and data source in session

//Case: Continue
//******************************************************************************************8
require_once "../lib.php";

if (isset($_POST['dataSource']) && isset($_SESSION[Session_wizard_key]["validate_key"]) && $_SESSION[Session_wizard_key]["validate_key"] == md5("srm_f92024_report_settings_valid_1010")) {

    if (!$_SESSION[Session_wizard_key]["edit_mode"]) {
        unsetSessionStartFromDataSource();
    }

    $datasources = array(
        "table",
        "sql"
    );
    $data_source = $_POST['dataSource'];

    if ($data_source == '' || !in_array($data_source, $datasources)) {

        if (!empty($page_errors)) {
            $page_errors .= "<br>";
        }

        $page_errors .= "* Please select a valid data source.";
        $is_form_valid = 0;
    }

    if ($is_form_valid === 1) {

        $_SESSION[Session_report_Settings_key]["datasource"] = $data_source;
        $response["result"]                                  = "success";
        $response["errorMessage"]                            = $page_errors;
        $response                                            = json_encode($response);
        echo $response;
        exit();
    } else {
        $response["result"]       = "error";
        $response["errorMessage"] = $page_errors;
        $response                 = json_encode($response);
        echo $response;
    }

    exit();
}
