<?php

$unified_installer_config_file = dirname(dirname(dirname(__FILE__))) . '/unified-install.config.php';
if (!file_exists($unified_installer_config_file)) {
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        UrlManipulator::redirect(".");
    }
}
defined('PCL_DIR') or die('illegal or unauthorized access to unified-installer-finalize file');
require_once PCL_DIR . "/ini.php";
SessionManipulator::start_session();
$plugins_to_install = Util::unstringify($_SESSION['plugins_to_install']);
$current_plugin = array_shift($plugins_to_install);
if (file_exists('unified-installer-check.php')) {
    unlink('unified-installer-check.php');
}
if (empty($plugins_to_install)) {
    $final_redirect_url = $_SESSION['installation_complete_url'];
    unset($_SESSION['plugins_to_install']);
    unset($_SESSION['installation_complete_url']);
    UrlManipulator::redirect($final_redirect_url);
}
$_SESSION['plugins_to_install'] = Util::stringify($plugins_to_install);
$plugins_to_install = Util::unstringify($_SESSION['plugins_to_install']);
$next_plugin = !empty($plugins_to_install) ? $plugins_to_install[0] : null;
$unified_installer_dir = dirname(__FILE__);
$plugins_dir = str_replace('\\', '/', dirname($unified_installer_dir));
$plugin_directory_full_path = $plugins_dir . '/' . $next_plugin;
$plugin_install_directory = $plugin_directory_full_path . '/install';
$plugin_config_file = $next_plugin . '-app-config.php';
$plugin_is_installed = file_exists($plugin_directory_full_path . '/' . $plugin_config_file);
if (!$plugin_is_installed) {
    $plugin_install_paths = UrlInspector::get_path($plugin_directory_full_path . '/install');
    $plugin_install_url = $plugin_install_paths['http_path'];
    if (UrlInspector::url_exists($plugin_install_url)) {
        UrlManipulator::redirect($plugin_install_url);
    }
}
    $mat[1]['error_condition'] = empty($db_user);
    $mat[1]['error_message'] = 'The Database user field cannot be empty';
    $mat[2]['error_condition'] = empty($db_name);
    $mat[2]['error_message'] = 'The Database name field cannot be empty';
    $mat[3]['error_condition'] = empty($plugins_to_install);
    //!isset($_POST['plugins']);
    $mat[3]['error_message'] = 'You must specify at least one plugin to install';
    $mat[4]['error_condition'] = empty($installation_complete_url);
    $mat[4]['error_message'] = 'Specify the URL to redirect to on successful install';
    $db = new MySql();
    $connection_id = @$db->connect($db_server, $db_user, $db_pass, $db_name);
    $mat[5]['error_condition'] = $connection_id === false || $connection_id < 0;
    $mat[5]['error_message'] = 'Unable to connect to the database server. <br/> Make sure the database server name, username and password are correct';
    $validate = Validator::validate($mat);
    if ($validate['error']) {
        $status_message = $validate['status_message'];
        require_once 'installer-form.php';
        exit;
    }
    $content = "<?php" . NL . NL . "require_once('" . PCL_DIR . "/ini.php');" . NL . NL . NL . "/**" . NL . "* database server name" . NL . "*/" . NL . "defined('DB_SERVER') or define('DB_SERVER', '{$db_server}');" . NL . NL . NL . "/**" . NL . "* database user name" . NL . "*/" . NL . "defined('DB_USER') or define('DB_USER', '{$db_user}');" . NL . NL . NL . "/**" . NL . "* database user password" . NL . "*/" . NL . "defined('DB_PASS') or define('DB_PASS', '{$db_pass}');" . NL . NL . NL . "/**" . NL . "* database name" . NL . "*/" . NL . "defined('DB_NAME') or define('DB_NAME', '{$db_name}');" . NL . NL . NL . "/**" . NL . "* database tables prefix" . NL . "*/" . NL . "defined('TABLES_PREFIX') or define('TABLES_PREFIX', '{$tables_prefix}');" . NL . NL . NL . "/**" . NL . "* File system path to installed plugins directory" . NL . "*/" . NL . "defined('PLUGINS_DIR') or define('PLUGINS_DIR',   '{$plugins_dir}');" . NL . "";
    $config_file = new FileWriter('../unified-install.config.php', 'WRITE_ONLY');
    $config_file->write($content);
    $_SESSION['plugins_to_install'] = Util::stringify($plugins_to_install);
    //used to pass the eligible plugins array to 'finalize.php'
    $_SESSION['installation_complete_url'] = $installation_complete_url;
    //ditto
    $paths = UrlInspector::get_path($plugins_dir . '/' . $eligible_plugins[0] . '/install');
    UrlManipulator::redirect($paths['http_path']);
}
//end if $_SERVER['REQUEST_METHOD'] == 'POST'
require_once 'installer-form.php';