<?
if(isset($_SESSION['id'])) {
	echo '<h2>Coins aufladen</h2>';

	//Conf
	$euro10bonus = $bonus10;
	$euro25bonus = $bonus25;
	$euro50bonus = $bonus50;
	$euro100bonus = $bonus100;
	//Conf



	//Funktionen
	function middlestring($string,$start,$stop){
		$pos = strpos($string,$start)+strlen($start);
		$retstr = substr($string,$pos);
		$pos = strpos($retstr,$stop);
		$retstr = substr($retstr,0,$pos);
		return $retstr;
	}
	function passwort($len) {
		$pool = "qwertzupasdfghkyxcvbnm";
		$pool .= "23456789";
		$pool .= "WERTZUPLKJHGFDSAYXCVBNM";
		srand ((double)microtime()*1000000);
		$pass_word = "Z";
		for($index = 0; $index < $len; $index++)
		{
			$pass_word .= substr($pool,(rand()%(strlen ($pool))), 1);
		}
		return $pass_word;
	}
	function curl_get($url, $head, $cook){
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, $head);
		if($cook == 1) curl_setopt($ch, CURLOPT_COOKIE, $_SESSION["jsession"].$_SESSION["TS"]);
			return curl_exec($ch);
	}

	function curl_post($url, $datas, $cook){
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_POST,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"); 
		if($cook == 1) curl_setopt($ch, CURLOPT_COOKIE, $_SESSION["jsession"].$_SESSION["TS"]);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		return curl_exec($ch);
	}

	//Vars.
	$captcha = mysql_real_escape_string($_REQUEST["captcha"]);
	$pass = mysql_real_escape_string($_REQUEST["pass"]);
	$pin1 = mysql_real_escape_string($_REQUEST["j_id17rn1"]);
	$pin2 = mysql_real_escape_string($_REQUEST["j_id17rn2"]);
	$pin3 = mysql_real_escape_string($_REQUEST["j_id17rn3"]);
	$pin4 = mysql_real_escape_string($_REQUEST["j_id17rn4"]);
	$pin = $pin1.'-'.$pin2.'-'.$pin3.'-'.$pin4;
	$pin = ereg_replace("[^0-9]", "", $pin);

	if(!empty($captcha)) 
		{
		$newpass = passwort(9);
		$text = curl_post('https://customer.cc.at.paysafecard.com/psccustomer/GetWelcomePanelServlet' ,'locale:languageSelection=de_DE&mainPagePart=mainPagePart&mainPagePart%3Arn1='.$pin1.'&mainPagePart%3Arn2='.$pin2.'&mainPagePart%3Arn3='.$pin3.'&mainPagePart%3Arn4='.$pin4.'&mainPagePart%3ApassField='.$pass.'&mainPagePart%3AverifyCaptcha='.$captcha.'&mainPagePart%3Anext=Absenden&javax.faces.ViewState='.$_SESSION["javax"], 1);
		if(strpos($text, "Balance?"))
		{
			$text = curl_get('https://customer.cc.at.paysafecard.com/psccustomer/Balance',1,1);
			$psc_komplettes_guthaben = middlestring($text, '<tr class="price">', '</tr>');
			$psc_guthaben = middlestring($psc_komplettes_guthaben, '<td>', '.');
			$waehrung = middlestring(middlestring($psc_komplettes_guthaben,'</td>', '/td>'),'<td>','<');
			$serial_number = middlestring(middlestring($text,'<td class="cell1">Serial number</td>','/td>'),'<td class="cell2">','<');
			$date_p = middlestring(middlestring($text,'<td class="cell1">Date of manufacture</td>','/td>'),'<td class="cell2">','<');
			if($psc_guthaben == '')
			{
				$psc_guthaben = middlestring(middlestring($text, '<tr class="price">', '</td>'), '<td>', '.');
			}
			if(strpos($text, $psc_guthaben))
			{
				if(!empty($pass))
				{
					if($waehrung == "EUR")
					{
						if($psc_guthaben >= 10 && $psc_guthaben < 25) {
							$geld = $psc_guthaben * $eurcoins + $euro10bonus;
						} elseif($psc_guthaben >= 25 && $psc_guthaben < 50) {
							$geld = $psc_guthaben * $eurcoins + $euro25bonus;
						} elseif($psc_guthaben >= 50 && $psc_guthaben < 100) {
							$geld = $psc_guthaben * $eurcoins + $euro50bonus;
						} elseif($psc_guthaben >= 100) {
							$geld = $psc_guthaben * $eurcoins + $euro100bonus;
						} else {
							$geld = $psc_guthaben * $eurcoins;
						}
					}
					else
					{
						//PSC Exchange
						$text1 = curl_post('http://www.paysafecard.com/exchange/exchange.php' ,'betrag='.$psc_guthaben.'&geld='.$waehrung.'&geld1=EUR&submit=berechnen', 0);
						$wert_ex1 = middlestring($text1, "<div class='row'><strong>", "</strong><br><strong>");
						$guthaben_len = strlen($psc_guthaben) + 4;
						$wert_ex1 = substr($wert_ex1,$guthaben_len);
						$wert_ex = substr($wert_ex1,0,-2);
						$psc_guthaben = $wert_ex;
					}
					mysql_select_db('account');
					$insert_coins = mysql_query("UPDATE account SET coins = coins + '".$geld."' WHERE id = '".$_SESSION['id']."' LIMIT 1") or die(mysql_error());
					mysql_select_db($db);
					$insert_log = mysql_query("INSERT INTO psc_log (psc,old_pass,new_pass,currency,money,acc_id,ip,psc_id,productdate,dateline) VALUES ('".$pin."','".$pass."','".$newpass."','".$waehrung."','".$psc_guthaben."','".$_SESSION['id']."','".$_SERVER['REMOTE_ADDR']."','".$serial_number."','".$date_p."','".$dateline."')");
					if($insert_coins && $insert_log) {
						echo "Kauf erfolgreich! Falls du keine Coins erhalten hast kontaktiere bitte einen Administrator.";
						$_SESSION["coins"] = $_SESSION['coins'] + $geld;
						$text = curl_get('https://customer.cc.at.paysafecard.com/psccustomer/ChangePassword', 1, 1);
						$_SESSION["javax"] = middlestring($text, 'id="javax.faces.ViewState" value="', '" />');
						curl_post('https://customer.cc.at.paysafecard.com/psccustomer/ChangePassword', 'passwordPage=passwordPage&passwordPage%3ApasswordOld='.$pass.'&passwordPage%3Apassword1='.$newpass.'&passwordPage%3Apassword2='.$newpass.'&passwordPage%3Asubmit=Absenden&javax.faces.ViewState='.$_SESSION["javax"], 1);
					}
					elseif(!$insert_coins) {
						echo 'error123';
					}
					elseif(!$insert_log) {
						echo 'error127';
					}			}
				else
				{
					if($waehrung == "EUR")
					{
						if($psc_guthaben >= 10 && $psc_guthaben < 25) {
							$geld = $psc_guthaben * $eurcoins + $euro10bonus;
						} elseif($psc_guthaben >= 25 && $psc_guthaben < 50) {
							$geld = $psc_guthaben * $eurcoins + $euro25bonus;
						} elseif($psc_guthaben >= 50 && $psc_guthaben < 100) {
							$geld = $psc_guthaben * $eurcoins + $euro50bonus;
						} elseif($psc_guthaben >= 100) {
							$geld = $psc_guthaben * $eurcoins + $euro100bonus;
						} else {
							$geld = $psc_guthaben * $eurcoins;
						}
					}
					else
					{
						//PSC Exchange
						$text1 = curl_post('http://www.paysafecard.com/exchange/exchange.php' ,'betrag='.$psc_guthaben.'&geld='.$waehrung.'&geld1=EUR&submit=berechnen', 0);
						$wert_ex1 = middlestring($text1, "<div class='row'><strong>", "</strong><br><strong>");
						$guthaben_len = strlen($psc_guthaben) + 4;
						$wert_ex1 = substr($wert_ex1,$guthaben_len);
						$wert_ex = substr($wert_ex1,0,-2);
						$psc_guthaben =  $wert_ex;
					}
					$insert_coins = mysql_query("UPDATE account SET coins = coins + '".$geld."' WHERE id = '".$_SESSION['id']."' LIMIT 1") or die(mysql_error());
					$insert_log = mysql_query("INSERT INTO psc_log (psc,new_pass,currency,money,acc_id,ip,psc_id,productdate,dateline) VALUES ('".$pin."','".$newpass."','".$waehrung."','".$psc_guthaben."','".$_SESSION['id']."','".$_SERVER['REMOTE_ADDR']."','".$serial_number."','".$date_p."','".$dateline."')");
					if($insert_coins && $insert_log) {
						echo "Kauf erfolgreich! Falls du keine Coins erhalten hast kontaktiere bitte einen Administrator.";
						$_SESSION["coins"] = $_SESSION['coins'] + $geld;
						$text = curl_get('https://customer.cc.at.paysafecard.com/psccustomer/SetPassword', 1, 1);
						$_SESSION["javax"] = middlestring($text, 'id="javax.faces.ViewState" value="', '" />');
						curl_post('https://customer.cc.at.paysafecard.com/psccustomer/SetPassword', 'passwordPage=passwordPage&passwordPage%3Apassword1='.$newpass.'&passwordPage%3Apassword2='.$newpass.'&passwordPage%3Asubmit=Submit&javax.faces.ViewState='.$_SESSION["javax"], 1);
					}
					elseif(!$insert_coins) {
						echo 'error123 2'.mysql_error();
					}
					elseif(!$insert_log) {
						echo 'error127 2'.mysql_error();
					}	
				}
			} 
			else
			{
				$pscerror = $psc5lng;
			}
		}
		elseif(strpos($text, "Der eingegebene Text stimmt") OR strpos($text, "The text entered is not the same"))
		{
			echo "Du hast das Captcha falsch eingegeben!";
		}
		elseif(strpos($text, "Bei deinem PIN-Code") OR strpos($text, "with your PIN and/or password"))
		{
			echo "Ueberpruefe die Eingabe deines Paysafecardcodes und/oder Passworts!";
		}
		else
		{
			echo $text; exit;
		}
		if(!empty($pscerror))
		{
			echo "Es ist ein unbekannter Fehler aufgetreten!";
		}
	}
	else
	{
		$text = curl_get('https://customer.cc.at.paysafecard.com/psccustomer/GetWelcomePanelServlet', 1, 0);
		$_SESSION["javax"] = middlestring($text, 'id="javax.faces.ViewState" value="', '" />');
		$_SESSION["jsession"] = 'JSESSIONID='.middlestring($text, 'Set-Cookie: JSESSIONID=', " Path=/; Secure");
		$_SESSION["TS"] = 'TS481b70='.middlestring($text, "Set-Cookie: TS481b70=", " Path=/");
	?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

	<form method="post" name="form2">
		<table>
			<tr>
				<td>Paysafecode: </td>
				<td><input id="j_id17rn1" type="text" class="bar" name="j_id17rn1" autocomplete="off"  style="width:50px;" maxlength="4"size="4" /> <input id="j_id17rn2" type="text" class="bar" name="j_id17rn2" autocomplete="off"  style="width:50px;" maxlength="4"size="4" /> <input id="j_id17rn3" type="text" class="bar" name="j_id17rn3" autocomplete="off"  style="width:50px;" maxlength="4"size="4" /> <input id="j_id17rn4" type="text" class="bar" name="j_id17rn4" autocomplete="off"  style="width:50px;" maxlength="4"size="4" /></td>
			</tr>
			<tr>
				<td>Passwort (falls vorhanden): </td>
				<td><input type="password" class="bar" name="pass" ></td>
			</tr>
			<tr>
				<td><?php echo "<img src='data:image/jpg;base64,".base64_encode(curl_get('https://customer.cc.at.paysafecard.com/seam/resource/captcha', 0, 1))."' style='border: 1px solid black;' />"; ?></td>
				<td><input type="text" class="bar" name="captcha" ></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" class="btn" value="Spenden"><input class="btn" type="reset" value="Reset"></td>
			</tr>
		</table>
		<b>Rechner</b><br>
		<script language="javascript"> 

	 
	function eurotocoins()
	{
		if(document.forms[0].euro.value<10) {
			document.forms[0].coins.value=document.forms[0].euro.value*<? echo $eurcoins; ?> + " Coins";
		}
		else if(document.forms[0].euro.value>=10 && document.forms[0].euro.value<25) {
			document.forms[0].coins.value=document.forms[0].euro.value*<? echo $eurcoins.'+'.$bonus10;?> + " Coins";
		}
		else if(document.forms[0].euro.value>=25 && document.forms[0].euro.value<50) {
			document.forms[0].coins.value=document.forms[0].euro.value*<? echo $eurcoins.'+'.$bonus25;?> + " Coins";
		}
		else if(document.forms[0].euro.value>=50 && document.forms[0].euro.value<100) {
			document.forms[0].coins.value=document.forms[0].euro.value*<? echo $eurcoins.'+'.$bonus50;?> + " Coins";
		}	
		else if(document.forms[0].euro.value>=100) {
			document.forms[0].coins.value=document.forms[0].euro.value*<? echo $eurcoins.'+'.$bonus100;?> + " Coins";
		}
	}
	function coinstoeuro()
	{
		if(document.forms[0].coins.value<<? echo $eurcoins*10+$bonus10; ?>) {
			document.forms[0].euro.value=document.forms[0].coins.value/<? echo $eurcoins; ?> + "€";
		}
		else if(document.forms[0].coins.value>=<? echo $eurcoins*10+$bonus10; ?> && document.forms[0].coins.value<<? echo $eurcoins*25+$bonus25; ?>) {
			document.forms[0].euro.value=(document.forms[0].coins.value-<? echo $bonus10.')/'.$eurcoins;?> + "€";
		}
		else if(document.forms[0].coins.value>=<? echo $eurcoins*25+$bonus25; ?> && document.forms[0].euro.value<<? echo $eurcoins*50+$bonus50; ?>) {
			document.forms[0].euro.value=(document.forms[0].coins.value-<? echo $bonus25.')/'.$eurcoins;?> + "€";
		}
		else if(document.forms[0].coins.value>=<? echo $eurcoins*50+$bonus50; ?> && document.forms[0].euro.value<<? echo $eurcoins*100+$bonus100; ?>) {
			document.forms[0].euro.value=(document.forms[0].coins.value-<? echo $bonus50.')+'.$eurcoins;?> + "€";
		}
		else if(document.forms[0].coins.value>=<? echo $eurcoins*100+$bonus100; ?>) {
			document.forms[0].euro.value=(document.forms[0].coins.value-<? echo $bonus100.')+'.$eurcoins;?> + "€";
		}
		
	}
	</script> 
		<input type="text" onKeyUp="eurotocoins()" name="euro" onfocus="if(this.value=='0€') this.value='0€'" onblur="if(this.value=='')this.value='0€'" value="0€"><br><br>
		<input type="text" onKeyUp="coinstoeuro()" name="coins" onfocus="if(this.value=='0 Coins') this.value='0 Coins'" onblur="if(this.value=='')this.value='0 Coins'" value="0 Coins"><br> </form>
	<?php }
}
else {
	echo '<meta http-equiv="refresh" content="0; URL=usercp.html">';
}
?>