<?php
class Settings
{
	function checkUser($x)
	{
		$request = $x;

		$users = array('asdf', 'Peter', 'Peter2', 'George');
		
		$valid = 'true';
		
		foreach($users as $user) {
		
			if( strtolower($user) == $request )
		
				$valid = 'false';
		
		}
		
		echo $valid;
		exit;
	}
	
	function ActiveAreas()
	{
		$bereiche = array();
		$query = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_bereiche WHERE Aktiv='1' AND Name!='img_captcha' ORDER BY Posi ASC");
		while($row = $query->fetcharray())
		{
			
			$row['BName'] = $GLOBALS['config_vars']['Sections_' . $row['Name']];
			array_push($bereiche,$row);
			
		}
		$GLOBALS['tmpl']->assign('bereiche', $bereiche);
		$out = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/section_settings_small.tpl');
		return $out;
	}
	
	function SectionSettings()
	{
		if(perm('settings'))
		{
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				foreach($_POST['Aktiv'] as $aktiv => $aid)
				{
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_bereiche SET Aktiv='" . $_POST['Aktiv'][$aktiv] . "' WHERE Id='$aktiv'");
				}
				header('Location:index.php?do=settings&sub=sectionsettings');
				exit;
			}
			
			
			$bereiche = array();
			$query = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_bereiche WHERE Name!='img_captcha' ORDER BY Posi ASC");
			while($row = $query->fetcharray())
			{
				
				$row['BName'] = $GLOBALS['config_vars']['Sections_' . $row['Name']];
				array_push($bereiche,$row);
				
			}
			$GLOBALS['tmpl']->assign('bereiche', $bereiche);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Global_SettingsSections']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/section_settings.tpl'));
			
		} else {
			no_perm();
		}
	}
	
	//=======================================================
	// Sektion anlegen
	//=======================================================
	function SectionNew()
	{
		if(perm('settings'))
		{
			if(isset($_POST['Name']) && !empty($_POST['Name']))
			{
				$_POST['Name'] = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9 ]*)/', '', $_POST['Name']);
				
				$qsql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_sektionen WHERE Id = '1'");
				$erg = $qsql->fetchrow();
				$serg = '';
				foreach($erg as $i => $fid)
				{
					switch($i)
					{
						case 'Name':  $fid=$_POST['Name']; break;
						case 'Name_2':$fid=$_POST['Name']; break;
						case 'Name_3':$fid=$_POST['Name']; break;
					}
					$serg .= "". ($i=='Id') ? "''" :",'$fid'"; 
				}
				
				$query = $GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_sektionen VALUES (" . $serg . ")");
				$Iid   = $GLOBALS['db']->InsertId();
				
				// Berechtigungen
				$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_berechtigungen WHERE Sektion = '1'");
				while($row = $sql->fetchrow())
				{
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_berechtigungen VALUES ('', '$row->Gruppe','$Iid','$row->Rechte','$row->Rechte_Admin')");
				}
			}
			header('Location:index.php?do=settings&sub=sectionsdisplay');
			exit;
		}
	}
	
	//=======================================================
	// Sektion loeschen
	//=======================================================
	function SectionsDelete($id)
	{
		if($id!=1)
		{
			// Galerie loeschen
			$sql = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_galerie WHERE Sektion = '$id'");
			while($row = $sql->fetchrow())
			{
				$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_galerie_bilder WHERE Galerie_Id = '$row->Id'");
				$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_galerie_bilderfavoriten WHERE Galerie_Id = '$row->Id'");
			}
			
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_galerie WHERE Sektion = '$id'");
			
			// Berechtigungen
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_berechtigungen WHERE Sektion = '$id'");
			$GLOBALS['db']->Query("ALTER TABLE " . PREFIX . "_berechtigungen AUTO_INCREMENT =1");
			
			// Schnellnavigation
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_quicknavi WHERE Sektion = '$id'");
			
			// Navigation
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_navi WHERE Id = '$id'");
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_navi_cat WHERE Id = '$id'");
			
			// Sektion loeschen
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_sektionen WHERE Id = '$id'");
			$GLOBALS['db']->Query("ALTER TABLE " . PREFIX . "_sektionen AUTO_INCREMENT =1");
			
			// News
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_news WHERE Sektion = '$id'");
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_news_kategorie WHERE Sektion = '$id'");
		}
		
		header('Location:index.php?do=settings&sub=sectionsdisplay');
		exit;
	}
	
	//=======================================================
	// Sektion bearbeiten
	//=======================================================
	function SectionsDisplay()
	{
		if(perm('settings'))
		{
			
			if(isset($_REQUEST['secaction']) && $_REQUEST['secaction']=='edit')
			{
				
				if(isset($_POST['save']) && $_POST['save']==1)
				{
					$q = "UPDATE " . PREFIX ."_sektionen 
					SET 
						Name = '" . $_POST['Name'] . "',
						Aktiv = '" . $_POST['Aktiv'] . "',	
						Meldung = '" . $_POST['Meldung'] . "',
						LimitNews = '" . $_POST['LimitNews'] . "', 	
						LimitNewsArchive = '" . $_POST['LimitNewsArchive'] . "',
						Tpl_shop = '" . $_POST['Tpl_shop'] . "',	
						Tpl_content = '" . $_POST['Tpl_content'] . "',	
						Tpl_news = '" . $_POST['Tpl_news'] . "',	 	
						Tpl_newsletter = '" . $_POST['Tpl_newsletter'] . "',	
						Tpl_newsarchive = '" . $_POST['Tpl_newsarchive'] . "',	 	
						Tpl_index = '" . $_POST['Tpl_index'] . "',	 	
						Tpl_sitemap = '" . $_POST['Tpl_sitemap'] . "',	 	
						Tpl_useraction = '" . $_POST['Tpl_useraction'] . "',	 	
						Tpl_calendar = '" . $_POST['Tpl_calendar'] . "',	 	
						Tpl_faq = '" . $_POST['Tpl_faq'] . "',	 	
						Tpl_gallery = '" . $_POST['Tpl_gallery'] . "',	 	
						Tpl_articles = '" . $_POST['Tpl_articles'] . "',	 	
						Tpl_products = '" . $_POST['Tpl_products'] . "',	 	
						Tpl_downloads = '" . $_POST['Tpl_downloads'] . "',	 	
						Tpl_links = '" . $_POST['Tpl_links'] . "',	 	
						Tpl_register = '" . $_POST['Tpl_register'] . "',	 	
						Tpl_misc = '" . $_POST['Tpl_misc'] . "',	 	
						Tpl_forums = '" . $_POST['Tpl_forums'] . "',	 	
						Tpl_members = '" . $_POST['Tpl_members'] . "',	 	
						Tpl_pn = '" . $_POST['Tpl_pn'] . "',	 	
						Tpl_denied = '" . $_POST['Tpl_denied'] . "',	 	
						Tpl_pwlost = '" . $_POST['Tpl_pwlost'] . "',
						Tpl_manufacturer = '" . $_POST['Tpl_manufacturer'] . "',
						Tpl_cheats = '" . $_POST['Tpl_cheats'] . "',
						Tpl_poll = '" . $_POST['Tpl_polls'] . "',
						Tpl_guestbook = '" . $_POST['Tpl_guestbook'] . "',
						Tpl_imprint = '" . $_POST['Tpl_imprint'] . "',
						Tpl_search = '" . $_POST['Tpl_search'] . "',
						Template = '" . $_POST['Template'] . "',
						CSS_Theme = '" . $_POST['CSS_Theme'] . "',
						Passwort = '" . @$_POST['Passwort'] . "'
						
					WHERE 
						Id = '$_REQUEST[section]'
						";
						 $GLOBALS['db']->Query($q);
					echo '<script>window.opener.location.reload();</script>';
				}
				
				
				
				$sec = (isset($_REQUEST['section'])  && is_numeric($_REQUEST['section'])) ? (int)$_REQUEST['section'] : 1;
				$res = $GLOBALS['db']->RowQuery("SELECT * FROM ".PREFIX."_sektionen WHERE Id = '{$sec}'");
				
				// Theme
				$d = BASE_DIR . 'theme';
				$handle = opendir($d); 
				$folders = array();
				while ($file = readdir ($handle)){ 
					if($file != '.' && $file != '..' && $file != '.htaccess') { 
						$f = '';
						if(is_dir($d.'/'.$file)){ 
							$f->Name = $file;
							array_push($folders,$f);
						} 	
					} 
				}
				
				// Templates fuer Bereiche listen
				$d = BASE_DIR . 'theme/' . $res->Template . '/page';
				$handle = opendir($d); 
				$templates = array();
				while ($file = readdir ($handle)){ 
					if($file != '.' && $file != '..' && $file != '.htaccess') { 
						$f = '';
						if(is_file($d.'/'.$file)){ 
							$f->Name = $file;
							array_push($templates,$f);
						} 	
					} 
				}
				
				$GLOBALS['tmpl']->assign('folders', $folders);
				$GLOBALS['tmpl']->assign('templates', $templates);
				$GLOBALS['tmpl']->assign('res', $res);
				$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Sections_Edit']);
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/sections.tpl'));
			} else {
				// Uebersicht
				$sections = array();
				$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_sektionen ORDER BY Id ASC");
				while($row = $sql->fetchrow())
				{
					array_push($sections,$row);
				}
				
				$GLOBALS['tmpl']->assign('sections', $sections);
				$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Sections']);
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/sectionsdisplay.tpl'));
			}
		}	
	}
	
	//=======================================================
	// Sektionsuebersicht
	//=======================================================
	function Sections()
	{
		if(perm('settings'))
		{
			
			if(isset($_POST['save']) && $_POST['save']==1)
			{
			}
			
			$sec = (isset($_REQUEST['section'])  && is_numeric($_REQUEST['section'])) ? (int)$_REQUEST['section'] : 1;
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM ".PREFIX."_sektionen WHERE Id = '{$sec}'");
			
			// Theme
			$d = BASE_DIR . 'theme';
			$handle = opendir($d); 
			$folders = array();
			while ($file = readdir ($handle)){ 
				if($file != '.' && $file != '..' && $file != '.htaccess') { 
					$f = '';
					if(is_dir($d.'/'.$file)){ 
						$f->Name = $file;
						array_push($folders,$f);
					} 	
				} 
			}
			
			// Templates fuer Bereiche listen
			$d = BASE_DIR . 'theme/' . $res->Template . '/page';
			$handle = opendir($d); 
			$templates = array();
			while ($file = readdir ($handle)){ 
				if($file != '.' && $file != '..' && $file != '.htaccess') { 
					$f = '';
					if(is_file($d.'/'.$file)){ 
						$f->Name = $file;
						array_push($templates,$f);
					} 	
				} 
			}
			
			$GLOBALS['tmpl']->assign('folders', $folders);
			$GLOBALS['tmpl']->assign('templates', $templates);
			$GLOBALS['tmpl']->assign('res', $res);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Shop_payment_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/sections.tpl'));
		}
	}
	
	//=======================================================
	//=======================================================
	function globalSettings()
	{
		if(perm('settings'))
		{
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$q = "UPDATE ".PREFIX."_einstellungen SET 
					Firma = '" . trim($_POST['Firma']) . "',  	
					Impressum = '" . imgToPrefix(trim($_POST['Impressum'])) . "',  	
					Seitenname = '" . trim($_POST['Seitenname']) . "',  	
					Seitenbetreiber = '" . trim($_POST['Seitenbetreiber']) . "',  	
					Strasse = '" . trim($_POST['Strasse']) . "', 	
					Stadt = '" . trim($_POST['Stadt']) . "', 
					LizenzCode  = '" . @trim($_POST['LizenzCode']) . "',
					Mail_Typ = '" . $_POST['Mail_Typ'] . "',
					Mail_Port = '" . $_POST['Mail_Port'] . "', 	
					Mail_Host = '" . trim($_POST['Mail_Host']) . "', 	
					Mail_Username = '" . trim($_POST['Mail_Username']) . "', 	
					Mail_Passwort = '" . trim($_POST['Mail_Passwort']) . "', 	
					Mail_Sendmailpfad = '" . trim($_POST['Mail_Sendmailpfad']) . "',
					Mail_WordWrap = '50',
					Mail_Absender = '" . $_POST['Mail_Absender'] . "',
					Mail_Name = '" . $_POST['Mail_Name'] . "',
					Mail_Fuss = '" . $_POST['Mail_Fuss'] . "',
					Mail_Header = '" . $_POST['Mail_Header'] . "',
					Mail_Fuss_HTML = '" . $_POST['Mail_Fuss_HTML'] . "',
					EmailGesperrt = '" . trim($_POST['EmailGesperrt']) . "',
					EmailDomainsGesperrt = '" . trim($_POST['EmailDomainsGesperrt']) . "',
					IpGesperrt = '" . trim(str_replace($_SERVER['REMOTE_ADDR'],'0.0.0.0',$_POST['IpGesperrt'])) . "',
					Spamwoerter = '" . trim($_POST['Spamwoerter']) . "',
					SpamRegEx = '" . $_POST['SpamRegEx'] . "',
					KoobiCode_Aktiv = '" . $_POST['KoobiCode_Aktiv'] . "',
					KoobiCode_Smilies = '" . $_POST['KoobiCode_Smilies'] . "',
					KommentarFormat = '" . $_POST['KommentarFormat'] . "',
					KoobiCode_Bild = '" . $_POST['KoobiCode_Bild'] . "',
					KoobiCode_Links = '" . $_POST['KoobiCode_Links'] . "',
					KoobiCode_Email = '" . $_POST['KoobiCode_Email'] . "',
					Kommentar_Laenge = '" . $_POST['Kommentar_Laenge'] . "',
					Kommentar_Moderiert = '" . $_POST['Kommentar_Moderiert'] . "',
					Kommentare_Seite = '" . $_POST['Kommentare_Seite'] . "', 
					Kommentare_Icon = '" . $_POST['Kommentare_Icon'] . "',
					Kommentare_IconBreite = '" . $_POST['Kommentare_IconBreite'] . "', 
					shop_is_startpage = '" . $_POST['shop_is_startpage'] . "',
					Reg_Typ = '" . $_POST['Reg_Typ'] . "',
					Reg_Firma = '" . @$_POST['Reg_Firma'] . "',
					Reg_Ust = '" . @$_POST['Reg_Ust'] . "',
					Reg_Fon = '" . @$_POST['Reg_Fon'] . "',
					Reg_Fax = '" . @$_POST['Reg_Fax'] . "',
					Reg_Birth = '" . @$_POST['Reg_Birth'] . "',
					Sicherheitscode = '" . $_POST['Sicherheitscode'] . "',
					use_seo = '" . @$_POST['use_seo'] . "',
					server_time_diff_h = '" . @$_POST['server_time_diff_h'] . "',
					server_time_diff_m = '" . @$_POST['server_time_diff_m'] . "',
					RoundeCorners = '" . @$_POST['RoundeCorners'] . "'
				";
				$GLOBALS['db']->Query($q);
				if($_POST['use_seo']==1) echo '<iframe width="1" height="1" style="display:none" src="../index.php"></iframe>';
			}
			
			$file_check = BASE_DIR . '.htaccess';
			if(!file_exists($file_check)) $GLOBALS['tmpl']->assign('rewrite_error1', $GLOBALS['config_vars']['RwEHtacces_ne'] . "<br />");
			if(!is_writable($file_check)) $GLOBALS['tmpl']->assign('rewrite_error2', $GLOBALS['config_vars']['RwEHtacces_nw']);
			
			if(function_exists('apache_get_modules'))
			{
				$modules = apache_get_modules();
				if(!in_array('mod_rewrite', $modules))
				{
					$GLOBALS['tmpl']->assign('rewrite_error1',$GLOBALS['config_vars']['RwEHtacces_nomod']);
				}
			}
			
			$row = $GLOBALS['db']->RowQuery("SELECT * FROM ".PREFIX."_einstellungen");
			
			$GLOBALS['tmpl']->assign('row', $row);
			$GLOBALS['tmpl']->assign('Impressum', fckInit(imgPrefix($row->Impressum) . ' ', 'Impressum', 350, 'Settings'));
			$GLOBALS['tmpl']->assign('ispro', (VERSION == 'PRO' ? 1 : 0));
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Settings_general']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/general.tpl'));
		}
	}
	
	//=======================================================
	// Laender / Steuern
	//=======================================================
	function Countries()
	{
		if(perm('settings'))
		{
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1){
				foreach ($_POST['Name'] as $id => $val){
					if(!empty($_POST['Name'][$id])){
						$GLOBALS['db']->Query("UPDATE " . PREFIX . "_laender SET Name = '" . $_POST['Name'][$id] . "',Aktiv = '" . $_POST['Aktiv'][$id] . "',Ust ='" . $_POST['Ust'][$id] . "' WHERE Id = '$id'");
					}
				}
				header("Location:index.php?do=settings&sub=countries".(($_REQUEST['page']!='') ? "&page=$_REQUEST[page]" : "&page=1"));
				exit;
			}
			
			// Auslesen
			$limit = 25;
			$sql = $GLOBALS['db']->Query("SELECT id FROM ".PREFIX."_laender ORDER BY Aktiv,Name ASC");
			$num = $sql->numrows();
			
			$a = prepage() * $limit - $limit;
			$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_laender ORDER BY Aktiv,Name ASC limit $a,$limit");
			
			$items = array();
			while($row = $sql->fetchrow())array_push($items,$row);
			if($num > $limit) $GLOBALS['tmpl']->assign('navi',$navi = pagenav(ceil($num / $limit), '', " <a class=\"page_navigation\" href=\"index.php?do=settings&amp;sub=countries&amp;page={s}\">{t}</a> "));
			
			$GLOBALS['tmpl']->assign('items', $items);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Settings_countries_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/countries.tpl'));
		}
	}
	
	
	function Languages($table='sprachen', $header='languages')
	{
		if(perm('settings'))
		{
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1){
				foreach ($_POST['Aktiv'] as $id => $val){
					#if(!empty($_POST['Sprache'][$id])){
					
						if($header=='languages') 
						{
							$db_l = "Sprache = '" . $_POST['Sprache'][$id] . "', Sprachcode  ='" . $_POST['Sprachcode'][$id] . "',";
						}
						
						if($header!='languages') 
						{
							if($_POST['Sprachcode'][$id]=='') $_POST['Aktiv'][$id] = '0';
						}
						
						$GLOBALS['db']->Query("UPDATE " . PREFIX . "_{$table}  
						SET 
							$db_l
							Aktiv = '" . $_POST['Aktiv'][$id] . "',
							Posi = '" . $_POST['Posi'][$id] . "' 
						WHERE Id = '$id'");
					#}
				}
				header("Location:index.php?do=settings&sub={$header}");
				exit;
			}
			
			$d = BASE_DIR . 'lang';
			$languages = array();
			$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_{$table} ORDER BY Posi ASC");
			while($row = $sql->fetchrow())
			{	
				$row->Exists = (file_exists($d.'/'.$row->Sprachcode.'/admin.txt')) ? 1 : 0;
				
				array_push($languages, $row);
			}
			
			
			$handle = opendir($d); 
			$folders = array();
			while ($file = readdir ($handle))
			{ 
				if($file != '.' && $file != '..' && $file != '.htaccess') 
				{ 
					$f = '';
					if(is_dir($d.'/'.$file))
					{ 
						#echo $d.'/'.$file.'/admin.txt<br>';					
						$f->Name = $file;
						$f->Long = (@isset($GLOBALS['config_vars']['Lang_' . strtolower($file)])) ? $GLOBALS['config_vars']['Lang_' . strtolower($file)] : $file;
						$f->Exists = (file_exists($d.'/'.$file.'/admin.txt')) ? 1 : 0;
						#echo $f->Exists . "<br>";
						array_push($folders,$f);
					} 
				} 
			} 
			
			$la = ($header=='languages') ? 'Settings_languages' : 'Settings_languages_a';
			$tp = ($header=='languages') ? 'languages.tpl' : 'languages_admin.tpl';
			
			$GLOBALS['tmpl']->assign('folders', $folders);
			$GLOBALS['tmpl']->assign('languages', $languages);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars'][$la]);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/settings/' . $tp));
		}
	}
}
?>
