lib_remote.php 33 KB

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