rcon.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. if(!DEFINED('EGP'))
  3. exit(header('Refresh: 0; URL=http://'.$_SERVER['SERVER_NAME'].'/404'));
  4. class rcon
  5. {
  6. public static function cmd($server, $cmd = 'status')
  7. {
  8. include(LIB.'games/query/Buffer.php');
  9. include(LIB.'games/query/BaseSocket.php');
  10. include(LIB.'games/query/Socket.php');
  11. include(LIB.'games/query/GoldSourceRcon.php');
  12. include(LIB.'games/query/SourceQuery.php');
  13. $sq = new SourceQuery();
  14. list($ip, $port) = explode(':', $server['address']);
  15. $sq->Connect($ip, $port, 3, SourceQuery::GOLDSOURCE);
  16. $sq->SetRconPassword(rcon::rcon_passwd($server));
  17. $out = $sq->Rcon($cmd);
  18. $sq->Disconnect();
  19. return $out;
  20. }
  21. public static function players($data)
  22. {
  23. $aPlayers = array();
  24. $n = 1;
  25. $lines = explode("\n", $data);
  26. foreach($lines as $line)
  27. {
  28. if(strpos($line, '#') === FALSE)
  29. continue;
  30. $start = strpos($line, '"')+1;
  31. $end = strrpos($line, '"');
  32. $name = htmlspecialchars(substr($line, $start, $end-$start));
  33. $line = trim(substr($line, $end + 1));
  34. $aData = array_values(array_diff(explode(' ', $line), array('', ' ')));
  35. $steamid = trim($aData[1]);
  36. $ip = trim(sys::first(explode(':', $aData[6])));
  37. if(sys::valid($steamid, 'steamid') || sys::valid($ip, 'ip'))
  38. continue;
  39. $aPlayers[$n]['name'] = $name;
  40. $aPlayers[$n]['steamid'] = $steamid;
  41. $aPlayers[$n]['frags'] = trim($aData[2]);
  42. $aPlayers[$n]['time'] = trim($aData[3]);
  43. $aPlayers[$n]['ping'] = trim($aData[4]);
  44. $aPlayers[$n]['ip'] = $ip;
  45. $whois = rcon::country($ip);
  46. $aPlayers[$n]['ico'] = $whois['ico'];
  47. $aPlayers[$n]['country'] = $whois['name'];
  48. $n+=1;
  49. }
  50. return $aPlayers;
  51. }
  52. public static function rcon_passwd($server)
  53. {
  54. global $cfg, $sql, $user;
  55. include(LIB.'ssh.php');
  56. $sql->query('SELECT `address`, `passwd` FROM `control` WHERE `id`="'.$server['unit'].'" LIMIT 1');
  57. $unit = $sql->get();
  58. if(!$ssh->auth($unit['passwd'], $unit['address']))
  59. sys::outjs(array('e' => sys::text('error', 'ssh')));
  60. $ssh->set('cat /servers/'.$server['uid'].'/cstrike/server.cfg | grep rcon_password');
  61. $get = explode(' ', str_replace('"', '', trim($ssh->get())));
  62. $rcon = trim(end($get));
  63. if(!isset($rcon{0}))
  64. sys::outjs(array('r' => 'Необходимо установить rcon пароль (rcon_password).', 'url' => $cfg['http'].'control/id/'.$server['unit'].'/server/'.$server['unit'].'/section/settings/subsection/server'), $nmch);
  65. return $rcon;
  66. }
  67. public static function country($ip)
  68. {
  69. global $SxGeo;
  70. $cData = $SxGeo->getCityFull($ip);
  71. $ico = sys::country($cData['country']['iso']);
  72. return array('ico' => $ico, 'name' => empty($cData['country']['name_ru']) ? 'Не определена' : $cData['country']['name_ru']);
  73. }
  74. }
  75. ?>