<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
class Login
{
	var $_tpl_file = 'login.tpl';
	var $_user_tpl = 'userpanel.tpl';
	var $_register_tpl = 'register.tpl';
	var $_register_ok_tpl = 'register_ok.tpl';
	var $_changepass_tpl = 'changepass.tpl';
	var $_muster = '/[^ ._A-Za-zÀ-ÖØ-öø-ÿ0-9:-]/';
	var $_muster_strasse = '/[^ \/._A-Za-zÀ-ÖØ-öø-ÿ0-9-]/';
	var $_muster_email = '/^[ -._A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$/';
	var $deZip = 5;
	var $chZip = 4;
	var $atZip = 4;
	var $frZip = 5;
	var $itZip = 5;
	
	//=================================================================
	// Pruefen ob Benutzer angemeldet ist und nicht gesperrt ist
	//=================================================================
	function UserLogin($Source)
	{
		if(get_active('Login'))
		{
			$tpl_file = $this->_tpl_file;
			$Email_locked = -1;
			
			// Keine Session gespeichert
			if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] != 1)
			{
				// Sind Cookies mit Login-Daten gesetzt?
				if(isset($_COOKIE['koobi_login_email']) && !empty($_COOKIE['koobi_login_email']) && isset($_COOKIE['koobi_login_pass']) && !empty($_COOKIE['koobi_login_pass']))
				{
					$login_email = eregi_replace('[^@_.A-Za-zÀ-ÖØ-öø-ÿ0-9-]', '', $_COOKIE['koobi_login_email']);
					$login_pass  = eregi_replace('[^_.A-Za-zÀ-ÖØ-öø-ÿ0-9-]', '', $_COOKIE['koobi_login_pass']);
					
					$Email_locked = ($this->lockedEmails($login_email)) ? 1 : 0;
					
					$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();
					
					// Wenn Benutzer nicht gefunden wurde...
					if($num != 1 || $Email_locked == 1)
					{	
						setcookie("koobi_login_email", '', time() + (3600 * 31 * 6));
						setcookie("koobi_login_pass", '', time() + (3600 * 31 * 6));
						$tpl_file = $this->_tpl_file;
					} else {
						// Benutzer wurde gefunden
						$_SESSION['loggedin'] = 1;
						$_SESSION['login_email'] = $login_email;
						$_SESSION['login_pass'] = $login_pass;
						$_SESSION['user_name'] = $row->Benutzername;
						$_SESSION['benutzer_id'] = $row->Id;
						$_SESSION['user_group'] = $row->Gruppe;
						$_SESSION['user_group_misc'] = @explode(';', $row->GruppeMisc);
						$_SESSION['unsichtbar'] = $row->Unsichtbar;
						
						if(!empty($row->Vorname)) $_SESSION['benutzer_vorname'] = $row->Vorname;
						if(!empty($row->Nachname)) $_SESSION['benutzer_nachname'] = $row->Nachname;
						if(!empty($row->Strasse_Nr)) $_SESSION['benutzer_strasse'] = $row->Strasse_Nr;
						if(!empty($row->Postleitzahl)) $_SESSION['benutzer_plz'] = $row->Postleitzahl;
						if(!empty($row->Ort)) $_SESSION['benutzer_ort'] = $row->Ort;
						if(!empty($row->Firma)) $_SESSION['benutzer_firma'] = $row->Firma;
						if(!empty($row->UStId)) $_SESSION['benutzer_ustid'] = $row->UStId;
						if(!empty($row->Telefon)) $_SESSION['benutzer_fon'] = $row->Telefon;
						if(!empty($row->Telefax)) $_SESSION['benutzer_fax'] = $row->Telefax;
						
						$tpl_file = $this->_user_tpl;
						
					}
				}
				$tpl_out = $GLOBALS['tmpl']->fetch($Source . '/user/' . $tpl_file);
			} else {
			
				// Es ist eine Session mit Daten gespeichert. Es muss geprueft werden, ob der Benutzer inzwischen auch nicht gesperrt wurde
				$login_email = eregi_replace('[^@_.A-Za-zÀ-ÖØ-öø-ÿ0-9-]', '', $_SESSION['login_email']);
				$login_pass  = eregi_replace('[^_.A-Za-zÀ-ÖØ-öø-ÿ0-9-]', '', $_SESSION['login_pass']);
				
				$Email_locked = ($this->lockedEmails($login_email)) ? 1 : 0;
				
				$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_benutzer WHERE Email = '{$login_email}' AND Kennwort = '{$login_pass}' AND Aktiv = '1' LIMIT 1");
				$row = $sql->fetchrow();
				
				if(!is_object($row) || $Email_locked == 1)
				{
					$this->unsetSess();
				} else {
					$_SESSION['benutzer_id'] = $row->Id;
					$_SESSION['loggedin'] = 1;
					$_SESSION['login_email'] = $login_email;
					$_SESSION['login_pass'] = $login_pass;
					$_SESSION['user_name'] = $row->Benutzername;
					$_SESSION['benutzer_id'] = $row->Id;
					$_SESSION['user_group'] = $row->Gruppe;
					$_SESSION['user_group_misc'] = @explode(';', $row->GruppeMisc);
					$_SESSION['unsichtbar'] = $row->Unsichtbar;
					
					if(!empty($row->Vorname)) $_SESSION['benutzer_vorname'] = $row->Vorname;
					if(!empty($row->Nachname)) $_SESSION['benutzer_nachname'] = $row->Nachname;
					if(!empty($row->Strasse_Nr)) $_SESSION['benutzer_strasse'] = $row->Strasse_Nr;
					if(!empty($row->Postleitzahl)) $_SESSION['benutzer_plz'] = $row->Postleitzahl;
					if(!empty($row->Ort)) $_SESSION['benutzer_ort'] = $row->Ort;
					if(!empty($row->Firma)) $_SESSION['benutzer_firma'] = $row->Firma;
					if(!empty($row->UStId)) $_SESSION['benutzer_ustid'] = $row->UStId;
					if(!empty($row->Telefon)) $_SESSION['benutzer_fon'] = $row->Telefon;
					if(!empty($row->Telefax)) $_SESSION['benutzer_fax'] = $row->Telefax;
				}
				
				$tpl_out = $GLOBALS['tmpl']->fetch($Source . '/user/' . ((!is_object($row) || $Email_locked == 1) ? $this->_tpl_file : $this->_user_tpl) );
			}
			
			return $tpl_out;
		}
	}
	
	//=================================================================
	// Ausloggen
	//=================================================================
	function Logout()
	{
		if(isset($_SESSION['user_name'])) Logger('User ' . $_SESSION['user_name'] . ' logged out.', '', $_SESSION['benutzer_id']);
		
		$this->unsetSess();
		
		setcookie("koobi_login_email", '', time() + (3600 * 31 * 6));
		setcookie("koobi_login_pass", '', time() + (3600 * 31 * 6));
		
		$back = (isset($_REQUEST['backurl'])) ? str_replace('&amp;', '&', base64_decode($_REQUEST['backurl'])) : 'index.php';
		refer($back);
		exit;
	}
	
	function unsetSess()
	{
		
		if(isset($_SESSION['loggedin'])) unset($_SESSION['loggedin']);
		if(isset($_SESSION['benutzer_id'])) unset($_SESSION['benutzer_id']);
		if(isset($_SESSION['login_email'])) unset($_SESSION['login_email']);
		if(isset($_SESSION['login_pass'])) unset($_SESSION['login_pass']);
		if(isset($_SESSION['wrong_login_data'])) unset($_SESSION['wrong_login_data']);
		if(isset($_SESSION['user_group'])) unset($_SESSION['user_group']);
		if(isset($_SESSION['user_group_misc'])) unset($_SESSION['user_group_misc']);
		if(isset($_SESSION['perm_admin'])) unset($_SESSION['perm_admin']);
		if(isset($_SESSION['benutzer_vorname'])) unset($_SESSION['benutzer_vorname']);
		if(isset($_SESSION['benutzer_nachname'])) unset($_SESSION['benutzer_nachname']);
		if(isset($_SESSION['benutzer_strasse'])) unset($_SESSION['benutzer_strasse']);
		if(isset($_SESSION['benutzer_plz'])) unset($_SESSION['benutzer_plz']);
		if(isset($_SESSION['benutzer_ort'])) unset($_SESSION['benutzer_ort']);
		if(isset($_SESSION['benutzer_firma'])) unset($_SESSION['benutzer_firma']);
		if(isset($_SESSION['benutzer_ustid'])) unset($_SESSION['benutzer_ustid']);
		if(isset($_SESSION['benutzer_fon'])) unset($_SESSION['benutzer_fon']);
		if(isset($_SESSION['benutzer_fax'])) unset($_SESSION['benutzer_fax']);
		if(isset($_SESSION['user_name'])) unset($_SESSION['user_name']);
		if(isset($_SESSION['r_vorname'])) unset($_SESSION['r_vorname']);
		if(isset($_SESSION['r_nachname'])) unset($_SESSION['r_nachname']);
		if(isset($_SESSION['r_strasse'])) unset($_SESSION['r_strasse']);
		if(isset($_SESSION['r_plz'])) unset($_SESSION['r_plz']);
		if(isset($_SESSION['r_ort'])) unset($_SESSION['r_ort']);
		if(isset($_SESSION['r_telefon'])) unset($_SESSION['r_telefon']);
		if(isset($_SESSION['products'])) unset($_SESSION['products']);
		if(isset($_SESSION['gewicht'])) unset($_SESSION['gewicht']);
		if(isset($_SESSION['price'])) unset($_SESSION['price']);
		if(isset($_SESSION['price_netto'])) unset($_SESSION['price_netto']);
		if(isset($_SESSION['currency'])) unset($_SESSION['currency']);
		if(isset($_SESSION['shopstep'])) unset($_SESSION['shopstep']);
		if(isset($_SESSION['r_email'])) unset($_SESSION['r_email']);
		if(isset($_SESSION['r_fax'])) unset($_SESSION['r_fax']);
		if(isset($_SESSION['r_firma'])) unset($_SESSION['r_firma']);
		if(isset($_SESSION['r_ustid'])) unset($_SESSION['r_ustid']);
		if(isset($_SESSION['r_land'])) unset($_SESSION['r_land']);
		if(isset($_SESSION['prod_seen'])) unset($_SESSION['prod_seen']);
		if(isset($_SESSION['payment_summ_extra'])) unset($_SESSION['payment_summ_extra']);
		if(isset($_SESSION['shipping_summ'])) unset($_SESSION['shipping_summ']);
	}
	
	function AjaxLogin()
	{
		if(get_active('Login'))
		{
			if(isset($_REQUEST['login_email'])&& isset($_REQUEST['login_pass']) )
			{
				if(function_exists('usleep')) 
					usleep(250000);
				else 
					sleep(1);
					
				$_SESSION['wrong_login_data'] = 1;
				
				$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)
				{
					$this->unsetSess();
					
					Logger('User ' . $login_email . ' wrong login.', '', '');
					
					setcookie("koobi_login_email", '', time() + (3600 * 31 * 6));
					setcookie("koobi_login_pass", '', time() + (3600 * 31 * 6));
			
					define('OUTPUT', '');
					define('NOOUT', '');
					
					$GLOBALS['tmpl']->assign('login_error_true',1);
					$c = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/user/login_raw.tpl');
					echo $c;
				
				} else {
					$_SESSION['loggedin'] = 1;
					$_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);
					if(!empty($row->Vorname)) $_SESSION['benutzer_vorname'] = $row->Vorname;
					if(!empty($row->Nachname)) $_SESSION['benutzer_nachname'] = $row->Nachname;
					if(!empty($row->Strasse_Nr)) $_SESSION['benutzer_strasse'] = $row->Strasse_Nr;
					if(!empty($row->Postleitzahl)) $_SESSION['benutzer_plz'] = $row->Postleitzahl;
					if(!empty($row->Ort)) $_SESSION['benutzer_ort'] = $row->Ort;
					if(!empty($row->Firma)) $_SESSION['benutzer_firma'] = $row->Firma;
					if(!empty($row->UStId)) $_SESSION['benutzer_ustid'] = $row->UStId;
					if(!empty($row->Telefon)) $_SESSION['benutzer_fon'] = $row->Telefon;
					if(!empty($row->Telefax)) $_SESSION['benutzer_fax'] = $row->Telefax;
					
					if(isset($_REQUEST['staylogged']) && $_REQUEST['staylogged'] == 1)
					{
						setcookie("koobi_login_email", $login_email, time() + (3600 * 31 * 6));
						setcookie("koobi_login_pass", $login_pass, time() + (3600 * 31 * 6));
					}
					
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_benutzer SET Logins=Logins+1 WHERE Email = '{$login_email}' AND Kennwort = '{$login_pass}' AND Aktiv = '1' LIMIT 1");
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_benutzer_logins (Id,Benutzer,Datum,Datum_dt,Ip,Email) VALUES ('', '$row->Id', '".time()."', NOW(), '".$_SERVER['REMOTE_ADDR']."', '$login_email')");
					
					Logger('User ' . $_SESSION['user_name'] . ' logged in.', '', $_SESSION['benutzer_id']);
					
					$c = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/user/userpanel_raw.tpl');
					echo $c;
				}
			}
		}
	}
	//=================================================================
	// Einloggen
	//=================================================================
	function NewLogin()
	{
		if(get_active('Login'))
		{
			if(isset($_REQUEST['login_email']) && !empty($_REQUEST['login_email']) && isset($_REQUEST['login_pass']) && !empty($_REQUEST['login_pass']))
			{
				if(function_exists('usleep')) usleep(800);
				$_SESSION['wrong_login_data'] = 1;
				
				$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)
				{
					$this->unsetSess();
					
					Logger('User ' . $login_email . ' wrong login.', '', '');
					
					setcookie("koobi_login_email", '', time() + (3600 * 31 * 6));
					setcookie("koobi_login_pass", '', time() + (3600 * 31 * 6));
			
					define('OUTPUT', '');
					define('NOOUT', '');
					
					$back = str_replace('&amp;', '&', base64_decode($_REQUEST['backurl']));
					refer($back);
					exit;
				} else {
					
					$_SESSION['loggedin'] = 1;
					$_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);
					if(!empty($row->Vorname)) $_SESSION['benutzer_vorname'] = $row->Vorname;
					if(!empty($row->Nachname)) $_SESSION['benutzer_nachname'] = $row->Nachname;
					if(!empty($row->Strasse_Nr)) $_SESSION['benutzer_strasse'] = $row->Strasse_Nr;
					if(!empty($row->Postleitzahl)) $_SESSION['benutzer_plz'] = $row->Postleitzahl;
					if(!empty($row->Ort)) $_SESSION['benutzer_ort'] = $row->Ort;
					if(!empty($row->Firma)) $_SESSION['benutzer_firma'] = $row->Firma;
					if(!empty($row->UStId)) $_SESSION['benutzer_ustid'] = $row->UStId;
					if(!empty($row->Telefon)) $_SESSION['benutzer_fon'] = $row->Telefon;
					if(!empty($row->Telefax)) $_SESSION['benutzer_fax'] = $row->Telefax;
					
					if(isset($_REQUEST['staylogged']) && $_REQUEST['staylogged'] == 1)
					{
						setcookie("koobi_login_email", $login_email, time() + (3600 * 31 * 6));
						setcookie("koobi_login_pass", $login_pass, time() + (3600 * 31 * 6));
					}
					
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_benutzer SET Logins=Logins+1 WHERE Email = '{$login_email}' AND Kennwort = '{$login_pass}' AND Aktiv = '1' LIMIT 1");
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_benutzer_logins (Id,Benutzer,Datum,Datum_dt,Ip,Email) VALUES ('', '$row->Id', '".time()."', NOW(), '".$_SERVER['REMOTE_ADDR']."', '$login_email')");
					
					Logger('User ' . $_SESSION['user_name'] . ' logged in.', '', $_SESSION['benutzer_id']);
					
					if(isset($_SESSION['wrong_login_data'])) unset($_SESSION['wrong_login_data']);
					
					
					$back = str_replace('&amp;', '&', base64_decode($_REQUEST['backurl']));
					refer($back);
					exit;
				}
				
			} else {
					$this->unsetSess();
					
					setcookie("koobi_login_email", '', time() + (3600 * 31 * 6));
					setcookie("koobi_login_pass", '', time() + (3600 * 31 * 6));
					
					define('OUTPUT', '');
					define('NOOUT', '');
					
					
					$back = str_replace('&amp;', '&', base64_decode($_REQUEST['backurl']));
					refer($back);
					exit;
			}
		}
	}
	
	
	
	//=================================================================
	// Funktion zur Pruefung ob E-Mail gesperrt ist
	//=================================================================
	function lockedEmails($email)
	{
		$gesperrte_mails = array();
		$row = $GLOBALS['db']->RowQuery("SELECT EmailDomainsGesperrt, EmailGesperrt FROM " . PREFIX . "_einstellungen");
		$mails = @str_replace(array("\r\n","\n"), ',', trim($row->EmailGesperrt));
		$mails_domains = str_replace(array("\r\n","\n"), ',', trim($row->EmailDomainsGesperrt));
		
		$gesperrte_mails = explode(",", $mails);
		$gesperrte_mail_domains = explode(",", $mails_domains);
		
		$email_domain_post = @explode("@", $email);
		
		if(in_array($email, $gesperrte_mails) || in_array("@" . $email_domain_post[1], $gesperrte_mail_domains)) return true;
		return false;
	}
	
	
	function CheckUserData($oext='',$valid='')
	{
		if(get_active('Register'))
		{
			$uname = (isset($_REQUEST['reg_username']) && !empty($_REQUEST['reg_username'])) ? trim(strtolower(eregi_replace('[^@._A-Za-zÀ-ÖØ-öø-ÿ0-9 ]', '', $_REQUEST['reg_username']))) : '';
			$mail  = (isset($_REQUEST['reg_email']) && !empty($_REQUEST['reg_email'])) ? trim(strtolower(eregi_replace('[^@._A-Za-zÀ-ÖØ-öø-ÿ0-9-]', '', $_REQUEST['reg_email']))) : '';
			$scode  = (isset($_REQUEST['scode']) && !empty($_REQUEST['scode'])) ? $_REQUEST['scode'] : '';
		
			if($scode!='') 
			{
				if($scode!=$_SESSION['comment_secure_result'])
					$valid = 'false';
				else
					$valid = 'true';
			}
			
			if($mail!='') 
			{
				$mailex = ($oext!='') ? " AND Email != '$oext'" : "";
				$res = $GLOBALS['db']->RowQuery("SELECT Email FROM " . PREFIX . "_benutzer WHERE Email = '$mail' {$mailex}");
				if(is_object($res)) $valid = 'false';
				else $valid = 'true'; 
			}
			
			if($uname!='') 
			{
				$userex = ($oext!='') ? " AND Benutzername != '$oext'" : "";
				$res = $GLOBALS['db']->RowQuery("SELECT Benutzername FROM " . PREFIX . "_benutzer WHERE Benutzername = '$uname' {$userex}");
				if(is_object($res)) $valid = 'false';
				else $valid = 'true'; 
			}
			echo $valid;
		}
	}
	
	//=================================================================
	// Funktion Neuregistrierung
	//=================================================================
	function registerNew($shop=0)
	{
		if(get_active('Register'))
		{
			$use_code = -1;
			
			if($GLOBALS['settings']['Sicherheitscode']==1) 
			{
				$GLOBALS['tmpl']->assign('use_code', 1);
				$use_code = 1;
			}
		
			if(isset($_SESSION['loggedin']))
			{
				$this->registerOk(1);
			} else {
			
				$final = false;
				
				if(!isset($_REQUEST['send']) && $use_code == 1)
				{
					giveSecure();
					
				} 
				elseif (isset($_REQUEST['send']) && $_REQUEST['send']=='1') 
				{
					$scode_form = (isset($_POST['scode']) && !empty($_POST['scode'])) ? $_POST['scode'] : '';
					$scode_ses = (get_active('img_captcha')) ? @$_SESSION['scode'] : @$_SESSION['comment_secure_result'];
					$muster = $this->_muster;
					$muster_strasse = $this->_muster_strasse;
					$muster_email = $this->_muster_email;
					
					$error = '';
					
					//================================================================
					// E-Mail Check
					//================================================================
					@$_POST['reg_email'] = trim($_POST['reg_email']);
					if(empty($_POST['reg_email'])) $error[] = $GLOBALS['config_vars']['RegE_nomail'];
					if(@!preg_match("/^[ -._A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$/", $_POST['reg_email']))  $error[] = $GLOBALS['config_vars']['RegE_wrongmail'];
					if(empty($_POST['reg_email2']) || $_POST['reg_email'] != $_POST['reg_email2']) $error[] = $GLOBALS['config_vars']['RegE_wrongmail2'];
					
					//================================================================
					// Pruefen, ob E-Mail schon verwendet wird bzw. ob diese
					// E-Mail oder E-Mail-Domaine gesperrt ist
					//================================================================
					if(empty($error))
					{
						$row_c = $GLOBALS['db']->RowQuery("SELECT Email FROM " . PREFIX . "_benutzer WHERE Email = '".$_POST['reg_email']."' LIMIT 1");
						if(is_object($row_c) && $row_c->Email != '') $error[] = $GLOBALS['config_vars']['RegE_mailinuse'];
					}
					
					if(empty($error))
					{
						if($this->lockedEmails($_POST['reg_email'])) $error[] = $GLOBALS['config_vars']['RegE_maillocked'];
					}
					
					//================================================================
					// Pruefen, ob Benutzername angegeben wurde bzw. ob dieser schon
					// verwendet wird
					//================================================================
					if(empty($_POST['reg_username'])) $error[] = $GLOBALS['config_vars']['RegE_noUsername'];
					if(!empty($_POST['reg_username']) && @preg_match("/[^ ._A-Za-zÀ-ÖØ-öø-ÿ0-9-]/", $_POST['reg_username']))  $error[] = $GLOBALS['config_vars']['RegE_wrongUsername'];
					
					@$_POST['reg_username'] = trim(eregi_replace('[^ ._A-Za-zÀ-ÖØ-öø-ÿ0-9-]', '', @$_POST['reg_username']));
					$row_c = $GLOBALS['db']->RowQuery("SELECT Benutzername FROM " . PREFIX . "_benutzer WHERE Benutzername = '" . $_POST['reg_username'] . "' LIMIT 1");
					if(is_object($row_c) && $row_c->Benutzername != '') $error[] = $GLOBALS['config_vars']['RegE_usernameUsed'];
					
					if(!SpamCheck($_POST['reg_username'])) $error[] = $GLOBALS['config_vars']['Profile_Username'] . ': ' . $GLOBALS['config_vars']['SpamUsed'];
					
					// Wenn Shop
					if(VERSION == 'PRO')
					{
						// Vorname
						if(empty($_POST['Vorname'])) $error[] = $GLOBALS['config_vars']['Profile_NoFirstName'];
						if(!empty($_POST['Vorname']) && @preg_match($muster, $_POST['Vorname']))  $error[] = $GLOBALS['config_vars']['Profile_CheckFirstName'];
						
						// Nachname
						if(empty($_POST['Nachname'])) $error[] = $GLOBALS['config_vars']['Profile_NoLastName'];
						if(!empty($_POST['Nachname']) && @preg_match($muster, $_POST['Nachname']))  $error[] = $GLOBALS['config_vars']['Profile_CheckLastName'];
						
						// Firma
						if(!empty($_POST['Firma']) && @preg_match($muster, $_POST['Firma']))  $error[] = $GLOBALS['config_vars']['Profile_WrongCompany'];
						
						// UStId
						if(!empty($_POST['UStId']) && @preg_match($muster, $_POST['UStId']))  $error[] = $GLOBALS['config_vars']['Profile_WrongVat'];
						
						// Strasse + Hausnummer
						if(empty($_POST['Strasse_Nr'])) $error[] = $GLOBALS['config_vars']['Profile_NoStreet'];
						if(!empty($_POST['Strasse_Nr']) && @preg_match($muster_strasse, $_POST['Strasse_Nr']))  $error[] = $GLOBALS['config_vars']['Profile_WrongStreet'];
						
						// Postleitzahl
						if(empty($_POST['Postleitzahl'])) $error[] = $GLOBALS['config_vars']['Profile_NoZip'];
						if(!empty($_POST['Postleitzahl']) && @preg_match($muster, $_POST['Postleitzahl']))  $error[] = $GLOBALS['config_vars']['Profile_WrongZip'];
						
						// Ort
						if(empty($_POST['Ort'])) $error[] = $GLOBALS['config_vars']['Profile_NoTown'];
						if(!empty($_POST['Ort']) && @preg_match($muster, $_POST['Ort']))  $error[] = $GLOBALS['config_vars']['Profile_WrongTown'];
						
						// Telefon
						if(!empty($_POST['Telefon']) && @preg_match($muster, $_POST['Telefon']))  $error[] = $GLOBALS['config_vars']['Profile_WrongPhone'];
						
						// Telefax
						if(!empty($_POST['Telefax']) && @preg_match($muster, $_POST['Telefax']))  $error[] = $GLOBALS['config_vars']['Profile_WrongFax'];					
					}
					
					
					switch($_REQUEST['country'])
					{	
						case 'DE':
							if(!empty($_POST['Postleitzahl']) && !preg_match ("/^[0-9]{".$this->deZip."}$/",$_POST['Postleitzahl'])) $error[] = $GLOBALS['config_vars']['Profile_WrongZip2'];
						break;
						
						case 'FR':
							if(!empty($_POST['Postleitzahl']) && !preg_match ("/^[0-9]{".$this->frZip."}$/",$_POST['Postleitzahl'])) $error[] = $GLOBALS['config_vars']['Profile_WrongZip2'];
						break;
						
						case 'CH':
							if(!empty($_POST['Postleitzahl']) && !preg_match ("/^[0-9]{".$this->chZip."}$/",$_POST['Postleitzahl'])) $error[] = $GLOBALS['config_vars']['Profile_WrongZip2'];
						break;
						
						case 'AT':
							if(!empty($_POST['Postleitzahl']) && !preg_match ("/^[0-9]{".$this->atZip."}$/",$_POST['Postleitzahl'])) $error[] = $GLOBALS['config_vars']['Profile_WrongZip2'];
						break;
						
						case 'IT':
							if(!empty($_POST['Postleitzahl']) && !preg_match ("/^[0-9]{".$this->itZip."}$/",$_POST['Postleitzahl'])) $error[] = $GLOBALS['config_vars']['Profile_WrongZip2'];
						break;
					}
					
					if(!empty($_POST['birth']))
					{
						if (!preg_match ("/(^[0-9]{2}).([0-9]{2}).([0-9]{4}$)/", $_POST['birth']))  $error[] = $GLOBALS['config_vars']['RegE_wrongBirth'];
						$year_d = substr($_POST['birth'],0,2);
						$year_m = substr($_POST['birth'],3,2);
						$year_b = substr($_POST['birth'],6);
						
						if($year_d < 1 || $year_d > 31) $error[] = $GLOBALS['config_vars']['RegE_wrongBirthD'];
						if($year_m < 1 || $year_m > 12) $error[] = $GLOBALS['config_vars']['RegE_wrongBirthM'];
						if($year_b < (date("Y") - 80) || $year_b > (date("Y") - 10)) $error[] = $GLOBALS['config_vars']['RegE_wrongBirthY'];
					}
					
					//================================================================
					// Stimmt Sicherheitscode mit gesendetem Code ueberein?
					//================================================================
					if($use_code==1)
					{
						if(($use_code && @empty($_POST['scode'])) || (!$_SESSION['scode'] && $use_code) || ($scode_form != $scode_ses && ($use_code)))
						{
							$error[] = $GLOBALS['config_vars']['Reg_SecurecodeWrong'];
							$secureCode = rand_string(5); 
							
							$_SESSION['scode'] = $secureCode; 
						} else {
							$_SESSION['scode'] = $_POST['scode'];
							$secureCode = $_POST['scode']; 
						}
					}
					
					
					if(empty($error))
					{
						$pass_nomd5 = rand_string(6);
						$pass = md5(md5($pass_nomd5));
						
						//================================================================
						// Land und Laender-Code
						//================================================================
						$Landcode = eregi_replace('[^A-Za-zÀ-ÖØ-öø-ÿ]', '', $_POST['country']);
						$sql_l = $GLOBALS['db']->Query("SELECT Name FROM " . PREFIX . "_laender WHERE Code = '" . $Landcode . "'");
						$row_l = $sql_l->fetchrow();
						
						if(is_object($row_l))
						{
							$Land = $row_l->Name;
						} else {
							$Landcode = $GLOBALS['settings']['Land'];
							$sql_l = $GLOBALS['db']->Query("SELECT Name FROM " . PREFIX . "_laender WHERE Code = '" . $Landcode . "'");
							$row_l = $sql_l->fetchrow();
							$Land = $row_l->Name;
						}
						
						//================================================================
						// Benutzer in Datenbank eintragen
						//================================================================
						$RegCode = rand_string(10);
						$aktiv = ($GLOBALS['settings']['Reg_Typ'] == 'norm') ? 1 : 1; //0
						
						if(VERSION == 'PRO')
						{
							$sql = $GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_benutzer 
							(
								Id,
								Regdatum,
								RegCode,
								Email,
								Kennwort,
								Benutzername,
								Geburtstag,
								Aktiv,
								Land,
								LandCode,
								Gruppe,
								Vorname,
								Nachname,
								Strasse_Nr,
								Postleitzahl,
								Ort,
								Telefon,
								Telefax,
								Firma,
								UStId
							) VALUES (
								'',
								'" . stime() . "',
								'" . $RegCode . "',
								'" . $_POST['reg_email'] . "',
								'" . $pass . "',
								'" . substr($_POST['reg_username'],0,20) . "',
								'" . $_POST['birth'] . "',
								'" . $aktiv . "',
								'" . $Land . "',
								'" . $Landcode . "',
								'3',
								'" . @$_POST['Vorname'] . "',
								'" . @$_POST['Nachname'] . "',
								'" . @$_POST['Strasse_Nr'] . "',
								'" . @$_POST['Postleitzahl'] . "',
								'" . @$_POST['Ort'] . "',
								'" . @$_POST['Telefon'] . "',
								'" . @$_POST['Telefax'] . "',
								'" . @$_POST['Firma'] . "',
								'" . @$_POST['UStId'] . "'
							)");
						} else {
							$sql = $GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_benutzer 
							(
								Id,
								Regdatum,
								RegCode,
								Email,
								Kennwort,
								Benutzername,
								Geburtstag,
								Aktiv,
								Land,
								LandCode,
								Gruppe
							) VALUES (
								'',
								'" . stime() . "',
								'" . $RegCode . "',
								'" . $_POST['reg_email'] . "',
								'" . $pass . "',
								'" . substr($_POST['reg_username'],0,20) . "',
								'" . $_POST['birth'] . "',
								'" . $aktiv . "',
								'" . $Land . "',
								'" . $Landcode . "',
								'3'
							)");
						}
						$iid = $GLOBALS['db']->InsertId();
						Logger('New User: ' . substr($_POST['reg_username'],0,20) . ' ('.$_POST['reg_email'].')', '', $iid);
						$Koobi = new Koobi;
						
						
						//================================================================
						// Sofortiger Login nach Registration
						//================================================================
						if($GLOBALS['settings']['Reg_Typ'] == 'norm')
						{
							$_SESSION['loggedin'] = 1;
							$_SESSION['login_email'] = $_POST['reg_email'];
							$_SESSION['login_pass'] = $pass;
							$_SESSION['user_name'] = $_POST['reg_username'];	
							$_SESSION['user_group'] = 3;
							$_SESSION['user_country'] = $Landcode;
							$_SESSION['benutzer_id'] = $iid;
						} else {
						//================================================================
						// Login erst nach E-Mail-Versand
						//================================================================
							$_SESSION['user_group'] = 2;
						}
						
						//================================================================
						// E-Mail an Benutzer versenden
						//================================================================
						$body_user = $GLOBALS['config_vars']['Reg_Email_1'];
						$body_user = str_replace("---USERNAME---", $_POST['reg_username'], $body_user);
						$body_user = str_replace("---WEBSITE---", thisHome(), $body_user);
						$body_user = str_replace("---EMAIL---", $_POST['reg_email'], $body_user);
						$body_user = str_replace("---PASS---", $pass_nomd5, $body_user);
						$body_user = str_replace("---WEBSITENAME---", $GLOBALS['settings']['Seitenname'], $body_user);
						
						
						$Koobi->koobi_mail($_POST['reg_email'], stripslashes($body_user), $GLOBALS['config_vars']['Reg_Email_Subject'], $GLOBALS['settings']['Mail_Absender'], stripslashes($GLOBALS['settings']['Mail_Name']), "text", "");
						
						$body_admin = $GLOBALS['config_vars']['Reg_Email_Admin'];
						$body_admin = str_replace("---USERNAME---", $_POST['reg_username'], $body_admin);
						$body_admin = str_replace("---TIME---", date("d.m.Y H:i", stime()), $body_admin);
						$body_admin = str_replace("---EMAIL---", $_POST['reg_email'], $body_admin);
						$body_admin = str_replace("---IP---", $_SERVER['REMOTE_ADDR'], $body_admin);
						
						$Koobi->koobi_mail($GLOBALS['settings']['Mail_Absender'], stripslashes($body_admin), $GLOBALS['config_vars']['Reg_Email_Admin_Subject'], $GLOBALS['settings']['Mail_Absender'], stripslashes($GLOBALS['settings']['Mail_Name']), "text", "");		
						$final = true;
						
						if($shop==1)
						{
							if($GLOBALS['settings']['Reg_Typ'] == 'norm')
								refer("index.php?p=shop&area=$_REQUEST[area]&action=shoporder&subaction=step2");
							else
								refer("index.php?p=shop&area=$_REQUEST[area]&action=shoporder&subaction=step2&inf=regcode");
						} else {
							if($GLOBALS['settings']['Reg_Typ'] == 'norm')
								refer("index.php?lang=$_REQUEST[lang]&p=register&sub=ok&area=$_REQUEST[area]");
							else
								refer("index.php?lang=$_REQUEST[lang]&p=register&sub=ok&area=$_REQUEST[area]&inf=regcode");
						}
						exit;
					}
					
					//================================================================
					// Wichtig: Sicherheitscode in der Session berschreiben
					//================================================================
				
					$GLOBALS['tmpl']->assign('error', $error);
					if($use_code==1) $GLOBALS['tmpl']->assign('secureCode', $secureCode);
					$GLOBALS['tmpl']->assign('use_code', $use_code);
					
					giveSecure();
				}
				
				$GLOBALS['tmpl']->assign('headernav', $GLOBALS['config_vars']['RegNewTitle']);
				$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['RegNewTitle']);
				$GLOBALS['tmpl']->assign('settings', $GLOBALS['settings']);
				$GLOBALS['tmpl']->assign('countries', $this->getCountries());
				$GLOBALS['tmpl']->assign('startyear', date("Y")-80);
				$GLOBALS['tmpl']->assign('endyear', date("Y")-10);
				$tpl_out = ($final==true) ? $this->_register_ok_tpl : $this->_register_tpl;
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/user/' . $tpl_out));
			}
		} else {
			not_active();
		}
	}
	
	
	//=================================================================
	// Funktion zur Ausgabe der Seite nach der Registrierung
	//=================================================================
	function registerOk($allready=0)
	{
		$GLOBALS['tmpl']->assign('headernav', $GLOBALS['config_vars']['RegNewTitle']);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['RegNewTitle']);
		$GLOBALS['tmpl']->assign('allready', $allready);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/user/' . $this->_register_ok_tpl));
	}
	
	
	//=================================================================
	// Funktion Laender auslesen
	//=================================================================
	function getCountries()
	{
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_laender WHERE Aktiv=1 ORDER BY Name ASC");
		while($row = $sql->fetcharray())
		{
			$row['Code'] = strtolower($row['Code']);
			$laender[] = $row;
		}
		return $laender;		
	}
	
	//=================================================================
	// Funktion zur Aenderung des Kennwortes
	//=================================================================
	function Changepass()
	{
		if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] != 1)
		{
			$GLOBALS['tmpl']->assign('not_logged', 1);
		} else {
		
			if (isset($_POST['send']) && $_POST['send']=='1') 
			{
				$error = '';
				
				if(empty($_POST['oldpass'])) $error[] = $GLOBALS['config_vars']['ChangePass_E_NoPass'];
				if(empty($_POST['newpass'])) $error[] = $GLOBALS['config_vars']['ChangePass_E_NoNewPass'];
				if(!empty($_POST['newpass']) && @ereg("[^_A-Za-zÀ-ÖØ-öø-ÿ0-9-]", $_POST['newpass']))  $error[] = $GLOBALS['config_vars']['ChangePass_E_BadPass'];
				if(!empty($_POST['newpass']) && strlen($_POST['newpass']) < 5) $error[] = $GLOBALS['config_vars']['ChangePass_E_PassShort'];
				if(empty($_POST['newpass2'])) $error[] = $GLOBALS['config_vars']['ChangePass_E_NoNewPass2'];
				if(!empty($_POST['newpass']) && !empty($_POST['newpass2']) && $_POST['newpass'] != $_POST['newpass2']) $error[] = $GLOBALS['config_vars']['ChangePass_E_PassNotMatch'];
				
				if(empty($error))
				{
					$sql = $GLOBALS['db']->Query("SELECT Kennwort FROM " . PREFIX . "_benutzer WHERE Id = '" . $_SESSION['benutzer_id'] . "' AND Kennwort = '" . md5(md5($_POST['oldpass'])) . "' AND Email = '" . $_SESSION['login_email'] . "' LIMIT 1");
					$num = $sql->numrows();
					if($num < 1) $error[] = $GLOBALS['config_vars']['ChangePass_E_WrongPass'];
				}
			
				// Fehler
				if(!empty($error))
				{
					$GLOBALS['tmpl']->assign('error', $error);
					Logger('Error while changing password for '.$_SESSION['user_name'].'.', '', $_SESSION['benutzer_id']);
				} else {
				// keine Fehler
					$_SESSION['login_pass'] = md5(md5($_POST['newpass']));
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_benutzer SET Kennwort = '" . $_SESSION['login_pass'] . "' WHERE Id = '" . $_SESSION['benutzer_id'] . "' AND Kennwort = '" . md5(md5($_POST['oldpass'])) . "' AND Email = '" . $_SESSION['login_email'] . "'");
					
					Logger('User ' . $_SESSION['user_name'] . ' has changed the password.', '', $_SESSION['benutzer_id']);
					
					$GLOBALS['tmpl']->assign('register_ok', 1);
					$_POST['oldpass'] = '';
					$_POST['newpass'] = '';
				}
			}
		}

		$GLOBALS['tmpl']->assign('headernav', $GLOBALS['config_vars']['ChangePass']);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['ChangePass']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/user/' . $this->_changepass_tpl));
	}
	
	
	
}
?>