Guest User

lgsl_class.php //itocean Kizuna

a guest
Jul 22nd, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 38.86 KB | None | 0 0
  1. <?php
  2.  
  3.  /*----------------------------------------------------------------------------------------------------------\
  4.  |                                                                                                            |
  5.  |                      [ LIVE GAME SERVER LIST ] [ © RICHARD PERRY FROM GREYCUBE.COM ]                       |
  6.  |                                                                                                            |
  7.  |    Released under the terms and conditions of the GNU General Public License Version 3 (http://gnu.org)    |
  8.  |                                                                                                            |
  9.  \-----------------------------------------------------------------------------------------------------------*/
  10. //
  11. //------------------------------------------------------------------------------------------------------------+
  12. //
  13. //------------------------------------------------------------------------------------------------------------+
  14.  
  15.   if (!function_exists('lgsl_url_path')) { // START OF DOUBLE LOAD PROTECTION
  16.  
  17. //------------------------------------------------------------------------------------------------------------+
  18. //
  19. //------------------------------------------------------------------------------------------------------------+
  20.  
  21.   function lgsl_bg($rotation_overide = "no")
  22.   {
  23.     global $lgsl_config;
  24.     global $lgsl_bg_rotate;
  25.  
  26.     if ($rotation_overide !== "no")
  27.     {
  28.       $lgsl_bg_rotate = $rotation_overide ? TRUE : FALSE;
  29.     }
  30.     else
  31.     {
  32.       $lgsl_bg_rotate = $lgsl_bg_rotate ? FALSE : TRUE;
  33.     }
  34.  
  35.     $background = $lgsl_bg_rotate ? $lgsl_config['background'][1] : $lgsl_config['background'][2];
  36.  
  37.     return $background;
  38.   }
  39.  
  40. //------------------------------------------------------------------------------------------------------------+
  41. //
  42.   function lgsl_link($s = "")
  43.   {
  44.     global $lgsl_config, $lgsl_url_path;
  45.  
  46.     $index = $lgsl_config['direct_index'] ? "index.php" : "";
  47.  
  48.     switch($lgsl_config['cms'])
  49.     {
  50.       case "e107":
  51.         $link = $s ? e_PLUGIN."lgsl/{$index}?s={$s}" : e_PLUGIN."lgsl/{$index}";
  52.       break;
  53.  
  54.       case "joomla":
  55.         $link = $s ? JRoute::_("index.php?option=com_lgsl&s={$s}") : JRoute::_("index.php?option=com_lgsl");
  56.       break;
  57.  
  58.       case "drupal":
  59.         $link = $s ? url("LGSL/{$s}") : url("LGSL");
  60.       break;
  61.  
  62.       case "phpnuke":
  63.         $link = $s ? "modules.php?name=LGSL&s={$s}" : "modules.php?name=LGSL";
  64.       break;
  65.  
  66.       default: // "sa"
  67.         $link = $s ? $lgsl_url_path."../{$index}?s={$s}" : $lgsl_url_path."../{$index}";
  68.       break;
  69.     }
  70.  
  71.     return $link;
  72.   }
  73.  
  74. //------------------------------------------------------------------------------------------------------------+
  75. //
  76.   function lgsl_database()
  77.   {
  78.     global $lgsl_database, $lgsl_config, $lgsl_file_path;
  79.  
  80.     if (!isset($lgsl_config['db']['prefix']))
  81.     {
  82.       $lgsl_config['db']['prefix'] = "";
  83.     }
  84.  
  85.     if (!$lgsl_config['db']['pass'])
  86.     {
  87.       switch($lgsl_config['cms'])
  88.       {
  89.         case "e107":
  90.           @include "{$lgsl_file_path}../../../e107_config.php";
  91.           $lgsl_config['db']['server'] = $mySQLserver;
  92.           $lgsl_config['db']['user']   = $mySQLuser;
  93.           $lgsl_config['db']['pass']   = $mySQLpassword;
  94.           $lgsl_config['db']['db']     = $mySQLdefaultdb;
  95.           $lgsl_config['db']['prefix'] = $mySQLprefix;
  96.         break;
  97.  
  98.         case "joomla":
  99.           @include_once "{$lgsl_file_path}../../../configuration.php";
  100.           $joomla_config = new JConfig();
  101.           $lgsl_config['db']['server'] = $joomla_config->host;
  102.           $lgsl_config['db']['user']   = $joomla_config->user;
  103.           $lgsl_config['db']['pass']   = $joomla_config->password;
  104.           $lgsl_config['db']['db']     = $joomla_config->db;
  105.           $lgsl_config['db']['prefix'] = $joomla_config->dbprefix;
  106.         break;
  107.  
  108.         case "drupal":
  109.           global $db_url, $db_prefix;
  110.           if (empty($db_url)) { @include "{$lgsl_file_path}../../../sites/default/settings.php"; }
  111.           $drupal_config = is_array($db_url) ? parse_url($db_url['default']) : parse_url($db_url);
  112.           $lgsl_config['db']['server'] = $drupal_config['host'];
  113.           $lgsl_config['db']['user']   = $drupal_config['user'];
  114.           $lgsl_config['db']['pass']   = isset($drupal_config['pass']) ? $drupal_config['pass'] : "";
  115.           $lgsl_config['db']['db']     = substr($drupal_config['path'], 1);
  116.           $lgsl_config['db']['prefix'] = isset($db_prefix['default']) ? $db_prefix['default'] : "";
  117.         break;
  118.  
  119.         case "phpnuke":
  120.           @include "{$lgsl_file_path}../../../config.php";
  121.           @include "{$lgsl_file_path}../../../conf.inc.php";
  122.           @include "{$lgsl_file_path}../../../includes/config.php";
  123.           $lgsl_config['db']['server'] = $dbhost;
  124.           $lgsl_config['db']['user']   = $dbuname;
  125.           $lgsl_config['db']['pass']   = $dbpass;
  126.           $lgsl_config['db']['db']     = $dbname;
  127.           $lgsl_config['db']['prefix'] = $prefix."_";
  128.         break;
  129.       }
  130.     }
  131.  
  132.     $lgsl_database  = mysql_connect($lgsl_config['db']['server'], $lgsl_config['db']['user'], $lgsl_config['db']['pass']) or die(mysql_error());
  133.     $lgsl_select_db = mysql_select_db($lgsl_config['db']['db'], $lgsl_database) or die(mysql_error());
  134.   }
  135.  
  136. //------------------------------------------------------------------------------------------------------------+
  137. //
  138.   function lgsl_query_cached($type, $ip, $c_port, $q_port, $s_port, $request, $id = NULL)
  139.   {
  140.     global $lgsl_config;
  141.  
  142.     lgsl_database();
  143.  
  144.     // LOOKUP SERVER
  145.  
  146.     if ($id != NULL)
  147.     {
  148.       $id           = intval($id);
  149.       $mysql_query  = "SELECT * FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE `id`='{$id}' LIMIT 1";
  150.       $mysql_result = mysql_query($mysql_query) or die(mysql_error());
  151.       $mysql_row    = mysql_fetch_array($mysql_result, MYSQL_ASSOC);
  152.       if (!$mysql_row) { return FALSE; }
  153.       list($type, $ip, $c_port, $q_port, $s_port) = array($mysql_row['type'], $mysql_row['ip'], $mysql_row['c_port'], $mysql_row['q_port'], $mysql_row['s_port']);
  154.     }
  155.     else
  156.     {
  157.       list($type, $ip, $c_port, $q_port, $s_port) = array(mysql_real_escape_string($type), mysql_real_escape_string($ip), intval($c_port), intval($q_port), intval($s_port));
  158.  
  159.       if (!$type || !$ip || !$c_port || !$q_port) { exit("LGSL PROBLEM: INVALID SERVER '{$type} : {$ip} : {$c_port} : {$q_port} : {$s_port}'"); }
  160.       $mysql_query  = "SELECT * FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE `type`='{$type}' AND `ip`='{$ip}' AND `q_port`='{$q_port}' LIMIT 1";
  161.       $mysql_result = mysql_query($mysql_query) or die(mysql_error());
  162.       $mysql_row    = mysql_fetch_array($mysql_result, MYSQL_ASSOC);
  163.  
  164.       if (!$mysql_row)
  165.       {
  166.         if (strpos($request, "a") === FALSE) { exit("LGSL PROBLEM: SERVER NOT IN DATABASE '{$type} : {$ip} : {$c_port} : {$q_port} : {$s_port}'"); }
  167.         $mysql_query  = "INSERT INTO `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` (`type`,`ip`,`c_port`,`q_port`,`s_port`,`cache`,`cache_time`) VALUES ('{$type}','{$ip}','{$c_port}','{$q_port}','{$s_port}','','')";
  168.         $mysql_result = mysql_query($mysql_query) or die(mysql_error());
  169.         $mysql_row    = array("id"=>mysql_insert_id(), "zone"=>"0", "comment"=>"");
  170.       }
  171.     }
  172.  
  173.     // UNPACK CACHE AND CACHE TIMES
  174.  
  175.     $cache      = empty($mysql_row['cache'])      ? array()      : unserialize(base64_decode($mysql_row['cache']));
  176.     $cache_time = empty($mysql_row['cache_time']) ? array(0,0,0) : explode("_", $mysql_row['cache_time']);
  177.  
  178.     // SET THE SERVER AS OFFLINE AND PENDING WHEN THERE IS NO CACHE
  179.  
  180.     if (empty($cache['b']) || !is_array($cache))
  181.     {
  182.       $cache      = array();
  183.       $cache['b'] = array();
  184.       $cache['b']['status']  = 0;
  185.       $cache['b']['pending'] = 1;
  186.     }
  187.  
  188.     // CONVERT HOSTNAME TO IP WHEN NEEDED
  189.  
  190.     if ($lgsl_config['host_to_ip'])
  191.     {
  192.       $ip = gethostbyname($ip);
  193.     }
  194.  
  195.     // UPDATE CACHE WITH FIXED VALUES
  196.  
  197.     $cache['b']['type']    = $type;
  198.     $cache['b']['ip']      = $ip;
  199.     $cache['b']['c_port']  = $c_port;
  200.     $cache['b']['q_port']  = $q_port;
  201.     $cache['b']['s_port']  = $s_port;
  202.     $cache['o']['request'] = $request;
  203.     $cache['o']['id']      = $mysql_row['id'];
  204.     $cache['o']['zone']    = $mysql_row['zone'];
  205.     $cache['o']['comment'] = $mysql_row['comment'];
  206.  
  207.     // UPDATE CACHE WITH LOCATION
  208.  
  209.     if (empty($cache['o']['location']))
  210.     {
  211.       $cache['o']['location'] = $lgsl_config['locations'] ? lgsl_query_location($ip) : "";
  212.     }
  213.  
  214.     // UPDATE CACHE WITH DEFAULT OFFLINE VALUES
  215.  
  216.     if (!isset($cache['s']))
  217.     {
  218.       $cache['s']               = array();
  219.       $cache['s']['game']       = $type;
  220.       $cache['s']['name']       = $lgsl_config['text']['nnm'];
  221.       $cache['s']['map']        = $lgsl_config['text']['nmp'];
  222.       $cache['s']['players']    = 0;
  223.       $cache['s']['playersmax'] = 0;
  224.       $cache['s']['password']   = 0;
  225.     }
  226.  
  227.     if (!isset($cache['e'])) { $cache['e'] = array(); }
  228.     if (!isset($cache['p'])) { $cache['p'] = array(); }
  229.  
  230.     // CHECK AND GET THE NEEDED DATA
  231.  
  232.     $needed = "";
  233.  
  234.     if (strpos($request, "c") === FALSE) // CACHE ONLY REQUEST
  235.     {
  236.       if (strpos($request, "s") !== FALSE && time() > ($cache_time[0]+$lgsl_config['cache_time'])) { $needed .= "s"; }
  237.       if (strpos($request, "e") !== FALSE && time() > ($cache_time[1]+$lgsl_config['cache_time'])) { $needed .= "e"; }
  238.       if (strpos($request, "p") !== FALSE && time() > ($cache_time[2]+$lgsl_config['cache_time'])) { $needed .= "p"; }
  239.     }
  240.  
  241.     if ($needed)
  242.     {
  243.       // UPDATE CACHE TIMES BEFORE QUERY - PREVENTS OTHER INSTANCES FROM QUERY FLOODING THE SAME SERVER
  244.  
  245.       $packed_times = time() + $lgsl_config['cache_time'] + 10;
  246.       $packed_times = "{$packed_times}_{$packed_times}_{$packed_times}";
  247.       $mysql_query  = "UPDATE `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` SET `cache_time`='{$packed_times}' WHERE `id`='{$mysql_row['id']}' LIMIT 1";
  248.       $mysql_result = mysql_query($mysql_query) or die(mysql_error());
  249.  
  250.       // GET WHAT IS NEEDED
  251.  
  252.       $live = lgsl_query_live($type, $ip, $c_port, $q_port, $s_port, $needed);
  253.  
  254.       if (!$live['b']['status'] && $lgsl_config['retry_offline'] && !$lgsl_config['feed']['method'])
  255.       {
  256.         $live = lgsl_query_live($type, $ip, $c_port, $q_port, $s_port, $needed);
  257.       }
  258.  
  259.       // CHECK AND CONVERT TO UTF-8 WHERE NEEDED
  260.  
  261.       $live = lgsl_charset_convert($live, lgsl_charset_detect($live));
  262.  
  263.       // IF SERVER IS OFFLINE PRESERVE SOME OF THE CACHE AND CLEAR THE REST
  264.  
  265.       if (!$live['b']['status'])
  266.       {
  267.         $live['s']['game']       = $cache['s']['game'];
  268.         $live['s']['name']       = $cache['s']['name'];
  269.         $live['s']['map']        = $cache['s']['map'];
  270.         $live['s']['password']   = $cache['s']['password'];
  271.         $live['s']['players']    = 0;
  272.         $live['s']['playersmax'] = $cache['s']['playersmax'];
  273.         $live['e']               = array();
  274.         $live['p']               = array();
  275.       }
  276.  
  277.       // MERGE LIVE INTO CACHE
  278.  
  279.       if (isset($live['b'])) { $cache['b'] = $live['b']; $cache['b']['pending'] = 0; }
  280.       if (isset($live['s'])) { $cache['s'] = $live['s']; $cache_time[0] = time(); }
  281.       if (isset($live['e'])) { $cache['e'] = $live['e']; $cache_time[1] = time(); }
  282.       if (isset($live['p'])) { $cache['p'] = $live['p']; $cache_time[2] = time(); }
  283.  
  284.       // UPDATE CACHE
  285.  
  286.       $packed_cache = mysql_real_escape_string(base64_encode(serialize($cache)));
  287.       $packed_times = mysql_real_escape_string(implode("_", $cache_time));
  288.       $mysql_query  = "UPDATE `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` SET `status`='{$cache['b']['status']}',`cache`='{$packed_cache}',`cache_time`='{$packed_times}' WHERE `id`='{$mysql_row['id']}' LIMIT 1";
  289.       $mysql_result = mysql_query($mysql_query) or die(mysql_error());
  290.     }
  291.  
  292.     // RETURN ONLY THE REQUESTED
  293.  
  294.     if (strpos($request, "s") === FALSE) { unset($cache['s']); }
  295.     if (strpos($request, "e") === FALSE) { unset($cache['e']); }
  296.     if (strpos($request, "p") === FALSE) { unset($cache['p']); }
  297.  
  298.     return $cache;
  299.   }
  300. //
  301. //------------------------------------------------------------------------------------------------------------+
  302. //EXAMPLE USAGE: lgsl_query_group( array("request"=>"sep", "hide_offline"=>0, "random"=>0, "type"=>"source", "game"=>"cstrike") )
  303.  
  304.   function lgsl_query_group($options = array())
  305.   {
  306.     if (!is_array($options)) { exit("LGSL PROBLEM: lgsl_query_group OPTIONS MUST BE ARRAY"); }
  307.  
  308.     global $lgsl_config;
  309.  
  310.     lgsl_database();
  311.  
  312.     $request      = isset($options['request'])      ? $options['request']              : "s";
  313.     $zone         = isset($options['zone'])         ? intval($options['zone'])         : 0;
  314.     $hide_offline = isset($options['hide_offline']) ? intval($options['hide_offline']) : intval($lgsl_config['hide_offline'][$zone]);
  315.     $random       = isset($options['random'])       ? intval($options['random'])       : intval($lgsl_config['random'][$zone]);
  316.     $type         = empty($options['type'])         ? ""                               : preg_replace("/[^a-z0-9_]/", "_", strtolower($options['type']));
  317.     $game         = empty($options['game'])         ? ""                               : preg_replace("/[^a-z0-9_]/", "_", strtolower($options['game']));
  318.     $mysql_order  = empty($random)                  ? "`cache_time` ASC"               : "rand()";
  319.     $server_limit = empty($random)                  ? 0                                : $random;
  320.  
  321.                        $mysql_where   = array("`disabled`=0");
  322.     if ($zone != 0)  { $mysql_where[] = "FIND_IN_SET('{$zone}',`zone`)"; }
  323.     if ($type != "") { $mysql_where[] = "`type`='{$type}'"; }
  324.  
  325.     $mysql_query  = "SELECT `id` FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE ".implode(" AND ", $mysql_where)." ORDER BY {$mysql_order}";
  326.     $mysql_result = mysql_query($mysql_query) or die(mysql_error());
  327.     $server_list  = array();
  328.  
  329.     while ($mysql_row = mysql_fetch_array($mysql_result, MYSQL_ASSOC))
  330.     {
  331.       if (strpos($request, "c") === FALSE && lgsl_timer("check")) { $request .= "c"; }
  332.  
  333.       $server = lgsl_query_cached("", "", "", "", "", $request, $mysql_row['id']);
  334.  
  335.       if ($hide_offline && empty($server['b']['status'])) { continue; }
  336.       if ($game && $game != preg_replace("/[^a-z0-9_]/", "_", strtolower($server['s']['game']))) { continue; }
  337.  
  338.       $server_list[] = $server;
  339.  
  340.       if ($server_limit && count($server_list) >= $server_limit) { break; }
  341.     }
  342.  
  343.     return $server_list;
  344.   }
  345. //
  346. //------------------------------------------------------------------------------------------------------------+
  347.  
  348.   function lgsl_group_totals($server_list = FALSE)
  349.   {
  350.     if (!is_array($server_list)) { $server_list = lgsl_query_group( array( "request"=>"sc" ) ); }
  351.  
  352.     $total = array("players"=>0, "playersmax"=>0, "servers"=>0, "servers_online"=>0, "servers_offline"=>0);
  353.  
  354.     foreach ($server_list as $server)
  355.     {
  356.       $total['players']    += $server['s']['players'];
  357.       $total['playersmax'] += $server['s']['playersmax'];
  358.  
  359.                                     $total['servers']         ++;
  360.       if ($server['b']['status']) { $total['servers_online']  ++; }
  361.       else                        { $total['servers_offline'] ++; }
  362.     }
  363.  
  364.     return $total;
  365.   }
  366. //
  367. //------------------------------------------------------------------------------------------------------------+
  368.  
  369.   function lgsl_query_cached_all($request) // LEGACY - DO NOT USE
  370.   {
  371.     return lgsl_query_group( array( "request"=>$request ) );
  372.   }
  373. //
  374. //------------------------------------------------------------------------------------------------------------+
  375.  
  376.   function lgsl_query_cached_zone($request, $zone) // LEGACY - DO NOT USE
  377.   {
  378.     return lgsl_query_group( array( "request"=>$request, "zone"=>$zone ) );
  379.   }
  380. //
  381. //------------------------------------------------------------------------------------------------------------+
  382.  
  383.   function lgsl_cached_totals() // LEGACY - DO NOT USE
  384.   {
  385.     return lgsl_group_totals();
  386.   }
  387. //
  388. //------------------------------------------------------------------------------------------------------------+
  389.  
  390.   function lgsl_lookup_id($id) // LEGACY - DO NOT USE
  391.   {
  392.     global $lgsl_config;
  393.  
  394.     lgsl_database();
  395.  
  396.     $id           = mysql_real_escape_string(intval($id));
  397.     $mysql_query  = "SELECT `type`,`ip`,`c_port`,`q_port`,`s_port` FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE `id`='{$id}' LIMIT 1";
  398.     $mysql_result = mysql_query($mysql_query) or die(mysql_error());
  399.     $mysql_row    = mysql_fetch_array($mysql_result, MYSQL_ASSOC);
  400.  
  401.     return $mysql_row;
  402.   }
  403. //
  404. //------------------------------------------------------------------------------------------------------------+
  405.  
  406.   function lgsl_timer($action)
  407.   {
  408.     global $lgsl_config;
  409.     global $lgsl_timer;
  410.  
  411.     if (!$lgsl_timer)
  412.     {
  413.       $microtime  = microtime();
  414.       $microtime  = explode(' ', $microtime);
  415.       $microtime  = $microtime[1] + $microtime[0];
  416.       $lgsl_timer = $microtime - 0.01;
  417.     }
  418.  
  419.     $time_limit = intval($lgsl_config['live_time']);
  420.     $time_php   = ini_get("max_execution_time");
  421.  
  422.     if ($time_limit > $time_php)
  423.     {
  424.       @set_time_limit($time_limit + 5);
  425.  
  426.       $time_php = ini_get("max_execution_time");
  427.  
  428.       if ($time_limit > $time_php)
  429.       {
  430.         $time_limit = $time_php - 5;
  431.       }
  432.     }
  433.  
  434.     if ($action == "limit")
  435.     {
  436.       return $time_limit;
  437.     }
  438.  
  439.     $microtime  = microtime();
  440.     $microtime  = explode(' ', $microtime);
  441.     $microtime  = $microtime[1] + $microtime[0];
  442.     $time_taken = $microtime - $lgsl_timer;
  443.  
  444.     if ($action == "check")
  445.     {
  446.       return ($time_taken > $time_limit) ? TRUE : FALSE;
  447.     }
  448.     else
  449.     {
  450.       return round($time_taken, 2);
  451.     }
  452.   }
  453. //
  454. //------------------------------------------------------------------------------------------------------------+
  455.  
  456.   function lgsl_server_misc($server)
  457.   {
  458.     global $lgsl_config, $lgsl_url_path;
  459.  
  460.     $misc['icon_details']       = $lgsl_url_path."other/icon_details.gif";
  461.     $misc['icon_game']          = lgsl_icon_game($server['b']['type'], $server['s']['game']);
  462.     $misc['icon_status']        = lgsl_icon_status($server['b']['status'], $server['s']['password'], $server['b']['pending']);
  463.     $misc['icon_location']      = lgsl_icon_location($server['o']['location']);
  464.     $misc['image_map']          = lgsl_image_map($server['b']['status'], $server['b']['type'], $server['s']['game'], $server['s']['map'], TRUE, substr(0, 1 ), $server['o']['id']);
  465.     $misc['image_map_password'] = lgsl_image_map_password($server['b']['status'], $server['s']['password']);
  466.     $misc['text_status']        = lgsl_text_status($server['b']['status'], $server['s']['password'], $server['b']['pending']);
  467.     $misc['text_type_game']     = lgsl_text_type_game($server['b']['type'], $server['s']['game']);
  468.     $misc['text_location']      = lgsl_text_location($server['o']['location']);
  469.     $misc['name_filtered']      = substr($server['s']['name'], 0, 20)  .((strlen($server['s']['name']) > 20) ? '...' : ''); // LEGACY
  470.     $misc['name_filtered2']      = substr($server['s']['name'], 0, 50)  .((strlen($server['s']['name']) > 50) ? '...' : ''); // LEGACY
  471.     $misc['software_link']      = lgsl_software_link($server['b']['type'], $server['b']['ip'], $server['b']['c_port'], $server['b']['q_port'], $server['b']['s_port']);
  472.     $misc['location_link']      = lgsl_location_link($server['o']['location']);
  473.  
  474.     return $misc;
  475.   }
  476. //
  477. //------------------------------------------------------------------------------------------------------------+
  478.  
  479.   function lgsl_icon_game($type, $game)
  480.   {
  481.     global $lgsl_file_path, $lgsl_url_path;
  482.  
  483.     $type = preg_replace("/[^a-z0-9_]/", "_", strtolower($type));
  484.     $game = preg_replace("/[^a-z0-9_]/", "_", strtolower($game));
  485.  
  486.     $path_list = array(
  487.     "icons/{$type}/{$game}.gif",
  488.     "icons/{$type}/{$game}.png",
  489.     "icons/{$type}/{$type}.gif",
  490.     "icons/{$type}/{$type}.png");
  491.  
  492.     foreach ($path_list as $path)
  493.     {
  494.       if (file_exists($lgsl_file_path.$path)) { return $lgsl_url_path.$path; }
  495.     }
  496.  
  497.     return "{$lgsl_url_path}other/icon_unknown.gif";
  498.   }
  499. //
  500. //------------------------------------------------------------------------------------------------------------+
  501.  
  502.   function lgsl_icon_status($status, $password, $pending = 0)
  503.   {
  504.     global $lgsl_url_path;
  505.  
  506.     if ($pending)  { return "{$lgsl_url_path}other/icon_unknown.gif"; }
  507.     if (!$status)  { return "./engine/images/off.png"; }
  508.     if ($password) { return "LOCKED"; }
  509.  
  510.     return "./engine/images/on.png";
  511.   }
  512. //
  513. //------------------------------------------------------------------------------------------------------------+
  514.  
  515.   function lgsl_icon_location($location)
  516.   {
  517.     global $lgsl_file_path, $lgsl_url_path;
  518.  
  519.     if (!$location) { return "{$lgsl_url_path}locations/OFF.png"; }
  520.  
  521.     if ($location)
  522.     {
  523.       $location = "locations/".preg_replace("/[^a-zA-Z0-9_]/", "_", $location).".png";
  524.  
  525.       if (file_exists($lgsl_file_path.$location)) { return $lgsl_url_path.$location; }
  526.     }
  527.  
  528.     return "{$lgsl_url_path}locations/XX.png";
  529.   }
  530.  
  531. //------------------------------------------------------------------------------------------------------------+
  532.  
  533.   function lgsl_image_map($status, $type, $game, $map, $check_exists = TRUE, $id = 0)
  534.   {
  535.     global $lgsl_file_path, $lgsl_url_path;
  536.  
  537.     $type = preg_replace("/[^a-z0-9_]/", "_", strtolower($type));
  538.     $game = preg_replace("/[^a-z0-9_]/", "_", strtolower($game));
  539.     $map  = preg_replace("/[^a-z0-9_]/", "_", strtolower($map));
  540.  
  541.     if ($check_exists !== TRUE) { return "{$lgsl_url_path}maps/{$type}/{$game}/{$map}.jpg"; }
  542.  
  543.     if ($status)
  544.     {
  545.       $path_list = array(
  546.       "maps/{$type}/{$game}/{$map}.jpg",
  547.       "maps/{$type}/{$game}/{$map}.gif",
  548.       "maps/{$type}/{$game}/{$map}.png",
  549.       "maps/{$type}/{$map}.jpg",
  550.       "maps/{$type}/{$map}.gif",
  551.       "maps/{$type}/{$map}.png",
  552.       "maps/{$type}/map_no_image.jpg",
  553.       "maps/{$type}/map_no_image.gif",
  554.       "maps/{$type}/map_no_image.png",
  555.       "other/map_no_image_{$id}.jpg",
  556.       "other/map_no_image_{$id}.gif",
  557.       "other/map_no_image_{$id}.png",
  558.       "other/map_no_image.jpg");
  559.     }
  560.     else
  561.     {
  562.       $path_list = array(
  563.       "maps/{$type}/map_no_response.jpg",
  564.       "maps/{$type}/map_no_response.gif",
  565.       "maps/{$type}/map_no_response.png",
  566.       "other/map_no_response_{$id}.jpg",
  567.       "other/map_no_response_{$id}.gif",
  568.       "other/map_no_response_{$id}.png",
  569.       "other/map_no_response.jpg");
  570.     }
  571.  
  572.     foreach ($path_list as $path)
  573.     {
  574.       if (file_exists($lgsl_file_path.$path)) { return "{$lgsl_url_path}{$path}"; }
  575.     }
  576.  
  577.     return "#LGSL_DEFAULT_IMAGES_MISSING#";
  578.   }
  579. //
  580. //------------------------------------------------------------------------------------------------------------+
  581.  
  582.   function lgsl_image_map_password($status, $password)
  583.   {
  584.     global $lgsl_url_path;
  585.  
  586.     if (!$password || !$status) { return "{$lgsl_url_path}other/map_overlay.gif"; }
  587.  
  588.     return "{$lgsl_url_path}other/map_overlay_password.gif";
  589.   }
  590. //
  591. //------------------------------------------------------------------------------------------------------------+
  592.  
  593.   function lgsl_text_status($status, $password, $pending = 0)
  594.   {
  595.     global $lgsl_config;
  596.  
  597.     if ($pending)  { return $lgsl_config['text']['pen']; }
  598.     if (!$status)  { return $lgsl_config['text']['nrs']; }
  599.     if ($password) { return $lgsl_config['text']['onp']; }
  600.  
  601.     return $lgsl_config['text']['onl'];
  602.   }
  603. //
  604. //------------------------------------------------------------------------------------------------------------+
  605.  
  606.   function lgsl_text_type_game($type, $game)
  607.   {
  608.     global $lgsl_config;
  609.  
  610.     return "[ {$lgsl_config['text']['typ']} {$type} ] [ {$lgsl_config['text']['gme']} {$game} ]";
  611.   }
  612. //
  613. //------------------------------------------------------------------------------------------------------------+
  614.  
  615.   function lgsl_text_location($location)
  616.   {
  617.     global $lgsl_config;
  618.  
  619.     return $location ? "{$lgsl_config['text']['loc']} {$location}" : "";
  620.   }
  621. //
  622. //------------------------------------------------------------------------------------------------------------+
  623.  
  624.   function lgsl_sort_servers($server_list)
  625.   {
  626.     global $lgsl_config;
  627.  
  628.     if (!is_array($server_list)) { return $server_list; }
  629.  
  630.     if     ($lgsl_config['sort']['servers'] == "id")      { usort($server_list, "lgsl_sort_servers_by_id");      }
  631.     elseif ($lgsl_config['sort']['servers'] == "zone")    { usort($server_list, "lgsl_sort_servers_by_zone");    }
  632.     elseif ($lgsl_config['sort']['servers'] == "type")    { usort($server_list, "lgsl_sort_servers_by_type");    }
  633.     elseif ($lgsl_config['sort']['servers'] == "status")  { usort($server_list, "lgsl_sort_servers_by_status");  }
  634.     elseif ($lgsl_config['sort']['servers'] == "players") { usort($server_list, "lgsl_sort_servers_by_players"); }
  635.  
  636.     return $server_list;
  637.   }
  638. //
  639. //------------------------------------------------------------------------------------------------------------+
  640.  
  641.   function lgsl_sort_fields($server, $fields_show, $fields_hide, $fields_other)
  642.   {
  643.     $fields_list = array();
  644.  
  645.     if (!is_array($server['p'])) { return $fields_list; }
  646.  
  647.     foreach ($server['p'] as $player)
  648.     {
  649.       foreach ($player as $field => $value)
  650.       {
  651.         if ($value === "") { continue; }
  652.         if (in_array($field, $fields_list)) { continue; }
  653.         if (in_array($field, $fields_hide)) { continue; }
  654.         $fields_list[] = $field;
  655.       }
  656.     }
  657.  
  658.     $fields_show = array_intersect($fields_show, $fields_list);
  659.  
  660.     if ($fields_other == FALSE) { return $fields_show; }
  661.  
  662.     $fields_list = array_diff($fields_list, $fields_show);
  663.  
  664.     return array_merge($fields_show, $fields_list);
  665.   }
  666. //
  667. //------------------------------------------------------------------------------------------------------------+
  668.  
  669.   function lgsl_sort_servers_by_id($server_a, $server_b)
  670.   {
  671.     if ($server_a['o']['id'] == $server_b['o']['id']) { return 0; }
  672.  
  673.     return ($server_a['o']['id'] > $server_b['o']['id']) ? 1 : -1;
  674.   }
  675. //
  676. //------------------------------------------------------------------------------------------------------------+
  677.  
  678.   function lgsl_sort_servers_by_zone($server_a, $server_b)
  679.   {
  680.     if ($server_a['o']['zone'] == $server_b['o']['zone']) { return 0; }
  681.  
  682.     return ($server_a['o']['zone'] > $server_b['o']['zone']) ? 1 : -1;
  683.   }
  684. //
  685. //------------------------------------------------------------------------------------------------------------+
  686.  
  687.   function lgsl_sort_servers_by_type($server_a, $server_b)
  688.   {
  689.     $result = strcasecmp($server_a['b']['type'], $server_b['b']['type']);
  690.  
  691.     if ($result == 0)
  692.     {
  693.       $result = strcasecmp($server_a['s']['game'], $server_b['s']['game']);
  694.     }
  695.  
  696.     return $result;
  697.   }
  698. //
  699. //------------------------------------------------------------------------------------------------------------+
  700.  
  701.   function lgsl_sort_servers_by_status($server_a, $server_b)
  702.   {
  703.     if ($server_a['b']['status'] == $server_b['b']['status']) { return 0; }
  704.  
  705.     return ($server_a['b']['status'] < $server_b['b']['status']) ? 1 : -1;
  706.   }
  707. //
  708. //------------------------------------------------------------------------------------------------------------+
  709.  
  710.   function lgsl_sort_servers_by_players($server_a, $server_b)
  711.   {
  712.     if ($server_a['s']['players'] == $server_b['s']['players']) { return 0; }
  713.  
  714.     return ($server_a['s']['players'] < $server_b['s']['players']) ? 1 : -1;
  715.   }
  716. //
  717. //------------------------------------------------------------------------------------------------------------+
  718.  
  719.   function lgsl_sort_extras($server)
  720.   {
  721.     if (!is_array($server['e'])) { return $server; }
  722.  
  723.     ksort($server['e']);
  724.  
  725.     return $server;
  726.   }
  727. //
  728. //------------------------------------------------------------------------------------------------------------+
  729.  
  730.   function lgsl_sort_players($server)
  731.   {
  732.     global $lgsl_config;
  733.  
  734.     if (!is_array($server['p'])) { return $server; }
  735.  
  736.     if     ($lgsl_config['sort']['players'] == "name")  { usort($server['p'], "lgsl_sort_players_by_name");  }
  737.     elseif ($lgsl_config['sort']['players'] == "score") { usort($server['p'], "lgsl_sort_players_by_score"); }
  738.  
  739.     return $server;
  740.   }
  741. //
  742. //------------------------------------------------------------------------------------------------------------+
  743.  
  744.   function lgsl_sort_players_by_score($player_a, $player_b)
  745.   {
  746.     if ($player_a['score'] == $player_b['score']) { return 0; }
  747.  
  748.     return ($player_a['score'] < $player_b['score']) ? 1 : -1;
  749.   }
  750. //
  751. //------------------------------------------------------------------------------------------------------------+
  752.  
  753.   function lgsl_sort_players_by_name($player_a, $player_b)
  754.   {
  755.     // REMOVE NON ALPHA NUMERIC ASCII WHILE LEAVING UPPER UTF-8 CHARACTERS
  756.     $name_a = preg_replace("/[\x{00}-\x{2F}\x{3A}-\x{40}\x{5B}-\x{60}\x{7B}-\x{7F}]/", "", $player_a['name']);
  757.     $name_b = preg_replace("/[\x{00}-\x{2F}\x{3A}-\x{40}\x{5B}-\x{60}\x{7B}-\x{7F}]/", "", $player_b['name']);
  758.  
  759.     if (function_exists("mb_convert_case"))
  760.     {
  761.       $name_a = @mb_convert_case($name_a, MB_CASE_LOWER, "UTF-8");
  762.       $name_b = @mb_convert_case($name_b, MB_CASE_LOWER, "UTF-8");
  763.       return strcmp($name_a, $name_b);
  764.     }
  765.     else
  766.     {
  767.       return strcasecmp($name_a, $name_b);
  768.     }
  769.   }
  770. //
  771. //------------------------------------------------------------------------------------------------------------+
  772.  
  773.   function lgsl_charset_detect($server)
  774.   {
  775.     if (!function_exists("mb_detect_encoding")) { return "AUTO"; }
  776.  
  777.     $test = "";
  778.  
  779.     if (isset($server['s']['name'])) { $test .= " {$server['s']['name']} "; }
  780.  
  781.     if (isset($server['p']) && $server['p'])
  782.     {
  783.       foreach ($server['p'] as $player)
  784.       {
  785.         if (isset($player['name'])) { $test .= " {$player['name']} "; }
  786.       }
  787.     }
  788.  
  789.     $charset = @mb_detect_encoding($server['s']['name'], "UTF-8, Windows-1252, ISO-8859-1, ISO-8859-15");
  790.  
  791.     return $charset ? $charset : "AUTO";
  792.   }
  793. //
  794. //------------------------------------------------------------------------------------------------------------+
  795.  
  796.   function lgsl_charset_convert($server, $charset)
  797.   {
  798.     if (!function_exists("mb_convert_encoding")) { return $server; }
  799.  
  800.     if (is_array($server))
  801.     {
  802.       foreach ($server as $key => $value)
  803.       {
  804.         $server[$key] = lgsl_charset_convert($value, $charset);
  805.       }
  806.     }
  807.     else
  808.     {
  809.       $server = @mb_convert_encoding($server, "UTF-8", $charset);
  810.     }
  811.  
  812.     return $server;
  813.   }
  814. //
  815. //------------------------------------------------------------------------------------------------------------+
  816.  
  817.   function lgsl_server_html($server, $word_wrap = 20)
  818.   {
  819.     foreach ($server as $key => $value)
  820.     {
  821.       $server[$key] = is_array($value) ? lgsl_server_html($value, $word_wrap) : lgsl_string_html($value, FALSE, $word_wrap);
  822.     }
  823.  
  824.     return $server;
  825.   }
  826. //
  827. //------------------------------------------------------------------------------------------------------------+
  828.  
  829.   function lgsl_string_html($string, $xml_feed = FALSE, $word_wrap = 0)
  830.   {
  831.     if ($word_wrap) { $string = lgsl_word_wrap($string, $word_wrap); }
  832.  
  833.     if ($xml_feed != FALSE)
  834.     {
  835.       $string = htmlspecialchars($string, ENT_QUOTES);
  836.     }
  837.     elseif (function_exists("mb_convert_encoding"))
  838.     {
  839.       $string = htmlspecialchars($string, ENT_QUOTES);
  840.       $string = @mb_convert_encoding($string, "HTML-ENTITIES", "UTF-8");
  841.     }
  842.     else
  843.     {
  844.       $string = htmlentities($string, ENT_QUOTES, "UTF-8");
  845.     }
  846.  
  847.     if ($word_wrap) { $string = lgsl_word_wrap($string); }
  848.  
  849.     return $string;
  850.   }
  851. //
  852. //------------------------------------------------------------------------------------------------------------+
  853.  
  854.   function lgsl_word_wrap($string, $length_limit = 0)
  855.   {
  856.     if (!$length_limit)
  857.     {
  858. //    http://www.quirksmode.org/oddsandends/wbr.html
  859. //    return str_replace("\x05\x06", " ",       $string); // VISIBLE
  860. //    return str_replace("\x05\x06", "&shy;",   $string); // FF2 VISIBLE AND DIV NEEDED
  861.       return str_replace("\x05\x06", "&#8203;", $string); // IE6 VISIBLE
  862.     }
  863.  
  864.     $word_list = explode(" ", $string);
  865.  
  866.     foreach ($word_list as $key => $word)
  867.     {
  868.       $word_length = function_exists("mb_strlen") ? mb_strlen($word, "UTF-8") : strlen($word);
  869.  
  870.       if ($word_length < $length_limit) { continue; }
  871.  
  872.       $word_new = "";
  873.  
  874.       for ($i=0; $i<$word_length; $i+=$length_limit)
  875.       {
  876.         $word_new .= function_exists("mb_substr") ? mb_substr($word, $i, $length_limit, "UTF-8") : substr($word, $i, $length_limit);
  877.         $word_new .= "\x05\x06";
  878.       }
  879.  
  880.       $word_list[$key] = $word_new;
  881.     }
  882.  
  883.     return implode(" ", $word_list);
  884.   }
  885. //
  886. //------------------------------------------------------------------------------------------------------------+
  887.  
  888.   function lgsl_location_link($location)
  889.   {
  890.     if (!$location) { return "#"; }
  891.  
  892.     if (strlen($location) == 2) { return "http://www.wipmania.com/map/{$location}"; }
  893.  
  894.     return "http://www.google.com/search?q=".urlencode($location);
  895.   }
  896. //
  897. //------------------------------------------------------------------------------------------------------------+
  898.  
  899.   function lgsl_query_location($ip)
  900.   {
  901.     global $lgsl_config;
  902.  
  903.     if ($lgsl_config['locations'] !== 1) { return $lgsl_config['locations']; }
  904.  
  905.     $ip = gethostbyname($ip);
  906.  
  907.     if (long2ip(ip2long($ip)) == "255.255.255.255") { return "XX"; }
  908.  
  909.     $url = "http://api.wipmania.com/".urlencode($ip)."?".urlencode($_SERVER['HTTP_HOST']);
  910.  
  911.     if (function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec'))
  912.     {
  913.       $lgsl_curl = curl_init();
  914.  
  915.       curl_setopt($lgsl_curl, CURLOPT_HEADER, 0);
  916.       curl_setopt($lgsl_curl, CURLOPT_TIMEOUT, 2);
  917.       curl_setopt($lgsl_curl, CURLOPT_RETURNTRANSFER, 1);
  918.       curl_setopt($lgsl_curl, CURLOPT_CONNECTTIMEOUT, 2);
  919.       curl_setopt($lgsl_curl, CURLOPT_URL, $url);
  920.  
  921.       $location = curl_exec($lgsl_curl);
  922.  
  923.       if (curl_error($lgsl_curl)) { $location = "XX"; }
  924.  
  925.       curl_close($lgsl_curl);
  926.     }
  927.     else
  928.     {
  929.       $location = @file_get_contents($url);
  930.     }
  931.  
  932.     if (strlen($location) != 2) { $location = "XX"; }
  933.  
  934.     return $location;
  935.   }
  936. //
  937. //------------------------------------------------------------------------------------------------------------+
  938.  
  939.   function lgsl_realpath($path)
  940.   {
  941.     // WRAPPER SO IT CAN BE DISABLED
  942.  
  943.     global $lgsl_config;
  944.  
  945.     return $lgsl_config['no_realpath'] ? $path : realpath($path);
  946.   }
  947. //
  948. //------------------------------------------------------------------------------------------------------------+
  949.  
  950.   function lgsl_file_path()
  951.   {
  952.     // GET THE LGSL_CLASS.PHP PATH
  953.  
  954.     $lgsl_path = __FILE__;
  955.  
  956.     // SHORTEN TO JUST THE FOLDERS AND ADD TRAILING SLASH
  957.  
  958.     $lgsl_path = dirname($lgsl_path)."/";
  959.  
  960.     // CONVERT WINDOWS BACKSLASHES TO FORWARDSLASHES
  961.  
  962.     $lgsl_path = str_replace("\\", "/", $lgsl_path);
  963.  
  964.     return $lgsl_path;
  965.   }
  966. //
  967. //------------------------------------------------------------------------------------------------------------+
  968.  
  969.   function lgsl_url_path()
  970.   {
  971.     // CHECK IF PATH HAS BEEN SET IN CONFIG
  972.  
  973.     global $lgsl_config;
  974.  
  975.     if ($lgsl_config['url_path'])
  976.     {
  977.       return $lgsl_config['url_path'];
  978.     }
  979.  
  980.     // USE FULL DOMAIN PATH TO AVOID ALIAS PROBLEMS
  981.  
  982.     $host_path  = (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != "on") ? "http://" : "https://";
  983.     $host_path .= request_var('HTTP_HOST','');
  984.  
  985.     // GET FULL PATHS ( EXTRA CODE FOR WINDOWS AND IIS - NO DOCUMENT_ROOT - BACKSLASHES - DOUBLESLASHES - ETC )
  986.  
  987.     if (request_var('DOCUMENT_ROOT',''))   
  988.     {
  989.       $base_path = lgsl_realpath($_SERVER['DOCUMENT_ROOT']);
  990.       $base_path = str_replace("\\", "/", $base_path);
  991.       $base_path = str_replace("//", "/", $base_path);
  992.     }
  993.     else
  994.     {
  995.       $file_path = request_var('SCRIPT_NAME','');
  996.       $file_path = str_replace("\\", "/", $file_path);
  997.       $file_path = str_replace("//", "/", $file_path);
  998.  
  999.       $base_path = request_var('PATH_TRANSLATED','');
  1000.       $base_path = str_replace("\\", "/", $base_path);
  1001.       $base_path = str_replace("//", "/", $base_path);
  1002.       $base_path = substr($base_path, 0, -strlen($file_path));
  1003.     }
  1004.  
  1005.     $lgsl_path = dirname(lgsl_realpath(__FILE__));
  1006.     $lgsl_path = str_replace("\\", "/", $lgsl_path);
  1007.  
  1008.     // REMOVE ANY TRAILING SLASHES
  1009.  
  1010.     if (substr($base_path, -1) == "/") { $base_path = substr($base_path, 0, -1); }
  1011.     if (substr($lgsl_path, -1) == "/") { $lgsl_path = substr($lgsl_path, 0, -1); }
  1012.  
  1013.     // USE THE DIFFERENCE BETWEEN PATHS
  1014.  
  1015.     if (substr($lgsl_path, 0, strlen($base_path)) == $base_path)
  1016.     {
  1017.       $url_path = substr($lgsl_path, strlen($base_path));
  1018.  
  1019.       return $host_path.$url_path."/";
  1020.     }
  1021.  
  1022.     return "/#LGSL_PATH_PROBLEM#{$base_path}#{$lgsl_path}#/";
  1023.   }
  1024. //
  1025. //------------------------------------------------------------------------------------------------------------+
  1026. //
  1027. //------------------------------------------------------------------------------------------------------------+
  1028. //
  1029.  
  1030.   } // END OF DOUBLE LOAD PROTECTION
  1031. //
  1032. //------------------------------------------------------------------------------------------------------------+
  1033. //
  1034. //------------------------------------------------------------------------------------------------------------+
  1035. //
  1036.  
  1037.   global $lgsl_file_path, $lgsl_url_path;
  1038.  
  1039.   $lgsl_file_path = lgsl_file_path();
  1040.  
  1041.   if (isset($_GET['lgsl_debug']))
  1042.   {
  1043.     echo "<hr /><pre>".print_r($_SERVER, TRUE)."</pre>
  1044.          <hr />#d0# ".__FILE__."
  1045.          <hr />#d1# ".@realpath(__FILE__)."
  1046.          <hr />#d2# ".dirname(__FILE__)."
  1047.          <hr />#d3# {$lgsl_file_path}
  1048.          <hr />#d4# {$_SERVER['DOCUMENT_ROOT']}
  1049.          <hr />#d5# ".@realpath($_SERVER['DOCUMENT_ROOT']);
  1050.   }
  1051.  
  1052.   require $lgsl_file_path."lgsl_config.php";
  1053.   require $lgsl_file_path."lgsl_protocol.php";
  1054.  
  1055.   $lgsl_url_path = lgsl_url_path();
  1056.  
  1057.   if (isset($_GET['lgsl_debug']))
  1058.   {
  1059.     echo "<hr />#d6# {$lgsl_url_path}
  1060.          <hr />#c0# {$lgsl_config['url_path']}
  1061.          <hr />#c1# {$lgsl_config['no_realpath']}
  1062.          <hr />#c2# {$lgsl_config['feed']['method']}
  1063.          <hr />#c3# {$lgsl_config['feed']['url']}
  1064.          <hr />#c4# {$lgsl_config['cache_time']}
  1065.          <hr />#c5# {$lgsl_config['live_time']}
  1066.          <hr />#c6# {$lgsl_config['timeout']}
  1067.          <hr />#c7# {$lgsl_config['cms']}
  1068.          <hr />";
  1069.   }
  1070.  
  1071.   if (!isset($lgsl_config['locations']))
  1072.   {
  1073.     exit("LGSL PROBLEM: lgsl_config.php FAILED TO LOAD OR MISSING ENTRIES");
  1074.   }
  1075. //
  1076. //------------------------------------------------------------------------------------------------------------+
  1077. //
Add Comment
Please, Sign In to add comment