<?php

class USERPROFIL {
	
	private $db;
	private $uid;
	private $delete_true = false;
	private $array;
	
	public function __construct() {
		global $datenbank;
		$this->db = $datenbank;
	}
	
	public function set_uid($uid){
		if(!is_numeric($uid)){ $this->uid = 0;}else{ $this->uid = $this->db->escape($uid);}
	}
	
	public function get_daten(){
		$aus = $this->db->get_row("
		SELECT u.*, e.*, k.*, n.* FROM ". PREFIX . USER . " AS u LEFT JOIN  ". PREFIX . EMAIL . " AS e ON e.uid = u.uid LEFT JOIN ". PREFIX . KONTO ." AS k ON k.uid = u.uid LEFT JOIN ". PREFIX . NICKP ." AS n ON n.uid = u.uid WHERE u.uid = '" . $this->uid . "' ",true);
		return $aus;
	}
	
	public function delete_account($pw){
		global $error,$meldung;
		$this->pass_pruef($pw);
		if($this->delete_true == true){
			$this->db->delete(PREFIX . KONTO , array("uid" => $this->uid),1);
			$this->db->delete(PREFIX . EMAIL, array("uid" => $this->uid),1);
			$this->db->delete(PREFIX . USER , array("uid" => $this->uid),1);
			$this->db->delete(PREFIX . WERBER , array("uid" => $this->uid),1);
			$this->db->update(PREFIX . WERBER, array("werber" => 0), array("werber" => $this->uid));
			$this->db->delete(PREFIX . BUCH , array("uid" => $this->uid),1);
			$this->db->delete(PREFIX . RELOAD , array("uid" => $this->uid),1);
			$this->db->delete(PREFIX . S_ANF , array("uid" => $this->uid),1);
			header('Location: '. $_SERVER['HTTP_HOST']);
		}else{
			$error = 1;
			$meldung = PASS_FALSE;
		}
	}
	
	protected function pass_pruef($pw){
		if($this->db->num_rows("SELECT id FROM ". PREFIX . KONTO ." WHERE uid = '$this->uid' AND passwort = '". pw_erstellen($pw) ."' ") == 1){
			$this->delete_true = true;
		}
	}
	
	public function user_daten_save(){
		global $error,$meldung;
		$this->make_array();
		if ($this->array['newsletter'] == 0 and $this->array['paidmails'] == 0) $mailstatus = 0;
        if ($this->array['newsletter'] == 1 and $this->array['paidmails'] == 0) $mailstatus = 1;
        if ($this->array['newsletter'] == 0 and $this->array['paidmails'] == 1) $mailstatus = 2;
        if ($this->array['newsletter'] == 1 and $this->array['paidmails'] == 1) $mailstatus = 3;
		$array = array(
			"freigabe_fuer" => $mailstatus,
			"emailadresse" => $this->array['emailadresse']
		);
		$array_user = array(
			"max_forced" => $this->array['max_forced'],
			"max_paidlinks" => $this->array['max_paidlinks']
		);
		$update_mail = $datenbank->update(PREFIX . MAIL , $array, array("uid" => $this->uid), 1);
		if($update_mail == true){ $meldung .= MAILEMPFANG_TRUE;}else{$error = 1; $meldung .= MAILEMPFANG_FALSE;}
		$update_user = $datenbank->update(PREFIX . USER , $array_user, array("uid" => $this->uid), 1);
		if($update_user == true){ $meldung .= BANNER_TRUE;}else{ $error = 1; $meldung .= BANNER_FALSE;}
	}
	
	public function pw_erneuern(){
		global $error,$meldung;
		$this->make_array();
		if(empty($this->array['pwd']) OR empty($this->array['pwd2'])){ $error = 1; $meldung .= ANMELDEN_PASS1_FALSE;}
		if($this->array['pwd'] != $this->array['pwd2']){ $error = 1; $meldung .= ANMELDEN_PASS_KONTROLLE;}
		if($error == 0){
			$update = $this->db->update(PREFIX . KONTO , array("passwort" => pw_erstellen($this->array['pwd'])), array("uid" => $this->uid) , 1);
			if($update == true){
				$meldung .= UPDATE_PASS_TRUE;
			}else{
				$error = 1;
				$meldung .= UPDATE_PASS_FALSE;
			}
		}
	}
	
	private function make_array(){
		global $_POST;
		$array = array();
		foreach($_POST AS $key => $value){
			$array[$key] = $this->db->escape($value);
		}
		$this->array = $array;
	}
}

?>