<?php

/* INSTALLER AJAX
---------------------------------*/

if (!isset($_GET['ajax-ops']) || !defined('PARENT')) {
  exit;
}

$arr    = array('status' => 'err', 'txt' => array('System Error', 'An error has occurred during install, please check the error log.<br><br><b>logs/install-error-report.txt</b><br><br>If install logging isn`t enabled, enable it in "install/control/config.php" and run the installer again.'));
$engine = (isset($_POST['engine']) && in_array($_POST['engine'], array('MyISAM', 'InnoDB')) ? $_POST['engine'] : 'MyISAM');
$c      = (isset($_POST['charset']) ? $_POST['charset'] : $defChar);
$btns   = '<hr><div class="text-center"><div class="btn-group" role="group"><button class="btn btn-primary" style="outline:none!important" type="button" onclick="window.location=\'../index.php\'"><i class="fa fa-eye fa-fw"></i> View</button><button class="btn btn-success" type="button" onclick="window.location=\'https://www.maianmedia.com\'"><i class="fa fa-check-circle fa-fw"></i> More Free Software</button><button class="btn btn-info" type="button" onclick="window.location=\'../' . ADM_FLDR_NAME . '/index.php\'"><i class="fa fa-lock fa-fw"></i> Admin</button></div></div>';
$tableD = [];

if ($DB->db_server_version() < 5000) {
  if ($c) {
    $split     = explode('_', $c);
    $tableType = 'DEFAULT CHARACTER SET ' . $split[0] . mswNL();
    $tableType .= 'COLLATE ' . $c . mswNL();
  }
  $tableType .= 'TYPE = ' . $engine;
} else {
  if ($c) {
    $split     = explode('_', $c);
    $tableType = 'CHARSET = ' . $split[0] . mswNL();
    $tableType .= 'COLLATE ' . $c . mswNL();
  }
  $tableType .= 'ENGINE = ' . $engine;
}

switch($_GET['ajax-ops']) {
  case 'install':
    // Set timezone for installer..
    date_default_timezone_set($_POST['timezone']);
    define('INSTALL_RUN', 1);
    include(PATH . 'control/tables.php');
    if ($count > 0) {
      $arr['txt'][1] = 'One or more database tables could not be installed. Check the error log for more information.<br><br><b>logs/install-error-report.txt</b><br><br>If install logging isn`t enabled, enable it in "install/control/config.php" and run the installer again.';
    } else {
      include(PATH . 'control/data.php');
      if ($dtcount > 0) {
        $arr['txt'][1] = 'The database errored when installing the default system data. Check the error log for more information.<br><br><b>logs/install-error-report.txt</b><br><br>If install logging isn`t enabled, enable it in "install/control/config.php" and run the installer again.';
      } else {
        if (isset($_POST['demo'])) {
          include(PATH . 'control/demo.php');
          if ($dcount > 0) {
            $arr['txt'][1] = 'An error occurred when installing the demo system. Check the error log for more information.<br><br><b>logs/install-error-report.txt</b><br><br>If install logging isn`t enabled, enable it in "install/control/config.php" and run the installer again.';
          }
        }
        if ($count == 0) {
          mswInsLog('Installation completed', 'instruction');
          $arr['status'] = 'ok';
          $arr['txt'] = array('Installation Completed', '<b>' . SCRIPT_NAME . '</b> installed with no errors and the software is ready to use.<br><br>Please refer to the installation instructions for further actions if required.<br><br>For help, see the <a href="../docs/support.html" onclick="window.open(this);return false" style="text-decoration:underline"><b>support section</b></a> in the <a href="../docs/index.html" onclick="window.open(this);return false" style="text-decoration:underline"><b>documentation</b></a>.<br><br>In your admin CP, if you are unsure of any options, click the "Help" link.<br><br>Enjoy.' . $btns);
        }
      }
    }
    break;
  case 'upgrade':
    // Set timezone for upgrade..
    date_default_timezone_set($SETTINGS->timezone);
    define('UPGRADE_RUN', 1);
    if (in_array(SCRIPT_VERSION, $skipVersions)) {
      include(PATH . 'control/upgrade/_version.php');
      include(PATH . 'control/upgrade/_fixes.php');
      $arr['next'] = 'done';
    } else {
      if (isset($_GET['ustage'])) {
        switch ($_GET['ustage']) {
          case 'start':
            // System offline first
            include(PATH . 'control/upgrade/_mode.php');
            include(PATH . 'control/upgrade/_tables.php');
            $arr['next'] = (count($ops) == 1 ? 'done' : '1');
            $arr['prev'] = '0';
            break;
          case $_GET['ustage']:
            sleep(2);
            switch ($_GET['ustage']) {
              case '1':
                include(PATH . 'control/upgrade/affiliates.php');
                break;
              case '2':
                include(PATH . 'control/upgrade/commissions.php');
                break;
              case '3':
                include(PATH . 'control/upgrade/payments.php');
                break;
              case '4':
                include(PATH . 'control/upgrade/layout.php');
                break;
              case '5':
                include(PATH . 'control/upgrade/settings.php');
                break;
              case '6':
                include(PATH . 'control/upgrade/indexes.php');
                break;
            }
            if ($_GET['ustage'] == count($ops) - 1) {
              include(PATH . 'control/upgrade/_fixes.php');
              include(PATH . 'control/upgrade/_version.php');
              $mode = 'online';
              $arr['next'] = 'done';
            } else {
              $arr['next'] = ($_GET['ustage'] + 1);
              $arr['prev'] = $_GET['ustage'];
            }
            break;
        }
      }
    }
    break;
}

echo $JSON->encode($arr);
exit;

?>