<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

/*
// check if international and then translate
	include_once('class.InputDecode.php');
	$ls_DomainTranslated = InputDecode::decode($ls_DomainOrig);
	if ($ls_DomainTranslated != $ls_DomainOrig) {
		// has national characters

		$ls_DomainName = escapeshellarg($ls_DomainTranslated);
		global $bindir;
		$exec = "$bindir/idn_translate.pl UTF8 $ls_DomainName";
		exec($exec, $list, $return);
		if ($return != 0) {
			// error
			echo "<p><b>Error:</b> idn_translate.pl returned with non-zero status:<pre>".implode("\n", $list)."</pre></p>";
			return 0;
		}
		if (sizeof($list) < 1) {
			// error
			echo "<p><b>Error:</b> idn_translate.pl returned with zero output:<pre>".implode("\n", $list)."</pre></p>";
			return 0;
		}
		if (!preg_match("/^to_idn:\s+([a-zA-Z0-9\.-]+)$/", $list[0], $matches)) {
			// error
			echo "<p><b>Error:</b> Can not parse output of the idn_translate.pl utility:<pre>".implode("\n", $list)."</pre></p>";
			return 0;
		} 
		$ls_Domain = $matches[1]; 	
	} else {
		$ls_Domain = $ls_DomainOrig;
	}
*/

function save_idn_alias($domain_id, $idn_name, $id) {

	include_once("class.Data.php");

	$nl = new SQLDataRow('idn_aliases', false, 'id');
	$nl->setValue('idn_name', $idn_name);
	$nl->setValue('domain_id', $domain_id);
	if ($id !== false) {
		$id *= 1;
/*		
		if (!$this->checkId($id))
		{
			return false;
		}
*/
		$nl->setValue('id', $id);
		return $nl->update();
	} else {
		return $nl->insert();
	}
}

function get_html_idn_name($domain_name) {
	$res = db_query("SELECT d.domain, i.idn_name FROM domains d, idn_aliases i WHERE d.id = i.domain_id AND d.domain = '$domain_name'");
	if (db_num_rows($res) != 1) {
		return false;
	}
	$row = db_fetch_array($res);

	include_once('class.InputDecode.php');
	return InputDecode::utf8ToUnicodeEntities($row['idn_name']);
}

// $ls_DomainTranslated - in utf8
function translate_name_to_idn($ls_DomainTranslated) {
	$ls_DomainName = escapeshellarg($ls_DomainTranslated);
	global $bindir, $iconv_utf8;
	if( !(isset($iconv_utf8) && $iconv_utf8 ) ) {
		$iconv_utf8 = 'UTF-8';
	}
	$exec = "$bindir/idn_translate.pl $iconv_utf8 $ls_DomainName";
	$list = array();
	exec($exec, $list, $return);
	if ($return != 0) {
		// error
		echo "<p><b>Error:</b> idn_translate.pl returned with non-zero status:<pre>".implode("\n", $list)."</pre></p>";
//		message(ERROR, "idn_translate.pl returned with non-zero status:<pre>".implode("\n", $list)."</pre>");
		exit();
	}
	if (sizeof($list) < 1) {
		// error
		echo "<p><b>Error:</b> idn_translate.pl returned with empty output:<pre>".implode("\n", $list)."</pre></p>";
//		message(ERROR, "idn_translate.pl returned with empty output:<pre>".implode("\n", $list)."</pre>");
		exit();
	}
	$matches = array(); 
	if (!preg_match("/^to_idn:\s+([a-zA-Z0-9\._-]+)$/", $list[0], $matches)) {
		// error
		echo "<p><b>Error:</b> Can not parse output of the idn_translate.pl utility:<pre>".implode("\n", $list)."</p>";
//		message(ERROR, "Can not parse output of the idn_translate.pl utility:<pre>".implode("\n", $list)."</pre>");
		exit();
	} 
	return $matches[1]; 
}

?>
