<?php
/*======================================================================*\
|| #################################################################### ||
|| # EoT-Clan BF3 Leaderboard                                         # ||
|| # ---------------------------------------------------------------- # ||
|| # Dieses Copyright darf nicht entfernt werden                      # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # -----------------BF3 Leaderboard is a free Software--------------# ||
|| #                     http://eot-clan.net                          # ||
|| #################################################################### ||
\*======================================================================*/
 require_once '../inc/config.php';
 require_once ('../language/'.$row['language'].'.php');
	$bError = false;
	$inaktiv = $row['inaktivTime']*86400;
	
	if ($inaktiv == 0){
		$inaktiv = 86400;
	}

	    $qTime = time();
      $sSql = "SELECT a.soldiername,b.autoupdateEnabled,b.ident,b.secretkey,b.platform,a.date_update_bf3s,a.playerupdate,a.invisible 
                FROM ".DBPrefix."generalStats a
              JOIN ".DBPrefix."admin b ON pid 
               WHERE a.date_update_bf3s+$inaktiv > $qTime 
                AND invisible = ''
              ORDER BY a.playerupdate ASC LIMIT 5";
              
              
      $result = mysql_query($sSql);
      $sSoldiers = array();
  while($ApiData = mysql_fetch_assoc($result)){
          $apiSoldiers[] = $ApiData['soldiername'];
           $Enabled = $ApiData['autoupdateEnabled'];
           $BF3APIIdent = $ApiData['ident'];
           $BF3APIKey = $ApiData['secretkey'];
           $platform = $ApiData['platform'];
     #print_r($ApiData['soldiername']);
         $data=updatePlayer($platform,rawurlencode($ApiData['soldiername']));
     // Debuging
     #echo '<pre>';
     #print_r($data);
     #echo '</pre>';
     
       $pUpdateTime = time();
       $sSql ="	UPDATE ".DBPrefix."generalStats SET playerupdate ='{$pUpdateTime}' WHERE soldiername = '{$ApiData['soldiername']}'";
        mysql_query($sSql) OR die(mysql_error());
  } 
   
        
       function updatePlayer($platform,$name) {
	        global $BF3APIIdent;
	        $data=array();
	        $data['ident']=$BF3APIIdent;
	        $data['time']=time();
	        $data['player']=$name;
	       return getAPIDataSigned('playerupdate',$platform,$data);
        }


       function getAPIDataSigned($type,$platform,$data) {
	       global $BF3APIKey;
	       // Convert data to JSON
	       $urlbase64=array('+'=>'-','/'=>'_','='=>'');
	       $data=strtr(base64_encode(json_encode($data)),$urlbase64);

	       $postdata=array();
	       $postdata['data']=$data;
	       $postdata['sig']=strtr(base64_encode(hash_hmac('sha256',$data,$BF3APIKey,true)),$urlbase64);

		       // Run POST Request via CURL
		       $c=curl_init('http://api.bf3stats.com/'.$platform.'/'.$type.'/');
		       curl_setopt($c,CURLOPT_HEADER,false);
		       curl_setopt($c,CURLOPT_POST,true);
		       curl_setopt($c,CURLOPT_USERAGENT,'BF3StatsAPI/0.1');
		       curl_setopt($c,CURLOPT_HTTPHEADER,array('Expect:'));
		       curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
		       curl_setopt($c,CURLOPT_POSTFIELDS,$postdata);
		       $data=curl_exec($c);
		       $statuscode=curl_getinfo($c,CURLINFO_HTTP_CODE);
		       curl_close($c);
	
		       if($statuscode==200) {
			       // Decode JSON Data
			       $data=json_decode($data,true);
			       return $data;

		       } else {
			       echo "BF3 Stats API error status: ".$statuscode;
			       return false;
			       exit;
		      }
	     }        
        	sleep(20);
		define('INCLUDE_ME', true);
      include_once 'updateAlldata.php';
      