<?php

class ANMELDEN{
	
	protected $uid;
	protected $ak;
	protected $werber;
	private $db;
	private $error = 0;
	private $meldung;
	private $post = array;
	private $config;
	private $session;
	
	public function __construct(){
		global $datenbank,$grundconfig,$_SESSION;
		$this->db = $datenbank;
		$this->config = $grundconfig;
		$this->session = $_SESSION;
	}
	
	private function make_array($post){
		foreach($post AS $key => $value){
			if($key != '' && $value = ''){
				$this->post[$key] = $this->db->escape($value);
			}
		}
	}
	
	function anmelden_los(){
		global $_POST;
		$this->make_array($_POST);
		$this->pruef();
		if($this->error == 0){
			$this->escapen();
			$this->konto($this->post['password1']);
			$this->email();
			$this->user();
			$this->werber();
			$this->nickpage();
			$this->aktivierung();
			$this->ak_senden();
			if($this->error == 0){
				return true;
			}else{
				return false;
			}
		}
	}
	
	function ak_senden(){
		$betreff = "Ihre Anmeldung auf ". $this->config->seitenname;
		$email_message = 'Hallo ' . $this->post['nickname'] . ',
    	Du hast Dich soeben erfolgreich bei ' . $this->config->seitenname . ' angemeldet.

    	Bitte aktiviere jetzt Deinen Account, klicke dazu bitte auf den
    	Aktivierungslink: ' . $this->config->domain . '/?page=/intern/aktivieren&ak=' . $this->ak . '

    	----------------------------------------------------------
    	Kontonummer: ' . $this->uid . '
    	Passwort: Ihnen bekannt*
    	----------------------------------------------------------

    	Nach der Aktivierung kannst Du dich sofort einloggen und bei
    	uns teilnehmen.

    	Mit freundlichen Grüßen
    	Das ' . $this->config->seitenname . ' Team
    	';
		$Class = Classloader('mail');
		$Class->senden($_POST['emailadresse'],$betreff,$email_message);
	}
	
	function aktivierung(){
		$this->ak = md5($this->uid . time() );
		$insert = array(
			"uid" => $this->uid,
			"ak" => $this->ak
		);
		$this->db->insert(PREFIX . AKT , $insert);
	}
	
	function nickpage(){
		$insert = array(
			"uid" => $this->uid
		);
		$this->db->insert(PREFIX . NICKP, $insert);
	}
	
	function werber(){
		$insert = array(
			"uid" => $this->uid,
			"werber" => $this->werber_select($this->session['werber']),
			"umsatz" => 0,
			"zuordnungszeit" => time()
		);
		$this->db->insert(PREFIX . WERBER , $insert);
	}
	
	function user(){
		$insert = array(
			"uid" => $this->uid,
			"nickname" => $this->post['nickname'],
			"vorname" => $this->post['vorname'],
			"nachname" => $this->post['nachname'],
			"angemeldet_seit" => time(),
			"bdate" => $this->bdate($this->post['bdate'])
		);
		$this->db->insert(PREFIX . USER , $insert);
	}
 
	function konto($pass){
		$pw = pw_erstellen($pass);
		$this->db->insert(PREFIX . KONTO, array("passwort" => $pw, "status" => 0));
		$this->uid = $datenbank->lastid();
	}
	
	function email(){
		$insert = array(
			"uid" => $this->uid,
			"emailadresse" => $this->post['emailadresse'],
			"freigabe_fuer" => $this->post['newsletter']
		);
		$this->db->insert(PREFIX . EMAIL , $insert);
	}
	
	function bdate($date){
		$explode = explode(".",$date,100);
		$bdate = strtotime($date.'T 00:00:00');
		return $bdate;
	}
	
	function pruef(){
		if(empty($this->post['nachname'])){ $this->error = 1; $this->meldung .= ANMELDEN_NAME_FALSE;}
		if(empty($this->post['bdate'])){ $this->error = 1; $this->meldung .= ANMELDEN_GEB_FALSE;} 
		if(empty($this->post['vorname'])){ $this->error = 1; $this->meldung .= ANMELDEN_VORNAME_FALSE;}
		$this->mailadresse($this->post['emailadresse']);
		$this->nickname($this->post['nickname']);
		$this->pass($this->post['password1'],$_POST['password2']);
		if($this->post['agb'] != "ja"){ $this->error = 1; $this->meldung .= ANEMLDEN_AGB_FALSE; }
	}
	
	function mail_denied($mail){
		$denied = explode(';', $this->config->denied_domains);
		if($this->config->ip_erlaubt != ''){
			$ip_denied = explode(',', $this->config->ip_erlaubt);
			if(!in_array(geoIP($ip),$ip_denied)){
				$this->error = 1;
				$this->meldung .= ANMELDEN_IP_FALSE;}
		}
		foreach ($denied AS $nr => $emaildomain){
			if (($nr < (count ($denied) - 2) || strlen ($emaildomain) >= 5) && strpos ($mail, $emaildomain) !== false){
				$this->error = 1;
				$this->meldung .= ANMELDEN_MAIL_ANBIETER_FALSE;
			}
		}
	}
	
	function mailadresse($mail){
		if(!filter_var( $mail, FILTER_VALIDATE_EMAIL )) { $this->error = 1; $this->meldung .= ANMELDEN_EMAIL_FALSE;}
		$select = $this->db->num_rows("SELECT * FROM ". PREFIX . EMAIL ." WHERE emailadresse = '$mail' ");
		if($select == true){
			$this->error = 1;
			$this->meldung .= ANMELDEN_MAIL_EXIST;
		}
		$this->mail_denied($mail);
	}
	
	function pass($pass1,$pass2){
		if(empty($pass1)){ $this->error = 1; $this->meldung .= ANMELDEN_PASS1_FALSE;}
		if(empty($pass2)){ $this->error = 1; $this->meldung .= ANMELDEN_PASS2_FALSE;}
		if($pass1 != $pass2){ $this->error = 1; $this->meldung .= ANMELDEN_PASS_KONTROLLE_FALSE;}
		if(strlen($pass1) < 8){ $this->error = 1; $this->meldung .= ANMELDEN_PASS_SHORT; }
	}
	
	function nickname($nick){
		if(empty($nick)){$this->error = 1; $this->meldung = ANMELDEN_NICKNAME_FALSE;}
		$select = $this->db->exists(PREFIX . USER , 'nickname', array("nickname" => $nick));
		if($select == true){
			$this->error = 1;
			$this->meldung .= ANMLEDEN_NICKNAME_EXIST;
		}
	}
	
	function werber_select($werber){
		if($werber != ''){
			$ab = $this->db->exists(PREFIX . USER , 'uid' , array("uid" => $werber));
			if(!is_numeric($werber) OR $ab == false){
				$this->werber = 0;
			}
		}else{
			$this->werber = $werber;
		}
	}
}

?>