<?php

defined('_IN_JOHNADM') or die('Error: restricted access');

/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::class);

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);
$config = $container->get('config')['johncms'];

if (!is_dir(ROOT_PATH . 'files/sql/')) {
    mkdir(ROOT_PATH . 'files/sql');
}


if ($systemUser->id !== '1') {
    header('Location: /?err');
    exit;
}
 
 

echo '<div class="mainblok">';
    if ($_FILES['fail']['size'] > 0) {
        $file = strtolower($_FILES['fail']['name']);
        $al_ext = array('txt', 'sql');
        $ext = explode(".", $file);
        if (count($ext) != 2 || !in_array($ext[1], $al_ext)) {
            echo $tools->displayError('By sending files that have allowed only a name and one extension (<b> name.ext </b>) and the extension should be txt or sql');
        } else {
            $file = $realtime . '.' . $ext[1];
            if ((move_uploaded_file($_FILES["fail"]["tmp_name"], "../files/sql/$file")) == true) {
                @chmod("$fname", 0777);
                @chmod("../files/sql/$fname", 0777);
                $sql = file_get_contents('../files/sql/' . $file);
                $sql = str_replace("\'", "'", trim($sql));
                $sql = explode(";(\n|\r)*", $sql);
//if error use preg_split
                $total_sql = 0;
                $total_sqk_ok = 0;
                for ($i = 0; $i < count($sql); $i++) {
                    if ($sql[$i] != '') {
                        ++$total_sql;
                        if ($db->query($sql[$i])) {
                            ++$total_sql_ok;
                        }
                    }
                }
                if ($total_sql) {
                    echo '<div class="gmenu">' . (($total_sql_ok == 1 && $total_sql = 1) ? 'The query completed successfully' : ' Achieved ' . $total_sql_ok . ' requests from the ' . $total_sql) . '</div>';
                } else {
                    echo '<div class="rmenu">Error</div>';
                    unlink("../files/sql/$file");
                }
            } else {
                echo $tools->displayError('Error temporarily save the file maybe directory not found!');
                echo '<div class="gmenu"><a href="index.php?act=mysql">Repeat</a></div>';
                echo '<div class="phdr"><a href="index.php">Panel</a></div>';
           }
        }
    } else
        if (isset($_POST['submit']) && $_POST['sql']) {
            $sql = str_replace("\'", "'", trim($_POST['sql']));
            $sql = explode(";(\n|\r)*", $sql);
            $total_sql = 0;
            $total_sqk_ok = 0;
            for ($i = 0; $i < count($sql); $i++) {
                if ($sql[$i] != '') {
                    ++$total_sql;
                    if ($db->query($sql[$i])) {
                        ++$total_sql_ok;
                    }
                }
            }
            if ($total_sql) {
                echo '<div class="gmenu">' . (($total_sql_ok == 1 && $total_sql = 1) ? ' The query completed successfully ' : 'Done ' . $total_sql_ok . ' requests from the ' . $total_sql) . '</div>';
            } else {
                echo '<div class="rmenu">Error</div>';
                echo '<div class="gmenu"><a href="index.php?act=mysql">Repeat</a></div>';
                echo '<div class="phdr"><a href="index.php">Panel</a></div>';
           }
        } else {
        	$array = array (
        		1=> 'CREATE TABLE `table` (`pole` varchar(500) NOT NULL default \'0\') ENGINE=MyISAM DEFAULT CHARSET=utf8;',
            	'DROP TABLE IF EXISTS `table`;',
            	'RENAME TABLE `table` TO `table2`;',
            	'TRUNCATE `table`;',
            	'ALTER TABLE `table` ADD `pole` varchar(500) NOT NULL default \'0\';',
            	'ALTER TABLE `table` DROP `pole`;',
           		'DELETE FROM `table` WHERE `pole`=\'\';',
            	'OPTIMIZE TABLE `table`',
            	'UPDATE `table` SET `pole`=\'\' WHERE `pole`=\'\';',
            	'INSERT INTO `table` SET `browser` = \'\';'
			);
        	$array_two = array (
        		1=> 'Create a table',
            	'Remove table',
            	'Rename the worksheet',
            	'Cleat the table',
            	'Add Field',
            	'Remove field',
           	'Delete entry',
            	'Optimize table',
            	'Update Record',
            	'Add Item'
			);
			$implode = array();
			echo '<div class="phdr"><b>MySQL Request</b></div>';
            echo '<div class="rmenu"><form action="index.php?act=mysql" method="post" enctype="multipart/form-data"><b>Request:</b><br />
      		<textarea rows="3" name="sql">' . $array[$id] . '</textarea><br /><b>Imports:</b><br/><input type="file" name="fail"/><br /><input type="submit" name="submit" value="Run"/></form></div><div class="gmenu">';
      		foreach($array_two as $key=>$val) {
            	echo '<a href="index.php?act=mysql&amp;id=' . $key . '">' . $val . '</a><br />';
			}
			echo '</div><div class="gmenu"> The file must be in UTF-8 and have the extension txt or sql </div>';
            echo '<div class="phdr"><a href="backupmysql.php"><b>Manage SQL</b></a> | <a href="index.php">Panel</a></div>';
        }

echo '</div>';




?>