<?php
/* $Id: step_1.inc.php,v 1.15 2004/12/19 18:02:13 leyeti Exp $ */
if(!defined("SETUP_RUNNING")) die('Use index.php for running setup!');

include("../include/mSelectBox.class.php");

// ====================================================
// Redirect Wrapper
// ====================================================
//
function redirect($uri) {

    if(PHP_OS=="WINNT" || PHP_OS=="WIN32") {
	// we have problems with REFRESH with opera, so we use
	// location for opera and refresh for IE on WIN32
	if(strstr($_SERVER["HTTP_USER_AGENT"], "Opera")) {
	    header("Location: $uri");
	    exit();
	}
	else {
	    header("Refresh: 0; url=\"$uri\"");
	    exit();
	}
    }
    else {
	header("Location: $uri");
	exit();
    }
}
// ====================================================

/* get languages from the lang files in setup */
$_SESSION["SETUP"]['languages'] = array();
$dir = opendir(ROOTPATH.'/'.SETUP_DIR."/lang/");
while (($fn = readdir($dir)) !== false) {
    if(strstr($fn, '.lang')){
	$fh = fopen(ROOTPATH.'/'.SETUP_DIR . "/lang/".$fn, "r");
	$l = fgets($fh, 1024);
	fclose($fh);
	$la = explode(':', trim(substr($l, 1)));
	$_SESSION["SETUP"]['languages'][$la[2].';'.$la[1]] = $la[0];
    }
}
closedir($dir);
asort($_SESSION["SETUP"]['languages']);
$smarty->assign('languages', $_SESSION["SETUP"]['languages']);

/* check posted data in case of refresh */
if (isset($_POST["lang_charset"])) {
    $lc = explode(";", $_POST["lang_charset"]);
    $_SESSION["SETUP"]["lang"] = $lc[0];
    $_SESSION["SETUP"]["charset"] = $lc[1];
}

/* see if the user regional settings matches something we know */
if (isset($_SESSION["SETUP"]["lang"]) and isset($_SESSION["SETUP"]["charset"])) {
    $lang = $_SESSION["SETUP"]["lang"].";".$_SESSION["SETUP"]["charset"];
}
elseif (defined("LANGUAGE_AUTO") && LANGUAGE_AUTO == TRUE) {	
	//try to detect language
	$setupurl = ((isset($_SERVER['HTTPS'])) ? "https://" : "http://").$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
	$browserLang = explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
	foreach($browserLang as $bLang){		
	    foreach(array_keys($_SESSION["SETUP"]['languages']) as $lang){
			if (stristr($bLang, substr($lang,0,strpos($lang,';')))!==false){			    
				redirect($setupurl."?lang_charset=$lang&".SID);
				break;
			}
	    }
	}//foreach $browserLang
}
$smarty->assign("setlang", $lang);

$smarty->append("subnav", array(0=>"refresh",1=>Lang::getLanguageString("refresh"),2=>1));
?>