<?php

##by fatma younis 29-05-2018
// get the config file
include_once("../public/config.php");
//get the do the action
$do = isset($_GET['do']) ? $_GET['do'] : '';

/* ======================
  Controller Name :-restoredaybackup
  ======================== */
//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    //include_once("../public/authentication.php");
    $smarty->display("restoredaybackupview/getfile.html");
}
##
elseif ($do == "runsql") {
    //include_once("../public/authentication.php");
    try {
        $data = runsql();
        echo "<span style='color:green'> DONE. </span><br/>";
        print_r("<pre>");
        echo "<div style='border:1px solid green;margin-right:60%'><span style='color:green'> no of queries excuted successfully : " . $data[0] . " </span><br/>";
        echo "<span style='color:red'> no of queries gave errors : " . $data[1] . " </span><br/></div>";
        print_r("</pre>");
    } catch (Exception $e) {
        //echo $e;
        print_r("<pre style='text-align:left'>");
        echo "<span style='color:red'> Error. </span><br/>";
        print_r("</pre>");
    }
}


/* ===============================
  function in this CONTROLLER
  ================================ */

function runsql() {

    global $myBankEx;
    $errors = $success = 0;

    if ($_FILES['pfile']['error'] == UPLOAD_ERR_OK               //checks for errors
            && is_uploaded_file($_FILES['pfile']['tmp_name'])) { //checks that file is uploaded
        $handle = fopen($_FILES['pfile']['tmp_name'], 'rb');
        if ($handle) {
            $commands = file_get_contents($_FILES['pfile']['tmp_name']);
            ##end
            fclose($handle);
            $con = mysqli_connect(ConnectionProperty::getHost(), ConnectionProperty::getUser(), ConnectionProperty::getPassword(), ConnectionProperty::getDatabase());
            // Check connection
            if ($con->connect_error) {
                die("Connection failed: " . $con->connect_error);
            }
            //$charset = mysql_client_encoding($con);

            mysqli_query("SET NAMES 'utf8'");
            mysqli_query('SET CHARACTER_SET utf8');



            //delete comments
            $lines = explode("\n", $commands);
            $commands = '';
            foreach ($lines as $line) {
                $line = trim($line);
                if ($line && !strpos($line, '--')) {
                    $commands .= $line . "\n";
                }
            }

            //convert to array
            $commands = explode(";", $commands);
            foreach ($commands as $command) {
                if (trim($command)) {
                    if ($con->query($command) === TRUE) {
                        //echo "success";
//                        print_r("<pre>");
//                        print_r($command . "<br/><span style='color:green'>SUCESS</sapn><hr>");
//                        print_r("</pre>");
                        $success++;
                    } else {
                        //echo "Error: " . $conn->error . "<br/>";
                        $errorMessage = $con->error;
//                        if (stripos($errorMessage, "Duplicate column name") !== false) {
//                            //now  it is dublicate column doenot matter
//                            $noOfDuplicateColumnName++;
//                        } elseif (stripos($errorMessage, "Unknown column") !== false) {
//                            $noOfUnknownColumn++;
//                        } elseif (stripos($errorMessage, "Multiple primary key defined") !== false) {
//
//                        } else {
                        $errors++;
                        //show error
                        print_r("<pre>");
                        print_r($command . "<br/><span style='color:red'>Error: " . $errorMessage . "</sapn><hr>");
                        print_r("</pre>");
//                        }
                    }
                }
            }


//            $commands = trim($commands);
//            $res = mysqli_multi_query($conn, $commands); //mysqli_query for one query , mysqli_multi_query for multi query
            //print_r($commands);
            //print_r($res);
            //var_dump(mysqli_error($conn));
            //var_dump(mysqli_affected_rows($conn));
        }
    }

    return array($success, $errors);
}

?>