lib_remote.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 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 = "")
  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);
  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
  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);
  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. public function universal_start($home_id, $game_home, $game_binary, $run_dir, $startup_cmd,
  502. $server_port, $server_ip, $cpu, $nice, $preStart = "", $envVars = "")
  503. {
  504. $params_array = $this->encrypt_params($home_id, $game_home, $game_binary,
  505. $run_dir, $startup_cmd, $server_port, $server_ip, $cpu, $nice, $preStart, $envVars);
  506. $this->add_enc_chk($params_array);
  507. $request = xmlrpc_encode_request("universal_start", $params_array);
  508. $response = $this->sendRequest($request);
  509. if($response === NULL)
  510. return -1;
  511. if (is_array($response) && xmlrpc_is_fault($response))
  512. return -2;
  513. return $response;
  514. }
  515. public function lock_additional_home_files($game_home, $filesToLockUnlock, $action)
  516. {
  517. $params_array = $this->encrypt_params($game_home, $filesToLockUnlock, $action);
  518. $this->add_enc_chk($params_array);
  519. $request = xmlrpc_encode_request("lock_additional_files", $params_array);
  520. $response = $this->sendRequest($request);
  521. if($response === NULL)
  522. return -1;
  523. if (is_array($response) && xmlrpc_is_fault($response))
  524. return -2;
  525. return $response;
  526. }
  527. /// \returns the os of the remote host.
  528. public function what_os()
  529. {
  530. $args = NULL;
  531. $this->add_enc_chk($args);
  532. $request = xmlrpc_encode_request("what_os", $args);
  533. $status = $this->sendRequest($request);
  534. return "$status";
  535. }
  536. /// \return Available IP addresses of the remote host.
  537. /// \return empty array if no ip's are found.
  538. /// \return array containing the ip's on success.
  539. public function discover_ips()
  540. {
  541. $args = "chk";
  542. $args = $this->encryptParam($args);
  543. $this->add_enc_chk($args);
  544. $request = xmlrpc_encode_request("discover_ips", $args);
  545. $status = $this->sendRequest($request);
  546. if ( $status == 0 )
  547. return array();
  548. return explode(",",$status);
  549. }
  550. /// \brief Checks if the server is running.
  551. /// \return 1 If is
  552. /// \return 0 If is not
  553. /// \return -1 If agent could not be reached.
  554. public function is_screen_running($screen_type,$home_id)
  555. {
  556. $params = $this->encrypt_params($screen_type,$home_id);
  557. $this->add_enc_chk($params);
  558. $request = xmlrpc_encode_request("is_screen_running", $params);
  559. $status = $this->sendRequest($request);
  560. if ( $status === 1 )
  561. return 1;
  562. else if ( $status === 0 )
  563. return 0;
  564. else
  565. return -1;
  566. }
  567. public function mon_stats()
  568. {
  569. $args = $this->encrypt_params("mon_stats");
  570. $this->add_enc_chk($args);
  571. $request = xmlrpc_encode_request("mon_stats", $args);
  572. $response = $this->sendRequest($request);
  573. @list($retval,$data_tmp) = @explode(";",$response);
  574. $data = NULL;
  575. if ( $retval > 0 )
  576. {
  577. $lines = explode('\n',$data_tmp);
  578. foreach ($lines as $line)
  579. {
  580. $data .= base64_decode($line);
  581. }
  582. }
  583. return $data;
  584. }
  585. /// \brief copies a game home on the filesystem.
  586. /// \return 1 On success.
  587. /// \return 0 When server offline / could not be connected.
  588. /// \return -1 When error occurred
  589. /// Usually a -1 happens because of a connection timeout during the copy. This is expected
  590. public function clone_home($source_home, $dest_home, $owner)
  591. {
  592. $params_array = $this->encrypt_params($source_home, $dest_home, $owner);
  593. $this->add_enc_chk($params_array);
  594. $request = xmlrpc_encode_request("clone_home", $params_array);
  595. $status = $this->sendRequest($request);
  596. // Copy was successful.
  597. if( $status === 1 )
  598. return 1;
  599. // Copy failed.
  600. else if ( $status === 0 )
  601. return 0;
  602. // Connection problems.
  603. else
  604. return -1;
  605. }
  606. /// \brief removes a game home from the filesystem.
  607. /// \return 1 On success.
  608. /// \return 0 When server offline / could not be connected.
  609. /// \return -1 When error occurred
  610. public function remove_home($game_home_del)
  611. {
  612. $args = $this->encryptParam($game_home_del);
  613. $this->add_enc_chk($args);
  614. $request = xmlrpc_encode_request("remove_home", $args);
  615. $status = $this->sendRequest($request);
  616. // Delete was successful.
  617. if( $status === 1 )
  618. return 1;
  619. // Delete failed.
  620. else if ( $status === 0 )
  621. return 0;
  622. // Connection problems.
  623. else
  624. return -1;
  625. }
  626. public function remote_restart_server($home_id,$server_ip,$server_port,
  627. $control_protocol,$control_password,$control_type,
  628. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart = "", $envVars = "")
  629. {
  630. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  631. $control_protocol,$control_password,$control_type,
  632. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart,$envVars);
  633. $this->add_enc_chk($params_array);
  634. $request = xmlrpc_encode_request("restart_server", $params_array);
  635. $status = $this->sendRequest($request);
  636. // Error server cant stop.
  637. if( $status === -2 )
  638. return -2;
  639. // Error server cant start.
  640. else if ( $status === -1 )
  641. return -1;
  642. //// OK successfully restarted.
  643. else if ( $status === 1 )
  644. return 1;
  645. // Connection problems.
  646. else
  647. return 0;
  648. }
  649. public function sudo_exec($command)
  650. {
  651. $args = $this->encryptParam($command);
  652. $this->add_enc_chk($args);
  653. $request = xmlrpc_encode_request("sudo_exec", $args);
  654. $status = $this->sendRequest($request);
  655. @list($retval,$data_tmp) = @explode(";",$status);
  656. $data = NULL;
  657. if ( $retval > 0 )
  658. {
  659. $lines = explode('\n',$data_tmp);
  660. foreach ($lines as $line)
  661. {
  662. $data .= base64_decode($line)."\n";
  663. }
  664. return $data;
  665. }
  666. return 0;
  667. }
  668. public function exec($command)
  669. {
  670. $args = $this->encryptParam($command);
  671. $this->add_enc_chk($args);
  672. $request = xmlrpc_encode_request("exec", $args);
  673. $response = $this->sendRequest($request);
  674. @list($retval,$data_tmp) = @explode(";",$response);
  675. $data = NULL;
  676. if ( $retval > 0 )
  677. {
  678. $lines = explode('\n',$data_tmp);
  679. foreach ($lines as $line)
  680. {
  681. $data .= base64_decode($line);
  682. }
  683. }
  684. return $data;
  685. }
  686. public function secure_path($action, $path)
  687. {
  688. $params_array = $this->encrypt_params($action, $path);
  689. $this->add_enc_chk($params_array);
  690. $request = xmlrpc_encode_request("secure_path", $params_array);
  691. $status = $this->sendRequest($request);
  692. @list($retval,$data_tmp) = @explode(";",$status);
  693. $data = NULL;
  694. if ( $retval > 0 )
  695. {
  696. $lines = explode('\n',$data_tmp);
  697. foreach ($lines as $line)
  698. {
  699. $data .= base64_decode($line);
  700. }
  701. }
  702. return $data;
  703. }
  704. public function get_chattr($path)
  705. {
  706. $args = $this->encryptParam($path);
  707. $this->add_enc_chk($args);
  708. $request = xmlrpc_encode_request("get_chattr", $args);
  709. $status = $this->sendRequest($request);
  710. @list($retval,$data_tmp) = @explode(";",$status);
  711. $data = NULL;
  712. if ( $retval > 0 )
  713. {
  714. $lines = explode('\n',$data_tmp);
  715. foreach ($lines as $line)
  716. {
  717. $data .= base64_decode($line);
  718. }
  719. }
  720. return $data;
  721. }
  722. public function ftp_mgr($action, $login = "", $password = "", $home_path = "")
  723. {
  724. $params_array = $this->encrypt_params($action, $login, $password, $home_path);
  725. $this->add_enc_chk($params_array);
  726. $request = xmlrpc_encode_request("ftp_mgr", $params_array);
  727. $status = $this->sendRequest($request);
  728. @list($retval,$data_tmp) = @explode(";",$status);
  729. $data = '';
  730. if ( $retval > 0 )
  731. {
  732. $lines = explode('\n',$data_tmp);
  733. foreach ($lines as $line)
  734. {
  735. $decoded_line = base64_decode($line);
  736. if(!preg_match("/^[\s|\t]*$/", $decoded_line))
  737. $data .= "$decoded_line\n";
  738. }
  739. return $data;
  740. }
  741. return 0;
  742. }
  743. public function compress_files($files,$destination,$archive_name,$archive_type)
  744. {
  745. $params_array = $this->encrypt_params($files,$destination,$archive_name,$archive_type);
  746. $this->add_enc_chk($params_array);
  747. $request = xmlrpc_encode_request("compress_files",$params_array);
  748. return $this->sendRequest($request);
  749. }
  750. public function stop_fastdl()
  751. {
  752. $args = NULL;
  753. $this->add_enc_chk($args);
  754. $request = xmlrpc_encode_request("stop_fastdl",$args);
  755. return $this->sendRequest($request);
  756. }
  757. public function start_fastdl()
  758. {
  759. $args = NULL;
  760. $this->add_enc_chk($args);
  761. $request = xmlrpc_encode_request("start_fastdl",$args);
  762. return $this->sendRequest($request);
  763. }
  764. public function restart_fastdl()
  765. {
  766. $args = NULL;
  767. $this->add_enc_chk($args);
  768. $request = xmlrpc_encode_request("restart_fastdl",$args);
  769. return $this->sendRequest($request);
  770. }
  771. public function fastdl_status()
  772. {
  773. $args = NULL;
  774. $this->add_enc_chk($args);
  775. $request = xmlrpc_encode_request("fastdl_status",$args);
  776. $response = $this->sendRequest($request);
  777. if($response === -1 or $response === 0)
  778. return -1;
  779. return 1;
  780. }
  781. public function fastdl_get_aliases()
  782. {
  783. $args = NULL;
  784. $this->add_enc_chk($args);
  785. $request = xmlrpc_encode_request("fastdl_get_aliases",$args);
  786. $response = $this->sendRequest($request);
  787. if(!is_array($response) or count($response) == 0)
  788. return -1;
  789. return $response;
  790. }
  791. public function fastdl_add_alias($alias,$home,$match_file_extension,$match_client_ip)
  792. {
  793. $params_array = $this->encrypt_params($alias,$home,$match_file_extension,$match_client_ip);
  794. $this->add_enc_chk($params_array);
  795. $request = xmlrpc_encode_request("fastdl_add_alias",$params_array);
  796. return $this->sendRequest($request);
  797. }
  798. public function fastdl_del_alias($aliases)
  799. {
  800. if(is_array($aliases))
  801. {
  802. $params_array = array();
  803. foreach($aliases as $alias)
  804. {
  805. $params_array[] = $this->encryptParam($alias);
  806. }
  807. }
  808. else
  809. $params_array = array(0 => $this->encryptParam($aliases));
  810. $this->add_enc_chk($params_array);
  811. $request = xmlrpc_encode_request("fastdl_del_alias",$params_array);
  812. return $this->sendRequest($request);
  813. }
  814. public function fastdl_get_info()
  815. {
  816. $args = NULL;
  817. $this->add_enc_chk($args);
  818. $request = xmlrpc_encode_request("fastdl_get_info",$args);
  819. $response = $this->sendRequest($request);
  820. if($response === -1 or $response == 0)
  821. return -1;
  822. return $response;
  823. }
  824. public function fastdl_create_config($fd_address, $fd_port, $listing, $autostart_on_agent_startup)
  825. {
  826. $params_array = $this->encrypt_params($fd_address, $fd_port, $listing, $autostart_on_agent_startup);
  827. $this->add_enc_chk($params_array);
  828. $request = xmlrpc_encode_request("fastdl_create_config",$params_array);
  829. return $this->sendRequest($request);
  830. }
  831. public function agent_restart()
  832. {
  833. $args = $this->encryptParam('restart');
  834. $this->add_enc_chk($args);
  835. $request = xmlrpc_encode_request("agent_restart", $args);
  836. $response = $this->sendRequest($request);
  837. if($response === -1)
  838. return -1;
  839. return 1;
  840. }
  841. public function scheduler_list_tasks()
  842. {
  843. $args = NULL;
  844. $this->add_enc_chk($args);
  845. $request = xmlrpc_encode_request("scheduler_list_tasks", $args);
  846. $response = $this->sendRequest($request);
  847. if($response === -1 or $response == 0)
  848. return -1;
  849. else
  850. {
  851. $data = array();
  852. foreach ($response as $id => $task)
  853. {
  854. $task = trim(base64_decode($task));
  855. $data[$id] = $task;
  856. }
  857. ksort($data);
  858. return $data;
  859. }
  860. }
  861. public function scheduler_del_task($id)
  862. {
  863. $args = $this->encryptParam($id);
  864. $this->add_enc_chk($args);
  865. $request = xmlrpc_encode_request("scheduler_del_task",$args);
  866. return $this->sendRequest($request);
  867. }
  868. public function scheduler_add_task($job)
  869. {
  870. $args = $this->encryptParam($job);
  871. $this->add_enc_chk($args);
  872. $request = xmlrpc_encode_request("scheduler_add_task",$args);
  873. return $this->sendRequest($request);
  874. }
  875. public function scheduler_edit_task($job_id, $job)
  876. {
  877. $params_array = $this->encrypt_params($job_id, $job);
  878. $this->add_enc_chk($params_array);
  879. $request = xmlrpc_encode_request("scheduler_edit_task",$params_array);
  880. return $this->sendRequest($request);
  881. }
  882. public function remote_get_file_part($file, $offset, &$data)
  883. {
  884. $params_array = $this->encrypt_params($file, $offset);
  885. $this->add_enc_chk($params_array);
  886. $request = xmlrpc_encode_request("get_file_part",$params_array);
  887. $response = $this->sendRequest($request);
  888. if ( $response === NULL )
  889. return -1;
  890. if ( is_array($response) && xmlrpc_is_fault($response))
  891. return -1;
  892. if ( $response === -1 )
  893. return -1;
  894. list($cur_offset,$data_tmp) = explode(";",$response);
  895. $data = base64_decode($data_tmp);
  896. return $cur_offset;
  897. }
  898. public function shell_action($action, $arguments)
  899. {
  900. $params_array = $this->encrypt_params($action, $arguments);
  901. $this->add_enc_chk($params_array);
  902. $request = xmlrpc_encode_request("shell_action", $params_array);
  903. $response = $this->sendRequest($request);
  904. if (is_array($response) && xmlrpc_is_fault($response))
  905. return NULL;
  906. $data = NULL;
  907. if (is_array($response) and !empty($response))
  908. {
  909. $data = array();
  910. foreach ($response as $key => $value)
  911. {
  912. $data[$key] = base64_decode($value);
  913. }
  914. return $data;
  915. }
  916. @list($retval,$data_tmp) = @explode(";",$response);
  917. if ( $retval > 0 )
  918. {
  919. $lines = explode('\n',$data_tmp);
  920. foreach ($lines as $line)
  921. {
  922. $data .= base64_decode($line);
  923. }
  924. }
  925. return $data;
  926. }
  927. public function stop_update($home_id)
  928. {
  929. $args = $this->encryptParam($home_id);
  930. $this->add_enc_chk($args);
  931. $request = xmlrpc_encode_request("stop_update", $args);
  932. $response = $this->sendRequest($request);
  933. if ($response === NULL)
  934. return -1;
  935. if (is_array($response) && xmlrpc_is_fault($response))
  936. return -1;
  937. if ($response === 1)
  938. return -1;
  939. return 1;
  940. }
  941. public function remote_query($protocol, $game_type, $ip, $c_port, $q_port, $s_port)
  942. {
  943. $params_array = $this->encrypt_params($protocol, $game_type, $ip, $c_port, $q_port, $s_port);
  944. $this->add_enc_chk($params_array);
  945. $request = xmlrpc_encode_request("remote_query", $params_array);
  946. $response = $this->sendRequest($request);
  947. if (is_array($response) && xmlrpc_is_fault($response))
  948. return NULL;
  949. if ($response === -1 or $response === 0)
  950. return NULL;
  951. return base64_decode($response);
  952. }
  953. }
  954. ?>