Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.25 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Script Setting
  5.  */
  6. if (!defined('__SERVER_HOST')) {
  7.     define('__SERVER_HOST', '174.140.168.155'); // Server IP or Hostname
  8. }
  9. if (!defined('__LOGIN_PORT')) {
  10.     define('__LOGIN_PORT', 6900); // eAthena Login Server Port
  11. }
  12. if (!defined('__CHAR_PORT')) {
  13.     define('__CHAR_PORT', 6121); // eAthena Char Server Port
  14. }
  15. if (!defined('__MAP_PORT')) {
  16.     define('__MAP_PORT', 5121); // eAthena Map Server Port
  17. }
  18. if (!defined('__MYSQL_HOST')) {
  19.     define('__MYSQL_HOST', '174.140.168.155'); // MySQL Hostname or IP
  20. }
  21. if (!defined('__MYSQL_USER')) {
  22.     define('__MYSQL_USER', 'USERNAME'); // MySQL Username
  23. }
  24. if (!defined('__MYSQL_PASS')) {
  25.     define('__MYSQL_PASS', 'PASSWORD'); // MySQL Password
  26. }
  27. if (!defined('__MYSQL_BASE')) {
  28.     define('__MYSQL_BASE', 'DATABASE'); // MySQL Database Name
  29. }
  30. if (!defined('__SERVER_STATUS_CACHE_FILE')) {
  31.     define('__SERVER_STATUS_CACHE_FILE', 'server_status.cache');
  32. }
  33. if (!defined('__SERVER_STATUS_CACHE_FOR')) {
  34.     define('__SERVER_STATUS_CACHE_FOR', 5); // In Minutes
  35. }
  36. if (!defined('__SERVER_STATUS_CHECK_TIMEOUT')) {
  37.     define('__SERVER_STATUS_CHECK_TIMEOUT', 1); // In Seconds
  38. }
  39.  
  40. /*
  41.  * MySQL Connection Helper Class
  42.  */
  43. class MySQL
  44. {
  45.     var $mysql_host = null;
  46.     var $mysql_user = null;
  47.     var $mysql_pass = null;
  48.     var $mysql_base = null;
  49.     var $mysql_link = null;
  50.     var $mysql_result = null;
  51.     var $con_var_set = false;
  52.  
  53.     function MySQL($_host, $_user, $_pass, $_base)
  54.     {
  55.         if ((isset($_host) && !empty($_host)) &&
  56.             (isset($_user) && !empty($_user)) &&
  57.             (isset($_pass) && !empty($_pass)) &&
  58.             (isset($_base) && !empty($_base)))
  59.         {
  60.             $this->mysql_host = $_host;
  61.             $this->mysql_user = $_user;
  62.             $this->mysql_pass = $_pass;
  63.             $this->mysql_base = $_base;
  64.  
  65.             $this->con_var_set = true;
  66.         }
  67.     }
  68.  
  69.     function Connect()
  70.     {
  71.         if ($this->con_var_set)
  72.         {
  73.             $this->mysql_link = @mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_pass) or trigger_error(mysql_error(), E_USER_ERROR);
  74.  
  75.             if ($this->mysql_link != null) {
  76.                 @mysql_select_db($this->mysql_base, $this->mysql_link) or trigger_error(mysql_error(), E_USER_ERROR);
  77.             }
  78.         }
  79.     }
  80.  
  81.     function FreeResult()
  82.     {
  83.         if ($this->mysql_result != null && is_resource($this->mysql_result)) {
  84.             @mysql_free_result($this->mysql_result) or trigger_error(mysql_error(), E_USER_ERROR);
  85.         }
  86.     }
  87.  
  88.     function Query($_query)
  89.     {
  90.         $this->FreeResult();
  91.  
  92.         if (isset($_query) && !empty($_query)) {
  93.             $this->mysql_result = @mysql_query($_query, $this->mysql_link) or trigger_error(mysql_error(), E_USER_ERROR);
  94.         }
  95.     }
  96.  
  97.     function FetchAssoc()
  98.     {
  99.         return is_resource($this->mysql_result) ? @mysql_fetch_assoc($this->mysql_result) : array();
  100.     }
  101.  
  102.     function NumRows()
  103.     {
  104.         return is_resource($this->mysql_result) ? @mysql_num_rows($this->mysql_result) : 0;
  105.     }
  106.  
  107.     function Close()
  108.     {
  109.         $this->FreeResult();
  110.  
  111.         if ($this->mysql_link != null && is_resource($this->mysql_link))
  112.         {
  113.             @mysql_close($this->mysql_link) or trigger_error(mysql_error(), E_USER_ERROR);
  114.         }
  115.     }
  116. }
  117.  
  118. /*
  119.  *  Error Reporting Config
  120.  */
  121. error_reporting(E_USER_ERROR);
  122. @ini_set('display_errors', 1);
  123.  
  124. /*
  125.  * Create Cache File If It Does Not Exists
  126.  */
  127. if (!file_exists(__SERVER_STATUS_CACHE_FILE))
  128. {
  129.     $default_status = json_encode(array('loginOnline' => 0, 'charOnline' => 0, 'mapOnline' => 0, 'onlineNow' => 0, 'serverPeaked' => 0, 'lastChecked' => 0));
  130.     $handle = @fopen(__SERVER_STATUS_CACHE_FILE, 'w') or trigger_error('Could not create file : '. __SERVER_STATUS_CACHE_FILE, E_USER_ERROR);
  131.     @fwrite($handle, $default_status, strlen($default_status)) or trigger_error('Could not write cache : '. $default_status, E_USER_ERROR);
  132.     @fclose($handle) or trigger_error('Could not close cache file handle', E_USER_ERROR);
  133. }
  134.  
  135. /*
  136.  * Load Cache File
  137.  */
  138. $cached_data = @file_get_contents(__SERVER_STATUS_CACHE_FILE) or trigger_error('Could not load cached data from file : '. __SERVER_STATUS_CACHE_FILE, E_USER_ERROR);
  139. $cached_status = @json_decode($cached_data) or trigger_error('Could not decode cached data : '. $cached_data, E_USER_ERROR);
  140. if (is_object($cached_status))
  141. {
  142.     // Extract Data
  143.     $cachedLoginOnline  = $cached_status->{'loginOnline'};
  144.     $cachedCharOnline   = $cached_status->{'charOnline'};
  145.     $cachedMapOnline    = $cached_status->{'mapOnline'};
  146.     $cachedLastChecked  = $cached_status->{'lastChecked'};
  147.     $cachedOnlineNow    = $cached_status->{'onlineNow'};
  148.     $cachedServerPeaked = $cached_status->{'serverPeaked'};
  149.  
  150.     // Update Server Status If Needed
  151.     if (($cachedLastChecked + (60 * intval(__SERVER_STATUS_CACHE_FOR))) < time())
  152.     {
  153.         // Temp Status
  154.         $_temp_loginOnline  = 0;
  155.         $_temp_charOnline   = 0;
  156.         $_temp_mapOnline    = 0;
  157.         $_temp_onlineNow    = 0;
  158.         $_temp_serverPeaked = 0;
  159.  
  160.         // Check Login Server
  161.         if ($l = @fsockopen(__SERVER_HOST, __LOGIN_PORT, $errno, $errstr, __SERVER_STATUS_CHECK_TIMEOUT)) {
  162.             $_temp_loginOnline = 1;
  163.             @fclose($l);
  164.         }
  165.  
  166.         // Check Char Server
  167.         if ($c = @fsockopen(__SERVER_HOST, __CHAR_PORT, $errno, $errstr, __SERVER_STATUS_CHECK_TIMEOUT)) {
  168.             $_temp_charOnline = 1;
  169.             @fclose($c);
  170.         }
  171.  
  172.         // Check Map Server
  173.         if ($m = @fsockopen(__SERVER_HOST, __MAP_PORT, $errno, $errstr, __SERVER_STATUS_CHECK_TIMEOUT)) {
  174.             $_temp_mapOnline = 1;
  175.             @fclose($m);
  176.         }
  177.  
  178.         // Create MySQL Object
  179.         $mysql = new MySQL(__MYSQL_HOST, __MYSQL_USER, __MYSQL_PASS, __MYSQL_BASE);
  180.  
  181.         // Connect To MySQL Server
  182.         $mysql->Connect();
  183.  
  184.         // Check How Many Players Are Online
  185.         $mysql->Query("SELECT COUNT(*) AS `onlineNow` FROM `char` WHERE `online` = 1");
  186.         if ($mysql->NumRows() == 1) {
  187.             $row = $mysql->FetchAssoc();
  188.             $_temp_onlineNow = $row['onlineNow'];
  189.         }
  190.  
  191.         // Check If Players Online Now Is Greater Than Last Peak
  192.         if ($_temp_onlineNow > $cachedServerPeaked) {
  193.             $_temp_serverPeaked = $_temp_onlineNow;
  194.         } else {
  195.             $_temp_serverPeaked = $cachedServerPeaked;
  196.         }
  197.  
  198.         // Close MySQL Connection
  199.         $mysql->Close();
  200.  
  201.         // Store Cached Data
  202.         $server_status = json_encode(array('loginOnline' => $_temp_loginOnline, 'charOnline' => $_temp_charOnline, 'mapOnline' => $_temp_mapOnline, 'onlineNow' => $_temp_onlineNow, 'serverPeaked' => $_temp_serverPeaked, 'lastChecked' => time()));
  203.         $handle = @fopen(__SERVER_STATUS_CACHE_FILE, 'w') or trigger_error('Could not create file : '. __SERVER_STATUS_CACHE_FILE, E_USER_ERROR);
  204.         @fwrite($handle, $server_status, strlen($server_status)) or trigger_error('Could not write cache : '. $server_status, E_USER_ERROR);
  205.         @fclose($handle) or trigger_error('Could not close cache file handle', E_USER_ERROR);
  206.  
  207.         // Return Server Status From Live Checking
  208.         $loginOnline  = ($_temp_loginOnline == 1 ? true : false);
  209.         $charOnline   = ($_temp_charOnline  == 1 ? true : false);
  210.         $mapOnline    = ($_temp_mapOnline   == 1 ? true : false);
  211.         $onlineNow    = number_format($_temp_onlineNow);
  212.         $serverPeaked = number_format($_temp_serverPeaked);
  213.     }
  214.     else
  215.     {
  216.         // Return Server Status From Cache
  217.         $loginOnline  = ($cachedLoginOnline == 1 ? true : false);
  218.         $charOnline   = ($cachedCharOnline  == 1 ? true : false);
  219.         $mapOnline    = ($cachedMapOnline   == 1 ? true : false);
  220.         $onlineNow    = number_format($cachedOnlineNow);
  221.         $serverPeaked = number_format($cachedServerPeaked);
  222.     }
  223. }
  224. else {
  225.     trigger_error('Could not parse cached status, please delete this file : '. __SERVER_STATUS_CACHE_FILE, E_USER_ERROR);
  226. }
  227.  
  228. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement