<?php
/*======================================================================*\
|| #################################################################### ||
|| # EoT-Clan BF3 Leaderboard                                         # ||
|| # ---------------------------------------------------------------- # ||
|| # Dieses Copyright darf nicht entfernet werden                     # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # -----------------BF3 Leaderboard is a free Software--------------# ||
|| #                   http://eot-clan.net                            # ||
|| #################################################################### ||
\*======================================================================*/
include '../inc/config.php';
require_once ('../language/'.$row['language'].'.php');
require_once ('../inc/BattlelogApi/battlelog.api.php');

$_GET = security::escape($_GET);
$_POST = security::escape($_POST);
session_start();

if(isset($_SESSION["username"]))
   {
   	
   	
$tTime = time();
   if(isset($_POST['addplayer'])) {
      $addplayer2 = $_POST['addplayer'];
   }else{
     $addplayer2 = '0';
 }
 
 
$addplayer = rawurlencode($addplayer2);
$url = 'http://api.bf3stats.com/'.$row['platform'].'/player/';
$postdata = 'player='.$addplayer.'?opt=clear,scores,urls';

$ch = curl_init($url.'?'.$postdata);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($ch);
curl_close($ch);

$data = json_decode($data,true);

#print_r($data);

if(isset($data['error']['code'])){
	$error = 1;
}else{
  $error = 2;
}

if(isset($data['name'])){
	$playerN = $data['name'];
}else{
	$playerN = "";
}



if($error == 1 or $playerN == ""){
echo '
<html>
	<head>
		<title>'.LANG_ADMIN_TITLE.'</title>
		'.$styleSettings.'
	</head>
 <td class="defaultUpdate" style="width:500px; margin-left: -250px;margin-top: -100px;position: absolute;left: 50%;top: 50%;">'.LANG_ADMIN_INSERT_ERROR.'
</table></div>
</html>
';
}else{
	
	
//Allgemein
$g_name 		= $data['name'];
$g_points 		= $data['stats']['scores']['score'];
	    $bID = 	parse_url($data['battlelog']);
			$bID = explode('/',$bID['path']);
			$bID = $bID['5'];
	

   if(isset($_POST['vip'])) {
      $vip = 1; 
   }else{
     $vip = 0;
 }
  
  $platform = $_POST['Pplatform'];
  $persona = $data['name'];
  if ($row['platform'] != 'pc'){
    $persona = $_POST['persona'];
  }
  
  
   $battlelog = new BattlelogApiCurl;
   $vet = "";
   
   if ($platform == 1){
	   $soldier = $battlelog->SoldierInfo($g_name);      
      if(is_array($soldier['0']['veteranStatus']['games'])){
        $vet = implode(',',$soldier['0']['veteranStatus']['games']);
      }
  }else{
  	$soldier = $battlelog->SoldierInfo($persona);      
      if(is_array($soldier['0']['veteranStatus']['games'])){
        $vet = implode(',',$soldier['0']['veteranStatus']['games']);
      }
   }


mysql_query ("INSERT INTO ".DBPrefix."generalStats (soldiername,points,last_update,vip_member,battlelogID,veteran,Pplatform,persona) VALUES ('$g_name','$g_points','$tTime','$vip','{$bID}','{$vet}','{$platform}','{$persona}')")

or die ("Ungültige Abfrage: " . mysql_error());

echo '
<html>
	<head>
		<title>'.LANG_ADMIN_TITLE.'</title>
		'.$styleSettings.'
  </head>
  <table class="defaultUpdate" style="width:470px; margin-left: -250px;margin-top: -100px;position: absolute;left: 50%;top: 50%;" border="0" cellspacing="0" cellpadding="4"><td>'.LANG_ADMIN_INSERT_CORRECT.'
	</td></table>
</html>'; 
}


}else{
	?>
	<title>BF3 Admin-Session Error</title>
	<?php echo $styleSettings; ?>
<title>Login error!!</title>
         <table class="defaultUpdate" style="width:470px; margin-left: -250px;margin-top: -100px;position: absolute;left: 50%;top: 50%;" border="0" cellspacing="0" cellpadding="4"><td>
<?php echo LANG_LOGIN_LOGIN_ERROR; ?>
</td></table>
     <?php
 }