#
# Copyright (c) 2004-2006 OIC Group, Inc.
# Written and Designed by James Hunt
#
# This file is part of Exponent
#
# Exponent is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License as published by the Free
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################
if (!defined('EXPONENT')) {
    exit('');
}
if (!defined('SYS_BACKUP')) {
    include_once BASE . 'subsystems/backup.php';
}
$errors = null;
$template = new template('importer', '_eql_results', $loc);
//GREP:UPLOADCHECK
if (!exponent_backup_restoreDatabase($db, $_FILES['file']['tmp_name'], $errors)) {
    $template->assign('success', 0);
    $template->assign('errors', $errors);
} else {
    $template->assign('success', 1);
}
$template->output();
    foreach ($db->getTables() as $t) {
        // FIX table prefix problem
        if (substr($t, 0, 14 + strlen($db->prefix)) == $db->prefix . 'installer_test') {
            $db->dropTable(str_replace($db->prefix, '', $t));
        }
    }
    echo '<br /><br />';
    echo $i18n['passed'];
    echo '<br /><br />';
    if (isset($_POST['install_default'])) {
        if (!defined('SYS_BACKUP')) {
            include_once BASE . 'subsystems/backup.php';
        }
        $eql = BASE . 'install/sitetypes/db/_default.eql';
        $errors = array();
        exponent_backup_restoreDatabase($db, $eql, $errors, 0);
        if (count($errors)) {
            echo $i18n['errors_encountered_eql'] . '<br /><br />';
            foreach ($errors as $e) {
                echo $e . '<br />';
            }
        } else {
            echo $i18n['eql_success'];
        }
    }
    ?>
	<br /><br /><a href='?page=admin_user'><?php 
    echo $i18n['continue'];
    ?>
</a>.
	<?php 
##################################################
if (!defined('EXPONENT')) {
    exit('');
}
$errors = array();
if (EXPONENT != $_POST['from_version']) {
    // User wants to upgrade from a previous major or minor version (i.e. 0.95 to 0.96, or 0.96 to 1.0)
    if (!defined('SYS_BACKUP')) {
        include_once BASE . 'subsystems/backup.php';
    }
    $eql = exponent_backup_dumpDatabase($db, null, $_POST['from_version']);
    $tempnam = tempnam(BASE . 'tmp', 'eql');
    $fh = fopen($tempnam, 'w');
    fwrite($fh, $eql);
    fclose($fh);
    exponent_backup_restoreDatabase($db, $tempnam, $errors, $_POST['from_version']);
    unlink($tempnam);
} else {
    // User wants to upgrade from a potentially different revision of the major and minor version (0.96.2 to 0.96.3)
    // This means we need to go through and essentially run Install Tables for them (to alter tables minorly)
    $dir = BASE . 'datatypes/definitions';
    if (is_readable($dir)) {
        $dh = opendir($dir);
        while (($file = readdir($dh)) !== false) {
            if (is_readable($dir . '/' . $file) && is_file($dir . '/' . $file) && substr($file, -4, 4) == '.php' && substr($file, -9, 9) != '.info.php') {
                $tablename = substr($file, 0, -4);
                $dd = (include $dir . '/' . $file);
                $info = array();
                if (is_readable($dir . '/' . $tablename . '.info.php')) {
                    $info = (include $dir . '/' . $tablename . '.info.php');
                }