<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css"></head><body>
<center>
<?php
$ckfile = tempnam("/tmp", "CURLCOOKIE"); //preparing our cookie file

function getItemData($itemid){

try {
    	$dbhandle = new PDO("sqlite:wr-s.sqlite");
    	$sql = "SELECT * FROM Items WHERE Id = $itemid";
	$result = $dbhandle->query($sql);
    } catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

return $result->fetchall(PDO::FETCH_ASSOC);
}

function nxaccess($usr_n, $usr_p, $ic, $pr, $gic, $priceno){
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';
$url = "https://passport.nexoneu.com/Login.aspx?nexoneuTheme=NexonEU";
$username = $usr_n;
$password = $usr_p;


$f = fopen('log.txt', 'w'); // file to write request header for debug purpose

/**
    Get __VIEWSTATE & __EVENTVALIDATION
 */
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$html = curl_exec($ch);
preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);
$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);


// Collecting all POST fields
$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['txtLogin0'] = $username;
$postfields['txtPassword0'] = $password;
$postfields['btnLogin.x'] = '0';
$postfields['btnLogin.y'] = '0';

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$page = curl_exec($ch);

preg_match('/mainHeader_lblCashBalance">(.+)<\/span>/i', $page, $nx_amount); //get nexoncashbalance
$nexoncash = trim($nx_amount[1]);

if ($ic != "" && $nexoncash >= $pr){
$rand = mt_rand(100000,999999);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, 'http://en.warrock.nexoneu.com/Shop/Detail?id='.$ic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$weap_detail = curl_exec($ch);

curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, "http://en.warrock.nexoneu.com/shop/PurchaseItem?".$rand."&productNo=".$gic."&quantity=1&price=".$pr."&shopItemCode=".$ic."&PriceNo=".$priceno);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$purchase_detail = curl_exec($ch);
curl_close( $ch );

$weap_detail = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $weap_detail);
$purchase_detail = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $purchase_detail);
$retdata = array($nexoncash, $weap_detail, $purchase_detail);
}else{
$retdata = array($nexoncash);
curl_close( $ch );
}

return $retdata;
}

//check if itemdata has been transmitted
$item = htmlEntities($_POST["item"]);
if($item != ""){
	$row = getItemData($item);
	$ic = $row[0][ic];
	$gic = $row[0][gic];
	$pric = $row[0][price];
	$no = $row[0][no];
}else{
header('Location: index.php');
exit;
}

$rand = mt_rand(100000,999999); //stop caching
?>


<div class="box ap">
<b>To complete your purchase you need to login into your NXE Account.</b>
<?php

$userid = htmlEntities($_POST["id"]);
$userpass = htmlEntities($_POST["pw"]);

if ($userid != "" && $userpass != ""){
	$retdata = nxaccess($userid, $userpass, '', '', '', ''); //we may need to try twice to login successfully
		if($retdata[0] == ''){
			//try again
			$retdata = nxaccess($userid, $userpass, '', '', '', '');
			if($retdata[0] == ''){?>
				<script type="text/javascript" language="Javascript">  
					alert("Please check your login.")
				</script><?
			}
		}
		if ($retdata[1] < $pric && $retdata[0] != ''){?>
			<script type="text/javascript" language="Javascript">  
			alert("You don't own enough NX Cash.")
			window.location = "index.php"
			</script><?
		}else{ 
			$retdata = nxaccess($userid, $userpass, $ic, $pric, $gic, $no);
			if (strpos($retdata[2],'"Result":true') !== false) { //checking for success of purchase
			?>
   				<script type="text/javascript" language="Javascript">  
					alert("Purchase successfull and completed.")
					window.location = "index.php"
				</script><?
			}else{ //something strange happened, purchase failed
			?>
				<script type="text/javascript" language="Javascript">  
					alert("A error occurred.")
					window.location = "index.php"
				</script><?
			}
		}
}else{?>
		<?

}
?>
<form method="post" action="buy.php" style="margin: 0;">
<?echo "<input type='hidden' name='item' value='$item'>";?>
<table width="326" border="0" cellspacing="0" cellpadding="0" style="padding-top: 24px;">
<tr>
	<td width="107" class="login_txt">
		<b>User ID</b>
	</td>
	<td width="142" align="left">
		<input type="text" name ="id" id="id" autocomplete="off" tabindex="1" />
	</td>
	<td width="69" rowspan="3" align="right">
		<input type="image" id="login" tabindex="3" src="img/bl.jpg" width="90" height="60" align="absmiddle"/>
	</td>
</tr>
<tr>
       <td height="6" colspan="3"></td>
</tr>
<tr>
       <td class="login_txt">
       	<b>Password</b>
       </td>
       <td align="left">
       	<input type="password" name ="pw" id="pw" autocomplete="off" tabindex="2" />
       </td>
</tr>
</table>
</form>
</div></center>
</body></html>