<?php

/* UPGRADE
------------------------------------------------*/

header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
header('Content-type: text/html; charset=utf-8');
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
  header('X-UA-Compatible: IE=edge,chrome=1');
}

date_default_timezone_set('UTC');

define('PARENT', 1);
define('PATH', __dir__ . '/');
define('IPATH', __dir__ . '/../');

include(PATH . 'control/config.php');
include(IPATH . 'control/timezones.php');
include(IPATH . 'control/' . (file_exists(PATH . 'control/user-options.php') ? 'user-' : '') . 'options.php');
if(file_exists(IPATH . 'control/_cfg.php')){include(IPATH . 'control/_cfg.php');}else{die('File "control/_cfg.php" is missing, please see installation instructions.');}
include(IPATH . 'control/functions.php');
include(PATH . 'control/functions.php');
include(IPATH . 'control/system/constants.php');
include(IPATH . 'control/classes/system/class.db.php');
include(IPATH . 'control/classes/system/class.json.php');

// AUTO LOADER > COMPOSER
include(IPATH . 'control/lib/vendor/autoload.php');

// TRACY DEBUGGER
use Tracy\Debugger;
Debugger::$showBar = (defined('DEVELOPMENT_ENV') ? true : false);
Debugger::$strictMode = (defined('DEVELOPMENT_ENV') ? true : false);
Debugger::$errorTemplate = IPATH . 'control/system/headers/500-debug.php';
Debugger::enable((defined('DEVELOPMENT_ENV') ? Debugger::DEVELOPMENT : Debugger::PRODUCTION), IPATH . 'logs');

try {
  $DB = new db();
  $DB->db_conn();
  $JSON = new json();

  include(PATH . 'control/arrays.php');

  $cmd     = (isset($_GET['s']) ? $_GET['s'] : '1');
  $title   = SCRIPT_NAME . ': Upgrade';
  $count   = 0;
  $defChar = 'utf8mb4_general_ci';
  $sqlVer  = $DB->db_version();

  // Skip versions and only update version number
  $skipVersions = [];

  if (!in_array(DB_PREFIX . 'settings', $DB->db_table_status())) {
    header("Location: index.php");
    exit;
  }

  $Q = @$DB->db_query("SELECT * FROM `" . DB_PREFIX . "settings`", true);
  $SETTINGS = @$DB->db_object($Q);

  // Re-Run?
  if (isset($_GET['rr']) && in_array($_GET['rr'], array('1.0'))) {
    if ($_GET['rr'] != $SETTINGS->version) {
      $DB->db_query("UPDATE `" . DB_PREFIX . "settings` SET `version` = '" . $DB->db_escape($_GET['rr']) . "'");
      header("Location: upgrade.php");
      exit;
    }
  }

  if (isset($_GET['ajax-ops'])) {
    include(PATH . 'control/_ajax.php');
    exit;
  }

  include(PATH . 'content/header.php');
  include(PATH . 'content/upgrade.php');
  include(PATH . 'content/footer.php');
} catch(Exception $e) {
  echo 'The installer cannot run. Error returned by server:<br><br>';
  echo '"<b>' . $e->getMessage() . '</b>"<br><br>';
  echo 'If you understand the reason for this error and can fix it locally, please update and reload page.<br><br>'; 
  echo 'If you do not understand this message, please <a href="https://www.maianmedia.com/support">contact us</a>.<br><br>';
  echo 'Sorry for any inconvenience.<br><br>Maian Media';
} catch(Error $e) {
  echo 'The installer cannot run. Error returned by server:<br><br>';
  echo '"<b>' . $e->getMessage() . '</b>"<br><br>';
  echo 'If you understand the reason for this error and can fix it locally, please update and reload page.<br><br>'; 
  echo 'If you do not understand this message, please <a href="https://www.maianmedia.com/support">contact us</a>.<br><br>';
  echo 'Sorry for any inconvenience.<br><br>Maian Media';
}


?>
