<?php
if(!defined("BASE_DIR")) exit;
class Login
{
	
	function listLanguages()
	{
		$languages = array();
		$sql = $GLOBALS['db']->Query("SELECT Id, Sprachcode FROM " . PREFIX . "_sprachen_admin WHERE Aktiv=1 ORDER BY Posi ASC");
		while($row = $sql->fetchrow())
		{
			if(file_exists(BASE_DIR . 'lang/' . $row->Sprachcode . '/admin.txt')) 
			{
				$row->Sprache = $GLOBALS['config_vars']['Lang_' . $row->Sprachcode];
				array_push($languages, $row);
			}
		}
		return $languages ;
	}
	
	function listSections()
	{
		$sections = array();
		$sql = $GLOBALS['db']->Query("SELECT Id, Name FROM " . PREFIX . "_sektionen ORDER BY ID ASC");
		while($row = $sql->fetchrow())
		{
			array_push($sections, $row);
		}
		return $sections;
	}
	
	function newLoginForm()
	{
		unset($_SESSION['admin_lang']);
		unset($_SESSION['a_area']);
		
		$GLOBALS['tmpl']->assign('sections', $this->listSections());
		$GLOBALS['tmpl']->assign('languages', $this->listLanguages());
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/login/login.tpl'));
	}
	
	
	function SectionSwitch($id)
	{
		if(isset($_SESSION['loggedin']) && $_SESSION['loggedin']==1 && isset($_SESSION['user_group']))
		{
			$group = abs((int)$_SESSION['user_group']);
			$id    = abs((int)$id);
			
			$row_perm = $GLOBALS['db']->RowQuery("SELECT Rechte_Admin FROM " . PREFIX . "_berechtigungen WHERE Gruppe = '" . $group . "' AND Sektion = '$id'" );
			if(is_object($row_perm))
			{
				$res_p = $GLOBALS['db']->RowQuery("SELECT Rechte_Admin FROM ".PREFIX."_berechtigungen WHERE Sektion='$id' AND Gruppe='" . $group . "'");
				$perms = explode(',',@$res_p->Rechte_Admin);
				if(in_array('all',$perms) || in_array('adminpanel',$perms))
				{
					$row_sect = $GLOBALS['db']->RowQuery("SELECT Name FROM " . PREFIX . "_sektionen WHERE Id = '$id'" );
					$perms_arr = explode(',', $res_p->Rechte_Admin);
					foreach($perms_arr as $perm) $_SESSION['perm_admin']["{$perm}{$id}"] = 1;
					
					unset($_SESSION['a_area']);
					
					$_SESSION['section_name'] = $row_sect->Name;
					$_SESSION['loggedin'] = 1;
					$_SESSION['a_area'] = $id;
					$_SESSION['section_new'] = $id;
					header('Location:index.php');
					exit;
				} else {
					header('Location:index.php');
					exit;
				}
			} else {
				header('Location:index.php');
				exit;
			}
		}
	}
	
	function newLogin()
	{
		
		if(isset($_POST['lang']) && $_POST['lang']!='')
		{
			setcookie("preselected_lang", $_POST['lang'], time() + (3600 * 31 * 6));
		}
		
		$Email_locked = -1;	
		if(function_exists('usleep')) usleep(800);
		sleep(1);
		
		if(isset($_REQUEST['login_email']) && !empty($_REQUEST['login_email']) && isset($_REQUEST['login_pass']) && !empty($_REQUEST['login_pass']))
		{
			$login_email = eregi_replace('[^@.A-Za-zÀ-ÖØ-öø-ÿ0-9\-]', '', $_REQUEST['login_email']);
			$login_pass  = md5(md5(eregi_replace('[^A-Za-zÀ-ÖØ-öø-ÿ0-9]', '', $_REQUEST['login_pass'])));
			
			$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_benutzer WHERE Email = '{$login_email}' AND Kennwort = '{$login_pass}' AND Aktiv = '1' LIMIT 1");
			$num = $sql->numrows();
			$row = $sql->fetchrow();
			
			if($num != 1)
			{
				Logger('Admin-Login failed: ' . $login_email , '3', @$_SESSION['benutzer_id']);
				unset($_SESSION['perm_admin']);
				unset($_SESSION['user_group']);
				$GLOBALS['tmpl']->assign('message', $GLOBALS['config_vars']['LoginFalse']);
				$GLOBALS['tmpl']->assign('sections', $this->listSections());
				$GLOBALS['tmpl']->assign('languages', $this->listLanguages());
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/login/login.tpl'));
			} else {
				$area = abs((int)$_REQUEST['area']);
				
				$row_perm = $GLOBALS['db']->RowQuery("SELECT Rechte_Admin FROM " . PREFIX . "_berechtigungen WHERE Gruppe = '" . $row->Gruppe . "' AND Sektion = '$area'" );
				$row_sect = $GLOBALS['db']->RowQuery("SELECT Name FROM " . PREFIX . "_sektionen WHERE Id = '$area'" );
				
				if(is_object($row_perm))
				{
					$perms_arr = explode(',', $row_perm->Rechte_Admin);
					foreach($perms_arr as $perm) $_SESSION['perm_admin']["{$perm}{$area}"] = 1;
				}
				
				if($row->Gruppe==1) $_SESSION['perm']["adminpanel{$area}"] = 1;
				if(perm('adminpanel'))
				{
					$_SESSION['section_name'] = $row_sect->Name;
					$_SESSION['admin_lang'] = $_REQUEST['lang'];
					$_SESSION['loggedin'] = 1;
					$_SESSION['a_area'] = $area;
					$_SESSION['benutzer_id'] = $row->Id;
					$_SESSION['login_email'] = $login_email;
					$_SESSION['login_pass'] = $login_pass;
					$_SESSION['user_name'] = $row->Benutzername;
					$_SESSION['user_group'] = $row->Gruppe;
					$_SESSION['user_group_misc'] = @explode(';', $row->GruppeMisc);
					Logger('Admin-Login passed: ' . $login_email , '3', @$_SESSION['benutzer_id']);
					header("Location:index.php?do=main");
				} else {
					Logger('Admin-Login failed: ' . $login_email . '(No Permission Section)' , '3', @$_SESSION['benutzer_id']);
					
					$GLOBALS['tmpl']->assign('message', $GLOBALS['config_vars']['NoPermSection']);
					$GLOBALS['tmpl']->assign('sections', $this->listSections());
					$GLOBALS['tmpl']->assign('languages', $this->listLanguages());
					$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/login/login.tpl'));
				}
			}
		} else {
			$GLOBALS['tmpl']->assign('message', $GLOBALS['config_vars']['LoginFalse']);
			$GLOBALS['tmpl']->assign('sections', $this->listSections());
			$GLOBALS['tmpl']->assign('languages', $this->listLanguages());
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/login/login.tpl'));
		}
	}
}
?>