<?php
require_once("inc/protecao-admin.php");

@mysqli_query($conexao,"CREATE TABLE IF NOT EXISTS `configuracoes_backup` (
  `ftp_servidor` varchar(255) NOT NULL,
  `ftp_usuario` varchar(255) NOT NULL,
  `ftp_senha` varchar(255) NOT NULL,
  `bkp_arquivos` char(3) NOT NULL,
  `bkp_mysql` char(3) NOT NULL,
  `bkp_mysql_stats` char(3) NOT NULL,
  `log` longtext NOT NULL
) ENGINE=MyISAM;");

if($_POST["ftp_servidor"]) {

$dados_config_bkp_atual = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM configuracoes_backup"));

if(empty($dados_config_bkp_atual['ftp_servidor'])) {

mysqli_query($conexao,"INSERT INTO configuracoes_backup (ftp_servidor,ftp_usuario,ftp_senha,bkp_arquivos,bkp_mysql,bkp_mysql_stats) VALUES ('".$_POST["ftp_servidor"]."','".$_POST["ftp_usuario"]."','".$_POST["ftp_senha"]."','".$_POST["bkp_arquivos"]."','".$_POST["bkp_mysql"]."','".$_POST["bkp_mysql_stats"]."')");

} else {

mysqli_query($conexao,"Update configuracoes_backup set ftp_servidor = '".$_POST["ftp_servidor"]."', ftp_usuario = '".$_POST["ftp_usuario"]."', ftp_senha = '".$_POST["ftp_senha"]."', bkp_arquivos = '".$_POST["bkp_arquivos"]."', bkp_mysql = '".$_POST["bkp_mysql"]."', bkp_mysql_stats = '".$_POST["bkp_mysql_stats"]."'");

}

// Cria o sessão do status das ações executadas e redireciona.
$_SESSION["status_acao"] = "<div class='alert alert-success text-center' role='alert'>Backup configurado com sucesso.</div>";

header("Location: /admin/admin-backup");
exit();
}

$dados_config_bkp = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM configuracoes_backup"));

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Administra&ccedil;&atilde;o</title>
  <link rel="stylesheet" href="/inc/materialdesignicons.min.css">
  <link rel="stylesheet" href="/inc/font-awesome.min.css">
  <link rel="stylesheet" href="/inc/vendor.bundle.base.css">
  <link rel="stylesheet" href="/inc/vendor.bundle.addons.css">
  <link rel="stylesheet" href="/inc/theme.css">
  <link rel="shortcut icon" href="/img/favicon.ico">
  <script type="text/javascript" src="/admin/inc/ajax.js"></script>
  <script type="text/javascript" src="/admin/inc/javascript.js"></script>
</head>

<body>
  <div class="container-scroller">
      <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
      <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center">
        <a class="navbar-brand brand-logo" href="/admin/admin-configuracoes"><span>Administra&ccedil;&atilde;o</span></a>
        <a class="navbar-brand brand-logo-mini" href="/admin/admin-configuracoes"><img src="/img/icones/img-icone-logo.png" title="Painel" width="32" height="32" border="0"></a>
      </div>
      <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end justify-content-lg-start">
        <button class="navbar-toggler navbar-toggler align-self-center" type="button" data-toggle="minimize">
          <span class="mdi mdi-menu text-icones"></span>
        </button>
        <li class="nav-item nav-search d-none d-lg-block">
            <div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text" id="search">
                  <i class="mdi mdi-magnify"></i>
                </span>
              </div>
              <input type="text" class="form-control" id="busca_chave" placeholder="Busca" aria-label="search" aria-describedby="search">
            </div>
          </li>
        <ul class="navbar-nav navbar-nav-right">
          <li class="nav-item">
            <a class="nav-link" href="/admin/sair"><i class="mdi mdi-logout f-22 text-icones" data-toggle="tooltip" data-placement="bottom" title="Sair/logout"></i></a>
          </li>
        </ul>
        <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas">
          <span class="mdi mdi-menu text-icones"></span>
        </button>
      </div>
    </nav>

    <!-- modal -->                
      <div class="modal fade" id="log-sistema" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-body" id="log-sistema-conteudo" style="text-align:center"><img src="/img/ajax-loader.gif" /></div>
            </div>
        </div>
      </div>
      
    <div class="container-fluid page-body-wrapper">      
    <?php require_once("menu-admin.php"); ?>
      <div class="main-panel">
        <div class="content-wrapper">
          <div id="local-status-acoes"><?php echo $_SESSION['status_acao'];unset($_SESSION['status_acao']); ?></div>
          <div class="row">            

            <div class="col-lg-12">
              <div class="card">
                  <div class="card-header">
                    <header>Backup</header>
                  </div>
                  <div class="card-body">
                      <form method="post" action="/admin/admin-backup" name="configurar">
                                            <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                                              <tr>
                                                <td width="200" height="50" align="left" style="padding-left:5px;">Servidor FTP</td>
                                                <td align="left"><input name="ftp_servidor" type="text" class="form-control" id="ftp_servidor" style="width:100%;" value="<?php echo $dados_config_bkp["ftp_servidor"]; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Usuario FTP</td>
                                                <td align="left"><input name="ftp_usuario" type="text" class="form-control" id="ftp_usuario" style="width:100%;" value="<?php echo $dados_config_bkp["ftp_usuario"]; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Senha FTP</td>
                                                <td align="left"><input name="ftp_senha" type="password" class="form-control" id="ftp_senha" style="width:100%;" value="<?php echo $dados_config_bkp["ftp_senha"]; ?>" /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Backup Arquivos</td>
                                                <td align="left" class="texto_padrao"><input name="bkp_arquivos" id="bkp_arquivos" type="checkbox" value="sim" <?php if($dados_config_bkp["bkp_arquivos"] == "sim") { echo 'checked="checked"';} ?> /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Backup MySQL</td>
                                                <td align="left" class="texto_padrao"><input name="bkp_mysql" id="bkp_mysql" type="checkbox" value="sim" <?php if($dados_config_bkp["bkp_mysql"] == "sim") { echo 'checked="checked"';} ?> /></td>
                                              </tr>
                                              <tr>
                                                <td height="50" align="left" style="padding-left:5px;">Backup MySQL Estatisticas</td>
                                                <td align="left" class="texto_padrao"><input name="bkp_mysql_stats" id="bkp_mysql_stats" type="checkbox" value="sim" <?php if($dados_config_bkp["bkp_mysql_stats"] == "sim") { echo 'checked="checked"';} ?> /></td>
                                              </tr><tr>
                                            <td height="50" colspan="2" align="right"><button type="button" class="btn btn-info" onClick="document.configurar.submit();"><?php echo lang_botao_titulo_configurar; ?></button></td>
                                          </tr>
                                            </table>
                                        </form>
                  </div>
              </div>
            </div>

            <div class="col-lg-12">
              <div class="card">
                  <div class="card-header">
                    <header>Cron</header>
                  </div>
                  <div class="card-body">
                     <input style="width:100%; border:0;" onclick="this.select();" value="0 1 * * * /usr/bin/php /home/painel/public_html/robots/backup-painel-mysql.php" />
                  </div>
              </div>
            </div>

            <div class="col-lg-12">
              <div class="card">
                  <div class="card-header">
                    <header>Log Backup</header>
                  </div>
                  <div class="card-body">
                     <?php echo nl2br($dados_config_bkp["log"]); ?>
                  </div>
              </div>
            </div>

          </div>
        </div>
      </div>      

    </div>
  </div>
  <script src="/inc/vendor.bundle.base.js"></script>
  <script src="/inc/vendor.bundle.addons.js"></script>
  <script src="/inc/theme.js"></script>
  <script>
  $('#busca_chave').keypress(function (e) {
    if (e.which == 13) {
       window.location = "/admin/admin-streamings/resultado/"+document.getElementById("busca_chave").value;
    }
  }); 
  </script>
</body>
</html>