<?
error_reporting(E_ALL & ~E_NOTICE);
class language {
	var $cacheFile;

	function language() {
		global $sys, $fm;
		/**
		 * get list of folders from /languages
		 */
		$dirList = $fm->oneDirList(gluePath($sys['root'], "/system/data/languages/"));
		foreach ($dirList as $key => $value) {
			if (substr($value, -5) == ".lang") {
				$GLOBALS['availableLanguages'][] = str_replace(".lang", "", $value);
			}
		}
		/**
		 * User is requesting language
		 */
		 
		 $_REQUEST['language'] = $_GET['language']?$_GET['language']:$_POST['language'];
		 
		if ($_REQUEST['language']) {
			if (in_array($_REQUEST['language'], $GLOBALS['availableLanguages'])) {
				$GLOBALS['currentLanguage'] = $_REQUEST['language'];
				setcookie("language", $GLOBALS['currentLanguage'], time()+(3600*24*31), "/");  /* expire in 1 hour */
			}
		} else {
			if ($_COOKIE['language']) {
				/**
				 * Check to see if 
				 */
				if (in_array($_COOKIE['language'], $GLOBALS['availableLanguages'])) {
					$GLOBALS['currentLanguage'] = $_COOKIE['language'];
					setcookie("language", $GLOBALS['currentLanguage'], time()+(3600*24*31), "/");  /* expire in 1 hour */
				}
			} else {
				/**
				 * Default language
				 */
				$GLOBALS['currentLanguage'] = "english";
			}
		}
		
		/**
		 * Default language - if user trying an invalid language
		 */
		if (!$GLOBALS['currentLanguage']) {
			$GLOBALS['currentLanguage'] = "english";
		}
		
		$this->cacheFile = gluePath($sys['root']."/system/data/temp/".$GLOBALS['currentLanguage'].".lang.tmp");
		$this->langFile = gluePath($sys['root'], "/system/data/languages/".$GLOBALS['currentLanguage'].".lang");
		
		//check if cached
		$this->cacheFileExists = file_exists($this->cacheFile);
		if ($this->cacheFileExists) {
			$temp = file_get_contents($this->cacheFile);
			$temp = explode("@@", $temp);
			$cachedFileTimeStamp = $temp[0];
			$cachedFileTimeStamp = unserialize($temp[1]);
			clearstatcache();
			$this->refreshCache = (($this->cacheFileExists == false) || ($this->cacheFileExists == true && (filemtime($this->langFile) != $cachedFileTimeStamp)));
			$this->cachedData = $cachedLangData;
		} else {
			global $fm;
			$fm->newFile($this->cacheFile, "");
			$this->isNewLanguage = 1;
		}
	}
	
	function getLanguageData() {
		global $sys;

		if (!$this->refreshCache) {
				//echo "cache file loaded";
			if (is_array($this->cachedData)) {
				$this->data = $this->cachedData;
			} else {
				$this->data = array();
			}
		} else {
			//parse language file
				//echo "lang file parsed";
			$contents = file_get_contents($this->langFile);
			$exploded = explode("\r\n\r\n", $contents);
			foreach ($exploded as $pair) {
				$entry = explode("\r\n", $pair);
				if ($entry[1] != "") {
					$arrayFromContents[$entry[0]] = $entry[1];
				}
			}
			if (is_array($arrayFromContents)) {
				$this->data = $arrayFromContents;
			} else {
				$this->data = array();
			}
		}
		$this->initailCheckSum = crc32(serialize($this->data));
		return true;
	}
	
	function translate($string, $vars = array()) {
		//find text in array
		if (!array_key_exists($string, $this->data)) {
			$this->data[$string] = $string;
		}
		$translation = $this->data[$string];
		for ($i = 0 ; $i < sizeof($vars) ; $i++) {
			$translation = str_replace("%".($i+1)."", $vars[$i], $translation);
		}
		return $translation;
	}
	
	function display($string, $vars = array()) {
		echo $this->translate($string, $vars);
		return true;
	}
	
	function get($string, $vars = array()) {
		return $this->translate($string, $vars);
	}
	
	function updateLanguageData() {
		global $fm;
		//update file if new translated text was found
		$newTraslatedTextFound = ($this->initailCheckSum != crc32(serialize($this->data)));
		if ($newTraslatedTextFound && !$this->isNewLanguage) {
		//echo "lang file resaved";
			$newData4Contents = "";
			$i = 0;
			$sizeofdata = sizeof($this->data);
			foreach ($this->data as $key => $value) {
				if (strlen($value) > 0) {
					$newData4Contents .= $key."\r\n".$value;
					if ($i < $sizeofdata-1) {
						$newData4Contents .= "\r\n\r\n";
					}
				}
				$i++;
			}
			$fm->newFileContent($this->langFile, $newData4Contents);
		}
		if ($this->refreshCache) {
		//echo "cache file resaved";
			$contents = filemtime($this->langFile)."@@".serialize($this->data);
			$fm->newFileContent($this->cacheFile, $contents);
		}
	}
}

function smarty_block_translate($params, $string, &$smarty) {
	global $lang;
	for ($i = 0 ; $i < sizeof($params) ; $i++) {
		$temp[$i] = $params[($i+1)];
	}
	$params = $temp;
	$lang->display($string, $params);
}
?>