rsync_install.php 15 KB

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