<?php
#
# HLstatsX - Real-time player and clan rankings and statistics for Half-Life 2
# http://www.hlstatsx.com/
# Copyright (C) 2005-2007 Tobias Oetzel (Tobi@hlstatsx.com)
#
# HlstatsX is an enhanced version of HLstats made by Simon Garner
# HLstats - Real-time player and clan rankings and statistics for Half-Life
# http://sourceforge.net/projects/hlstats/
# Copyright (C) 2001  Simon Garner
#             
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
	 
	if ($auth->userdata["acclevel"] < 80) die ("Access denied!");
?>

&nbsp;&nbsp;&nbsp;&nbsp;<img src="<?php echo $g_options["imgdir"]; ?>/downarrow.gif" width=9 height=6 border=0 align="middle" alt="downarrow.gif"><b>&nbsp;<?php echo $task->title; ?></b><p>

<?php

    if (isset($_POST['confirm']))
    {
      echo "<ul>\n";
      echo "<li>Requesting countrylist from <b>master.hlstatsx.com:27501</b>...";
      $data = array();
      $db->query("SELECT playerId, lastAddress FROM hlstats_Players WHERE lastAddress<>'' AND (country='' OR city='') LIMIT 0, 5000");
      while ($rowdata = $db->fetch_array())
        $data[] = array($rowdata['playerId'], $rowdata['lastAddress']);
        
      $host = gethostbyname("master.hlstatsx.com");
      $port = 27501;
      $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

      $i = 0;
      $p_data = "";
      while ($i < count($data)) {
        $p_data .= $data[$i][0].chr(0xFE).$data[$i][1].chr(255);
        if ( (($i > 0) && ($i % 10 == 0)) || ($i == count($data)-1)) {
          $packet = chr(255).chr(255)."A".chr(255)."1.00".chr(255).$p_data;
          $bytes_sent = socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);
          $p_data = "";
        }  
        $i++;
      }    
      $recv_bytes   = 0;
      $buffer       = "";
      $timeout      = 5;
      $answer       = "";
      $read         = array($socket);
      $update_count = 0;
      while (socket_select($read, $write = NULL, $except = NULL, &$timeout) > 0) {
        $recv_bytes += socket_recvfrom($socket, &$buffer, 2000, 0, &$host, &$port);
        if (($buffer[0] == chr(255)) && ($buffer[1] == chr(255)) && ($buffer[2] == "A") && ($buffer[3] == chr(255)) && 
            ($buffer[4] == "1") && ($buffer[5] == ".") && ($buffer[6] == "0") && ($buffer[7] == "0") && ($buffer[8] == chr(255))) { 
          $answer     .= substr($buffer, 9, strlen($buffer));
        }  
        $buffer     = "";
      }
      $player_data = explode(chr(255), $answer);
      foreach ($player_data as $entry) {
        $entry_data = explode(chr(254), $entry);
        if (count($entry_data) == 7) {
          $query = "UPDATE hlstats_Players SET city='".mysql_real_escape_string(htmlentities($entry_data[1], ENT_COMPAT, "UTF-8"))."', country='".mysql_real_escape_string(htmlentities($entry_data[5], ENT_COMPAT, "UTF-8"))."', 
                    flag='".htmlentities($entry_data[6], ENT_COMPAT, "UTF-8")."', state='".mysql_real_escape_string(htmlentities($entry_data[2], ENT_COMPAT, "UTF-8"))."', lat='$entry_data[3]', lng='$entry_data[4]' 
                    WHERE playerId='$entry_data[0]'";
          $db->query($query);
          $update_count++;
        }  
      }
      socket_close($socket);
      echo "Updated <b>".$update_count."</b> players...";
      echo "<b>OK</b></li>";
      echo "</ul>\n";
    } else {
        
?>        

<form method="POST">
<table width="60%" align="center" border=0 cellspacing=0 cellpadding=0>

<tr>
    <td bgcolor="<?php echo $g_options["table_border"]; ?>">
        <table width="100%" border=0 cellspacing=1 cellpadding=10>
        
        <tr>
            <td bgcolor="<?php echo $g_options["table_bgcolor1"]; ?>"><?php echo $g_options["font_normal"]; ?>

Get the country data for all the players which so far are not assigned by the HLstatsX masterserver.<p>

<input type="hidden" name="confirm" value="1">
<center><input type="submit" value="  Synchronize Countries  "></center>
<?php echo $g_options["fontend_normal"]; ?></td>
        </tr>
        
        </table></td>
</tr>

</table>
</form>

<?php
    }
?>    
    