<?php

class Verdienen{
	private $db;
	private $session = array();
	private $art;
	private $config;
	private $ip;
	private $tan;
	private $get = array();
	private $konto;
	
	function __construct(){
		global $datenbank,$_SESSION,$grundconfig,$_SERVER,$_GET,$konto;
		$this->db = $datenbank;
		$this->session = $this->db->escape($_SESSION);
		$this->config = $grundconfig;
		$this->ip = $_SERVER['REMOTE_ADDR'];
		$this->get = $this->db->escape($_GET);
		$this->konto = $konto;
	}
	
	public function SetArt($art){
		$this->art = htmlspecialchars($this->db->escape($art));
	}
	
	public function Statik(){
		$fstats_gesamt = $this->db->get_row(
			"
			SELECT 
				COUNT(t2.tan) AS anzahlreload,
				COUNT(t1.tan) AS ganzahl, 
				SUM(t1.aufendhalt) AS gaufenthalt,
				SUM(t1.verdienst) AS gverdienst 
			FROM ". PREFIX . WERBUNG ." t1
			LEFT JOIN
				". PREFIX . RELOAD ."
			AS 
				t2
			ON 
				t2.tan = t1.tan 
			WHERE 
				t1.werbeart = '$this->art' ",true);
		$fstats_uebrig = $this->db->get_row("
				SELECT 
					SUM(t2.aufendhalt) AS reloadaufendhalt,
					SUM(t2.verdienst) AS reloadverdienst
				FROM ". PREFIX . WERBUNG ." t2
				LEFT JOIN
					". PREFIX . RELOAD ." AS t1 
				ON t2.tan = t1.tan
				WHERE 
					t2.tan = t1.tan 
				AND 
					t1.uid = '". $this->session['uid'] ."' 
				AND
					bis <= ". time() ."",true);
		$aus = "
			<table class=\"table\">
				<tr>
					<td align='center''>gesamt: ".$fstats_gesamt->ganzahl."</td>
					<td>&nbsp;&nbsp;</td>
					<td align='center'>uebrig: ". number_format($fstats_gesamt->ganzahl - $fstats_gesamt->anzahlreload,0,',','.') ."</td>
				</tr>
				<tr>
					<td align='center'>&Oslash; Aufenthalt: ".number_format($fstats_gesamt->gaufenthalt - $fstats_uebrig->reloadaufendhalt / $fstats_gesamt->ganzahl,2,",",".")." Sekunden</td>
					<td>&nbsp;&nbsp;</td>
					<td align='center''>&Oslash; Verg&uuml;tung: ".number_format($fstats_gesamt->gverdienst - $fstats_uebrig->reloadverdienst / $fstats_gesamt->ganzahl,2,",",".")." ".$this->config->waehrung."</td>
				</tr>
				<tr>
					<td align='center''>Gesamtwert: ". number_format($fstats_gesamt->gverdienst,2,',','.') ." ".$this->config->waehrung."</td>
					<td>&nbsp;&nbsp;</td>
					<td align='center'>Offen: ". number_format($fstats_gesamt->gverdienst - $fstats_uebrig->reloadverdienst,2,',','.') ." ".$this->config->waehrung."</td>
				</tr>
			</table>
			
			<div class='progress'>
				<div class='progress-bar progress-bar-striped progress-bar-animated' role='progressbar' style='width: ". round(100/$fstats_gesamt->ganzahl * $fstats_gesamt->anzahlreload,2) ."%; color:black; text-align: center;' aria-valuenow='". round(100/$fstats_gesamt->ganzahl * $fstats_gesamt->anzahlreload,2) ."' aria-valuemin='0' aria-valuemax='100'>
					<span class='justify-content-center d-flex position-absolute w-100'>". round(100/$fstats_gesamt->ganzahl * $fstats_gesamt->anzahlreload,2) ."% best&auml;tigt</span>
				</div>
			</div>
			<hr>";
		return $aus;
	}
	
	public function Ausgabe(){
		$aus = '';
		if($this->db->num_rows("SELECT * FROM ".PREFIX . KONTO ." WHERE uid = '".$this->session['uid']."' AND forced_sperre != '0' ") > 0){
			$aus = '<div class="alert alert-danger">Du bist f&uuml;r den Klickbereich gesperrt</div>';
		}else{
			if($this->art == 'forcedbanner'){ $aus = $this->ForcedBannerAusgabe();}
			if($this->art == 'paidmail'){ $aus = $this->PaidMailAusgabe();}
		}
		return $aus;
	}
	
	private function Query(){
		$usr = $this->db->get_row('SELECT max_forced FROM ' . PREFIX. USER . ' WHERE uid = ' . $this->session['uid'] . ' LIMIT 1',true);
		return $this->db->get_results ('
		SELECT t1.* FROM ' . PREFIX . WERBUNG . ' t1
		LEFT JOIN ' . PREFIX . FBBLACK . ' AS t3 ON t3.kid = t1.kid 
		AND t3.werbeart=t1.werbeart LEFT JOIN ' . PREFIX . RELOAD . ' t2 ON (t1.tan = t2.tan AND (t2.uid = ' . $this->session['uid'] . ' OR t2.ip = "' . $this->ip . '") AND t2.bis >= ' . time() . ' AND t2.bis != 0)
		WHERE (t3.kid IS NULL OR LOCATE(t3.sponsor, t1.ziel) = 0) 
		AND t2.tan IS NULL AND t1.werbeart = "'. $this->art .'" AND t1.menge > 0 AND t1.status = 1 AND t1.verdienst > 0 AND t1.sponsor != ' . $this->session['uid'] . ' 
		ORDER BY t1.verdienst DESC LIMIT ' . $usr->max_forced );
	}
	
	private function PaidMailAusgabe(){
		$aus = '';
		$result = $this->Query();
		
		foreach($result AS $res) {
			$aus .= '
				<div class="card text-center" id="mail_'.$res->tan.'">
					<div class="card-header">
						'. $res->betreff .'
					</div>
					<div class="card-body">
						<p class="card-text">'. nl2br($res->text) .'</p>
						<a href="pclick.php?tan=' . $res->tan . '&uid=' . $this->session['uid'] . '" target="_new" onclick="$( \'#mail_'.$res->tan.'\' ).hide( \'slow\' );" class="btn btn-success">Klick</a>
					</div>
					<div class="card-footer text-muted">
						Aufendhalt: '. $res->aufendhalt .' sek&nbsp;||&nbsp; Verdienst: '. number_format($res->verdienst,2,',','.') .' '. $this->config->waehrung .'&nbsp;||&nbsp; Reload: '. $res->reload .' min
					</div>
				</div>
			';
		}
		if (count((array)$result) > 0){
			$aus .= '<br><div align="center"><input type="button" name="mehr_banner" class="btn btn-info" value="Weitere laden " onclick="javascript:location.reload();" /></div>';
		}else{
			$aus .= '<div class="alert alert-warning text-center">Alles bereits im Reload !</div>';
		}
		return $aus;
	}
	
	private function ForcedBannerAusgabe(){
		$aus = '';
		$result = $this->Query();
		
		foreach($result AS $res) {
			$aus .= '
			<div align="center" id="banner_' . $res->tan . '">
				<a href="top_forcedbanner.php?tan=' . $res->tan . '" target="_blank">
					<img src="' . $res->banner . '" border="0" height="60" width="468" alt="' . $res->tan . '" onClick="document.getElementById(\'banner_' . $res->tan . '\').style.display=\'none\';">
				</a>'
			. '<br>
			Reload:' . $res->reload / 3600 . ' Std. | Verdienst: ' . number_format($res->verdienst, 2, ',', '.') . ' | Aufenthalt: ' . $res->aufendhalt . ' Sekunden<br><br>
			</div>
			';
		}
		if (count((array)$result) > 0){
			$aus .= '<div align="center"><input type="button" name="mehr_banner" class="btn btn-info" value="Weitere Banner !" onclick="javascript:location.reload();" /></div>';
		}else{
			$aus .= '<div class="alert alert-warning text-center">Alle Banner im Reload !</div>';
		}
		return $aus;
	}
	
	public function SetTan($tan){
		$this->tan = htmlspecialchars($this->db->escape($tan));
	}
	
	private function Login(){
		if ($this->session['login'] != true || $this->session['uid'] <= 0) die('Bitte einloggen!');
	}
	
	public function Abfrage(){
		$this->Login();
		$abfrage = $this->db->get_row("SELECT aufendhalt FROM ". PREFIX . WERBUNG ." WHERE tan = '$this->tan' ",true);
		$reload = $this->db->num_rows("SELECT * FROM ". PREFIX . RELOAD ." WHERE tan = '$this->tan' AND bis > '". time() ."' AND (uid = '". $this->session['uid'] ."' OR ip = '$this->ip' ) ");
		return array("wartezeit" => $abfrage->aufendhalt, "reload" => $reload);
	}
	
	private function ReloadCheck(){
		return $this->db->num_rows("SELECT uid FROM ". PREFIX . RELOAD ." WHERE tan = '$this->tan' AND (uid = '". $this->session['uid'] ."' OR ip = '$this->ip') ");
	}
	
	public function ReloadPruefen(){
		global $_SESSION;
		$aus = array("forced_error" => 'false');
		if ($this->ReloadCheck() == 0) {
			$forced = $this->db->get_row("SELECT * FROM ". PREFIX . WERBUNG ." WHERE tan = '$this->tan' ",true);
			$aus['wartezeit'] = $forced->aufendhalt;
			$aus['headmsg'] = 'Vergütung in ' . $forced->aufendhalt . ' Sek.!';
			$aus['tan'] = $forced->tan;
			if ($this->get['auszahlen'] != 'true') {
				$aus['puk'] = md5($this->session['uid'] . $forced->aufendhalt . date("d.m.Y", time()) );
				$_SESSION['earlies_payout' . $this->get['tan']] = time() + $wartezeit - 1;
			}
		} else {
			$aus['headmsg'] = 'Banner noch im Reload!';
			$aus['force_error'] = 'true';
		}
		return $aus;
	}
	
	public function ReloadZahlung($Abfrage){
		$aus = array();
		$reload = $this->db->num_rows("SELECT * FROM ". PREFIX . RELOAD ." WHERE tan = '$this->tan' AND bis > '". time() ."' AND (uid = '". $this->session['uid'] ."' OR ip = '$this->ip' ) ");
		$forced = $this->db->get_row("SELECT * FROM ". PREFIX . WERBUNG ." WHERE tan = '$this->tan' ",true);
		if ($this->get['auszahlen'] == 'true' && $reload == 0 && $Abfrage['force_error'] != 'true' && $this->get['puk'] == md5($this->session['uid'] . $forced->aufendhalt . date("d.m.Y", time()) ) && time() >= $this->session['earlies_payout' . $this->get['tan']]) {
			$this->db->update(PREFIX . KONTO, array("klicks" => 'klicks + 1', "kv" => "kv + ".$forced->verdienst, "fc_klicks" => "fc_klicks + 1"),array("uid" => $this->session['uid']),1,false);
			$this->konto->kontobuchung_a('+', $forced->verdienst, create_code(14), $this->session['uid'], 1, $this->art.'verdienst');
			refumsatz ($forced->verdienst, $this->session['uid']);
			rallysystem ($this->session['uid'], '1', $forced->verdienst);
			//bilanz($forced->preis, $forced->verdienst,'Werbebereich',$this->art);
			$new_reload = time() + $forced->reload;
			$this->db->insert(PREFIX . RELOAD, array("ip" => $this->ip,"uid" => $this->session['uid'],"tan" => $forced->tan,"bis" => $new_reload));
			$this->db->update(PREFIX . WERBUNG,array("menge" => "menge-1"), array("tan" => $this->get['tan']),1,false);
			$_SESSION['earlies_payout' . $_GET['tan']] = '';
			$aus['headmsg'] = $forced->verdienst . ' ' . $this->config->waehrung . ' erhalten!';
		} elseif ($this->get['auszahlen'] == 'true' && time() < $this->session['earlies_payout' . $this->get['tan']]) {
			$aus['headmsg'] = 'Wartezeit umgangen.';
		} elseif ($this->get['auszahlen'] == 'true' && $Abfrage['force_error'] != 'true') {
			$aus['headmsg'] = 'Pin abgelaufen';
		}
		return $aus;
	}
}
?>