addons_installer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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,$totalsize)
  25. {
  26. $mbytes = round($kbytes / 1024, 2);
  27. if($kbytes > 0)
  28. {
  29. $pct = round(( $kbytes / $totalsize ) * 100, 2);
  30. }
  31. else
  32. {
  33. $pct = "-";
  34. }
  35. #echo "Percent is $pct";
  36. return "$totalsize;$mbytes;$pct";
  37. }
  38. require_once("includes/lib_remote.php");
  39. require_once("modules/config_games/server_config_parser.php");
  40. require_once("protocol/lgsl/lgsl_protocol.php");
  41. function exec_ogp_module() {
  42. global $db,$view;
  43. $home_id = $_REQUEST['home_id'];
  44. $mod_id = $_REQUEST['mod_id'];
  45. $ip = $_REQUEST['ip'];
  46. $port = $_REQUEST['port'];
  47. $user_id = $_SESSION['user_id'];
  48. $isAdmin = $db->isAdmin( $_SESSION['user_id'] );
  49. if($isAdmin)
  50. $home_info = $db->getGameHome($home_id);
  51. else
  52. $home_info = $db->getUserGameHome($_SESSION['user_id'],$home_id);
  53. if ( $home_info === FALSE )
  54. {
  55. print_failure(get_lang('no_rights'));
  56. echo create_back_button("addonsmanager","user_addons");
  57. return;
  58. }
  59. $home_cfg_id = $home_info['home_cfg_id'];
  60. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$home_info['home_cfg_file']);
  61. $addon_type = isset($_REQUEST['addon_type']) ? $_REQUEST['addon_type'] : "";
  62. $state = isset($_REQUEST['state']) ? $_REQUEST['state'] : "";
  63. $pid = isset($_REQUEST['pid']) ? $_REQUEST['pid'] : -1;
  64. if ( $state != "" )
  65. {
  66. $addon_id = $_REQUEST['addon_id'];
  67. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  68. $addons_rows = $db->resultQuery("SELECT url, path, post_script FROM OGP_DB_PREFIXaddons WHERE addon_id=".$addon_id);
  69. $addon_info = $addons_rows[0];
  70. $url = $addon_info['url'];
  71. $filename = basename($url);
  72. #### This makes replacements to the bash script:
  73. if($addon_info['post_script'] != "")
  74. {
  75. $addon_info['post_script'] = strip_real_escape_string($addon_info['post_script']);
  76. $check_passed = FALSE;
  77. $address_at_post = $ip.":".$port;
  78. $ip_ports = $db->getHomeIpPorts($home_info['home_id']);
  79. foreach($ip_ports as $ip_port);
  80. {
  81. $address_owned = $ip_port['ip'].":".$ip_port['port'];
  82. if($address_owned == $address_at_post)
  83. {
  84. $check_passed = TRUE;
  85. $ip = $ip_port['ip'];
  86. $port = $ip_port['port'];
  87. }
  88. }
  89. if($check_passed)
  90. {
  91. $home_info['ip'] = $ip;
  92. $home_info['port'] = $port;
  93. if( isset($server_xml->gameq_query_name) )
  94. {
  95. require_once("modules/gamemanager/home_handling_functions.php");
  96. $home_info['query_port'] = get_query_port($server_xml, $home_info['port']);
  97. }
  98. elseif( isset($server_xml->lgsl_query_name) )
  99. {
  100. $get_q_and_s = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $home_info['port'], "", "");
  101. $home_info['query_port'] = $get_q_and_s['1'];
  102. }
  103. $home_info["incremental"] = $db->incrementalNumByHomeId( $home_info['home_id'], $home_info['mods'][$mod_id]['mod_cfg_id'], $home_info['remote_server_id'] );
  104. $post_script = preg_replace( "/\%home_path\%/i", $home_info['home_path'], $addon_info['post_script']);
  105. $post_script = preg_replace( "/\%home_name\%/i", $home_info['home_name'], $post_script);
  106. $post_script = preg_replace( "/\%control_password\%/i", $home_info['control_password'], $post_script);
  107. $post_script = preg_replace( "/\%max_players\%/i", $home_info['mods'][$mod_id]['max_players'], $post_script);
  108. $post_script = preg_replace( "/\%ip\%/i", $home_info['ip'], $post_script);
  109. $post_script = preg_replace( "/\%port\%/i", $home_info['port'], $post_script);
  110. $post_script = preg_replace( "/\%query_port\%/i", $home_info['query_port'], $post_script);
  111. $post_script = preg_replace( "/\%incremental\%/i", $home_info['incremental'], $post_script);
  112. }
  113. }
  114. #### end of replacememnts
  115. if ( $state == "start" AND $addon_id != "" )
  116. $pid = $remote->start_file_download( $addon_info['url'], $home_info['home_path']."/".$addon_info['path'], $filename, "uncompress", $post_script);
  117. $headers = get_headers($url, 1);
  118. $download_available = !$headers ? FALSE : TRUE;
  119. // Check if any error occured
  120. if($download_available)
  121. {
  122. $bytes = is_array($headers['Content-Length']) ? $headers['Content-Length'][1] : $headers['Content-Length'];
  123. // Display the File Size
  124. $totalsize = $bytes / 1024;
  125. clearstatcache();
  126. }
  127. $kbytes = $remote->rsync_progress($home_info['home_path']."/".$addon_info['path']."/".$filename);
  128. list($totalsize,$mbytes,$pct) = explode(";",do_progress($kbytes,$totalsize));
  129. $totalmbytes = round($totalsize / 1024, 2);
  130. $pct = $pct > 100 ? 100 : $pct;
  131. echo "<h2>" . htmlentities($home_info['home_name']) . "</h2>";
  132. echo '<div class="dragbox bloc rounded" style="background-color:#dce9f2;" >
  133. <h4>'.get_lang('install')." ".$filename." ${mbytes}MB/${totalmbytes}MB</h4>
  134. <div style='background-color:#dce9f2;' >
  135. ";
  136. $bar = '';
  137. for( $i = 1; $i <= $pct; $i++ )
  138. {
  139. $bar .= '<img style="width:0.92%;vertical-align:middle;" src="images/progressBar.png">';
  140. }
  141. echo "<center>$bar <b style='vertical-align:top;display:inline;font-size:1.2em;color:red;' >$pct%</b></center>
  142. </div>
  143. </div>";
  144. if ( ( $pct == "100" or !$download_available ) AND $post_script != "" )
  145. {
  146. $log_retval = $remote->get_log( "post_script",
  147. $pid,
  148. clean_path($home_info['home_path']."/".$server_xml->exe_location),
  149. $script_log);
  150. if ($log_retval == 0)
  151. {
  152. print_failure(get_lang('agent_offline'));
  153. }
  154. elseif ($log_retval == 1 || $log_retval == 2)
  155. {
  156. echo "<pre class='log'>".$script_log."</pre>";
  157. }
  158. elseif( $remote->is_screen_running("post_script",$pid) == 1 )
  159. {
  160. print_failure(get_lang_f('unable_to_get_log',$log_retval));
  161. }
  162. }
  163. if( $pct == "100" or !$download_available or ( $download_available and $pct == "-" and $pid > 0 ) )
  164. {
  165. if(!$download_available)
  166. {
  167. print_failure(get_lang('failed_to_start_file_download'));
  168. }
  169. elseif( $remote->is_file_download_in_progress($pid) === 1 )
  170. {
  171. print_success(get_lang_f('wait_while_decompressing', $filename));
  172. echo "<p><a href=\"?m=addonsmanager&amp;p=addons&amp;state=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id".
  173. "&amp;ip=$ip&amp;port=$port&amp;addon_id=$addon_id&amp;pid=$pid\">".get_lang('refresh')."</a></p>";
  174. $view->refresh("?m=addonsmanager&amp;p=addons&amp;state=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id".
  175. "&amp;ip=$ip&amp;port=$port&addon_id=$addon_id&amp;pid=$pid",5);
  176. }
  177. elseif( $remote->is_file_download_in_progress($pid) === 0 AND $remote->is_screen_running("post_script",$pid) === 0 )
  178. {
  179. print_success(get_lang('addon_installed_successfully'));
  180. echo "<p><a href=\"?m=addonsmanager&amp;p=user_addons&amp;home_id=$home_id".
  181. "&amp;mod_id=$mod_id&amp;ip=$ip&amp;port=$port\">".get_lang('back')."</a></p>";
  182. $view->refresh("?m=addonsmanager&amp;p=user_addons&amp;home_id=$home_id".
  183. "&amp;mod_id=$mod_id&amp;ip=$ip&amp;port=$port",10);
  184. return;
  185. }
  186. }
  187. else
  188. {
  189. echo "<p><a href=\"?m=addonsmanager&amp;p=addons&amp;state=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id".
  190. "&amp;ip=$ip&amp;port=$port&amp;addon_id=$addon_id&amp;pid=$pid\">".get_lang('refresh')."</a></p>";
  191. $view->refresh("?m=addonsmanager&amp;p=addons&amp;state=refresh&amp;home_id=$home_id&amp;mod_id=$mod_id".
  192. "&amp;ip=$ip&amp;port=$port&amp;addon_id=$addon_id&amp;pid=$pid",5);
  193. }
  194. }
  195. elseif( $addon_type != "" )
  196. {
  197. ?>
  198. <h2><?php echo htmlentities($home_info['home_name'])."&nbsp;".get_lang($addon_type) ;?></h2>
  199. <table class='center'>
  200. <form method='get'>
  201. <input type='hidden' name='m' value='addonsmanager' />
  202. <input type='hidden' name='p' value='addons' />
  203. <input type='hidden' name='home_id' value='<?php echo $home_id; ?>' />
  204. <input type='hidden' name='mod_id' value='<?php echo $mod_id; ?>' />
  205. <input type='hidden' name='ip' value='<?php echo $ip; ?>' />
  206. <input type='hidden' name='port' value='<?php echo $port; ?>' />
  207. <input type='hidden' name='state' value='start' />
  208. <tr><td align='right'><?php print_lang('game_name'); ?>: </td><td align='left'><?php echo $home_info['game_name']; ?></td></tr>
  209. <tr><td align='right'><?php print_lang('directory'); ?>: </td><td align='left'><?php echo $home_info['home_path']; ?></td></tr>
  210. <tr><td align='right'><?php print_lang('remote_server'); ?>: </td>
  211. <td align='left'><?php echo "$home_info[remote_server_name] ($home_info[agent_ip]:$home_info[agent_port])"; ?></td></tr>
  212. <tr><td align='right'><?php print_lang('select_addon'); ?>: </td>
  213. <td align='left'>
  214. <select name="addon_id">
  215. <?php
  216. $addons = $db->resultQuery("SELECT addon_id, name FROM OGP_DB_PREFIXaddons WHERE addon_type='".$addon_type."' AND home_cfg_id=" . $home_cfg_id);
  217. foreach($addons as $addon)
  218. {
  219. ?>
  220. <option value="<?php echo $addon['addon_id']; ?>"><?php echo $addon['name']; ?></option>
  221. <?php
  222. }
  223. ?>
  224. </select>
  225. </td></tr>
  226. <tr><td colspan='2' class='info'>&nbsp;</td></tr>
  227. <td align='left'>
  228. &nbsp;
  229. </td></tr><tr><td align="right">
  230. <input type="submit" name="update" value="<?php print_lang('install'); ?>" />
  231. </form></td><td>
  232. <form method="get">
  233. <input type="hidden" name="m" value="addonsmanager" />
  234. <input type="hidden" name="p" value="user_addons" />
  235. <input type="hidden" name="home_id" value="<?php echo $home_id; ?>" />
  236. <input type="hidden" name="mod_id" value="<?php echo $mod_id; ?>" />
  237. <input type="hidden" name="ip" value="<?php echo $ip; ?>" />
  238. <input type="hidden" name="port" value="<?php echo $port; ?>" />
  239. <input type="submit" value="<?php print_lang('back'); ?>" />
  240. </form>
  241. </td></tr>
  242. </table>
  243. <?php
  244. }
  245. }
  246. ?>