<?php
/* $Id: step_3.inc.php,v 1.16 2003/11/19 15:53:46 k-fish Exp $ */
if(!defined("SETUP_RUNNING")) die('Use index.php for running setup!');

if($_SESSION["SETUP"]['uninstall'] === true){
    $_SESSION["SETUP"]["settings_ok"] = true;
    @include($configFile);

    if(isset($_POST['createfilebackup'])){
	include(ROOTPATH.'/include/tar/tar.class.php');

	$tar = new tar();
	chdir(ROOTPATH);
	$cdir = opendir('data_cache');
	while(($dir = readdir($cdir)) !== false){
	    if($dir != '.' && $dir != '..' && is_dir('data_cache/'.$dir)){
		$tar->addDirectory('data_cache/'.$dir);
		@$mdir = opendir('data_cache/'.$dir);
		while(($file = readdir($mdir)) !== false){
		    if($file != '.' && $file != '..' && $file != 'index.htm' && $file != 'index.html')
			$tar->addFile('data_cache/'.$dir.'/'.$file);
		}
	    }
	}
	$tar->toTar(ROOTPATH.'/data_cache/mgw-files-backup-'.date('Y-m-d').'.tar',false);
	unset($tar);
	$smarty->assign('filebackupname', 'mgw-files-backup-'.date('Y-m-d').'.tar');
	$smarty->assign('filebackuplink', ROOTURL.'data_cache/mgw-files-backup-'.date('Y-m-d').'.tar');
    }

    if(isset($_POST['createdbbackup'])){
	include_once(ROOTPATH.'/include/tar/tar.class.php');
	include(ROOTPATH."/include/adodb/toexport.inc.php");
	include(ROOTPATH."/include/adodb/adodb.inc.php");

	$tar = new tar();

	$conn = ADONewConnection($appconf["dbvendor"]);
	if ($conn->Connect($appconf["dbhost"], $appconf["dbuser"], $appconf["dbpassword"], $appconf["dbname"])){
	    $tables = $conn->MetaTables();
	    foreach($tables as $table){
	        if(substr($table,0,4) == "mgw_"){
		    $res = $conn->Execute('SELECT * FROM '.$table);
		    $tar->addData("$table.csv",rs2csv($res));
		}
	    }

	    $tar->toTar(ROOTPATH.'/data_cache/mgw-db-backup-'.date('Y-m-d').'.tar',false);
	    $smarty->assign('dbbackupname', 'mgw-db-backup-'.date('Y-m-d').'.tar');
	    $smarty->assign('dbbackuplink', ROOTURL.'data_cache/mgw-db-backup-'.date('Y-m-d').'.tar');
	}
    }

    $smarty->assign('uninstall', true);
}
else{
    $smarty->assign('uninstall', false);
    /* settings should always be checked after showing this page */
    $_SESSION["SETUP"]["settings_ok"] = false;

    // allow only DBs PHP can handle
    $extensions = get_loaded_extensions();
    $dbDrivers = array('mysql' => array('mysql', 'MySQL'),
		       'pgsql' => array('postgres7', 'PostgreSQL')
		       );
    // at least for these we need the name of the PHP extensions
    //'' => array('', 'IBM DB2'), // loge, go for it!
    //'' => array('', 'Generic ODBC'),
    //'' => array('mssql', 'MS-SQL'),
    //'' => array('oci8', 'Oracle 8'),
    //'' => array('msql', 'mSQL'),
    //'' => array('ibase', 'interbase'),
    //'' => array('sybase', 'Sybase'),
    //'' => array('fbsql', 'Frontbase')
    foreach($dbDrivers as $dbext => $dbd){
	if(in_array($dbext, $extensions))
	    $dbs[$dbd[0]] = $dbd[1];
    }
    $smarty->assign('dbs', $dbs);
    $smarty->assign('dbtype', (isset($_SESSION["SETUP"]["dbtype"])) ? $_SESSION["SETUP"]["dbtype"] : '');

    if (!isset($_SESSION["SETUP"]["dbhost"])) {
	$smarty->assign("dbhost", "localhost");
    } else {
	$smarty->assign("dbhost", $_SESSION["SETUP"]["dbhost"]);
    }
    if (isset($_SESSION["SETUP"]["dbuser"])) $smarty->assign("dbuser", $_SESSION["SETUP"]["dbuser"]); else $smarty->assign("dbuser", "");
    if (!isset($_SESSION["SETUP"]["dbname"])) {
	$smarty->assign("dbname", "mgw");
    } else {
	$smarty->assign("dbname", $_SESSION["SETUP"]["dbname"]);
    }

    if (!isset($_SESSION["SETUP"]["rooturl"])) {
	$tmp = "http://" . $_SERVER["HTTP_HOST"].str_replace("setup","",dirname($_SERVER["PHP_SELF"]));
	$smarty->assign("rooturl", $tmp);
    } else {
	$smarty->assign("rooturl", $_SESSION["SETUP"]["rooturl"]);
    }
    if (isset($_SESSION["SETUP"]["skipurlcheck"]) and $_SESSION["SETUP"]["skipurlcheck"]==1) {
	$smarty->assign("skipurlchecked", "checked=\"checked\"");
    } else {
	$smarty->assign("skipurlchecked", "");
    }
    if (isset($_SESSION["SETUP"]["dynamicurl"]) and $_SESSION["SETUP"]["dynamicurl"]==1) {
	$smarty->assign("dynamicurlchecked", "checked=\"checked\"");
    } else {
	$smarty->assign("dynamicurlchecked", "checked=\"checked\"");
    }
    if (isset($_SESSION["SETUP"]["forcessl"]) and $_SESSION["SETUP"]["forcessl"]==1) {
	$smarty->assign("forcesslchecked", "checked=\"checked\"");
    } elseif(isset($_SERVER['HTTPS'])) {
	$smarty->assign("forcesslchecked", "checked=\"checked\"");
    } else {
	$smarty->assign("forcesslchecked", "");
    }

    if (!isset($_SESSION["SETUP"]["sessionname"])) {
	$smarty->assign("sessionname", "MGWSESSION");
    } else {
	$smarty->assign("sessionname", $_SESSION["SETUP"]["sessionname"]);
    }

    if (!isset($_SESSION["SETUP"]["usedbsessions"])) {
	$smarty->assign("usedbsessions", false);
    } else {
	$smarty->assign("usedbsessions", ($_SESSION["SETUP"]["usedbsessions"]) ? 'checked="checked"' : '');
    }

    if (!isset($_SESSION["SETUP"]["rootpath"])) {
	$smarty->assign("rootpath", ROOTPATH);
    } else {
	$smarty->assign("rootpath", $_SESSION["SETUP"]["rootpath"]);
    }

    // only allow LDAP if PHP can handle it
    if(in_array('ldap', get_loaded_extensions()))
	$smarty->assign('amethods', array('sql' => 'SQL',
					  'ldap' => 'LDAP',
					  'ntlm' => 'NTLM / .htaccess'
					  ));
    else
	$smarty->assign('amethods', array('sql' => 'SQL',
					  'ntlm' => 'NTLM / .htaccess'
					  ));
    if (isset($_SESSION["SETUP"]["auth_method"])) $smarty->assign("auth_method", $_SESSION["SETUP"]["auth_method"]);

    if (isset($_SESSION["SETUP"]["encrypt_pwd"]) and $_SESSION["SETUP"]["encrypt_pwd"]==1) {
	$smarty->assign("encrypt_pwd_checked", "checked=\"checked\"");
    } else {
	$smarty->assign("encrypt_pwd_checked", "");
    }

    if (isset($errors)) $smarty->assign("errors", $errors);
    $alertArray = array();
    if($_SESSION["js_for_errors"]){
	if (isset($dberrorarray)) foreach ($dberrorarray as $part) array_push($alertArray, addslashes($part));
	if (isset($miscerrorarray)) foreach ($miscerrorarray as $part) array_push($alertArray, addslashes($part));
    }
    else{
	if (isset($dberrorarray)) foreach ($dberrorarray as $part) array_push($alertArray, $part);
	if (isset($miscerrorarray)) foreach ($miscerrorarray as $part) array_push($alertArray, $part);
    }
    $smarty->assign("jserrorarray", $alertArray);
}
?>