<?php

class bettel{
	private $db;
	private $session = array();
	private $gconfig;
	private $config;
	private $uid = 0;
	private $error = 0;
	private $meldung = '';
	private $cookie = 0;
	private $ip;
	private $refferer;
	private $server;
	private $url;
	
	function __construct(){
		global $_SESSION,$datenbank,$grundconfig,$_SERVER;
		$this->url = new stdClass;
		$this->db = $datenbank;
		$this->session = $this->db->escape($_SESSION);
		$this->gconfig = $grundconfig;
		$this->ip = $_SERVER['REMOTE_ADDR'];
		if(isset($_SERVER['HTTP_REFERER'])){
			$this->refferer = $this->db->escape($_SERVER['HTTP_REFERER']);
		}else{
			$this->refferer = 'direktaufruf';
		}
	}
	
	public function Top25(){
		$aus = '';
		$rang = 1;
		$result = $this->db->get_results('SELECT k.uid, k.angebettelt, k.bv, u.nickname FROM ' . PREFIX . KONTO . ' k LEFT JOIN ' . PREFIX . USER . ' u ON u.uid = k.uid WHERE k.angebettelt > 0 ORDER BY angebettelt DESC LIMIT 25');
		foreach($result AS $res){
			$aus .= '
				<tr>
					<td>' . $rang++ . '.&nbsp;</td>
					<td>&nbsp;' . $res->nickname . '</td>
					<td align="right">' . number_format($res->angebettelt, 0) . '&nbsp;</td>
					<td align="right">' . number_format($res->bv, 2, ',', '.') . ' ' . $this->config->waehrung . '&nbsp;</td>
				</tr>
			';
		}
		return $aus;
	}
	
	private function AngebetteltPruef(){
		if($this->session['uid'] == $this->uid || $this->uid == $this->cookie){
			$this->error = 1;
			$this->meldung = BETTEL_SELF_TRUE;
		}
	}
	
	private function ReloadCheck(){
		if($this->db->num_rows("SELECT bis FROM " . PREFIX . RELOAD . " WHERE ip = '$this->ip' and tan = 'bettelaufruf' and bis >= " . time() . " LIMIT 1") == 1){
			$this->error = 1;
			$this->meldung = BETTEL_RELOAD_TRUE;
		}
	}
	
	private function BettelSperre(){
		if($this->db->num_rows("SELECT uid FROM ". PREFIX . KONTO ." WHERE uid = '$this->uid' AND bettel_sperre = '1' ") != 0){
			$this->error = 1;
			$this->meldung = BETTEL_SPERRE_TRUE;
		}
	}
	
	private function CheckReferer(){
		if(!isset($this->server['HTTP_REFERER'])){
			$this->server['HTTP_REFERER'] = 'kein Referer';
			$this->url->host = 'direktaufruf';
		}else{
			$url = parse_url($this->server);
			$this->url->host = $url['host'];
		}
	}
	
	private function BettelAuszahlung(){
		$this->CheckReferer();
		$bettelsumme = rand($this->gconfig->min_betteln * 100, $this->gconfig->max_betteln * 100) / 100;
		$new_reload = time() + $this->gconfig->reload_betteln;
		$bettelb = array(
			"uid" => $this->uid,
			"refferer" => $this->server['HTTP_REFERER'],
			"url" => 'https://'.$this->url->host,
			"betrag" => $bettelsumme,
			"zeit" => time(),
			"status" => 1
		);
		//$this->db->insert(PREFIX . RELOAD, array("ip" => $this->ip,"uid" => $this->uid ,"tan" => 'bettelaufruf',"bis" => $new_reload) );
		if($this->gconfig->bettel_direkt == 1 && $this->db->num_rows("SELECT id FROM ". PREFIX . BETTELURLS ." WHERE url = '". $bettelb['refferer'] ."' ") == 0){
			$this->db->query("UPDATE ". PREFIX . KONTO ." SET angebettelt = angebettelt + 1, bv = bv + $bettelsumme, kontostand_a = kontostand_a + $bettelsumme WHERE uid = '$this->uid' ");
			$bilanz = Classloader('bilanz');
			refumsatz ($bettelsumme, $this->uid);
			rallysystem ($this->uid, '6', $bettelsumme);
			$bilanz->bilanz(0,$bettelsumme,'werbekosten','Bettellink');
			$this->meldung = '<b>Du hast für den User ' . $this->uid . ' gerade ' . $bettelsumme . ' ' . $this->gconfig->waehrung . ' erbettelt!</b>';
		}else{
			$bettelb['status'] = 0;
			$this->meldung = 'Leider konnte dein Aufruf nicht gewertet werden, da du keinen Referer &uuml;bergeben hast.';
		}
		
		$this->db->insert(PREFIX . BETTELB, $bettelb,1);
	}
	
	public function BettelAufruf($get,$cookie,$server){
		$this->server = $server;
		if(isset($cookie['uid'])){
			if(is_numeric($cookie['uid'])){$this->cookie = $cookie['uid'];}
		}
		if(is_numeric($get['ref'])){ $this->uid = $get['ref'];}
		if($this->gconfig->reload_betteln > 0){
			$this->AngebetteltPruef();
			if($this->error == 0){ $this->ReloadCheck();}
			if($this->error == 0){ $this->BettelSperre();}
			if($this->error == 0){ $this->BettelAuszahlung();}
		}else{
			$this->error = 1;
			$this->meldung = BETTEL_NOT_ACTIVE;
		}
		
		return $aus = array("error" => $this->error, "meldung" => $this->meldung);
	}
}
?>