rsync_install.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. function do_progress($kbytes,$rsyncPath)
  25. {
  26. $mbytes = round($kbytes / 1024, 2);
  27. $sizes = file("modules/gamemanager/sizes.list", FILE_IGNORE_NEW_LINES)or print_failure("Can't open sizes.list");
  28. # Adds a backslash on each slash so it can be used as patern at preg_match
  29. $rsyncPath = addcslashes($rsyncPath,"/");
  30. # Sets a default file size
  31. $totalsize = 0;
  32. # loops all available sizes to work over a string instead of an array.
  33. foreach($sizes as $key => $size)
  34. {
  35. # If the rsync path matches the path at the string on the variable $size
  36. # then lists the path and the total size on separated variables
  37. # using explode over the string saved on $size
  38. if ( preg_match("/$rsyncPath/i", $size) )
  39. list( $path, $totalsize ) = explode( ";", $size );
  40. }
  41. if($kbytes > 0)
  42. {
  43. $pct = round(( $kbytes / $totalsize ) * 100, 2);
  44. }
  45. else
  46. {
  47. $pct = "unavailable";
  48. }
  49. #echo "Percent is $pct";
  50. $pct = $pct > 100 ? 100 : $pct;
  51. return "$totalsize;$mbytes;$pct";
  52. }
  53. function update_local_copies()
  54. {
  55. $last_updated = filemtime("modules/gamemanager/sizes.list");
  56. $nowtime=time();
  57. $diff = $nowtime - $last_updated;
  58. if( $diff < 86400)
  59. {
  60. #echo "Now $nowtime last $last_updated diff $diff<br>";
  61. return 0;
  62. }
  63. echo "Updating local cache of rsync meta data files<br>";
  64. $update_files = array('sizes.list', 'rsync.list', 'rsync_sites.list');
  65. $update_urls = array('rsync.opengamepanel.org', 'dls.atl.webehostin.com');
  66. $context = array(
  67. 'ssl' => array(
  68. 'verify_peer' => false,
  69. 'verify_peer_name' => false,
  70. 'allow_self_signed' => true
  71. )
  72. );
  73. foreach($update_files as $file_chk)
  74. {
  75. #echo "Trying to update $file_chk<br>";
  76. foreach($update_urls as $site)
  77. {
  78. #echo "Trying $file_chk from $site<br>";
  79. if(!is_writable("modules/gamemanager/$file_chk"))
  80. {
  81. print_failure("modules/gamemanager/$file_chk is not writable...please make it writable by the webserver");
  82. }
  83. if($tmp_content = file_get_contents("http://$site/sync_data/$file_chk", false, $context))
  84. {
  85. if(!file_put_contents("modules/gamemanager/$file_chk",$tmp_content)){echo "Failed to write<br>";};
  86. break;
  87. }
  88. }
  89. }
  90. if(is_writable('modules/gamemanager'))
  91. {
  92. $tmp = 'modules/gamemanager';
  93. }
  94. elseif(is_writable('/tmp'))
  95. {
  96. $tmp = 'modules/gamemanager';
  97. }
  98. else
  99. {
  100. return "-1";
  101. }
  102. }
  103. require_once("includes/lib_remote.php");
  104. require_once("modules/config_games/server_config_parser.php");
  105. function exec_ogp_module() {
  106. //update_local_copies(); #Disabled until the rsync_sites.list file from master servers is corrected.
  107. global $db,$view,$settings;
  108. $home_id = isset($_REQUEST['home_id']) ? $_REQUEST['home_id'] : "";
  109. $mod_id = isset($_REQUEST['mod_id']) ? $_REQUEST['mod_id'] : "";
  110. $state = isset($_POST['state']) ? $_POST['state'] : "";
  111. $update = isset($_GET['update']) ? $_GET['update'] : "";
  112. $rsync_remote_sites = file("modules/gamemanager/rsync_sites.list"); #load offical rsync sites
  113. $rsync_local_sites = file("modules/gamemanager/rsync_sites_local.list"); #load user custom sites
  114. $settings['rsync_available'] = isset($settings['rsync_available']) ? $settings['rsync_available'] : "1";
  115. if(is_array($rsync_local_sites) && $settings['rsync_available'] == "1") {
  116. $rsync_sites = array_merge($rsync_remote_sites, $rsync_local_sites);
  117. } elseif($settings['rsync_available'] == "2") {
  118. $rsync_sites = $rsync_remote_sites;
  119. } elseif( $settings['rsync_available'] == "3") {
  120. $rsync_sites = $rsync_local_sites;
  121. }
  122. $isAdmin = $db->isAdmin($_SESSION['user_id']);
  123. if ($isAdmin) {
  124. $home_info = $db->getGameHome($home_id);
  125. } else {
  126. $home_info = $db->getUserGameHome($_SESSION['user_id'],$home_id);
  127. }
  128. if ( $home_info === FALSE || preg_match("/u/",$home_info['access_rights']) != 1 )
  129. {
  130. print_failure( get_lang("no_rights") );
  131. echo create_back_button("gamemanager","game_monitor");
  132. return;
  133. }
  134. $home_id = $home_info['home_id'];
  135. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  136. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  137. if( isset($server_xml->lgsl_query_name) )
  138. {
  139. $lgslname = $server_xml->lgsl_query_name;
  140. if($lgslname == "quake3")
  141. {
  142. if($server_xml->game_name == "Quake 3")
  143. $lgslname = "q3";
  144. }
  145. }
  146. elseif( isset($server_xml->gameq_query_name) )
  147. {
  148. $lgslname = $server_xml->gameq_query_name;
  149. if($lgslname == "minecraft")
  150. {
  151. if($server_xml->game_name == "Bukkit")
  152. $lgslname = "bukkit";
  153. elseif($server_xml->game_name == "Tekkit")
  154. $lgslname = "tekkit";
  155. }
  156. }
  157. elseif( isset($server_xml->protocol) )
  158. $lgslname = $server_xml->protocol;
  159. else
  160. $lgslname = $server_xml->mods->mod['key'];
  161. if( preg_match("/win32/", $server_xml->game_key) OR preg_match("/win64/", $server_xml->game_key) )
  162. $os = "windows";
  163. elseif( preg_match("/linux/", $server_xml->game_key) )
  164. $os = "linux";
  165. echo "<h2>Update $home_info[home_name]</h2>";
  166. $screenRunningForHome = $remote->is_screen_running(OGP_SCREEN_TYPE_HOME,$home_id);
  167. if ($screenRunningForHome === 1)
  168. {
  169. print_failure( get_lang("server_running_cant_update") );
  170. return;
  171. }else if($screenRunningForHome === -1){
  172. print_failure( get_lang("agent_offline") );
  173. return;
  174. }
  175. $update_active = $remote->get_log(OGP_SCREEN_TYPE_UPDATE,
  176. // Note exec location should not be added here as the log is in root where rsync is executed.
  177. $home_id,clean_path($home_info['home_path']),
  178. $log_txt,30);
  179. if ($update_active === 0)
  180. {
  181. print_failure( get_lang("agent_offline") );
  182. $view->refresh("{CURRENT_PAGE}", 5);
  183. return;
  184. }
  185. // Start update.
  186. elseif ($state == 'start' && $update_active != 1)
  187. {
  188. $precmd = $home_info['mods'][$mod_id]['precmd'] == "" ?
  189. ( $home_info['mods'][$mod_id]['def_precmd'] == "" ? $server_xml->pre_install :
  190. $home_info['mods'][$mod_id]['def_precmd'] ) : $home_info['mods'][$mod_id]['precmd'];
  191. $postcmd = $home_info['mods'][$mod_id]['postcmd'] == "" ?
  192. ( $home_info['mods'][$mod_id]['def_postcmd'] == "" ? $server_xml->post_install :
  193. $home_info['mods'][$mod_id]['def_precmd'] ) : $home_info['mods'][$mod_id]['postcmd'];
  194. $exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location );
  195. $exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name );
  196. if( isset( $_REQUEST['master_server_home_id'] ) )
  197. {
  198. $ms_home_id = $_REQUEST['master_server_home_id'];
  199. if ($db->getMasterServer($home_info['remote_server_id'], $home_info['home_cfg_id']) == $ms_home_id) {
  200. if ($ms_home_id !== $home_id) {
  201. $ms_info = $db->getGameHome($ms_home_id);
  202. print_success(get_lang_f("starting_copy_with_master_server_named",htmlentities($ms_info['home_name'])));
  203. $rsync = $remote->masterServerUpdate( $home_id,$home_info['home_path'],$ms_home_id,$ms_info['home_path'],$exec_folder_path,$exec_path,$precmd,$postcmd );
  204. $master = "&amp;master=true";
  205. } else {
  206. print_failure(get_lang('cannot_update_from_own_self'));
  207. $view->refresh('?m=gamemanager&p=game_monitor', 2);
  208. return;
  209. }
  210. } else {
  211. $db->logger(get_lang_f('update_attempt_from_nonmaster_server', $_SESSION['users_login'], $home_id, $ms_home_id));
  212. print_failure(get_lang('attempting_nonmaster_update'));
  213. $view->refresh('?m=gamemanager&p=game_monitor', 2);
  214. return;
  215. }
  216. }
  217. else
  218. {
  219. $url_id = (isset($_POST['url_id']) && (int)$_POST['url_id'] > 0 ? (int)$_POST['url_id'] -1 : null);
  220. if (!is_null($url_id) && array_key_exists($url_id, $rsync_sites)) {
  221. $urlArr = explode('|', $rsync_sites[$url_id]);
  222. $url = $urlArr[0] . "/ogp_game_installer/$lgslname/$os/";
  223. } else {
  224. print_failure(get_lang('unknown_rsync_mirror'));
  225. $view->refresh('?m=gamemanager&p=game_monitor');
  226. return;
  227. }
  228. print_success(get_lang_f("starting_sync_with", $url));
  229. // Additional files to lock
  230. if(isset($server_xml->lock_files) && !empty($server_xml->lock_files)){
  231. $lockFiles = trim($server_xml->lock_files);
  232. }else{
  233. $lockFiles = "";
  234. }
  235. $rsync = $remote->start_rsync_install($home_id, $home_info['home_path'], $url, $exec_folder_path, $exec_path, $precmd, $postcmd, $lockFiles);
  236. $master = "";
  237. }
  238. if( $rsync === 0 )
  239. {
  240. print_failure( get_lang("failed_to_start_rsync_update") );
  241. return;
  242. }
  243. else if ( $rsync === 1 || $rsync === -1)
  244. {
  245. if($rsync === -1){
  246. print_failure(get_lang("timed_out"));
  247. }
  248. print_success(get_lang("update_started"));
  249. echo "<p><a href=\"?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id$master\">".
  250. get_lang("refresh_rsync_status") ."</a></p>";
  251. $view->refresh("?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id$master",5);
  252. return;
  253. }
  254. }
  255. elseif($update_active == 1)
  256. {
  257. echo "<p class='note'></p>\n";
  258. if ( isset( $_POST['stop_update_x'] ) )
  259. {
  260. $remote->stop_update($home_id);
  261. print_success("Update stopped.");
  262. $view->refresh("?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id", 2);
  263. return;
  264. }
  265. $update_complete = false;
  266. echo "<form method=POST><input type='image' name='stop_update' onsubmit='submit-form();' src='modules/administration/images/remove.gif'>". get_lang("stop_update") ."</input></form>";
  267. if (empty($log_txt))
  268. $log_txt = not_available;
  269. if(!isset($_GET['master']))
  270. {
  271. $kbytes = $remote->rsync_progress($home_info['home_path']);
  272. list($totalsize,$mbytes,$pct) = explode(";",do_progress($kbytes,$lgslname."/".$os));
  273. $totalmbytes = round($totalsize / 1024, 2);
  274. echo '<div class="dragbox bloc rounded" style="background-color:#dce9f2;" >
  275. <h4>'. get_lang("update_in_progress") ." ${mbytes}MB/${totalmbytes}MB</h4>
  276. <div style='background-color:#dce9f2;' >
  277. ";
  278. $bar = '';
  279. for( $i = 1; $i <= $pct; $i++ )
  280. {
  281. $bar .= '<img style="width:0.92%;vertical-align:middle;" src="images/progressBar.png">';
  282. }
  283. echo "$bar <b style='vertical-align:top;display:inline;font-size:1.2em;color:red;' >$pct%</b>
  284. </div>
  285. </div><br>";
  286. }
  287. else
  288. {
  289. echo '<h4>'. get_lang("update_in_progress") .'</h4>';
  290. }
  291. echo "<pre>".$log_txt."</pre>\n".
  292. "<p><a href=\"?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id\">".
  293. get_lang("refresh_rsync_status") ."</a></p>";
  294. $view->refresh("?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id",5);
  295. return;
  296. }
  297. elseif($update != "update")
  298. {
  299. $view->refresh("{CURRENT_PAGE}", 60);
  300. print_success(get_lang("update_completed") );
  301. echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id=".$home_info['home_id']."'><< ". get_lang("back") ."</a></td></tr></table>";
  302. echo "<pre>".$log_txt."</pre>\n";
  303. echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id=".$home_info['home_id']."'><< ". get_lang("back") ."</a></td></tr></table>";
  304. $update_complete = true;
  305. }
  306. else
  307. {
  308. #echo "LGSL or GameQ query name is $server_xml->lgsl_query_name$server_xml->gameq_query_name";
  309. $sync_list = @file("modules/gamemanager/rsync.list", FILE_IGNORE_NEW_LINES);
  310. if(!is_array($sync_list))
  311. {
  312. if(!is_file("modules/gamemanager/rsync.list"))
  313. {
  314. print_failure("Trouble accessing http://www.opengamepanel.org/rsync.list<br>".
  315. "Make sure allow_fopen_url is set to \"On\" in your php.ini and opengamepanel.org is online<br>".
  316. "In the mean time, you can get a local copy of the file by running wget http://www.opengamepanel.org/sync_data/rsync.list -O /path/to/ogpweb/modules/gamemanager/rsync.list");
  317. return;
  318. }
  319. # print_failure("Error loading rsync.list");
  320. # return;
  321. $sync_list = @file("modules/gamemanager/rsync.list", FILE_IGNORE_NEW_LINES);
  322. if(!is_array($sync_list))
  323. {
  324. print_failure("Failed to open local copy of rsync.list in modules/gamemanager/rsync.list");
  325. return;
  326. }
  327. }
  328. $master_server_home_id = $db->getMasterServer( $home_info['remote_server_id'], $home_info['home_cfg_id'] );
  329. if ( in_array($lgslname, $sync_list) )
  330. {
  331. echo "Game type supported<br>
  332. <p class='center'>To add your own rsync site, create modules/gamemanager/rsync_sites_local.list and put a server name on each line.</p><br>
  333. <form action='?m=gamemanager&amp;p=rsync_install' method='post'>
  334. <table class='center'>
  335. <input type='hidden' name='home_id' value='$home_id' />
  336. <input type='hidden' name='mod_id' value='$mod_id' />
  337. <input type='hidden' name='lgslname' value='$lgslname' />
  338. <input type='hidden' name='state' value='start' />
  339. <tr><td align='right'>Game name:</td><td align='left'>$home_info[game_name]</td></tr>
  340. <tr><td align='right'>Directory:</td><td align='left'>$home_info[home_path]</td></tr>
  341. <tr><td align='right'>Remoteserver:</td>
  342. <td align='left'>$home_info[remote_server_name] ($home_info[agent_ip]:$home_info[agent_port])</td></tr>
  343. <tr><td align='right'>Rsync Server:</td>
  344. <td align='left'>".create_drop_box_from_array_rsync($rsync_sites,"url_id")."<br>";
  345. if( $master_server_home_id != FALSE AND $master_server_home_id != $home_id )
  346. {
  347. echo "<input type='checkbox' name='master_server_home_id' value='$master_server_home_id' /><b>". get_lang("update_from_local_master_server") ."</b>";
  348. }
  349. echo "</td></tr></table><p><input type='submit' name='update' value='". get_lang("update_from_selected_rsync_server") ."' /></p>
  350. </form>";
  351. }
  352. elseif($master_server_home_id != FALSE AND $master_server_home_id != $home_id)
  353. {
  354. $ms_home_info = $db->getGameHome($master_server_home_id);
  355. echo "<br>
  356. <p class='success'>Master server update available</p><br>
  357. <form action='?m=gamemanager&amp;p=rsync_install' method='post'>
  358. <table class='center'>
  359. <input type='hidden' name='home_id' value='$home_id' />
  360. <input type='hidden' name='mod_id' value='$mod_id' />
  361. <input type='hidden' name='lgslname' value='$lgslname' />
  362. <input type='hidden' name='state' value='start' />
  363. <tr><td align='right'>Game name:</td><td align='left'>$home_info[game_name]</td></tr>
  364. <tr><td align='right'>Master Server Name:</td><td align='left'>$ms_home_info[home_name]</td></tr>
  365. <tr><td align='right'>Master Server Directory:</td><td align='left'>$ms_home_info[home_path]</td></tr>
  366. <tr><td align='right'>Local Directory:</td><td align='left'>$home_info[home_path]</td></tr>".
  367. "<input type='hidden' name='master_server_home_id' value='$master_server_home_id' />".
  368. "</td></tr></table><p><input type='submit' name='update' value='". get_lang("update_from_local_master_server") ."' /></p>
  369. </form>";
  370. }
  371. else
  372. {
  373. print_failure("This game type [ $lgslname ] is not yet supported with rsync install");
  374. }
  375. }
  376. }
  377. ?>