PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/blackops/rcon/fake.php

https://code.google.com/p/php-blackops-rcon/
PHP | 546 lines | 261 code | 51 blank | 234 comment | 18 complexity | 709df255666393ff110590a156979daf MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Blackops rcon connection
  4. *
  5. * Copyright (c) 2010, EpicLegion
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  17. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  18. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  20. * POSSIBILITY OF SUCH DAMAGE.
  21. *
  22. * @author EpicLegion, Maximusya
  23. * @package rcon
  24. * @subpackage lib
  25. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  26. */
  27. class Blackops_Rcon {
  28. /**
  29. * Socket res
  30. *
  31. * @var resource
  32. */
  33. protected $socket = NULL;
  34. /**
  35. * Password
  36. *
  37. * @var string
  38. */
  39. protected $password = '';
  40. /**
  41. * Host (IP)
  42. *
  43. * @var string
  44. */
  45. protected $host = '';
  46. /**
  47. * Serve port
  48. * @var int
  49. */
  50. protected $port = 3074;
  51. /**
  52. * Constructor
  53. *
  54. * @param string $host
  55. * @param int $port
  56. * @param string $password
  57. */
  58. public function __construct($host = '', $port = 3074, $password = '')
  59. {
  60. $this->host = $host;
  61. $this->port = $port;
  62. $this->password = $password;
  63. }
  64. /**
  65. * Send command
  66. *
  67. * @param string $command
  68. * @param bool $return_response
  69. * @throws Exception
  70. * @return mixed
  71. */
  72. public function command($command, $return_response = TRUE)
  73. {
  74. Kohana::$log->add('error', $command);
  75. // Wait for response?
  76. if($return_response)
  77. {
  78. return '';
  79. }
  80. }
  81. /**
  82. * Connect to rcon
  83. *
  84. * @throws Exception
  85. */
  86. public function connect()
  87. {
  88. }
  89. /**
  90. * Disconnect
  91. */
  92. public function disconnect()
  93. {
  94. //fclose($this->socket);
  95. }
  96. /**
  97. * Read response
  98. *
  99. * @return string
  100. */
  101. public function get_response()
  102. {
  103. // No response lol
  104. return '';
  105. }
  106. /**
  107. * Get socket resource
  108. *
  109. * @return resource
  110. */
  111. public function get_socket()
  112. {
  113. return $this->socket;
  114. }
  115. /**
  116. * Set RCON password
  117. *
  118. * @param string $password
  119. */
  120. public function set_password($password = '')
  121. {
  122. $this->password = $password;
  123. }
  124. /**
  125. * Set server connection details
  126. *
  127. * @param string $host
  128. * @param int $port
  129. */
  130. public function set_server_info($host = '', $port = 3074)
  131. {
  132. $this->host = $host;
  133. $this->port = $port;
  134. }
  135. }
  136. class Blackops_Rcon_Commands {
  137. /**
  138. * Rcon connection
  139. *
  140. * @var Blackops_Rcon
  141. */
  142. protected $console = NULL;
  143. /**
  144. * Constructor
  145. *
  146. * @param Blackops_Rcon $console
  147. */
  148. public function __construct(Blackops_Rcon $console)
  149. {
  150. $this->console = $console;
  151. }
  152. /**
  153. * Read server cvar
  154. *
  155. * @param string $var
  156. * @return string
  157. */
  158. public function get_cvar($var)
  159. {
  160. // Command
  161. $response = $this->console->command($var);
  162. if($var == 'sv_mapRotation')
  163. {
  164. return 'gametype koth map mp_array map mp_nuked map mp_hanoi';
  165. }
  166. elseif($var == 'playlist_excludeMap')
  167. {
  168. return 'mp_array';
  169. }
  170. return '';
  171. // Match here
  172. $match = '';
  173. // Get value
  174. if(preg_match('#"'.$var.'" is: "(.*?)" default.+#is', $response, $match))
  175. {
  176. // Return match
  177. return $match[1];
  178. }
  179. else
  180. {
  181. // Nothing
  182. return '';
  183. }
  184. }
  185. /**
  186. * Ban player
  187. *
  188. * @param string|int $data
  189. * @param bool $ban_by_name
  190. */
  191. public function ban($data, $ban_by_name = FALSE)
  192. {
  193. // Name?
  194. if($ban_by_name)
  195. {
  196. // Ban by name
  197. $this->console->command('banuser "'.htmlspecialchars($data, ENT_QUOTES, 'UTF-8').'"');
  198. }
  199. else
  200. {
  201. // Ban by ID
  202. $this->console->command('banclient '.(int) $data);
  203. }
  204. }
  205. /**
  206. * Set c/dvar
  207. *
  208. * @param string $name
  209. * @param mixed $value
  210. * @param bool $dvar
  211. */
  212. public function cvar($name, $value, $dvar = FALSE)
  213. {
  214. // Admin dvar or cvar
  215. if($dvar)
  216. {
  217. $this->console->command('setadmindvar '.$name.' '.(is_int($value) ? $value : '"'.$value.'"'));
  218. }
  219. else
  220. {
  221. $this->console->command($name.' '.(is_int($value) ? $value : '"'.$value.'"'));
  222. }
  223. }
  224. /**
  225. * Get current playlist
  226. *
  227. * @return int|bool
  228. */
  229. public function get_playlist()
  230. {
  231. // Query server
  232. $response = $this->console->command('playlist');
  233. /*
  234. * playlist
  235. "playlist" is: "30" default: "1"
  236. Domain is any integer from 0 to 64
  237. */
  238. // Get playlist
  239. return '1';
  240. }
  241. /**
  242. * Get basic server info (map and player list)
  243. *
  244. * @return array
  245. */
  246. public function get_server_info()
  247. {
  248. return array_merge($this->get_teamstatus(), $this->get_real_server_info());
  249. }
  250. /**
  251. * Teamstatus query
  252. *
  253. * @return array
  254. */
  255. public function get_teamstatus()
  256. {
  257. // Send command
  258. $raw_response = $this->console->command('teamstatus');
  259. // Try again
  260. if(empty($raw_response))
  261. {
  262. $raw_response = $this->console->command('teamstatus');
  263. }
  264. // Break lines
  265. /*$response = explode("\x0a", $raw_response);
  266. // Valid response?
  267. if(count($response) <= 1)
  268. {
  269. throw new Exception('Cannot retrieve teamstatus');
  270. }*/
  271. // Array containing teamstatus
  272. $teamstatus = array(
  273. 'map' => 'mp_array',
  274. 'players' => array(
  275. 1 => array(
  276. 'id' => 1,
  277. 'score' => 1000,
  278. 'ping' => 100,
  279. 'guid' => 666666,
  280. 'name' => 'Fake user #1',
  281. 'team' => 0,
  282. 'lastmsg' => 1,
  283. 'address' => '192.1.1.1:3333',
  284. 'qport' => 3333,
  285. 'rate' => 1000
  286. ),
  287. 2 => array(
  288. 'id' => 2,
  289. 'score' => 1000,
  290. 'ping' => 100,
  291. 'guid' => 666666,
  292. 'name' => 'Fake user #2',
  293. 'team' => 1,
  294. 'lastmsg' => 1,
  295. 'address' => '192.1.1.1:3333',
  296. 'qport' => 3333,
  297. 'rate' => 1000
  298. ),
  299. 3 => array(
  300. 'id' => 3,
  301. 'score' => 1000,
  302. 'ping' => 100,
  303. 'guid' => 666666,
  304. 'name' => 'Fake user #3',
  305. 'team' => 2,
  306. 'lastmsg' => 1,
  307. 'address' => '192.1.1.1:3333',
  308. 'qport' => 3333,
  309. 'rate' => 1000
  310. ),
  311. 4 => array(
  312. 'id' => 4,
  313. 'score' => 1000,
  314. 'ping' => 100,
  315. 'guid' => 666666,
  316. 'name' => 'Fake user #4',
  317. 'team' => 1,
  318. 'lastmsg' => 1,
  319. 'address' => '192.1.1.1:3333',
  320. 'qport' => 3333,
  321. 'rate' => 1000
  322. ),
  323. ),
  324. 'error' => ''
  325. );
  326. // Remove header
  327. /*unset($response[0], $response[1], $response[2]);
  328. // Reader object
  329. $parser = new Blackops_Rcon_Statusparser;
  330. // Iterate players
  331. foreach($response as $line)
  332. {
  333. // Not a player?
  334. if(empty($line) OR strlen($line) < 80)
  335. {
  336. break;
  337. }
  338. // Parse line
  339. $line = $parser->parse($line);
  340. // Set player info
  341. $teamstatus['players'][$line['id']] = $line;
  342. }*/
  343. // Return
  344. return $teamstatus;
  345. }
  346. /**
  347. * Get real server info
  348. *
  349. * @return array
  350. */
  351. public function get_real_server_info()
  352. {
  353. // Send command
  354. $raw_response = $this->console->command('serverinfo');
  355. // Try again
  356. if(empty($raw_response))
  357. {
  358. $raw_response = $this->console->command('serverinfo');
  359. }
  360. // Break lines
  361. $response = explode("\x0a", $raw_response);
  362. return array();
  363. // Valid response?
  364. if(count($response) <= 1)
  365. {
  366. throw new Exception('Cannot retrieve server info');
  367. }
  368. // Remove header
  369. unset($response[0]);
  370. // Vars
  371. $server_info = array();
  372. $var_name = '';
  373. $var_value = '';
  374. // Iterate info
  375. foreach($response as $line)
  376. {
  377. // Tokenize
  378. $var_name = strtok($line, ' ');
  379. // Value
  380. $var_value = trim(substr($line, strlen($var_name)));
  381. // Set var
  382. if($var_name !== FALSE)
  383. {
  384. $server_info[$var_name] = is_numeric($var_value) ? (int) $var_value : $var_value;
  385. }
  386. }
  387. // Return
  388. return $server_info;
  389. }
  390. /**
  391. * Kick player
  392. *
  393. * @param string|int $data
  394. * @param bool $kick_by_name
  395. * @param string $reason
  396. */
  397. public function kick($data, $kick_by_name = FALSE, $reason = '')
  398. {
  399. // Name?
  400. if($kick_by_name)
  401. {
  402. // Kick by name
  403. if(!$reason)
  404. {
  405. $this->console->command('kick "'.htmlspecialchars($data, ENT_QUOTES, 'UTF-8').'"');
  406. }
  407. else
  408. {
  409. $this->console->command('kick "'.htmlspecialchars($data, ENT_QUOTES, 'UTF-8').'" "'.htmlspecialchars($reason, ENT_QUOTES, 'UTF-8').'"');
  410. }
  411. }
  412. else
  413. {
  414. // Kick by ID
  415. if(!$reason)
  416. {
  417. $this->console->command('clientkick '.(int) $data);
  418. }
  419. else
  420. {
  421. $this->console->command('clientkick '.(int) $data.' "'.htmlspecialchars($reason, ENT_QUOTES, 'UTF-8').'"');
  422. }
  423. }
  424. }
  425. /**
  426. * Message (public or private)
  427. *
  428. * @param string $message
  429. * @param string|int $client
  430. */
  431. public function message($message, $client = 'all')
  432. {
  433. // Linebreak?
  434. if(stristr($message, '[linebreak]') !== FALSE)
  435. {
  436. $message = explode('[linebreak]', $message);
  437. }
  438. else
  439. {
  440. $message = array($message);
  441. }
  442. // Global say
  443. if($client == 'all')
  444. {
  445. foreach($message as $v)
  446. {
  447. $this->console->command('say "'.strip_tags($v).'"');
  448. }
  449. }
  450. else
  451. {
  452. // Cast
  453. $client = (int) $client;
  454. // Send tell command
  455. foreach($message as $v)
  456. {
  457. $this->console->command('tell '.$client.' "'.strip_tags($v).'"');
  458. }
  459. }
  460. }
  461. /**
  462. * Set rcon console
  463. *
  464. * @param Blackops_Rcon $console
  465. */
  466. public function set_rcon(Blackops_Rcon $console)
  467. {
  468. $this->console = $console;
  469. }
  470. /**
  471. * Temporary ban player
  472. *
  473. * @param string|int $data
  474. * @param bool $ban_by_name
  475. */
  476. public function temp_ban($data, $ban_by_name = FALSE)
  477. {
  478. // Name?
  479. if($ban_by_name)
  480. {
  481. // Ban by name
  482. $this->console->command('tempbanuser "'.htmlspecialchars($data, ENT_QUOTES, 'UTF-8').'"');
  483. }
  484. else
  485. {
  486. // Ban by ID
  487. $this->console->command('tempbanclient '.(int) $data);
  488. }
  489. }
  490. }