<?php
if (! defined ( "ADMIN_FILE" ))
	die ( "Illegal File Access" );
function Languages() {
	global $pic_base_url, $admin_file, $pic_base_url;
	$lang = $_GET ['lang'];
	if (! preg_match ( '#[A-Za-z0-9_\-\.\++]#si', $lang ))
		begin_frame ( "Языки" );
	else
		begin_frame ( "Редактирование языка " . ucwords ( $lang ) );
	$num = 0;
	if ($lang) {
		if (! preg_match ( '#[A-Za-z0-9_\-\.\++]#si', $lang )) {
			location ( $admin_file . '.php?op=Langs', 5 );
			stdmsg ( "Ошибка", "Неправильный язык", 'error' );
			end_frame ();
			stdfoot ();
			die ();
		}
		if (! @include 'languages/lang_' . $lang . '/lang_main.php') {
			location ( $admin_file . '.php?op=Langs', 5 );
			stdmsg ( "Ошибка", "Неправильный язык", 'error' );
			end_frame ();
			stdfoot ();
			die ();
		}
		?><form action='<?=$admin_file?>.php?op=LanguageBuild&lang=<?=$lang?>'
	method='post'>
<table align='center' width='100%'>
	<tr valign='top'>
		<td align='right' width='1%'><b>Кодировка текста</b></td>
		<td align='left'><input type='text' name="lang[language_charset]"
			size=25
			value="<?=str_replace ( "'", "\'", htmlspecialchars_uni ( $tracker_lang ['language_charset'] ) )?>"></td>
	</tr>
<?
		@ksort ( $tracker_lang );
		foreach ( $tracker_lang as $code => $value ) {
			if ($code != 'language_charset')
				print ( "<tr valign='top'><td align='right' width='1%'><b>$code</b></td><td align='left'><textarea name=\"lang[" . $code . "]\" cols=70 rows=5>" . htmlspecialchars_uni ( $value ) . "</textarea></td></tr>" );
		}
		?>		
</table>
<span id='adding' align='center'></span> <script language="javascript"
	type="text/javascript" src="js/ajax.js"></script> <script
	type="text/javascript"> 
<!-- 
num = 0;
function adding() {
	this.num++;
    var ajax = new engine_ajax(); 
    ajax.onShow = function() { }; 
    var varsString = ""; 
    ajax.requestFile = "admin/ajax_modules/ajax_addlang.php"; 
    ajax.method = 'POST'; 
    //ajax.setVar("num", this.num); 
    ajax.element = 'adding'+this.num; 
    ajax.sendAJAX(varsString); 
    return false;
} 
max_nums = 100; // Максимальное кол-во добавляемых полей.
for(nums = 1; nums <= max_nums; nums ++)
	document.write ( '<div id=\'adding' + nums + '\'></div>' );
--></script>
<table align='center' style='border: 0px;'>
	<tr>
		<td></td>
		<td align='center' style='border: 0px;'><input type='submit'
			value='Сохранить'> <a onclick="return adding();"><input type='button'
			value='Добавить выражение'></a>
		<center><font size=1>*Максимальное кол-во добавляемых полей - 100, вы
		можете изменить эту цифру в этом файле</font></center>
		</td>
	</tr>
</table>
</form>
<?
	} else {
		$dir = opendir ( 'languages' );
		$lang = array ();
		while ( $file = readdir ( $dir ) ) {
			if (preg_match ( '#^lang_#i', $file ) && ! is_file ( $dir . '/' . $file ) && ! is_link ( $dir . '/' . $file )) {
				$filename = trim ( str_replace ( "lang_", "", $file ) );
				$displayname = preg_replace ( "/^(.*?)_(.*)$/", "\\1 [ \\2 ]", $filename );
				$displayname = preg_replace ( "/\[(.*?)_(.*)\]/", "[ \\1 - \\2 ]", $displayname );
				$lang [$displayname] = $filename;
			}
		}
		closedir ( $dir );
		@asort ( $lang );
		@reset ( $lang );
		
		$languages = '';
		while ( list ( $displayname, $filename ) = @each ( $lang ) ) {
			$languages .= '<tr><td align="center" width="100%"><b>' . ucwords ( $displayname ) . '</b> <a href="' . $admin_file . '.php?op=Langs&lang=' . $filename . '"><img src="' . $pic_base_url . 'pen.gif"></a> <a href="' . $admin_file . '.php?op=DeleteLang&lang=' . $filename . '"><img src="' . $pic_base_url . 'warned2.gif"></a></td></tr>';
		}
		print ( "<table align='center' width='100%'>" . $languages . "</table>" );
		print ( "<table align='center' width='100%'><tr><td align='center'><a href='" . $admin_file . ".php?op=LanguageAdd'><input type='button' value='Добавить язык'></a></td></tr></table>" );
	}
}
function LanguageBuild() {
	global $admin_file;
	$lang = $_GET ['lang'];
	if (! preg_match ( '#[A-Za-z0-9_\-\.\++]#si', $lang ) || ! is_array ( $_POST ['lang'] )) {
		location ( $admin_file . '.php?op=Langs', 5 );
		begin_frame ( "Сохранение языка " . ucwords ( $lang ) );
		stdmsg ( "Ошибка", "Неправильный язык", 'error' );
		end_frame ();
		stdfoot ();
		die ();
	}
	if (! $_POST ['lang'] ['language_charset'])
		$_POST ['lang'] ['language_charset'] = "windows-1251";
	$content = '$tracker_lang = array (';
	$num = 0;
	foreach ( $_POST ['lang'] as $code => $word ) {
		$content .= '"' . $code . '"=>' . "'" . $word . "'" . ($num != count ( $_POST ['lang'] ) - 1 ? ', 
	' : "");
		$num ++;
	}
	$content .= ');';
	$content = '<?php
# IMPORTANT: Do not edit below unless you know what you are doing!
if (! defined ( "IN_TRACKER" ))
	die ( "Hacking attempt!" );
' . $content . '
?>';
	if (! @opendir ( './languages/lang_' . $lang )) {
		@mkdir ( './languages/lang_' . $lang );
		$file = fopen ( './languages/lang_' . $lang . '/.htaccess', 'w' );
		fwrite ( $file, "deny from all" );
		fclose ( $file );
	}
	$file = fopen ( './languages/lang_' . $lang . '/lang_main.php', 'w' );
	fwrite ( $file, $content );
	fclose ( $file );
	location ( $admin_file . '.php?op=Langs', 3 );
	begin_frame ( "Сохранение языка " . ucwords ( $lang ) );
	$empty = search_empty_values_in_array ( $_POST ['lang'] );
	$addname = $_POST ['addname'];
	$addvalue = $_POST ['addvalue'];
	if (is_array ( $empty ) || is_array ( $addname )) {
		$dir = opendir ( 'languages' );
		$lang = array ();
		while ( $file = readdir ( $dir ) ) {
			if (preg_match ( '#^lang_#i', $file ) && ! is_file ( $dir . '/' . $file ) && ! is_link ( $dir . '/' . $file )) {
				$filename = trim ( str_replace ( "lang_", "", $file ) );
				$displayname = preg_replace ( "/^(.*?)_(.*)$/", "\\1 [ \\2 ]", $filename );
				$displayname = preg_replace ( "/\[(.*?)_(.*)\]/", "[ \\1 - \\2 ]", $displayname );
				$lang [$displayname] = $filename;
			}
		}
		closedir ( $dir );
		@asort ( $lang );
		@reset ( $lang );
		foreach ( $lang as $language ) {
			include 'languages/lang_' . $language . '/lang_main.php';
			$new_lang = array ();
			foreach ( $tracker_lang as $code => $tr_lang )
				if (is_array ( $empty )) {
					if (! in_array ( $code, $empty ))
						$new_lang [$code] = $tr_lang;
				} else
					$new_lang [$code] = $tr_lang;
			$num = 0;
			$content = '';
			foreach ( $new_lang as $code => $word ) {
				$content .= '"' . $code . '"=>' . "'" . $word . "'" . ($num != count ( $new_lang ) - 1 ? ', 
	' : "");
				$num ++;
			}
			$num = 0;
			if (is_array ( $addname )) {
				foreach ( $addname as $num => $name ) {
					if ($name && $addvalue [$num])
						$content .= ', 
	"' . $name . '"=>' . "'" . str_replace ( "'", "\'", $addvalue [$num] ) . "'";
				}
			}
			$content = '<?php
# IMPORTANT: Do not edit below unless you know what you are doing!
if (! defined ( "IN_TRACKER" ))
	die ( "Hacking attempt!" );
$tracker_lang = Array(' . $content . ');
?>';
			$file = fopen ( './languages/lang_' . $language . '/lang_main.php', 'w' );
			fwrite ( $file, $content );
			fclose ( $file );
		}
	}
	print ( "<center>Успешно</center>" );
}
function DeleteLang() {
	global $admin_file;
	$lang = strtolower ( $_GET ['lang'] );
	location ( $admin_file . '.php?op=Langs', 5 );
	begin_frame ( "Удаление языка " . ucwords ( $lang ) );
	if (! preg_match ( '#[A-Za-z0-9_\-\.\++]#si', $lang )) {
		stdmsg ( "Ошибка", "Неправильный язык", 'error' );
		end_frame ();
		stdfoot ();
		die ();
	}
	$dir = @opendir ( './languages/lang_' . $lang );
	if (! $dir) {
		stdmsg ( "Ошибка", "Неправильный язык", 'error' );
		end_frame ();
		stdfoot ();
		die ();
	}
	$langs = array ();
	while ( $file = readdir ( $dir ) ) {
		if (! is_dir ( $dir . '/' . $file )) {
			$filename = trim ( $file );
			$langs [] = $filename;
		}
	}
	@closedir ( $dir );
	foreach ( $langs as $language ) {
		@unlink ( './languages/lang_' . $lang . '/' . $language );
	}
	if (@rmdir ( './languages/lang_' . $lang ))
		print ( "<center>Успешно</center>" );
	else
		print ( "<center>Неудача</center>" );
}
function LanguageAdd() {
	global $admin_file;
	$lang = strtolower ( $_POST ['lang'] );
	$name = $_POST ['name'];
	if (preg_match ( '#[A-Za-z0-9_\-\.\++]#si', $lang ))
		begin_frame ( "Добавление языка " . ucwords ( $name ) . " на основе " . ucwords ( $lang ) );
	else
		begin_frame ( "Добавление языка" );
	if ($lang) {
		if (! preg_match ( '#[A-Za-z0-9_\-\.\++]#si', $lang )) {
			location ( $admin_file . '.php?op=Langs', 5 );
			stdmsg ( "Ошибка", "Неправильный язык", 'error' );
			end_frame ();
			stdfoot ();
			die ();
		}
		if (! include 'languages/lang_' . $lang . '/lang_main.php') {
			location ( $admin_file . '.php?op=Langs', 5 );
			stdmsg ( "Ошибка", "Неправильный язык", 'error' );
			end_frame ();
			stdfoot ();
			die ();
		}
		?><form action='<?=$admin_file?>.php?op=LanguageBuild&lang=<?=$name?>'
	method='post'>
<table align='center' width='100%'><?
		foreach ( $tracker_lang as $code => $value ) {
			if ($code != 'language_charset')
				print ( "<tr valign='top'><td align='right' width='1%'><b>$code</b></td><td align='left'><textarea name=\"lang[" . $code . "]\" cols=70 rows=5>" . htmlspecialchars_uni ( $value ) . "</textarea></td></tr>" );
			else
				print ( "<tr valign='top'><td align='right' width='1%'><b>Кодировка текста</b></td><td align='left'><input type='text' name=\"lang[" . $code . "]\" size=25 value=\"" . str_replace ( "'", "\'", htmlspecialchars_uni ( $value ) ) . "\"></td></tr>" );
		}
		?>
</table>
<table align='center' style='border: 0px;'>
	<tr>
		<td></td>
		<td align='center' style='border: 0px;'><input type='submit'
			value='Сохранить'></td>
	</tr>
</table>
</form><?
	} else {
		$dir = opendir ( 'languages' );
		$lang = array ();
		while ( $file = readdir ( $dir ) ) {
			if (preg_match ( '#^lang_#i', $file ) && ! is_file ( $dir . '/' . $file ) && ! is_link ( $dir . '/' . $file )) {
				$filename = trim ( str_replace ( "lang_", "", $file ) );
				$displayname = preg_replace ( "/^(.*?)_(.*)$/", "\\1 [ \\2 ]", $filename );
				$displayname = preg_replace ( "/\[(.*?)_(.*)\]/", "[ \\1 - \\2 ]", $displayname );
				$lang [$displayname] = $filename;
			}
		}
		closedir ( $dir );
		@asort ( $lang );
		@reset ( $lang );
		
		$languages = '';
		while ( list ( $displayname, $filename ) = @each ( $lang ) ) {
			$languages .= '<option value=' . $displayname . '>' . ucwords ( $displayname ) . '</option>';
		}
		?>
<form action='<?=$admin_file?>.php?op=LanguageAdd' method='post'>
<table width='100%' align='center'>
	<tr>
		<td align='right' width='20%'>На основе</td>
		<td align='left' width='80%'><select name='lang'>
<?=$languages?>
		</select></td>
	</tr>
	<tr>
		<td align='right' width='20%'>Название</td>
		<td align='left' width='80%'><input type='text' value='' size=25
			name='name'></td>
	</tr>
	<tr>
		<td align='right' width='20%'></td>
		<td align='left' width='80%'><input type='submit' value='Далее'></td>
	</tr>
</table>
</form>
<?
	}
}
function search_empty_values_in_array($array) {
	$todelete = array ();
	foreach ( $array as $code => $arr )
		if (! str_replace ( " ", "", $arr ))
			$todelete [] = $code;
	return $todelete;
}
switch ($op) {
	case "Langs" :
		Languages ();
		break;
	case "LanguageAdd" :
		LanguageAdd ();
		break;
	case "LanguageBuild" :
		LanguageBuild ();
		break;
	case "DeleteLang" :
		DeleteLang ();
		break;
}
?>