<?php
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: inc/lang_util.php - version 1.0
*/

// Support for servers with php_gettext installed
define('PROJECT_DIR', realpath('./'));
if(PHP_OS == 'WINNT'){ define('LOCALE_DIR', PROJECT_DIR .'\manage\lang'); } else { define('LOCALE_DIR', PROJECT_DIR .'\manage\lang'); }
define('DEFAULT_LOCALE', 'en_US');
if(file_exists("inc/gettext.inc")){ require_once("inc/gettext.inc"); } else { require_once("../inc/gettext.inc"); }

$nLangARRAY = array();
$nLangARRAY = nGetLangActiveARRAY();

// Check for language change, and if found, change the language
$nSFLangChange = (isset($_GET['sfl']) ? $_GET['sfl'] : NULL);
if($nSFLangChange){
	$_SESSION["nSFLang"] = $nSFLangChange;
} else {
	// Auto-detect language based on URL
	$nScriptURL = (isset($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : NULL);
	$nThisPageFilename = nGetFileNameFromPath($nScriptURL);
	if($nThisPageFilename && $nPageID){
		$nThisPageFilename = str_replace(".html","",strtolower($nThisPageFilename));
		// get the page title based on ID in ALL languages
		foreach($nLangARRAY as $nLangCode => $nLangName){
			$nNavTitle = nGetTranslation($nLangCode,$nPageID,"nText","page");
			$nNavTitle = nPrepareNameForPath($nNavTitle); // for mod-rewrite
			if(strtolower($nNavTitle)==$nThisPageFilename){
				// language auto-detected, set vars accordingly
				$nSFLangChange = $nLangCode;
				$_SESSION["nSFLang"] = $nSFLangChange;
			}
		}
	}
}



// Check for language session var, and if not found, set it with default lang
if(isset($_SESSION["nSFLang"])){
	$nSFLang = $_SESSION["nSFLang"];
	// If set language is not in list of available languages, force default language
	if(!array_key_exists($nSFLang,$nLangARRAY)){ $nSFLang = ""; }
} else { $nSFLang = ""; }
if(!$nSFLang){ $_SESSION["nSFLang"] = $nDefaultLang; $nSFLang = $nDefaultLang; }

// Load language file
$nLangFilePath = "lang/".$nSFLang.".php";
$nEmergencyLangFilePath = "lang/def/".$nSFLang.".php";
if(file_exists("$nLangFilePath")){
	require("$nLangFilePath");
} else {
	if(file_exists("$nEmergencyLangFilePath")){ require("$nEmergencyLangFilePath"); } // load only if language file not found
};


// Utility functions
function nRemoveIntChars($nText){
	if(!$nText){ return; }
	$nText = htmlentities($nText, ENT_COMPAT, "UTF-8", false);
	$nText = preg_replace('/&([a-z]{1,2})(?:acute|lig|grave|ring|tilde|uml|cedil|caron);/i','\1',$nText);
	$nText = html_entity_decode($nText,ENT_COMPAT, "UTF-8");
	//$search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u,Ú,Í,Ó,É,Á,Ñ");
	//$replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u,U,I,O,E,A,N");
	//$nText = str_replace($search, $replace, $nText);
	return $nText;
}
function nConvertIntChars($nText){
	if(!$nText){ return; }
	// lowercase
	$nText = str_replace("ú","&uacute;",$nText);
	$nText = str_replace("í","&iacute;",$nText);
	$nText = str_replace("ó","&oacute;",$nText);
	$nText = str_replace("é","&eacute;",$nText);
	$nText = str_replace("á","&aacute;",$nText);
	$nText = str_replace("ñ","&ntilde;",$nText);
	// uppercase
	$nText = str_replace("Ú","&Uacute;",$nText);
	$nText = str_replace("Í","&Iacute;",$nText);
	$nText = str_replace("Ó","&Oacute;",$nText);
	$nText = str_replace("É","&Eacute;",$nText);
	$nText = str_replace("Á","&Aacute;",$nText);
	$nText = str_replace("Ñ","&Ntilde;",$nText);
	return $nText;
}
function nGetTranslation($nLangCode,$nItemID,$nField,$nType){
	global $DBName,$Link;
	if(!$nLangCode || !$nItemID || !$nField || !$nType){ return; }
	$nTranslation = "";
	$Query = "SELECT $nField AS nField FROM nTranslate WHERE nLang='$nLangCode' AND nItemID='$nItemID' AND nType='$nType'";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	if ($Row = mysql_fetch_array($Result)) {
		$nTranslation = $Row["nField"];
	}
	return $nTranslation;
}
// returns array of active front-end languages in alpha order, default will be first item in list 
function nGetLangActiveARRAY(){
	global $nDefaultLang;
	$nDefaultLangARRAY = array();
	$nLangARRAY = array();
	// Get all files in directory
	$path = "lang/";    
	$dir_handle = @opendir($path) or die("Unable to open $path");    
	while ($file = readdir($dir_handle)) {
		if($file == "." || $file == ".." || $file == "index.php" )
			continue;
		$nFileCode = str_replace(".php","",$file);
		if(!is_dir("$path$file")){
			if(file_exists("$path$file")){ require("$path$file"); }
			if($nFileCode == $nDefaultLang){
				if($nLang){ $nDefaultLangARRAY["$nFileCode"] = $nLang; } // default language
			} else {
				if($nLang){ $nLangARRAY["$nFileCode"] = $nLang; }
			}
		}
	}
	closedir($dir_handle);
	// sorting, and add default to top of list
	krsort($nLangARRAY);
	foreach($nDefaultLangARRAY as $nLangCode => $nLangName) {
		$nLangARRAY["$nLangCode"] = $nLangName;
	}
	$nLangARRAY = array_reverse($nLangARRAY,true);
	return $nLangARRAY;
}
function nGetFileNameFromPath($nPath){
	$nPathARRAY = explode("/",$nPath);
	// make a duplicate of the last array item
	$nFileName = $nPathARRAY[count($nPathARRAY)-1];
	return $nFileName;
}
function nPrepareNameForPath($nName){
	if(!$nName){ return; }
	$nName = str_replace(" ","-",$nName);
	$nRemoveARRAY = array(".",",",";",":","'","!","$","#","?","&","*","%","@","(",")","/","\\","<",">");
	$nName = str_replace($nRemoveARRAY,"",$nName);
	$nName = str_replace("--","-",$nName);
	$nName = nRemoveIntChars($nName);
	$nName = nConvertIntChars($nName);
	return $nName;
}
?>