lib_remote.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. }
  955. ?>