lib_remote.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) Copyright (C) 2008 - 2013 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. function __construct($host,$port,$encryption_key,$timeout = 2)
  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. 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. @list($retval,$data_tmp) = @explode(";",$response);
  151. // We get log only with positive values.
  152. if ( $retval > 0 )
  153. {
  154. $lines = explode('\n',$data_tmp);
  155. foreach ($lines as $line)
  156. {
  157. $data .= base64_decode($line)."\n";
  158. }
  159. }
  160. return $retval;
  161. }
  162. /// \brief Stops remote server.
  163. /// \return 1 On success.
  164. /// \return 0 When server offline / could not be connected.
  165. /// \return -1 When error occurred
  166. public function remote_stop_server($home_id, $server_ip,
  167. $server_port, $control_protocol, $control_password, $control_type, $home_path)
  168. {
  169. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  170. $control_protocol,$control_password,$control_type,$home_path);
  171. $this->add_enc_chk($params_array);
  172. $request = xmlrpc_encode_request("stop_server", $params_array);
  173. $status = $this->sendRequest($request);
  174. // Error occurred on the remote end.
  175. if( $status === 1 )
  176. return -1;
  177. // Server successfully stopped.
  178. else if ( $status == 0 )
  179. return 1;
  180. // Connection problems.
  181. else
  182. return 0;
  183. }
  184. /// \brief Send a RCON command.
  185. /// \return 1 On success.
  186. public function remote_send_rcon_command($home_id, $server_ip,
  187. $server_port, $control_protocol, $control_password, $control_type, $rconCommand,&$data)
  188. {
  189. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  190. $control_protocol,$control_password,$control_type, $rconCommand);
  191. $this->add_enc_chk($params_array);
  192. $request = xmlrpc_encode_request("send_rcon_command", $params_array);
  193. $response = $this->sendRequest($request);
  194. @list($retval,$data_tmp) = @explode(";",$response);
  195. if ( $retval > 0 )
  196. {
  197. $lines = explode('\n',$data_tmp);
  198. foreach ($lines as $line)
  199. {
  200. $data .= base64_decode($line);
  201. }
  202. return 1;
  203. }
  204. elseif ( $retval === 0 )
  205. return 0;
  206. elseif ( $retval === -10 )
  207. return -10;
  208. else
  209. return -1;
  210. }
  211. /// \return 1 If success
  212. /// \return 0 If file does not exist.
  213. /// \return -1 In case of connection error
  214. /// \return -2 If failed to read file.
  215. public function remote_readfile($args,&$data)
  216. {
  217. $args = trim($args);
  218. $args = $this->encryptParam($args);
  219. $this->add_enc_chk($args);
  220. $request = xmlrpc_encode_request("readfile", $args);
  221. $response = $this->sendRequest($request);
  222. if ( $response === NULL )
  223. return -1;
  224. if ( is_array($response) && xmlrpc_is_fault($response))
  225. return -1;
  226. @list($retval,$data_tmp) = @explode(";",$response);
  227. $retval = (integer) $retval;
  228. if ( $retval === 0 )
  229. return 0;
  230. else if ( $retval === -1 )
  231. return -2;
  232. $data = base64_decode($data_tmp);
  233. return 1;
  234. }
  235. /// \return 1 If success
  236. /// \return 0 On failure
  237. /// \return -1 If agent could not be connected.
  238. public function remote_writefile($writefile, $content)
  239. {
  240. $content = base64_encode($content);
  241. $params = $this->encrypt_params($writefile,$content);
  242. $this->add_enc_chk($params);
  243. $request = xmlrpc_encode_request("writefile", $params);
  244. $response = $this->sendRequest($request);
  245. if ( $response === 1 )
  246. return 1;
  247. else if ( $response === 0 )
  248. return 0;
  249. else
  250. return -1;
  251. }
  252. /// \return 1 If success
  253. public function remote_rebootnow()
  254. {
  255. // Must have a parameter even if one is not used.
  256. $args = $this->encryptParam("reboot");
  257. $this->add_enc_chk($args);
  258. $request = xmlrpc_encode_request("rebootnow", $args);
  259. $response = $this->sendRequest($request);
  260. if ( $response )
  261. return 1;
  262. }
  263. /// Updates the mod located in the game home with steam.
  264. /// \return 1 If update started successfully
  265. /// \return 0 If error
  266. /// \return -1 In case of connection error.
  267. public function steam($home_id,$game_home,$mod,$exec_folder_path,$exec_path,$precmd,$postcmd)
  268. {
  269. $params = $this->encrypt_params($home_id,$game_home,$mod,$exec_folder_path,$exec_path,$precmd,$postcmd);
  270. $this->add_enc_chk($params);
  271. $request = xmlrpc_encode_request("steam", $params);
  272. $response = $this->sendRequest($request);
  273. if ( $response === 1 )
  274. return 1;
  275. else if ( $response === 0 )
  276. return 0;
  277. else
  278. return -1;
  279. }
  280. /// Updates the mod located in the game home with steamCmd.
  281. /// \return 1 If update started successfully
  282. /// \return 0 If error
  283. /// \return -1 In case of connection error.
  284. 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 = "")
  285. {
  286. $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);
  287. $this->add_enc_chk($params);
  288. $request = xmlrpc_encode_request("steam_cmd", $params);
  289. $response = $this->sendRequest($request);
  290. if ( $response === 1 )
  291. return 1;
  292. else if ( $response === 0 )
  293. return 0;
  294. else
  295. return -1;
  296. }
  297. /// Updates the mod located in the game home with master server.
  298. /// \return 1 If update started successfully
  299. /// \return 0 If error
  300. /// \return -1 In case of connection error.
  301. public function masterServerUpdate($home_id,$home_path,$ms_home_id,$ms_home_path,$exec_folder_path,$exec_path,$precmd,$postcmd)
  302. {
  303. $params = $this->encrypt_params($home_id,$home_path,$ms_home_id,$ms_home_path,$exec_folder_path,$exec_path,$precmd,$postcmd);
  304. $this->add_enc_chk($params);
  305. $request = xmlrpc_encode_request("master_server_update", $params);
  306. $response = $this->sendRequest($request);
  307. if ( $response === 1 )
  308. return 1;
  309. else if ( $response === 0 )
  310. return 0;
  311. else
  312. return -1;
  313. }
  314. /// \brief Checks if the game update is running for the certain gamehome.
  315. /// \return 1 if the update is active
  316. /// \return 0 if the update is not active
  317. /// \return -1 If unable to connect to the remote server.
  318. /// \return -2 In other errors.
  319. public function game_update_active($game_home,$mod)
  320. {
  321. $params = $this->encrypt_params($game_home, $mod);
  322. $this->add_enc_chk($params);
  323. $request = xmlrpc_encode_request("game_update_active", $params);
  324. if(!$response = $this->sendRequest($request) )
  325. return -1;
  326. else if ( $response === 1 )
  327. return 1;
  328. else if ( $response === 0 )
  329. return 0;
  330. // other errors.
  331. else
  332. return -2;
  333. }
  334. /// \return -1 If could not connect to the remote host.
  335. /// \return -3 In case of unknown error
  336. /// \todo This function is not complete. Also the agent side requires work.
  337. public function start_file_download($url, $dest, $filename, $action = "", $post_script = "" )
  338. {
  339. $params_array = $this->encrypt_params($url,$dest,$filename,$action,$post_script);
  340. $this->add_enc_chk($params_array);
  341. $request = xmlrpc_encode_request("start_file_download",$params_array);
  342. $response = $this->sendRequest($request);
  343. if( !$response )
  344. return -1;
  345. if (is_array($response) && xmlrpc_is_fault($response))
  346. return -3;
  347. return $response;
  348. }
  349. public function is_file_download_in_progress($pid)
  350. {
  351. $args = $this->encryptParam($pid);
  352. $this->add_enc_chk($args);
  353. $request = xmlrpc_encode_request("is_file_download_in_progress", $args);
  354. return $this->sendRequest($request);
  355. }
  356. public function uncompress_file($file_location, $destination)
  357. {
  358. $params_array = $this->encrypt_params($file_location,$destination);
  359. $this->add_enc_chk($params_array);
  360. $request = xmlrpc_encode_request("uncompress_file",$params_array);
  361. return $this->sendRequest($request);
  362. }
  363. /// \return -1 If could not connect to the remote host.
  364. /// \return -3 In case of unknown error
  365. /// \todo This function is not complete. Also the agent side requires work.
  366. public function start_rsync_install($home_id,$home_path,$url,$exec_folder_path,$exec_path,$precmd,$postcmd,$filesToLock="")
  367. {
  368. $params_array = $this->encrypt_params($home_id,$home_path,$url,$exec_folder_path,$exec_path,$precmd,$postcmd,$filesToLock);
  369. $this->add_enc_chk($params_array);
  370. $request = xmlrpc_encode_request("start_rsync_install",$params_array);
  371. $response = $this->sendRequest($request);
  372. if ( $response === 1 )
  373. return 1;
  374. else if ( $response === 0 )
  375. return 0;
  376. else
  377. return -1;
  378. }
  379. public function rsync_progress($home)
  380. {
  381. $args = $this->encryptParam($home);
  382. $this->add_enc_chk($args);
  383. $request = xmlrpc_encode_request("rsync_progress",$args);
  384. $response = $this->sendRequest($request);
  385. if( !$response )
  386. return -1;
  387. #if (is_array($response) && xmlrpc_is_fault($response))
  388. # return -3;
  389. return $response;
  390. }
  391. /// \return array of files in directory, when request success.
  392. /// \return -1 If unable to connect to the remote server.
  393. /// \return -2 In case directory was not accessible.
  394. /// \return -3 Any other error.
  395. public function remote_dirlist($args)
  396. {
  397. $args = $this->encryptParam($args);
  398. $this->add_enc_chk($args);
  399. $request = xmlrpc_encode_request("dirlist",$args);
  400. if( !$response = $this->sendRequest($request))
  401. return -1;
  402. if (is_array($response) && xmlrpc_is_fault($response))
  403. return -3;
  404. if( $response < 0 )
  405. return -2;
  406. return explode(";", $response);
  407. }
  408. /// \return array of files and file info (size owner etc) in directory, when request success.
  409. /// \return -1 If unable to connect to the remote server.
  410. /// \return -2 In case directory was not accessible.
  411. /// \return -3 Any other error.
  412. public function remote_dirlistfm($args)
  413. {
  414. $args = $this->encryptParam($args);
  415. $this->add_enc_chk($args);
  416. $request = xmlrpc_encode_request("dirlistfm", $args);
  417. $response = $this->sendRequest($request);
  418. if ( $response === NULL )
  419. return -1;
  420. if (is_array($response) && xmlrpc_is_fault($response))
  421. return -3;
  422. if( $response < 0 )
  423. return -2;
  424. if ( $response == 1 )
  425. return array();
  426. function base_64_decode_filename(&$item, $key){
  427. if( $key == 'filename' ) $item = base64_decode($item);
  428. }
  429. array_walk_recursive($response, 'base_64_decode_filename');
  430. return $response;
  431. }
  432. /// \returns the number of CPUs on the server
  433. /// \returns -1 If the server cannot be reached.
  434. public function cpu_count()
  435. {
  436. $args = NULL;
  437. $this->add_enc_chk($args);
  438. $request = xmlrpc_encode_request("cpu_count", $args);
  439. $status = $this->sendRequest($request);
  440. if ( empty($status) )
  441. {
  442. return -1;
  443. }
  444. return $status;
  445. }
  446. public function renice_process($home_id, $nice)
  447. {
  448. $params_array = $this->encrypt_params($home_id, $nice);
  449. $this->add_enc_chk($params_array);
  450. $request = xmlrpc_encode_request("renice_process",$params_array);
  451. return $this->sendRequest($response);
  452. }
  453. /// \return 1 If everything ok
  454. /// \return -1 If connection could not be established.
  455. /// \return -2 In other errors.
  456. /// \todo Other return values?
  457. public function universal_start($home_id, $game_home, $game_binary, $run_dir, $startup_cmd,
  458. $server_port, $server_ip, $cpu, $nice, $preStart = "", $envVars = "")
  459. {
  460. $params_array = $this->encrypt_params($home_id, $game_home, $game_binary,
  461. $run_dir, $startup_cmd, $server_port, $server_ip, $cpu, $nice, $preStart, $envVars);
  462. $this->add_enc_chk($params_array);
  463. $request = xmlrpc_encode_request("universal_start", $params_array);
  464. $response = $this->sendRequest($request);
  465. if($response === NULL)
  466. return -1;
  467. if (is_array($response) && xmlrpc_is_fault($response))
  468. return -2;
  469. return $response;
  470. }
  471. public function lock_additional_home_files($game_home, $filesToLockUnlock, $action)
  472. {
  473. $params_array = $this->encrypt_params($game_home, $filesToLockUnlock, $action);
  474. $this->add_enc_chk($params_array);
  475. $request = xmlrpc_encode_request("lock_additional_files", $params_array);
  476. $response = $this->sendRequest($request);
  477. if($response === NULL)
  478. return -1;
  479. if (is_array($response) && xmlrpc_is_fault($response))
  480. return -2;
  481. return $response;
  482. }
  483. /// \returns the os of the remote host.
  484. public function what_os()
  485. {
  486. $args = NULL;
  487. $this->add_enc_chk($args);
  488. $request = xmlrpc_encode_request("what_os", $args);
  489. $status = $this->sendRequest($request);
  490. return "$status";
  491. }
  492. /// \return Available IP addresses of the remote host.
  493. /// \return empty array if no ip's are found.
  494. /// \return array containing the ip's on success.
  495. public function discover_ips()
  496. {
  497. $args = "chk";
  498. $args = $this->encryptParam($args);
  499. $this->add_enc_chk($args);
  500. $request = xmlrpc_encode_request("discover_ips", $args);
  501. $status = $this->sendRequest($request);
  502. if ( $status == 0 )
  503. return array();
  504. return explode(",",$status);
  505. }
  506. /// \brief Checks if the server is running.
  507. /// \return 1 If is
  508. /// \return 0 If is not
  509. /// \return -1 If agent could not be reached.
  510. public function is_screen_running($screen_type,$home_id)
  511. {
  512. $params = $this->encrypt_params($screen_type,$home_id);
  513. $this->add_enc_chk($params);
  514. $request = xmlrpc_encode_request("is_screen_running", $params);
  515. $status = $this->sendRequest($request);
  516. if ( $status === 1 )
  517. return 1;
  518. else if ( $status === 0 )
  519. return 0;
  520. else
  521. return -1;
  522. }
  523. public function mon_stats()
  524. {
  525. $args = $this->encrypt_params("mon_stats");
  526. $this->add_enc_chk($args);
  527. $request = xmlrpc_encode_request("mon_stats", $args);
  528. $response = $this->sendRequest($request);
  529. @list($retval,$data_tmp) = @explode(";",$response);
  530. $data = NULL;
  531. if ( $retval > 0 )
  532. {
  533. $lines = explode('\n',$data_tmp);
  534. foreach ($lines as $line)
  535. {
  536. $data .= base64_decode($line);
  537. }
  538. }
  539. return $data;
  540. }
  541. /// \brief copies a game home on the filesystem.
  542. /// \return 1 On success.
  543. /// \return 0 When server offline / could not be connected.
  544. /// \return -1 When error occurred
  545. /// Usually a -1 happens because of a connection timeout during the copy. This is expected
  546. public function clone_home($source_home, $dest_home, $owner)
  547. {
  548. $params_array = $this->encrypt_params($source_home, $dest_home, $owner);
  549. $this->add_enc_chk($params_array);
  550. $request = xmlrpc_encode_request("clone_home", $params_array);
  551. $status = $this->sendRequest($request);
  552. // Copy was successful.
  553. if( $status === 1 )
  554. return 1;
  555. // Copy failed.
  556. else if ( $status === 0 )
  557. return 0;
  558. // Connection problems.
  559. else
  560. return -1;
  561. }
  562. /// \brief removes a game home from the filesystem.
  563. /// \return 1 On success.
  564. /// \return 0 When server offline / could not be connected.
  565. /// \return -1 When error occurred
  566. public function remove_home($game_home_del)
  567. {
  568. $args = $this->encryptParam($game_home_del);
  569. $this->add_enc_chk($args);
  570. $request = xmlrpc_encode_request("remove_home", $args);
  571. $status = $this->sendRequest($request);
  572. // Delete was successful.
  573. if( $status === 1 )
  574. return 1;
  575. // Delete failed.
  576. else if ( $status === 0 )
  577. return 0;
  578. // Connection problems.
  579. else
  580. return -1;
  581. }
  582. public function remote_restart_server($home_id,$server_ip,$server_port,
  583. $control_protocol,$control_password,$control_type,
  584. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart = "", $envVars = "")
  585. {
  586. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  587. $control_protocol,$control_password,$control_type,
  588. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart,$envVars);
  589. $this->add_enc_chk($params_array);
  590. $request = xmlrpc_encode_request("restart_server", $params_array);
  591. $status = $this->sendRequest($request);
  592. // Error server cant stop.
  593. if( $status === -2 )
  594. return -2;
  595. // Error server cant start.
  596. else if ( $status === -1 )
  597. return -1;
  598. //// OK successfully restarted.
  599. else if ( $status === 1 )
  600. return 1;
  601. // Connection problems.
  602. else
  603. return 0;
  604. }
  605. public function sudo_exec($command)
  606. {
  607. $args = $this->encryptParam($command);
  608. $this->add_enc_chk($args);
  609. $request = xmlrpc_encode_request("sudo_exec", $args);
  610. $status = $this->sendRequest($request);
  611. @list($retval,$data_tmp) = @explode(";",$status);
  612. $data = NULL;
  613. if ( $retval > 0 )
  614. {
  615. $lines = explode('\n',$data_tmp);
  616. foreach ($lines as $line)
  617. {
  618. $data .= base64_decode($line)."\n";
  619. }
  620. return $data;
  621. }
  622. return 0;
  623. }
  624. public function exec($command)
  625. {
  626. $args = $this->encryptParam($command);
  627. $this->add_enc_chk($args);
  628. $request = xmlrpc_encode_request("exec", $args);
  629. $response = $this->sendRequest($request);
  630. @list($retval,$data_tmp) = @explode(";",$response);
  631. $data = NULL;
  632. if ( $retval > 0 )
  633. {
  634. $lines = explode('\n',$data_tmp);
  635. foreach ($lines as $line)
  636. {
  637. $data .= base64_decode($line);
  638. }
  639. }
  640. return $data;
  641. }
  642. public function secure_path($action, $path)
  643. {
  644. $params_array = $this->encrypt_params($action, $path);
  645. $this->add_enc_chk($params_array);
  646. $request = xmlrpc_encode_request("secure_path", $params_array);
  647. $status = $this->sendRequest($request);
  648. @list($retval,$data_tmp) = @explode(";",$status);
  649. $data = NULL;
  650. if ( $retval > 0 )
  651. {
  652. $lines = explode('\n',$data_tmp);
  653. foreach ($lines as $line)
  654. {
  655. $data .= base64_decode($line);
  656. }
  657. }
  658. return $data;
  659. }
  660. public function get_chattr($path)
  661. {
  662. $args = $this->encryptParam($path);
  663. $this->add_enc_chk($args);
  664. $request = xmlrpc_encode_request("get_chattr", $args);
  665. $status = $this->sendRequest($request);
  666. @list($retval,$data_tmp) = @explode(";",$status);
  667. $data = NULL;
  668. if ( $retval > 0 )
  669. {
  670. $lines = explode('\n',$data_tmp);
  671. foreach ($lines as $line)
  672. {
  673. $data .= base64_decode($line);
  674. }
  675. }
  676. return $data;
  677. }
  678. public function ftp_mgr($action, $login = "", $password = "", $home_path = "")
  679. {
  680. $params_array = $this->encrypt_params($action, $login, $password, $home_path);
  681. $this->add_enc_chk($params_array);
  682. $request = xmlrpc_encode_request("ftp_mgr", $params_array);
  683. $status = $this->sendRequest($request);
  684. @list($retval,$data_tmp) = @explode(";",$status);
  685. $data = '';
  686. if ( $retval > 0 )
  687. {
  688. $lines = explode('\n',$data_tmp);
  689. foreach ($lines as $line)
  690. {
  691. $decoded_line = base64_decode($line);
  692. if(!preg_match("/^[\s|\t]*$/", $decoded_line))
  693. $data .= "$decoded_line\n";
  694. }
  695. return $data;
  696. }
  697. return 0;
  698. }
  699. public function compress_files($files,$destination,$archive_name,$archive_type)
  700. {
  701. $params_array = $this->encrypt_params($files,$destination,$archive_name,$archive_type);
  702. $this->add_enc_chk($params_array);
  703. $request = xmlrpc_encode_request("compress_files",$params_array);
  704. return $this->sendRequest($request);
  705. }
  706. public function stop_fastdl()
  707. {
  708. $args = NULL;
  709. $this->add_enc_chk($args);
  710. $request = xmlrpc_encode_request("stop_fastdl",$args);
  711. return $this->sendRequest($request);
  712. }
  713. public function start_fastdl()
  714. {
  715. $args = NULL;
  716. $this->add_enc_chk($args);
  717. $request = xmlrpc_encode_request("start_fastdl",$args);
  718. return $this->sendRequest($request);
  719. }
  720. public function restart_fastdl()
  721. {
  722. $args = NULL;
  723. $this->add_enc_chk($args);
  724. $request = xmlrpc_encode_request("restart_fastdl",$args);
  725. return $this->sendRequest($request);
  726. }
  727. public function fastdl_status()
  728. {
  729. $args = NULL;
  730. $this->add_enc_chk($args);
  731. $request = xmlrpc_encode_request("fastdl_status",$args);
  732. $response = $this->sendRequest($request);
  733. if($response === -1 or $response === 0)
  734. return -1;
  735. return 1;
  736. }
  737. public function fastdl_get_aliases()
  738. {
  739. $args = NULL;
  740. $this->add_enc_chk($args);
  741. $request = xmlrpc_encode_request("fastdl_get_aliases",$args);
  742. $response = $this->sendRequest($request);
  743. if(!is_array($response) or count($response) == 0)
  744. return -1;
  745. return $response;
  746. }
  747. public function fastdl_add_alias($alias,$home,$match_file_extension,$match_client_ip)
  748. {
  749. $params_array = $this->encrypt_params($alias,$home,$match_file_extension,$match_client_ip);
  750. $this->add_enc_chk($params_array);
  751. $request = xmlrpc_encode_request("fastdl_add_alias",$params_array);
  752. return $this->sendRequest($request);
  753. }
  754. public function fastdl_del_alias($aliases)
  755. {
  756. if(is_array($aliases))
  757. {
  758. $params_array = array();
  759. foreach($aliases as $alias)
  760. {
  761. $params_array[] = $this->encryptParam($alias);
  762. }
  763. }
  764. else
  765. $params_array = array(0 => $this->encryptParam($aliases));
  766. $this->add_enc_chk($params_array);
  767. $request = xmlrpc_encode_request("fastdl_del_alias",$params_array);
  768. return $this->sendRequest($request);
  769. }
  770. public function fastdl_get_info()
  771. {
  772. $args = NULL;
  773. $this->add_enc_chk($args);
  774. $request = xmlrpc_encode_request("fastdl_get_info",$args);
  775. $response = $this->sendRequest($request);
  776. if($response === -1 or $response == 0)
  777. return -1;
  778. return $response;
  779. }
  780. public function fastdl_create_config($fd_address, $fd_port, $listing, $autostart_on_agent_startup)
  781. {
  782. $params_array = $this->encrypt_params($fd_address, $fd_port, $listing, $autostart_on_agent_startup);
  783. $this->add_enc_chk($params_array);
  784. $request = xmlrpc_encode_request("fastdl_create_config",$params_array);
  785. return $this->sendRequest($request);
  786. }
  787. public function agent_restart()
  788. {
  789. $args = $this->encryptParam('restart');
  790. $this->add_enc_chk($args);
  791. $request = xmlrpc_encode_request("agent_restart", $args);
  792. $response = $this->sendRequest($request);
  793. if($response === -1)
  794. return -1;
  795. return 1;
  796. }
  797. public function scheduler_list_tasks()
  798. {
  799. $args = NULL;
  800. $this->add_enc_chk($args);
  801. $request = xmlrpc_encode_request("scheduler_list_tasks", $args);
  802. $response = $this->sendRequest($request);
  803. if($response === -1 or $response == 0)
  804. return -1;
  805. else
  806. {
  807. $data = array();
  808. foreach ($response as $id => $task)
  809. {
  810. $task = trim(base64_decode($task));
  811. $data[$id] = $task;
  812. }
  813. ksort($data);
  814. return $data;
  815. }
  816. }
  817. public function scheduler_del_task($id)
  818. {
  819. $args = $this->encryptParam($id);
  820. $this->add_enc_chk($args);
  821. $request = xmlrpc_encode_request("scheduler_del_task",$args);
  822. return $this->sendRequest($request);
  823. }
  824. public function scheduler_add_task($job)
  825. {
  826. $args = $this->encryptParam($job);
  827. $this->add_enc_chk($args);
  828. $request = xmlrpc_encode_request("scheduler_add_task",$args);
  829. return $this->sendRequest($request);
  830. }
  831. public function scheduler_edit_task($job_id, $job)
  832. {
  833. $params_array = $this->encrypt_params($job_id, $job);
  834. $this->add_enc_chk($params_array);
  835. $request = xmlrpc_encode_request("scheduler_edit_task",$params_array);
  836. return $this->sendRequest($request);
  837. }
  838. public function remote_get_file_part($file, $offset, &$data)
  839. {
  840. $params_array = $this->encrypt_params($file, $offset);
  841. $this->add_enc_chk($params_array);
  842. $request = xmlrpc_encode_request("get_file_part",$params_array);
  843. $response = $this->sendRequest($request);
  844. if ( $response === NULL )
  845. return -1;
  846. if ( is_array($response) && xmlrpc_is_fault($response))
  847. return -1;
  848. if ( $response === -1 )
  849. return -1;
  850. list($cur_offset,$data_tmp) = explode(";",$response);
  851. $data = base64_decode($data_tmp);
  852. return $cur_offset;
  853. }
  854. public function shell_action($action, $arguments)
  855. {
  856. $params_array = $this->encrypt_params($action, $arguments);
  857. $this->add_enc_chk($params_array);
  858. $request = xmlrpc_encode_request("shell_action", $params_array);
  859. $response = $this->sendRequest($request);
  860. if (is_array($response) && xmlrpc_is_fault($response))
  861. return NULL;
  862. $data = NULL;
  863. if (is_array($response) and !empty($response))
  864. {
  865. $data = array();
  866. foreach ($response as $key => $value)
  867. {
  868. $data[$key] = base64_decode($value);
  869. }
  870. return $data;
  871. }
  872. @list($retval,$data_tmp) = @explode(";",$response);
  873. if ( $retval > 0 )
  874. {
  875. $lines = explode('\n',$data_tmp);
  876. foreach ($lines as $line)
  877. {
  878. $data .= base64_decode($line);
  879. }
  880. }
  881. return $data;
  882. }
  883. public function stop_update($home_id)
  884. {
  885. $args = $this->encryptParam($home_id);
  886. $this->add_enc_chk($args);
  887. $request = xmlrpc_encode_request("stop_update", $args);
  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. return 1;
  896. }
  897. public function remote_query($protocol, $game_type, $ip, $c_port, $q_port, $s_port)
  898. {
  899. $params_array = $this->encrypt_params($protocol, $game_type, $ip, $c_port, $q_port, $s_port);
  900. $this->add_enc_chk($params_array);
  901. $request = xmlrpc_encode_request("remote_query", $params_array);
  902. $response = $this->sendRequest($request);
  903. if (is_array($response) && xmlrpc_is_fault($response))
  904. return NULL;
  905. if ($response === -1 or $response === 0)
  906. return NULL;
  907. return base64_decode($response);
  908. }
  909. }
  910. ?>