<?php
require_once("init.php");
$incdir = dirname(__FILE__);
define("INC", $incdir);

$area = ((!isset($area)) || ($_REQUEST['area']=="")) ? 1 : $_REQUEST['area'];
$limit = (isset($_REQUEST['pp']) && $_REQUEST['pp']!="") ? $_REQUEST['pp'] : 10;

$tmpl = new Koobi("templates/".ADMINTHEME."/languages/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->assign("kversion", KVERSION);
$tmpl->assign('uid', UID);
$tmpl->assign('pref', $pref);


if( (!permission('lang_new')) && (!permission('lang_edit')) && (!permission('lang_export')) ) { echo error($langadmin['noperms']); exit(); }

switch($_REQUEST['action']){
	
	case "deleteitem" :
	if(!permission('lang_export')) { echo error($langadmin['noperms']); exit(); }
	$sql = new dbc("DELETE FROM ".PREFIX."_languages_admin WHERE name='$_REQUEST[id]'");
	header("Location:$_REQUEST[redir]");
	break;
	
	case "newitem" :
	if(!permission('lang_new')) { echo error($langadmin['noperms']); exit(); }
	if( ($_REQUEST['newitem']!="") && ($_REQUEST['newvariable']!="")){
		$vari = trim(strip_tags($_REQUEST['newvariable']));
		
		if(!ereg("[^_A-Za-zÀ-ÖØ-öø-ÿ0-9-]", $vari)){
			
			$sql = new dbc("SELECT name  FROM ".PREFIX."_languages_admin WHERE name='$vari'");
			$num = $sql->numrows();
			
			if($num < 1){
				$sql = new dbc("INSERT INTO ".PREFIX."_languages_admin (
				code,
				name,
				value,
				value_original,
				original
				) VALUES (
				'$_REQUEST[language]',
				'$vari',
				'$_REQUEST[newitem]',
				'$_REQUEST[newitem]',
				'0'
				)
				");
			}
		}
	}
	header("Location:languages_admin.php?action=view&language=$_REQUEST[language]&list=1");
	break;
	
	// =====================================================================
	// <<-- EXPORTIEREN -->>
	// =====================================================================
	case "export":
	if(!permission('lang_export')) { echo error($langadmin['noperms']); exit(); }
	$xoutput = "<" .'?php' ."\r\n";
	$xoutput .= "# KOOBI- LANGUAGE ".date("d-m-Y")." #\r\n";
	$sql = new dbc("SELECT *  FROM ".PREFIX."_languages_admin WHERE code='$_REQUEST[language]'");
	while($row = $sql->fetchrow()){
		$row->value = eregi_replace('<br>', '<br />', $row->value);
		$xoutput .= '$' . "klang_admin['".$_REQUEST['language']."']['".$row->name."'] = \""  . str_replace("&", "&amp;", addslashes(html_entity_decode($row->value))). "\";\r\n";
	}
	
	$xoutput .= "?>";
	downloadfile($xoutput, 'admin-'.$pref['k_version_s'].'-'.$_REQUEST['language'].'.klp', 'application/octet-stream',1);
	break;
	
	
	
	
	case "new":
	if(!permission('lang_new')) { echo error($langadmin['noperms']); exit(); }
	$path = "../temp/";
	if(filesize($_FILES['files']['tmp_name']) >2 ){
		
		$langvorbidden = array();
		$sql = new dbc("SELECT distinct code FROM ".PREFIX."_languages_admin");
		while($row = $sql->fetchrow()){
			array_push($langvorbidden,$row->code);
		}
		
		$klang_admin = array();
		$message = array();
		$items = array();
		
		move_uploaded_file($_FILES['files']['tmp_name'], $path . $_FILES['files']['name']);
		
		if(!@include($path . $_FILES['files']['name']))
		{
			$error = 1;
			array_push($message, $langadmin['langupdate_false_cannotread']);
		}
		
		$code = array_keys($klang_admin);
		$lang = $klang_admin[$code[0]];
		
		
		// =====================================================================
		// <<-- UPDATE- METHODE NEU ODER AKTUALISIEREN
		// =====================================================================
		switch($_REQUEST['updatemethod']){
			case "new" :
			
			if(in_array($code[0],$langvorbidden))
			{
				$error = 1;
				array_push($message, $langadmin['language_languageexists']);
			} else {
				foreach($lang as $key => $value){
					$sql = new dbc("INSERT INTO " . $dbprefix . "_languages_admin (
					code,
					name,
					value,
					value_original
					) VALUES (
					'".$code[0]."',
					'$key',
					'$value',
					'$value'
					)
					");
				}
				header("Location:languages_admin.php?action=view&list=1&language=".$code[0]."");
			}
			break;
			
			
			case "update" :
			@mysql_connect($dbhost, $dbuser, $dbpass);
			@mysql_select_db($dbname);
			if($code[0] == LANGADMIN){
				foreach($lang as $key => $value){
					$query = "INSERT INTO ".PREFIX."_languages_admin (
					code,
					name,
					value,
					value_original,
					original 
					) VALUES (
					'".$code[0]."',
					'$key',
					'$value',
					'$value',
					'1'
					)";
					
					if (!@mysql_query($query)) {
						if($_REQUEST['extraoption'] != 1){
							$query_2 = "UPDATE ".PREFIX."_languages_admin SET 
							value = '$value',
							value_original = '$value' 
							WHERE 
							name = '$key' AND code = '".$code[0]."'
							";
							@mysql_query($query_2);
						}
					}
				}
				
				
				array_push($message, $langadmin['langupdate_ok']);
			}else {
				array_push($message, $langadmin['langupdate_false_same']);
				
			}
			break;
		}
		$lanrdeir = $code[0];
		@unlink($path . $_FILES['files']['name']);
		
	} else {
		$message = array();
		// =====================================================================
		// <<-- NEUE SPRACHE ANHAND EINER BESTEHENDEN  SPRACHE ERSTELLEN -->>
		// =====================================================================
		if($_REQUEST['newlang']!=""){
			$sql = new dbc("SELECT * FROM ".PREFIX."_languages_admin WHERE code='$_REQUEST[aktlang]'");
			while($row = $sql->fetchrow()){
				$sql2 = new dbc("INSERT INTO ".PREFIX."_languages_admin 
				(
				code,
				name,
				value,
				value_original
				) VALUES (
				'$_REQUEST[newlang]',
				'$row->name',
				'".addslashes("$row->value")."',
				'".addslashes("$row->value_original")."'
				)
				");
			}
			array_push($message, $langadmin['update_option_newlangadded']);
		}else {
			array_push($message, $langadmin['update_option_nolanselected']);
		}
	}
	
	
	foreach($message  as $result){
		$epre .= $result . "<br />";
	}
	$tmpl->assign('epre', $epre);
	$tmpl->display('message.tpl');
	
	break;
	
	
	// =====================================================================
	// <<-- UEBERSICHT -->>
	// =====================================================================
	case "" :
	case "view" :
	if(!permission('lang_new'))  { echo error($langadmin['noperms']); exit(); }
	
	
	if($_REQUEST['subaction']=="del"){
		if(!permission('lang_del'))  { echo error($langadmin['noperms']); exit(); }
		if( ($_REQUEST['del_language']!="") && ( $_REQUEST['del_language'] != $pref['lang'] ) ){
			$sql = new dbc("DELETE FROM ".PREFIX."_languages_admin WHERE code='$_REQUEST[del_language]'");
		}
		header("Location:languages_admin.php?action=view");
	}
	
	// =====================================================================
	// <<-- SPRACHEN, DIE ANGELEGT WERDEN KEONNEN -->>
	// =====================================================================
	$newlang = array(
	'Danish' => "dk",
	'Dutch' => "nl",
	'English' => "en",
	'Finnish' => "fi",
	'French' => "fr",
	'German' => "de",
	'Greek' => "cy",
	'Hungarian' => "hu",
	'Italian' => "it",
	'Norwegian' => "no",
	'Polish' => "pl",
	'Portuguese' => "pt",
	'Russian' => "ru",
	'Spanish' => "ar",
	'Swedish' => "se",
	'Turkish' => "tr"
	);
	$tmpl->assign('lang_keys', array_keys($newlang));
	$tmpl->assign('newlang', $newlang);
	
	
	$lang_exist = array();
	$languages = array();
	$sql = new dbc("SELECT distinct code FROM ".PREFIX."_languages_admin ");
	while($row = $sql->fetchrow()){
		switch($row->code){
			
			case "dk" : $row->thelang = "Danish"; break;
			case "nl" :$row->thelang = " Dutch"; break;
			case "en" : $row->thelang = "English"; break;
			case "fi" : $row->thelang = "Finnish"; break;
			case "fr" : $row->thelang = "French"; break;
			case "de" : $row->thelang = "German"; break;
			case "cy" : $row->thelang = "Greek"; break;
			case "hu" : $row->thelang = "Hungarian"; break;
			case "it" : $row->thelang = "Italian"; break;
			case "no" : $row->thelang = "Norwegian"; break;
			case "pl" : $row->thelang = "Polish"; break;
			case "pt" : $row->thelang = "Portuguese"; break;
			case "ru" : $row->thelang = "Russian"; break;
			case "ar" : $row->thelang = "Spanish"; break;
			case "se" : $row->thelang = "Swedish"; break;
			case "tr" : $row->thelang = "Turkish"; break;
			
			default : $row->thelang = $row->code; break;
		}
		
		array_push($lang_exist, $row->code);
		array_push($languages, $row);
	}
	
	$tmpl->assign('aktlang', $pref['langadmin']);
	$tmpl->assign('lang_exist', $lang_exist);
	
	// =====================================================================
	// <<-- ORIGINAL WIEDERHERSTELLEN -->>
	// =====================================================================
	if($_REQUEST['setoriginal']==1){
		$result = new dbc("UPDATE ".PREFIX."_languages_admin SET value=value_original WHERE name='$_REQUEST[id]' AND code='$_REQUEST[language]'");
	}
	
	if($_REQUEST['setasoriginal']==1){
		$result = new dbc("UPDATE ".PREFIX."_languages_admin SET value_original=value WHERE name='$_REQUEST[id]' AND code='$_REQUEST[language]'");
	}
	
	// =====================================================================
	// <<-- AKTUALISIEREN -->>
	// =====================================================================
	if($_REQUEST['list']==1){
		if($_REQUEST['save']==1){
			if($_POST['value']>=1){
				foreach ($_POST['value'] as $name => $value) {
					if( $value != "")  {
						$result = new dbc("UPDATE ".PREFIX."_languages_admin SET value='".$value."' WHERE name='$name' AND code='$_REQUEST[code]'");
						
					}
				}
			}
			
		}
		
		
		if($_REQUEST['search']==1){
			if($_REQUEST['q']!=""){
				$sstring = $_REQUEST['q'];
				if( strlen($sstring) >= 2 ){
					switch($_REQUEST['sm']){
						case "appro":
						$squery = " AND value like '%$sstring%'";
						break;
						
						case "exact":
						$squery = " AND value like '% $sstring %'";
						break;
					}
				}
				$tmpl->assign('q', stripslashes($sstring));
				$navextra = "&amp;search=1&amp;q=$sstring&amp;sm=$_REQUEST[sm]";
			}
			if($_REQUEST['q2']!=""){
				$sstring = $_REQUEST['q2'];
				if( strlen($sstring) >= 2 ){
					$squery = " AND name like '%$sstring%'";
					
				}
				$tmpl->assign('q2', stripslashes($sstring));
			$navextra = "&amp;search=1&amp;q=$sstring&amp;sm=$_REQUEST[sm]";}
		}
		
		if($_REQUEST['showown']==1) {
			$dbextra = " AND original='0'"; 
			$showown = "&amp;showown=1";
		}
		
		$sql = new dbc("SELECT code FROM ".PREFIX."_languages_admin WHERE code='$_REQUEST[language]' $squery $dbextra");
		$num = $sql->numrows();
		
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$sql = new dbc("SELECT * FROM ".PREFIX."_languages_admin WHERE code='$_REQUEST[language]'  $squery $dbextra limit $a,$limit");
		$tmpl->assign('navi', pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"languages_admin.php?show=$show$navextra$showown&ascdesc=$ascdesc&order=$order&action=view&list=1&language=$_REQUEST[language]&pp=$limit&page={s}\">{t}</a> "));
		
		
		$langitems = array();
		while($row = $sql->fetchrow()){
			array_push($langitems,$row);
		}
	}
	
	$tmpl->assign('languages', $languages);
	$tmpl->assign('langitems', $langitems);
	$tmpl->display('langview_admin.tpl');
	break;
}

?>