lib_remote.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. 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. @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. array_walk_recursive($response, function (&$item, $key) {
  427. if ($key == 'filename')$item = base64_decode($item);
  428. });
  429. return $response;
  430. }
  431. /// \returns the number of CPUs on the server
  432. /// \returns -1 If the server cannot be reached.
  433. public function cpu_count()
  434. {
  435. $args = NULL;
  436. $this->add_enc_chk($args);
  437. $request = xmlrpc_encode_request("cpu_count", $args);
  438. $status = $this->sendRequest($request);
  439. if ( empty($status) )
  440. {
  441. return -1;
  442. }
  443. return $status;
  444. }
  445. public function renice_process($home_id, $nice)
  446. {
  447. $params_array = $this->encrypt_params($home_id, $nice);
  448. $this->add_enc_chk($params_array);
  449. $request = xmlrpc_encode_request("renice_process",$params_array);
  450. return $this->sendRequest($response);
  451. }
  452. /// \return 1 If everything ok
  453. /// \return -1 If connection could not be established.
  454. /// \return -2 In other errors.
  455. /// \todo Other return values?
  456. public function universal_start($home_id, $game_home, $game_binary, $run_dir, $startup_cmd,
  457. $server_port, $server_ip, $cpu, $nice, $preStart = "", $envVars = "")
  458. {
  459. $params_array = $this->encrypt_params($home_id, $game_home, $game_binary,
  460. $run_dir, $startup_cmd, $server_port, $server_ip, $cpu, $nice, $preStart, $envVars);
  461. $this->add_enc_chk($params_array);
  462. $request = xmlrpc_encode_request("universal_start", $params_array);
  463. $response = $this->sendRequest($request);
  464. if($response === NULL)
  465. return -1;
  466. if (is_array($response) && xmlrpc_is_fault($response))
  467. return -2;
  468. return $response;
  469. }
  470. public function lock_additional_home_files($game_home, $filesToLockUnlock, $action)
  471. {
  472. $params_array = $this->encrypt_params($game_home, $filesToLockUnlock, $action);
  473. $this->add_enc_chk($params_array);
  474. $request = xmlrpc_encode_request("lock_additional_files", $params_array);
  475. $response = $this->sendRequest($request);
  476. if($response === NULL)
  477. return -1;
  478. if (is_array($response) && xmlrpc_is_fault($response))
  479. return -2;
  480. return $response;
  481. }
  482. /// \returns the os of the remote host.
  483. public function what_os()
  484. {
  485. $args = NULL;
  486. $this->add_enc_chk($args);
  487. $request = xmlrpc_encode_request("what_os", $args);
  488. $status = $this->sendRequest($request);
  489. return "$status";
  490. }
  491. /// \return Available IP addresses of the remote host.
  492. /// \return empty array if no ip's are found.
  493. /// \return array containing the ip's on success.
  494. public function discover_ips()
  495. {
  496. $args = "chk";
  497. $args = $this->encryptParam($args);
  498. $this->add_enc_chk($args);
  499. $request = xmlrpc_encode_request("discover_ips", $args);
  500. $status = $this->sendRequest($request);
  501. if ( $status == 0 )
  502. return array();
  503. return explode(",",$status);
  504. }
  505. /// \brief Checks if the server is running.
  506. /// \return 1 If is
  507. /// \return 0 If is not
  508. /// \return -1 If agent could not be reached.
  509. public function is_screen_running($screen_type,$home_id)
  510. {
  511. $params = $this->encrypt_params($screen_type,$home_id);
  512. $this->add_enc_chk($params);
  513. $request = xmlrpc_encode_request("is_screen_running", $params);
  514. $status = $this->sendRequest($request);
  515. if ( $status === 1 )
  516. return 1;
  517. else if ( $status === 0 )
  518. return 0;
  519. else
  520. return -1;
  521. }
  522. public function mon_stats()
  523. {
  524. $args = $this->encrypt_params("mon_stats");
  525. $this->add_enc_chk($args);
  526. $request = xmlrpc_encode_request("mon_stats", $args);
  527. $response = $this->sendRequest($request);
  528. @list($retval,$data_tmp) = @explode(";",$response);
  529. $data = NULL;
  530. if ( $retval > 0 )
  531. {
  532. $lines = explode('\n',$data_tmp);
  533. foreach ($lines as $line)
  534. {
  535. $data .= base64_decode($line);
  536. }
  537. }
  538. return $data;
  539. }
  540. /// \brief copies a game home on the filesystem.
  541. /// \return 1 On success.
  542. /// \return 0 When server offline / could not be connected.
  543. /// \return -1 When error occurred
  544. /// Usually a -1 happens because of a connection timeout during the copy. This is expected
  545. public function clone_home($source_home, $dest_home, $owner)
  546. {
  547. $params_array = $this->encrypt_params($source_home, $dest_home, $owner);
  548. $this->add_enc_chk($params_array);
  549. $request = xmlrpc_encode_request("clone_home", $params_array);
  550. $status = $this->sendRequest($request);
  551. // Copy was successful.
  552. if( $status === 1 )
  553. return 1;
  554. // Copy failed.
  555. else if ( $status === 0 )
  556. return 0;
  557. // Connection problems.
  558. else
  559. return -1;
  560. }
  561. /// \brief removes a game home from the filesystem.
  562. /// \return 1 On success.
  563. /// \return 0 When server offline / could not be connected.
  564. /// \return -1 When error occurred
  565. public function remove_home($game_home_del)
  566. {
  567. $args = $this->encryptParam($game_home_del);
  568. $this->add_enc_chk($args);
  569. $request = xmlrpc_encode_request("remove_home", $args);
  570. $status = $this->sendRequest($request);
  571. // Delete was successful.
  572. if( $status === 1 )
  573. return 1;
  574. // Delete failed.
  575. else if ( $status === 0 )
  576. return 0;
  577. // Connection problems.
  578. else
  579. return -1;
  580. }
  581. public function remote_restart_server($home_id,$server_ip,$server_port,
  582. $control_protocol,$control_password,$control_type,
  583. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart = "", $envVars = "")
  584. {
  585. $params_array = $this->encrypt_params($home_id,$server_ip,$server_port,
  586. $control_protocol,$control_password,$control_type,
  587. $home_path,$server_exe,$run_dir,$cmd,$cpu,$nice,$preStart,$envVars);
  588. $this->add_enc_chk($params_array);
  589. $request = xmlrpc_encode_request("restart_server", $params_array);
  590. $status = $this->sendRequest($request);
  591. // Error server cant stop.
  592. if( $status === -2 )
  593. return -2;
  594. // Error server cant start.
  595. else if ( $status === -1 )
  596. return -1;
  597. //// OK successfully restarted.
  598. else if ( $status === 1 )
  599. return 1;
  600. // Connection problems.
  601. else
  602. return 0;
  603. }
  604. public function sudo_exec($command)
  605. {
  606. $args = $this->encryptParam($command);
  607. $this->add_enc_chk($args);
  608. $request = xmlrpc_encode_request("sudo_exec", $args);
  609. $status = $this->sendRequest($request);
  610. @list($retval,$data_tmp) = @explode(";",$status);
  611. $data = NULL;
  612. if ( $retval > 0 )
  613. {
  614. $lines = explode('\n',$data_tmp);
  615. foreach ($lines as $line)
  616. {
  617. $data .= base64_decode($line)."\n";
  618. }
  619. return $data;
  620. }
  621. return 0;
  622. }
  623. public function exec($command)
  624. {
  625. $args = $this->encryptParam($command);
  626. $this->add_enc_chk($args);
  627. $request = xmlrpc_encode_request("exec", $args);
  628. $response = $this->sendRequest($request);
  629. @list($retval,$data_tmp) = @explode(";",$response);
  630. $data = NULL;
  631. if ( $retval > 0 )
  632. {
  633. $lines = explode('\n',$data_tmp);
  634. foreach ($lines as $line)
  635. {
  636. $data .= base64_decode($line);
  637. }
  638. }
  639. return $data;
  640. }
  641. public function secure_path($action, $path)
  642. {
  643. $params_array = $this->encrypt_params($action, $path);
  644. $this->add_enc_chk($params_array);
  645. $request = xmlrpc_encode_request("secure_path", $params_array);
  646. $status = $this->sendRequest($request);
  647. @list($retval,$data_tmp) = @explode(";",$status);
  648. $data = NULL;
  649. if ( $retval > 0 )
  650. {
  651. $lines = explode('\n',$data_tmp);
  652. foreach ($lines as $line)
  653. {
  654. $data .= base64_decode($line);
  655. }
  656. }
  657. return $data;
  658. }
  659. public function get_chattr($path)
  660. {
  661. $args = $this->encryptParam($path);
  662. $this->add_enc_chk($args);
  663. $request = xmlrpc_encode_request("get_chattr", $args);
  664. $status = $this->sendRequest($request);
  665. @list($retval,$data_tmp) = @explode(";",$status);
  666. $data = NULL;
  667. if ( $retval > 0 )
  668. {
  669. $lines = explode('\n',$data_tmp);
  670. foreach ($lines as $line)
  671. {
  672. $data .= base64_decode($line);
  673. }
  674. }
  675. return $data;
  676. }
  677. public function ftp_mgr($action, $login = "", $password = "", $home_path = "")
  678. {
  679. $params_array = $this->encrypt_params($action, $login, $password, $home_path);
  680. $this->add_enc_chk($params_array);
  681. $request = xmlrpc_encode_request("ftp_mgr", $params_array);
  682. $status = $this->sendRequest($request);
  683. @list($retval,$data_tmp) = @explode(";",$status);
  684. $data = '';
  685. if ( $retval > 0 )
  686. {
  687. $lines = explode('\n',$data_tmp);
  688. foreach ($lines as $line)
  689. {
  690. $decoded_line = base64_decode($line);
  691. if(!preg_match("/^[\s|\t]*$/", $decoded_line))
  692. $data .= "$decoded_line\n";
  693. }
  694. return $data;
  695. }
  696. return 0;
  697. }
  698. public function compress_files($files,$destination,$archive_name,$archive_type)
  699. {
  700. $params_array = $this->encrypt_params($files,$destination,$archive_name,$archive_type);
  701. $this->add_enc_chk($params_array);
  702. $request = xmlrpc_encode_request("compress_files",$params_array);
  703. return $this->sendRequest($request);
  704. }
  705. public function stop_fastdl()
  706. {
  707. $args = NULL;
  708. $this->add_enc_chk($args);
  709. $request = xmlrpc_encode_request("stop_fastdl",$args);
  710. return $this->sendRequest($request);
  711. }
  712. public function start_fastdl()
  713. {
  714. $args = NULL;
  715. $this->add_enc_chk($args);
  716. $request = xmlrpc_encode_request("start_fastdl",$args);
  717. return $this->sendRequest($request);
  718. }
  719. public function restart_fastdl()
  720. {
  721. $args = NULL;
  722. $this->add_enc_chk($args);
  723. $request = xmlrpc_encode_request("restart_fastdl",$args);
  724. return $this->sendRequest($request);
  725. }
  726. public function fastdl_status()
  727. {
  728. $args = NULL;
  729. $this->add_enc_chk($args);
  730. $request = xmlrpc_encode_request("fastdl_status",$args);
  731. $response = $this->sendRequest($request);
  732. if($response === -1 or $response === 0)
  733. return -1;
  734. return 1;
  735. }
  736. public function fastdl_get_aliases()
  737. {
  738. $args = NULL;
  739. $this->add_enc_chk($args);
  740. $request = xmlrpc_encode_request("fastdl_get_aliases",$args);
  741. $response = $this->sendRequest($request);
  742. if(!is_array($response) or count($response) == 0)
  743. return -1;
  744. return $response;
  745. }
  746. public function fastdl_add_alias($alias,$home,$match_file_extension,$match_client_ip)
  747. {
  748. $params_array = $this->encrypt_params($alias,$home,$match_file_extension,$match_client_ip);
  749. $this->add_enc_chk($params_array);
  750. $request = xmlrpc_encode_request("fastdl_add_alias",$params_array);
  751. return $this->sendRequest($request);
  752. }
  753. public function fastdl_del_alias($aliases)
  754. {
  755. if(is_array($aliases))
  756. {
  757. $params_array = array();
  758. foreach($aliases as $alias)
  759. {
  760. $params_array[] = $this->encryptParam($alias);
  761. }
  762. }
  763. else
  764. $params_array = array(0 => $this->encryptParam($aliases));
  765. $this->add_enc_chk($params_array);
  766. $request = xmlrpc_encode_request("fastdl_del_alias",$params_array);
  767. return $this->sendRequest($request);
  768. }
  769. public function fastdl_get_info()
  770. {
  771. $args = NULL;
  772. $this->add_enc_chk($args);
  773. $request = xmlrpc_encode_request("fastdl_get_info",$args);
  774. $response = $this->sendRequest($request);
  775. if($response === -1 or $response == 0)
  776. return -1;
  777. return $response;
  778. }
  779. public function fastdl_create_config($fd_address, $fd_port, $listing, $autostart_on_agent_startup)
  780. {
  781. $params_array = $this->encrypt_params($fd_address, $fd_port, $listing, $autostart_on_agent_startup);
  782. $this->add_enc_chk($params_array);
  783. $request = xmlrpc_encode_request("fastdl_create_config",$params_array);
  784. return $this->sendRequest($request);
  785. }
  786. public function agent_restart()
  787. {
  788. $args = $this->encryptParam('restart');
  789. $this->add_enc_chk($args);
  790. $request = xmlrpc_encode_request("agent_restart", $args);
  791. $response = $this->sendRequest($request);
  792. if($response === -1)
  793. return -1;
  794. return 1;
  795. }
  796. public function scheduler_list_tasks()
  797. {
  798. $args = NULL;
  799. $this->add_enc_chk($args);
  800. $request = xmlrpc_encode_request("scheduler_list_tasks", $args);
  801. $response = $this->sendRequest($request);
  802. if($response === -1 or $response == 0)
  803. return -1;
  804. else
  805. {
  806. $data = array();
  807. foreach ($response as $id => $task)
  808. {
  809. $task = trim(base64_decode($task));
  810. $data[$id] = $task;
  811. }
  812. ksort($data);
  813. return $data;
  814. }
  815. }
  816. public function scheduler_del_task($id)
  817. {
  818. $args = $this->encryptParam($id);
  819. $this->add_enc_chk($args);
  820. $request = xmlrpc_encode_request("scheduler_del_task",$args);
  821. return $this->sendRequest($request);
  822. }
  823. public function scheduler_add_task($job)
  824. {
  825. $args = $this->encryptParam($job);
  826. $this->add_enc_chk($args);
  827. $request = xmlrpc_encode_request("scheduler_add_task",$args);
  828. return $this->sendRequest($request);
  829. }
  830. public function scheduler_edit_task($job_id, $job)
  831. {
  832. $params_array = $this->encrypt_params($job_id, $job);
  833. $this->add_enc_chk($params_array);
  834. $request = xmlrpc_encode_request("scheduler_edit_task",$params_array);
  835. return $this->sendRequest($request);
  836. }
  837. public function remote_get_file_part($file, $offset, &$data)
  838. {
  839. $params_array = $this->encrypt_params($file, $offset);
  840. $this->add_enc_chk($params_array);
  841. $request = xmlrpc_encode_request("get_file_part",$params_array);
  842. $response = $this->sendRequest($request);
  843. if ( $response === NULL )
  844. return -1;
  845. if ( is_array($response) && xmlrpc_is_fault($response))
  846. return -1;
  847. if ( $response === -1 )
  848. return -1;
  849. list($cur_offset,$data_tmp) = explode(";",$response);
  850. $data = base64_decode($data_tmp);
  851. return $cur_offset;
  852. }
  853. public function shell_action($action, $arguments)
  854. {
  855. $params_array = $this->encrypt_params($action, $arguments);
  856. $this->add_enc_chk($params_array);
  857. $request = xmlrpc_encode_request("shell_action", $params_array);
  858. $response = $this->sendRequest($request);
  859. if (is_array($response) && xmlrpc_is_fault($response))
  860. return NULL;
  861. $data = NULL;
  862. if (is_array($response) and !empty($response))
  863. {
  864. $data = array();
  865. foreach ($response as $key => $value)
  866. {
  867. $data[$key] = base64_decode($value);
  868. }
  869. return $data;
  870. }
  871. @list($retval,$data_tmp) = @explode(";",$response);
  872. if ( $retval > 0 )
  873. {
  874. $lines = explode('\n',$data_tmp);
  875. foreach ($lines as $line)
  876. {
  877. $data .= base64_decode($line);
  878. }
  879. }
  880. return $data;
  881. }
  882. public function stop_update($home_id)
  883. {
  884. $args = $this->encryptParam($home_id);
  885. $this->add_enc_chk($args);
  886. $request = xmlrpc_encode_request("stop_update", $args);
  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. return 1;
  895. }
  896. public function remote_query($protocol, $game_type, $ip, $c_port, $q_port, $s_port)
  897. {
  898. $params_array = $this->encrypt_params($protocol, $game_type, $ip, $c_port, $q_port, $s_port);
  899. $this->add_enc_chk($params_array);
  900. $request = xmlrpc_encode_request("remote_query", $params_array);
  901. $response = $this->sendRequest($request);
  902. if (is_array($response) && xmlrpc_is_fault($response))
  903. return NULL;
  904. if ($response === -1 or $response === 0)
  905. return NULL;
  906. return base64_decode($response);
  907. }
  908. }
  909. ?>