lib_remote.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. require_once("Crypt/XXTEA.php");
  25. // Screen type for servers
  26. define("OGP_SCREEN_TYPE_HOME","HOME");
  27. define("OGP_SCREEN_TYPE_UPDATE","UPDATE");
  28. define("AGENT_ERROR_NOT_EXECUTABLE",-13);
  29. class OGPRemoteLibrary
  30. {
  31. private $port;
  32. private $host;
  33. private $encryption_key;
  34. private $enc;
  35. private $timeout;
  36. public function __construct($host,$port,$encryption_key,$timeout = 5)
  37. {
  38. $this->host = $host;
  39. $this->port = $port;
  40. $this->encryption_key = $encryption_key;
  41. $this->enc = new Crypt_XXTEA();
  42. $this->enc->setKey($this->encryption_key);
  43. $this->timeout = $timeout;
  44. }
  45. public function __destruct()
  46. {
  47. }
  48. private function sendRequest($request)
  49. {
  50. $context = stream_context_create
  51. (array('http' => array
  52. (
  53. 'method' => "POST",
  54. 'header' => "Content-Type: text/xml",
  55. 'content' => $request,
  56. 'timeout' => $this->timeout
  57. )));
  58. $status = @file_get_contents("http://".$this->host.":".$this->port."/RPC2", false, $context);
  59. return xmlrpc_decode($status);
  60. }
  61. private function encryptParam($param)
  62. {
  63. $param = base64_encode($param);
  64. $param = $this->enc->encrypt($param);
  65. return base64_encode($param);
  66. }
  67. private function encrypt_params()
  68. {
  69. $params_array = array();
  70. $args = func_get_args();
  71. foreach ($args as $arg)
  72. {
  73. array_push($params_array,$this->encryptParam($arg));
  74. }
  75. return $params_array;
  76. }
  77. /// \return FALSE If there was problems in the decoding.
  78. private function decryptParam($param)
  79. {
  80. $param_tmp = base64_decode($param_tmp,true);
  81. $param_tmp = $this->enc->decrypt($param);
  82. // Lets check in strict mode, so that errors are found.
  83. $param_tmp = base64_decode($param_tmp,true);
  84. if ( $param_tmp === FALSE )
  85. return FALSE;
  86. $param = $param_tmp;
  87. return TRUE;
  88. }
  89. private function add_enc_chk(&$args)
  90. {
  91. $param = "Encryption checking OK";
  92. if(is_array($args))
  93. {
  94. array_push($args, $this->encryptParam($param));
  95. }
  96. elseif(is_null($args))
  97. {
  98. $args = $this->encryptParam($param);
  99. }
  100. else
  101. {
  102. $args = array($args, $this->encryptParam($param));
  103. }
  104. }
  105. /// \returns 1 If file exists
  106. /// \returns 0 If file does not exist
  107. /// \returns -1 If server not available.
  108. public function rfile_exists($file)
  109. {
  110. $args = $this->encryptParam(trim($file));
  111. $this->add_enc_chk($args);
  112. $request = xmlrpc_encode_request("rfile_exists", $args);
  113. $status = $this->sendRequest($request);
  114. if ( $status === 0 )
  115. return 1;
  116. if ( empty($status) )
  117. return -1;
  118. // File does not exist.
  119. return 0;
  120. }
  121. /// \returns 1 If online
  122. /// \returns 0 If offline
  123. /// \returns -1 If encryption key mismatch
  124. public function status_chk()
  125. {
  126. $param = "hello";
  127. $args = $this->encryptParam($param);
  128. $request = xmlrpc_encode_request("quick_chk", $args);
  129. $status = $this->sendRequest($request);
  130. // If 1 is returned then the encryption key did not match.
  131. if ( $status === 1 )
  132. return -1;
  133. // When 0 is returned everythin is OK.
  134. else if ( $status === 0 )
  135. return 1;
  136. // We could not connect to the remote host, offline?.
  137. else
  138. return 0;
  139. }
  140. /// \returns 0 When server offline / could not be connected.
  141. /// \returns the log in $data in case the log can be found.
  142. public function get_log($screen_type,$home_id,$home_path,&$data,$nb_of_lines = 100, $console_log = false)
  143. {
  144. $params_array = $this->encrypt_params($screen_type,$home_id,$home_path,$nb_of_lines,$console_log);
  145. $this->add_enc_chk($params_array);
  146. $request = xmlrpc_encode_request('get_log',$params_array);
  147. $response = $this->sendRequest($request);
  148. if ( $response === NULL )
  149. return 0;
  150. if ( $response == -10 )
  151. return 'Agent Returned: -10. Home not found.';
  152. @list($retval,$data_tmp) = @explode(";",$response);
  153. // We get log only with positive values.
  154. if ( $retval > 0 )
  155. {
  156. $lines = explode('\n',$data_tmp);
  157. foreach ($lines as $line)
  158. {
  159. $data .= base64_decode($line)."\n";
  160. }
  161. }
  162. return $retval;
  163. }
  164. /// \brief Stops remote server.
  165. /// \return 1 On success.
  166. /// \return 0 When server offline / could not be connected.
  167. /// \return -1 When error occurred
  168. public function remote_stop_server($home_id, $server_ip,
  169. $server_port, $control_protocol, $control_password, $control_type, $home_path)
  170. {
  171. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  172. $control_protocol,$control_password,$control_type,$home_path);
  173. $this->add_enc_chk($params_array);
  174. $request = xmlrpc_encode_request("stop_server", $params_array);
  175. $status = $this->sendRequest($request);
  176. // Error occurred on the remote end.
  177. if( $status === 1 )
  178. return -1;
  179. // Server successfully stopped.
  180. else if ( $status == 0 )
  181. return 1;
  182. // Connection problems.
  183. else
  184. return 0;
  185. }
  186. /// \brief Send a RCON command.
  187. /// \return 1 On success.
  188. public function remote_send_rcon_command($home_id, $server_ip,
  189. $server_port, $control_protocol, $control_password, $control_type, $rconCommand,&$data)
  190. {
  191. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  192. $control_protocol,$control_password,$control_type, $rconCommand);
  193. $this->add_enc_chk($params_array);
  194. $request = xmlrpc_encode_request("send_rcon_command", $params_array);
  195. $response = $this->sendRequest($request);
  196. @list($retval,$data_tmp) = @explode(";",$response);
  197. if ( $retval > 0 )
  198. {
  199. $lines = explode('\n',$data_tmp);
  200. foreach ($lines as $line)
  201. {
  202. $data .= base64_decode($line);
  203. }
  204. return 1;
  205. }
  206. elseif ( $retval === 0 )
  207. return 0;
  208. elseif ( $retval === -10 )
  209. return -10;
  210. else
  211. return -1;
  212. }
  213. /// \return 1 If success
  214. /// \return 0 If file does not exist.
  215. /// \return -1 In case of connection error
  216. /// \return -2 If failed to read file.
  217. public function remote_readfile($args,&$data)
  218. {
  219. $args = trim($args);
  220. $args = $this->encryptParam($args);
  221. $this->add_enc_chk($args);
  222. $request = xmlrpc_encode_request("readfile", $args);
  223. $response = $this->sendRequest($request);
  224. if ( $response === NULL )
  225. return -1;
  226. if ( is_array($response) && xmlrpc_is_fault($response))
  227. return -1;
  228. @list($retval,$data_tmp) = @explode(";",$response);
  229. $retval = (integer) $retval;
  230. if ( $retval === 0 )
  231. return 0;
  232. else if ( $retval === -1 )
  233. return -2;
  234. $data = base64_decode($data_tmp);
  235. return 1;
  236. }
  237. /// \return 1 If success
  238. /// \return 0 On failure
  239. /// \return -1 If agent could not be connected.
  240. public function remote_writefile($writefile, $content)
  241. {
  242. $content = base64_encode($content);
  243. $params = $this->encrypt_params($writefile,$content);
  244. $this->add_enc_chk($params);
  245. $request = xmlrpc_encode_request("writefile", $params);
  246. $response = $this->sendRequest($request);
  247. if ( $response === 1 )
  248. return 1;
  249. else if ( $response === 0 )
  250. return 0;
  251. else
  252. return -1;
  253. }
  254. /// \return 1 If success
  255. public function remote_rebootnow()
  256. {
  257. // Must have a parameter even if one is not used.
  258. $args = $this->encryptParam("reboot");
  259. $this->add_enc_chk($args);
  260. $request = xmlrpc_encode_request("rebootnow", $args);
  261. $response = $this->sendRequest($request);
  262. if ( $response )
  263. return 1;
  264. }
  265. /// Updates the mod located in the game home with steam.
  266. /// \return 1 If update started successfully
  267. /// \return 0 If error
  268. /// \return -1 In case of connection error.
  269. public function steam($home_id,$game_home,$mod,$exec_folder_path,$exec_path,$precmd,$postcmd)
  270. {
  271. $params = $this->encrypt_params($home_id,$game_home,$mod,$exec_folder_path,$exec_path,$precmd,$postcmd);
  272. $this->add_enc_chk($params);
  273. $request = xmlrpc_encode_request("steam", $params);
  274. $response = $this->sendRequest($request);
  275. if ( $response === 1 )
  276. return 1;
  277. else if ( $response === 0 )
  278. return 0;
  279. else
  280. return -1;
  281. }
  282. /// Updates the mod located in the game home with steamCmd.
  283. /// \return 1 If update started successfully
  284. /// \return 0 If error
  285. /// \return -1 In case of connection error.
  286. public function steam_cmd($home_id,$game_home,$mod,$modname,$betaname,$betapwd,$user,$pass,$guard,$exec_folder_path,$exec_path,$precmd,$postcmd,$cfg_os,$lockFiles,$archBits)
  287. {
  288. $params = $this->encrypt_params($home_id,$game_home,$mod,$modname,$betaname,$betapwd,$user,$pass,$guard,$exec_folder_path,$exec_path,$precmd,$postcmd,$cfg_os,$lockFiles,$archBits);
  289. $this->add_enc_chk($params);
  290. $request = xmlrpc_encode_request("steam_cmd", $params);
  291. $response = $this->sendRequest($request);
  292. if ( $response === 1 )
  293. return 1;
  294. else if ( $response === 0 )
  295. return 0;
  296. else
  297. return -1;
  298. }
  299. // Returns the latest buildid for $appId
  300. public function fetch_steam_version($appId, $pureOutput = 0)
  301. {
  302. $params = $this->encrypt_params($appId, $pureOutput);
  303. $this->add_enc_chk($params);
  304. $request = xmlrpc_encode_request("fetch_steam_version", $params);
  305. $response = $this->sendRequest($request);
  306. return $response;
  307. }
  308. // Returns -10 if the steamapps/appmanifest file doesn't exist.
  309. // Returns the version installed otherwise.
  310. public function installed_steam_version($game_home, $mod, $pureOutput = 0)
  311. {
  312. $params = $this->encrypt_params($game_home, $mod, $pureOutput);
  313. $this->add_enc_chk($params);
  314. $request = xmlrpc_encode_request("installed_steam_version", $params);
  315. $response = $this->sendRequest($request);
  316. return $response;
  317. }
  318. // If server is running, stops it. Starts an update. And if the server was running, starts the server upon finishing the update.
  319. // Returns -10 if an update is currently in place.
  320. // Returns -9 if the server failed to stop.
  321. // Returns -8 if the update failed to start.
  322. // Returns -7 if the server failed to start.
  323. // Returns 1 on success. (updated, started)
  324. // Returns 2 if the update was successful, but the server wasn't originally running. So wasn't started.
  325. // Requires agent timeout to be set to a high value - otherwise return value will be null.
  326. public function automatic_steam_update(
  327. $home_id, $home_path, $server_ip, $server_port, $exec_path, $exec_folder_path,
  328. $control_protocol, $control_password, $control_type,
  329. $appId, $modname, $betaname, $betapwd, $user, $pass, $guard, $precmd, $postcmd, $cfg_os, $filesToLockUnlock,
  330. $startup_cmd, $cpu, $nice, $preStart, $envVars, $game_key, $archBits
  331. )
  332. {
  333. $params = $this->encrypt_params($home_id, $home_path, $server_ip, $server_port, $exec_path, $exec_folder_path,
  334. $control_protocol, $control_password, $control_type,
  335. $appId, $modname, $betaname, $betapwd, $user, $pass, $guard, $precmd, $postcmd, $cfg_os, $filesToLockUnlock,
  336. $startup_cmd, $cpu, $nice, $preStart, $envVars, $game_key, $archBits);
  337. $this->add_enc_chk($params);
  338. $request = xmlrpc_encode_request("automatic_steam_update", $params);
  339. $response = $this->sendRequest($request);
  340. return $response;
  341. }
  342. /// Updates the mod located in the game home with master server.
  343. /// \return 1 If update started successfully
  344. /// \return 0 If error
  345. /// \return -1 In case of connection error.
  346. public function masterServerUpdate($home_id,$home_path,$ms_home_id,$ms_home_path,$exec_folder_path,$exec_path,$precmd,$postcmd)
  347. {
  348. $params = $this->encrypt_params($home_id,$home_path,$ms_home_id,$ms_home_path,$exec_folder_path,$exec_path,$precmd,$postcmd);
  349. $this->add_enc_chk($params);
  350. $request = xmlrpc_encode_request("master_server_update", $params);
  351. $response = $this->sendRequest($request);
  352. if ( $response === 1 )
  353. return 1;
  354. else if ( $response === 0 )
  355. return 0;
  356. else
  357. return -1;
  358. }
  359. /// \brief Checks if the game update is running for the certain gamehome.
  360. /// \return 1 if the update is active
  361. /// \return 0 if the update is not active
  362. /// \return -1 If unable to connect to the remote server.
  363. /// \return -2 In other errors.
  364. public function game_update_active($game_home,$mod)
  365. {
  366. $params = $this->encrypt_params($game_home, $mod);
  367. $this->add_enc_chk($params);
  368. $request = xmlrpc_encode_request("game_update_active", $params);
  369. if(!$response = $this->sendRequest($request) )
  370. return -1;
  371. else if ( $response === 1 )
  372. return 1;
  373. else if ( $response === 0 )
  374. return 0;
  375. // other errors.
  376. else
  377. return -2;
  378. }
  379. /// \return -1 If could not connect to the remote host.
  380. /// \return -3 In case of unknown error
  381. /// \todo This function is not complete. Also the agent side requires work.
  382. public function start_file_download($url, $dest, $filename, $action = "", $post_script = "" )
  383. {
  384. $params_array = $this->encrypt_params($url,$dest,$filename,$action,$post_script);
  385. $this->add_enc_chk($params_array);
  386. $request = xmlrpc_encode_request("start_file_download",$params_array);
  387. $response = $this->sendRequest($request);
  388. if( !$response )
  389. return -1;
  390. if (is_array($response) && xmlrpc_is_fault($response))
  391. return -3;
  392. return $response;
  393. }
  394. public function is_file_download_in_progress($pid)
  395. {
  396. $args = $this->encryptParam($pid);
  397. $this->add_enc_chk($args);
  398. $request = xmlrpc_encode_request("is_file_download_in_progress", $args);
  399. return $this->sendRequest($request);
  400. }
  401. public function uncompress_file($file_location, $destination)
  402. {
  403. $params_array = $this->encrypt_params($file_location,$destination);
  404. $this->add_enc_chk($params_array);
  405. $request = xmlrpc_encode_request("uncompress_file",$params_array);
  406. return $this->sendRequest($request);
  407. }
  408. /// \return -1 If could not connect to the remote host.
  409. /// \return -3 In case of unknown error
  410. /// \todo This function is not complete. Also the agent side requires work.
  411. public function start_rsync_install($home_id,$home_path,$url,$exec_folder_path,$exec_path,$precmd,$postcmd,$filesToLock="")
  412. {
  413. $params_array = $this->encrypt_params($home_id,$home_path,$url,$exec_folder_path,$exec_path,$precmd,$postcmd,$filesToLock);
  414. $this->add_enc_chk($params_array);
  415. $request = xmlrpc_encode_request("start_rsync_install",$params_array);
  416. $response = $this->sendRequest($request);
  417. if ( $response === 1 )
  418. return 1;
  419. else if ( $response === 0 )
  420. return 0;
  421. else
  422. return -1;
  423. }
  424. public function rsync_progress($home)
  425. {
  426. $args = $this->encryptParam($home);
  427. $this->add_enc_chk($args);
  428. $request = xmlrpc_encode_request("rsync_progress",$args);
  429. $response = $this->sendRequest($request);
  430. if( !$response )
  431. return -1;
  432. #if (is_array($response) && xmlrpc_is_fault($response))
  433. # return -3;
  434. return $response;
  435. }
  436. /// \return array of files in directory, when request success.
  437. /// \return -1 If unable to connect to the remote server.
  438. /// \return -2 In case directory was not accessible.
  439. /// \return -3 Any other error.
  440. public function remote_dirlist($args)
  441. {
  442. $args = $this->encryptParam($args);
  443. $this->add_enc_chk($args);
  444. $request = xmlrpc_encode_request("dirlist",$args);
  445. if( !$response = $this->sendRequest($request))
  446. return -1;
  447. if (is_array($response) && xmlrpc_is_fault($response))
  448. return -3;
  449. if( $response < 0 )
  450. return -2;
  451. return explode(";", $response);
  452. }
  453. /// \return array of files and file info (size owner etc) in directory, when request success.
  454. /// \return -1 If unable to connect to the remote server.
  455. /// \return -2 In case directory was not accessible.
  456. /// \return -3 Any other error.
  457. public function remote_dirlistfm($args)
  458. {
  459. $args = $this->encryptParam($args);
  460. $this->add_enc_chk($args);
  461. $request = xmlrpc_encode_request("dirlistfm", $args);
  462. $response = $this->sendRequest($request);
  463. if ( $response === NULL )
  464. return -1;
  465. if (is_array($response) && xmlrpc_is_fault($response))
  466. return -3;
  467. if( $response < 0 )
  468. return -2;
  469. if ( $response == 1 )
  470. return array();
  471. array_walk_recursive($response, function (&$item, $key) {
  472. if ($key == 'filename')$item = base64_decode($item);
  473. });
  474. return $response;
  475. }
  476. /// \returns the number of CPUs on the server
  477. /// \returns -1 If the server cannot be reached.
  478. public function cpu_count()
  479. {
  480. $args = NULL;
  481. $this->add_enc_chk($args);
  482. $request = xmlrpc_encode_request("cpu_count", $args);
  483. $status = $this->sendRequest($request);
  484. if ( empty($status) )
  485. {
  486. return -1;
  487. }
  488. return $status;
  489. }
  490. public function renice_process($home_id, $nice)
  491. {
  492. $params_array = $this->encrypt_params($home_id, $nice);
  493. $this->add_enc_chk($params_array);
  494. $request = xmlrpc_encode_request("renice_process",$params_array);
  495. return $this->sendRequest($response);
  496. }
  497. /// \return 1 If everything ok
  498. /// \return -1 If connection could not be established.
  499. /// \return -2 In other errors.
  500. /// \todo Other return values?
  501. // Starts game server
  502. public function universal_start($home_id, $game_home, $game_binary, $run_dir, $startup_cmd,
  503. $server_port, $server_ip, $cpu, $nice, $preStart, $envVars, $game_key)
  504. {
  505. $params_array = $this->encrypt_params($home_id, $game_home, $game_binary,
  506. $run_dir, $startup_cmd, $server_port, $server_ip, $cpu, $nice, $preStart, $envVars, $game_key);
  507. $this->add_enc_chk($params_array);
  508. $request = xmlrpc_encode_request("universal_start", $params_array);
  509. $response = $this->sendRequest($request);
  510. if($response === NULL)
  511. return -1;
  512. if (is_array($response) && xmlrpc_is_fault($response))
  513. return -2;
  514. return $response;
  515. }
  516. public function lock_additional_home_files($game_home, $filesToLockUnlock, $action)
  517. {
  518. $params_array = $this->encrypt_params($game_home, $filesToLockUnlock, $action);
  519. $this->add_enc_chk($params_array);
  520. $request = xmlrpc_encode_request("lock_additional_files", $params_array);
  521. $response = $this->sendRequest($request);
  522. if($response === NULL)
  523. return -1;
  524. if (is_array($response) && xmlrpc_is_fault($response))
  525. return -2;
  526. return $response;
  527. }
  528. /// \returns the os of the remote host.
  529. public function what_os()
  530. {
  531. $args = NULL;
  532. $this->add_enc_chk($args);
  533. $request = xmlrpc_encode_request("what_os", $args);
  534. $status = $this->sendRequest($request);
  535. return "$status";
  536. }
  537. /// \return Available IP addresses of the remote host.
  538. /// \return empty array if no ip's are found.
  539. /// \return array containing the ip's on success.
  540. public function discover_ips()
  541. {
  542. $args = "chk";
  543. $args = $this->encryptParam($args);
  544. $this->add_enc_chk($args);
  545. $request = xmlrpc_encode_request("discover_ips", $args);
  546. $status = $this->sendRequest($request);
  547. if ( $status == 0 )
  548. return array();
  549. return explode(",",$status);
  550. }
  551. /// \brief Checks if the server is running.
  552. /// \return 1 If is
  553. /// \return 0 If is not
  554. /// \return -1 If agent could not be reached.
  555. public function is_screen_running($screen_type,$home_id)
  556. {
  557. $params = $this->encrypt_params($screen_type,$home_id);
  558. $this->add_enc_chk($params);
  559. $request = xmlrpc_encode_request("is_screen_running", $params);
  560. $status = $this->sendRequest($request);
  561. if ( $status === 1 )
  562. return 1;
  563. else if ( $status === 0 )
  564. return 0;
  565. else
  566. return -1;
  567. }
  568. public function mon_stats()
  569. {
  570. $args = $this->encrypt_params("mon_stats");
  571. $this->add_enc_chk($args);
  572. $request = xmlrpc_encode_request("mon_stats", $args);
  573. $response = $this->sendRequest($request);
  574. @list($retval,$data_tmp) = @explode(";",$response);
  575. $data = NULL;
  576. if ( $retval > 0 )
  577. {
  578. $lines = explode('\n',$data_tmp);
  579. foreach ($lines as $line)
  580. {
  581. $data .= base64_decode($line);
  582. }
  583. }
  584. return $data;
  585. }
  586. /// \brief copies a game home on the filesystem.
  587. /// \return 1 On success.
  588. /// \return 0 When server offline / could not be connected.
  589. /// \return -1 When error occurred
  590. /// Usually a -1 happens because of a connection timeout during the copy. This is expected
  591. public function clone_home($source_home, $dest_home, $owner)
  592. {
  593. $params_array = $this->encrypt_params($source_home, $dest_home, $owner);
  594. $this->add_enc_chk($params_array);
  595. $request = xmlrpc_encode_request("clone_home", $params_array);
  596. $status = $this->sendRequest($request);
  597. // Copy was successful.
  598. if( $status === 1 )
  599. return 1;
  600. // Copy failed.
  601. else if ( $status === 0 )
  602. return 0;
  603. // Connection problems.
  604. else
  605. return -1;
  606. }
  607. /// \brief removes a game home from the filesystem.
  608. /// \return 1 On success.
  609. /// \return 0 When server offline / could not be connected.
  610. /// \return -1 When error occurred
  611. public function remove_home($game_home_del)
  612. {
  613. $args = $this->encryptParam($game_home_del);
  614. $this->add_enc_chk($args);
  615. $request = xmlrpc_encode_request("remove_home", $args);
  616. $status = $this->sendRequest($request);
  617. // Delete was successful.
  618. if( $status === 1 )
  619. return 1;
  620. // Delete failed.
  621. else if ( $status === 0 )
  622. return 0;
  623. // Connection problems.
  624. else
  625. return -1;
  626. }
  627. public function remote_restart_server($home_id,$server_ip,$server_port,
  628. $control_protocol,$control_password,$control_type,
  629. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart, $envVars, $game_key)
  630. {
  631. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  632. $control_protocol,$control_password,$control_type,
  633. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart,$envVars, $game_key);
  634. $this->add_enc_chk($params_array);
  635. $request = xmlrpc_encode_request("restart_server", $params_array);
  636. $status = $this->sendRequest($request);
  637. // Error server cant stop.
  638. if( $status === -2 )
  639. return -2;
  640. // Error server cant start.
  641. else if ( $status === -1 )
  642. return -1;
  643. //// OK successfully restarted.
  644. else if ( $status === 1 )
  645. return 1;
  646. // Connection problems.
  647. else
  648. return 0;
  649. }
  650. public function sudo_exec($command)
  651. {
  652. $args = $this->encryptParam($command);
  653. $this->add_enc_chk($args);
  654. $request = xmlrpc_encode_request("sudo_exec", $args);
  655. $status = $this->sendRequest($request);
  656. @list($retval,$data_tmp) = @explode(";",$status);
  657. $data = NULL;
  658. if ( $retval > 0 )
  659. {
  660. $lines = explode('\n',$data_tmp);
  661. foreach ($lines as $line)
  662. {
  663. $data .= base64_decode($line)."\n";
  664. }
  665. return $data;
  666. }
  667. return 0;
  668. }
  669. public function exec($command)
  670. {
  671. $args = $this->encryptParam($command);
  672. $this->add_enc_chk($args);
  673. $request = xmlrpc_encode_request("exec", $args);
  674. $response = $this->sendRequest($request);
  675. @list($retval,$data_tmp) = @explode(";",$response);
  676. $data = NULL;
  677. if ( $retval > 0 )
  678. {
  679. $lines = explode('\n',$data_tmp);
  680. foreach ($lines as $line)
  681. {
  682. $data .= base64_decode($line);
  683. }
  684. }
  685. return $data;
  686. }
  687. public function secure_path($action, $path)
  688. {
  689. $params_array = $this->encrypt_params($action, $path);
  690. $this->add_enc_chk($params_array);
  691. $request = xmlrpc_encode_request("secure_path", $params_array);
  692. $status = $this->sendRequest($request);
  693. @list($retval,$data_tmp) = @explode(";",$status);
  694. $data = NULL;
  695. if ( $retval > 0 )
  696. {
  697. $lines = explode('\n',$data_tmp);
  698. foreach ($lines as $line)
  699. {
  700. $data .= base64_decode($line);
  701. }
  702. }
  703. return $data;
  704. }
  705. public function get_chattr($path)
  706. {
  707. $args = $this->encryptParam($path);
  708. $this->add_enc_chk($args);
  709. $request = xmlrpc_encode_request("get_chattr", $args);
  710. $status = $this->sendRequest($request);
  711. @list($retval,$data_tmp) = @explode(";",$status);
  712. $data = NULL;
  713. if ( $retval > 0 )
  714. {
  715. $lines = explode('\n',$data_tmp);
  716. foreach ($lines as $line)
  717. {
  718. $data .= base64_decode($line);
  719. }
  720. }
  721. return $data;
  722. }
  723. public function ftp_mgr($action, $login = "", $password = "", $home_path = "")
  724. {
  725. $params_array = $this->encrypt_params($action, $login, $password, $home_path);
  726. $this->add_enc_chk($params_array);
  727. $request = xmlrpc_encode_request("ftp_mgr", $params_array);
  728. $status = $this->sendRequest($request);
  729. @list($retval,$data_tmp) = @explode(";",$status);
  730. $data = '';
  731. if ( $retval > 0 )
  732. {
  733. $lines = explode('\n',$data_tmp);
  734. foreach ($lines as $line)
  735. {
  736. $decoded_line = base64_decode($line);
  737. if(!preg_match("/^[\s|\t]*$/", $decoded_line))
  738. $data .= "$decoded_line\n";
  739. }
  740. return $data;
  741. }
  742. return 0;
  743. }
  744. public function compress_files($files,$destination,$archive_name,$archive_type)
  745. {
  746. $params_array = $this->encrypt_params($files,$destination,$archive_name,$archive_type);
  747. $this->add_enc_chk($params_array);
  748. $request = xmlrpc_encode_request("compress_files",$params_array);
  749. return $this->sendRequest($request);
  750. }
  751. public function stop_fastdl()
  752. {
  753. $args = NULL;
  754. $this->add_enc_chk($args);
  755. $request = xmlrpc_encode_request("stop_fastdl",$args);
  756. return $this->sendRequest($request);
  757. }
  758. public function start_fastdl()
  759. {
  760. $args = NULL;
  761. $this->add_enc_chk($args);
  762. $request = xmlrpc_encode_request("start_fastdl",$args);
  763. return $this->sendRequest($request);
  764. }
  765. public function restart_fastdl()
  766. {
  767. $args = NULL;
  768. $this->add_enc_chk($args);
  769. $request = xmlrpc_encode_request("restart_fastdl",$args);
  770. return $this->sendRequest($request);
  771. }
  772. public function fastdl_status()
  773. {
  774. $args = NULL;
  775. $this->add_enc_chk($args);
  776. $request = xmlrpc_encode_request("fastdl_status",$args);
  777. $response = $this->sendRequest($request);
  778. if($response === -1 or $response === 0)
  779. return -1;
  780. return 1;
  781. }
  782. public function fastdl_get_aliases()
  783. {
  784. $args = NULL;
  785. $this->add_enc_chk($args);
  786. $request = xmlrpc_encode_request("fastdl_get_aliases",$args);
  787. $response = $this->sendRequest($request);
  788. if(!is_array($response) or count($response) == 0)
  789. return -1;
  790. return $response;
  791. }
  792. public function fastdl_add_alias($alias,$home,$match_file_extension,$match_client_ip)
  793. {
  794. $params_array = $this->encrypt_params($alias,$home,$match_file_extension,$match_client_ip);
  795. $this->add_enc_chk($params_array);
  796. $request = xmlrpc_encode_request("fastdl_add_alias",$params_array);
  797. return $this->sendRequest($request);
  798. }
  799. public function fastdl_del_alias($aliases)
  800. {
  801. if(is_array($aliases))
  802. {
  803. $params_array = array();
  804. foreach($aliases as $alias)
  805. {
  806. $params_array[] = $this->encryptParam($alias);
  807. }
  808. }
  809. else
  810. $params_array = array(0 => $this->encryptParam($aliases));
  811. $this->add_enc_chk($params_array);
  812. $request = xmlrpc_encode_request("fastdl_del_alias",$params_array);
  813. return $this->sendRequest($request);
  814. }
  815. public function fastdl_get_info()
  816. {
  817. $args = NULL;
  818. $this->add_enc_chk($args);
  819. $request = xmlrpc_encode_request("fastdl_get_info",$args);
  820. $response = $this->sendRequest($request);
  821. if($response === -1 or $response == 0)
  822. return -1;
  823. return $response;
  824. }
  825. public function fastdl_create_config($fd_address, $fd_port, $listing, $autostart_on_agent_startup)
  826. {
  827. $params_array = $this->encrypt_params($fd_address, $fd_port, $listing, $autostart_on_agent_startup);
  828. $this->add_enc_chk($params_array);
  829. $request = xmlrpc_encode_request("fastdl_create_config",$params_array);
  830. return $this->sendRequest($request);
  831. }
  832. public function agent_restart()
  833. {
  834. $args = $this->encryptParam('restart');
  835. $this->add_enc_chk($args);
  836. $request = xmlrpc_encode_request("agent_restart", $args);
  837. $response = $this->sendRequest($request);
  838. if($response === -1)
  839. return -1;
  840. return 1;
  841. }
  842. public function scheduler_list_tasks()
  843. {
  844. $args = NULL;
  845. $this->add_enc_chk($args);
  846. $request = xmlrpc_encode_request("scheduler_list_tasks", $args);
  847. $response = $this->sendRequest($request);
  848. if($response === -1 or $response == 0)
  849. return -1;
  850. else
  851. {
  852. $data = array();
  853. foreach ($response as $id => $task)
  854. {
  855. $task = trim(base64_decode($task));
  856. $data[$id] = $task;
  857. }
  858. ksort($data);
  859. return $data;
  860. }
  861. }
  862. public function scheduler_del_task($id)
  863. {
  864. $args = $this->encryptParam($id);
  865. $this->add_enc_chk($args);
  866. $request = xmlrpc_encode_request("scheduler_del_task",$args);
  867. return $this->sendRequest($request);
  868. }
  869. public function scheduler_add_task($job)
  870. {
  871. $args = $this->encryptParam($job);
  872. $this->add_enc_chk($args);
  873. $request = xmlrpc_encode_request("scheduler_add_task",$args);
  874. return $this->sendRequest($request);
  875. }
  876. public function scheduler_edit_task($job_id, $job)
  877. {
  878. $params_array = $this->encrypt_params($job_id, $job);
  879. $this->add_enc_chk($params_array);
  880. $request = xmlrpc_encode_request("scheduler_edit_task",$params_array);
  881. return $this->sendRequest($request);
  882. }
  883. public function remote_get_file_part($file, $offset, &$data)
  884. {
  885. $params_array = $this->encrypt_params($file, $offset);
  886. $this->add_enc_chk($params_array);
  887. $request = xmlrpc_encode_request("get_file_part",$params_array);
  888. $response = $this->sendRequest($request);
  889. if ( $response === NULL )
  890. return -1;
  891. if ( is_array($response) && xmlrpc_is_fault($response))
  892. return -1;
  893. if ( $response === -1 )
  894. return -1;
  895. list($cur_offset,$data_tmp) = explode(";",$response);
  896. $data = base64_decode($data_tmp);
  897. return $cur_offset;
  898. }
  899. public function shell_action($action, $arguments)
  900. {
  901. $params_array = $this->encrypt_params($action, $arguments);
  902. $this->add_enc_chk($params_array);
  903. $request = xmlrpc_encode_request("shell_action", $params_array);
  904. $response = $this->sendRequest($request);
  905. if (is_array($response) && xmlrpc_is_fault($response))
  906. return NULL;
  907. $data = NULL;
  908. if (is_array($response) and !empty($response))
  909. {
  910. $data = array();
  911. foreach ($response as $key => $value)
  912. {
  913. $data[$key] = base64_decode($value);
  914. }
  915. return $data;
  916. }
  917. @list($retval,$data_tmp) = @explode(";",$response);
  918. if ( $retval > 0 )
  919. {
  920. $lines = explode('\n',$data_tmp);
  921. foreach ($lines as $line)
  922. {
  923. $data .= base64_decode($line);
  924. }
  925. }
  926. return $data;
  927. }
  928. public function stop_update($home_id)
  929. {
  930. $args = $this->encryptParam($home_id);
  931. $this->add_enc_chk($args);
  932. $request = xmlrpc_encode_request("stop_update", $args);
  933. $response = $this->sendRequest($request);
  934. if ($response === NULL)
  935. return -1;
  936. if (is_array($response) && xmlrpc_is_fault($response))
  937. return -1;
  938. if ($response === 1)
  939. return -1;
  940. return 1;
  941. }
  942. public function remote_query($protocol, $game_type, $ip, $c_port, $q_port, $s_port)
  943. {
  944. $params_array = $this->encrypt_params($protocol, $game_type, $ip, $c_port, $q_port, $s_port);
  945. $this->add_enc_chk($params_array);
  946. $request = xmlrpc_encode_request("remote_query", $params_array);
  947. $response = $this->sendRequest($request);
  948. if (is_array($response) && xmlrpc_is_fault($response))
  949. return NULL;
  950. if ($response === -1 or $response === 0)
  951. return NULL;
  952. return base64_decode($response);
  953. }
  954. public function send_steam_guard_code($home_id, $sgc)
  955. {
  956. $params_array = $this->encrypt_params($home_id, $sgc);
  957. $this->add_enc_chk($params_array);
  958. $request = xmlrpc_encode_request("send_steam_guard_code", $params_array);
  959. $response = $this->sendRequest($request);
  960. if ($response === NULL)
  961. return -1;
  962. if (is_array($response) && xmlrpc_is_fault($response))
  963. return -1;
  964. if ($response === 1)
  965. return -1;
  966. return 1;
  967. }
  968. }
  969. ?>