addons_installer.php 11 KB

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