rsync_install.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. 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('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. if ( $remote->is_screen_running(OGP_SCREEN_TYPE_HOME,$home_id) == 1 )
  167. {
  168. print_failure( get_lang("server_running_cant_update") );
  169. return;
  170. }
  171. $update_active = $remote->get_log(OGP_SCREEN_TYPE_UPDATE,
  172. // Note exec location should not be added here as the log is in root where rsync is executed.
  173. $home_id,clean_path($home_info['home_path']),
  174. $log_txt,30);
  175. if ($update_active === 0)
  176. {
  177. print_failure( get_lang("agent_offline") );
  178. $view->refresh("{CURRENT_PAGE}", 5);
  179. return;
  180. }
  181. // Start update.
  182. elseif ($state == 'start' && $update_active != 1)
  183. {
  184. $precmd = $home_info['mods'][$mod_id]['precmd'] == "" ?
  185. ( $home_info['mods'][$mod_id]['def_precmd'] == "" ? $server_xml->pre_install :
  186. $home_info['mods'][$mod_id]['def_precmd'] ) : $home_info['mods'][$mod_id]['precmd'];
  187. $postcmd = $home_info['mods'][$mod_id]['postcmd'] == "" ?
  188. ( $home_info['mods'][$mod_id]['def_postcmd'] == "" ? $server_xml->post_install :
  189. $home_info['mods'][$mod_id]['def_precmd'] ) : $home_info['mods'][$mod_id]['postcmd'];
  190. $exec_folder_path = clean_path($home_info['home_path'] . "/" . $server_xml->exe_location );
  191. $exec_path = clean_path($exec_folder_path . "/" . $server_xml->server_exec_name );
  192. if( isset( $_REQUEST['master_server_home_id'] ) )
  193. {
  194. $ms_home_id = $_REQUEST['master_server_home_id'];
  195. if ($db->getMasterServer($home_info['remote_server_id'], $home_info['home_cfg_id']) == $ms_home_id) {
  196. if ($ms_home_id !== $home_id) {
  197. $ms_info = $db->getGameHome($ms_home_id);
  198. print_success(get_lang_f("starting_copy_with_master_server_named",htmlentities($ms_info['home_name'])));
  199. $rsync = $remote->masterServerUpdate( $home_id,$home_info['home_path'],$ms_home_id,$ms_info['home_path'],$exec_folder_path,$exec_path,$precmd,$postcmd );
  200. $master = "&amp;master=true";
  201. } else {
  202. print_failure(get_lang('cannot_update_from_own_self'));
  203. $view->refresh('?m=gamemanager&p=game_monitor', 2);
  204. return;
  205. }
  206. } else {
  207. $db->logger(get_lang_f('update_attempt_from_nonmaster_server', $_SESSION['users_login'], $home_id, $ms_home_id));
  208. print_failure(get_lang('attempting_nonmaster_update'));
  209. $view->refresh('?m=gamemanager&p=game_monitor', 2);
  210. return;
  211. }
  212. }
  213. else
  214. {
  215. $url_id = (isset($_POST['url_id']) && (int)$_POST['url_id'] > 0 ? (int)$_POST['url_id'] -1 : null);
  216. if (!is_null($url_id) && array_key_exists($url_id, $rsync_sites)) {
  217. $urlArr = explode('|', $rsync_sites[$url_id]);
  218. $url = $urlArr[0] . "/ogp_game_installer/$lgslname/$os/";
  219. } else {
  220. print_failure(get_lang('unknown_rsync_mirror'));
  221. $view->refresh('?m=gamemanager&p=game_monitor');
  222. return;
  223. }
  224. print_success(get_lang_f("starting_sync_with", $url));
  225. // Additional files to lock
  226. if(isset($server_xml->lock_files) && !empty($server_xml->lock_files)){
  227. $lockFiles = trim($server_xml->lock_files);
  228. }else{
  229. $lockFiles = "";
  230. }
  231. $rsync = $remote->start_rsync_install($home_id, $home_info['home_path'], $url, $exec_folder_path, $exec_path, $precmd, $postcmd, $lockFiles);
  232. $master = "";
  233. }
  234. if( $rsync === 0 )
  235. {
  236. print_failure( get_lang("failed_to_start_rsync_update") );
  237. return;
  238. }
  239. else if ( $rsync === 1 )
  240. {
  241. print_success(get_lang("update_started"));
  242. echo "<p><a href=\"?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id$master\">".
  243. refresh_rsync_status ."</a></p>";
  244. $view->refresh("?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id$master",5);
  245. return;
  246. }
  247. elseif( $rsync === 0 )
  248. {
  249. print_failure( get_lang("agent_offline") );
  250. return;
  251. }
  252. }
  253. elseif($update_active == 1)
  254. {
  255. echo "<p class='note'></p>\n";
  256. if ( isset( $_POST['stop_update_x'] ) )
  257. {
  258. $remote->stop_update($home_id);
  259. print_success("Update stopped.");
  260. $view->refresh("?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id", 2);
  261. return;
  262. }
  263. $update_complete = false;
  264. echo "<form method=POST><input type='image' name='stop_update' onsubmit='submit-form();' src='modules/administration/images/remove.gif'>". stop_update ."</input></form>";
  265. if (empty($log_txt))
  266. $log_txt = not_available;
  267. if(!isset($_GET['master']))
  268. {
  269. $kbytes = $remote->rsync_progress($home_info['home_path']);
  270. list($totalsize,$mbytes,$pct) = explode(";",do_progress($kbytes,$lgslname."/".$os));
  271. $totalmbytes = round($totalsize / 1024, 2);
  272. echo '<div class="dragbox bloc rounded" style="background-color:#dce9f2;" >
  273. <h4>'. update_in_progress ." ${mbytes}MB/${totalmbytes}MB</h4>
  274. <div style='background-color:#dce9f2;' >
  275. ";
  276. $bar = '';
  277. for( $i = 1; $i <= $pct; $i++ )
  278. {
  279. $bar .= '<img style="width:0.92%;vertical-align:middle;" src="images/progressBar.png">';
  280. }
  281. echo "$bar <b style='vertical-align:top;display:inline;font-size:1.2em;color:red;' >$pct%</b>
  282. </div>
  283. </div><br>";
  284. }
  285. else
  286. {
  287. echo '<h4>'. update_in_progress .'</h4>';
  288. }
  289. echo "<pre>".$log_txt."</pre>\n".
  290. "<p><a href=\"?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id\">".
  291. refresh_rsync_status ."</a></p>";
  292. $view->refresh("?m=gamemanager&amp;p=rsync_install&amp;update=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id",5);
  293. return;
  294. }
  295. elseif($update != "update")
  296. {
  297. $view->refresh("{CURRENT_PAGE}", 60);
  298. print_success(get_lang("update_completed") );
  299. echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id=".$home_info['home_id']."'><< ". back ."</a></td></tr></table>";
  300. echo "<pre>".$log_txt."</pre>\n";
  301. echo "<table class='center'><tr><td><a href='?m=gamemanager&amp;p=game_monitor&amp;home_id=".$home_info['home_id']."'><< ". back ."</a></td></tr></table>";
  302. $update_complete = true;
  303. }
  304. else
  305. {
  306. #echo "LGSL or GameQ query name is $server_xml->lgsl_query_name$server_xml->gameq_query_name";
  307. $sync_list = @file("modules/gamemanager/rsync.list", FILE_IGNORE_NEW_LINES);
  308. if(!is_array($sync_list))
  309. {
  310. if(!is_file("modules/gamemanager/rsync.list"))
  311. {
  312. print_failure("Trouble accessing http://www.opengamepanel.org/rsync.list<br>".
  313. "Make sure allow_fopen_url is set to \"On\" in your php.ini and opengamepanel.org is online<br>".
  314. "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");
  315. return;
  316. }
  317. # print_failure("Error loading rsync.list");
  318. # return;
  319. $sync_list = @file("modules/gamemanager/rsync.list", FILE_IGNORE_NEW_LINES);
  320. if(!is_array($sync_list))
  321. {
  322. print_failure("Failed to open local copy of rsync.list in modules/gamemanager/rsync.list");
  323. return;
  324. }
  325. }
  326. $master_server_home_id = $db->getMasterServer( $home_info['remote_server_id'], $home_info['home_cfg_id'] );
  327. if ( in_array($lgslname, $sync_list) )
  328. {
  329. echo "Game type supported<br>
  330. <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>
  331. <form action='?m=gamemanager&amp;p=rsync_install' method='post'>
  332. <table class='center'>
  333. <input type='hidden' name='home_id' value='$home_id' />
  334. <input type='hidden' name='mod_id' value='$mod_id' />
  335. <input type='hidden' name='lgslname' value='$lgslname' />
  336. <input type='hidden' name='state' value='start' />
  337. <tr><td align='right'>Game name:</td><td align='left'>$home_info[game_name]</td></tr>
  338. <tr><td align='right'>Directory:</td><td align='left'>$home_info[home_path]</td></tr>
  339. <tr><td align='right'>Remoteserver:</td>
  340. <td align='left'>$home_info[remote_server_name] ($home_info[agent_ip]:$home_info[agent_port])</td></tr>
  341. <tr><td align='right'>Rsync Server:</td>
  342. <td align='left'>".create_drop_box_from_array_rsync($rsync_sites,"url_id")."<br>";
  343. if( $master_server_home_id != FALSE AND $master_server_home_id != $home_id )
  344. {
  345. echo "<input type='checkbox' name='master_server_home_id' value='$master_server_home_id' /><b>". update_from_local_master_server ."</b>";
  346. }
  347. echo "</td></tr></table><p><input type='submit' name='update' value='". update_from_selected_rsync_server ."' /></p>
  348. </form>";
  349. }
  350. elseif($master_server_home_id != FALSE AND $master_server_home_id != $home_id)
  351. {
  352. $ms_home_info = $db->getGameHome($master_server_home_id);
  353. echo "<br>
  354. <p class='success'>Master server update available</p><br>
  355. <form action='?m=gamemanager&amp;p=rsync_install' method='post'>
  356. <table class='center'>
  357. <input type='hidden' name='home_id' value='$home_id' />
  358. <input type='hidden' name='mod_id' value='$mod_id' />
  359. <input type='hidden' name='lgslname' value='$lgslname' />
  360. <input type='hidden' name='state' value='start' />
  361. <tr><td align='right'>Game name:</td><td align='left'>$home_info[game_name]</td></tr>
  362. <tr><td align='right'>Master Server Name:</td><td align='left'>$ms_home_info[home_name]</td></tr>
  363. <tr><td align='right'>Master Server Directory:</td><td align='left'>$ms_home_info[home_path]</td></tr>
  364. <tr><td align='right'>Local Directory:</td><td align='left'>$home_info[home_path]</td></tr>".
  365. "<input type='hidden' name='master_server_home_id' value='$master_server_home_id' />".
  366. "</td></tr></table><p><input type='submit' name='update' value='". update_from_local_master_server ."' /></p>
  367. </form>";
  368. }
  369. else
  370. {
  371. print_failure("This game type [ $lgslname ] is not yet supported with rsync install");
  372. }
  373. }
  374. }
  375. ?>