cfg_text_replace.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 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. if (empty($server_home["ip"]))
  25. $server_home["ip"] = $ip;
  26. if (empty($server_home["port"]))
  27. $server_home["port"] = $port;
  28. $server_home["true"] = "";
  29. $last_param = json_decode($db->getLastParam($server_home["home_id"]), True);
  30. $server_home["max_players"] = isset($cli_param_data['PLAYERS']) ? $cli_param_data['PLAYERS'] : $last_param['players'];
  31. $server_home["webhost_ip"] = $_SERVER['SERVER_ADDR'];
  32. $server_home["incremental"] = $db->incrementalNumByHomeId( $server_home["home_id"], $server_home["mod_cfg_id"], $server_home["remote_server_id"] );
  33. $server_home["map"] = isset($cli_param_data['MAP']) ? $cli_param_data['MAP'] : $last_param['map'];
  34. if( isset($server_xml->gameq_query_name) )
  35. {
  36. $server_home["query_port"] = get_query_port($server_xml, $server_home['port']);
  37. }
  38. elseif( isset($server_xml->lgsl_query_name) )
  39. {
  40. $get_q_and_s = lgsl_port_conversion((string)$server_xml->lgsl_query_name, $server_home['port'], "", "");
  41. $server_home["query_port"] = $get_q_and_s['1'];
  42. }
  43. $replace_texts = $server_xml->replace_texts->text;
  44. $replace_id = 0;
  45. foreach ($replace_texts as $text => $array )
  46. {
  47. $param = $array['key'];
  48. $replacements[$replace_id]['info_param'] = $server_home["$param"];
  49. foreach ($array as $key => $value )
  50. {
  51. if ($key == "default")
  52. $replacements[$replace_id]['default'] = (string)$value;
  53. if ($key == "var")
  54. $replacements[$replace_id]['var'] = (string)$value;
  55. if ($key == "filepath")
  56. $replacements[$replace_id]['filepath'] = (string)$value;
  57. if ($key == "options")
  58. $replacements[$replace_id]['options'] = (string)$value;
  59. }
  60. $replace_id++;
  61. }
  62. $custom_fields = json_decode($db->getCustomFields($home_id), True);
  63. $fields = $server_xml->custom_fields->field;
  64. foreach ($fields as $text => $array )
  65. {
  66. foreach ($array as $key => $value )
  67. {
  68. if ($key == "default_value")
  69. {
  70. if (array_key_exists((string)$array['key'], $custom_fields))
  71. $replacements[$replace_id]['info_param'] = (string)$custom_fields[(string)$array['key']];
  72. else
  73. $replacements[$replace_id]['info_param'] = (string)$value;
  74. }
  75. if ($key == "default")
  76. $replacements[$replace_id]['default'] = (string)$value;
  77. if ($key == "var")
  78. $replacements[$replace_id]['var'] = (string)$value;
  79. if ($key == "filepath")
  80. $replacements[$replace_id]['filepath'] = (string)$value;
  81. if ($key == "options")
  82. $replacements[$replace_id]['options'] = (string)$value;
  83. }
  84. $replace_id++;
  85. }
  86. foreach($replacements as $key => $replacement)
  87. {
  88. $filepath = $replacement['filepath'];
  89. $file_replacements[$filepath][$key] = $replacement;
  90. }
  91. /* echo "<xmp>";
  92. print_r($file_replacements);
  93. echo "</xmp>"; */
  94. require_once('includes/lib_remote.php');
  95. $remote = new OGPRemoteLibrary($server_home['agent_ip'], $server_home['agent_port'], $server_home['encryption_key'] );
  96. foreach($file_replacements as $filepath => $replacements)
  97. {
  98. $file_info = $remote->remote_readfile($server_home['home_path']."/$filepath",$file_content);
  99. if ( $file_info === 0 )
  100. {
  101. $remote->exec( "touch ".$server_home['home_path']."/$filepath" );
  102. $file_info = "";
  103. }
  104. foreach($replacements as $replacement)
  105. {
  106. $info_param = $replacement['info_param'];
  107. $default = $replacement['default'];
  108. $var = $replacement['var'];
  109. $options = $replacement['options'];
  110. $param_enc = mb_detect_encoding($info_param);
  111. $file_enc = mb_detect_encoding($file_content);
  112. if($param_enc != $file_enc)
  113. $file_content = mb_convert_encoding($file_content, $param_enc, $file_enc);
  114. if( !in_array( $options, array("tags","tagValueByName","sc","sqc") ) )
  115. {
  116. $match_found = preg_match("/$default/m", $file_content);
  117. if($var == "")
  118. {
  119. $preg_info_param = preg_quote($info_param, "/");
  120. if ($options == "s")//separated
  121. $match_info_param = preg_match("/^\s$preg_info_param/m", $file_content);
  122. elseif ($options == "q")//quoted
  123. $match_info_param = preg_match("/^\"$preg_info_param\"/m", $file_content);
  124. elseif ($options == "sq")//separated & quoted
  125. $match_info_param = preg_match("/^\s\"$preg_info_param\"/m", $file_content);
  126. elseif ($options == "")
  127. $match_info_param = preg_match("/^$preg_info_param/m", $file_content);
  128. if($match_info_param == 1)
  129. continue;
  130. }
  131. }
  132. else
  133. $match_found = 1;
  134. if(!$match_found or $match_found === 0)
  135. {
  136. if ($options == "s")//separated
  137. $file_content .= "\n$var $info_param";
  138. elseif ($options == "q")//quoted
  139. $file_content .= "\n$var\"$info_param\"";
  140. elseif ($options == "sq")//separated & quoted
  141. $file_content .= "\n$var \"$info_param\"";
  142. elseif ($options == "key-regex")
  143. {
  144. $var = str_replace("%key%", $info_param, $var);
  145. $file_content .= "\n$var";
  146. }
  147. elseif ($options == "")
  148. $file_content .= "\n$var$info_param";
  149. }
  150. else
  151. {
  152. if ($options == "tags")
  153. $file_content = preg_replace("/(<$default$var>)(.*)(<\/$default>)/m", '${1}'.$info_param.'${3}', $file_content, 1);
  154. elseif ($options == "tagValueByName")
  155. $file_content = preg_replace('/('.$default.'.*name="'.$var.'".*value=)(".*")/m', '${1}"'.$info_param.'"', $file_content, 1);
  156. elseif($options == "s")//separated
  157. $file_content = preg_replace("/$default/m", "$var $info_param", $file_content, 1);
  158. elseif ($options == "q")//quoted
  159. $file_content = preg_replace("/$default/m", "$var\"$info_param\"", $file_content, 1);
  160. elseif ($options == "sq")//separated & quoted
  161. $file_content = preg_replace("/$default/m", "$var \"$info_param\"", $file_content, 1);
  162. elseif ($options == "sc")//separated & ending with a comma (used in JC2MP Example)
  163. $file_content = preg_replace("/$default/m", "$var $info_param,", $file_content, 1);
  164. elseif ($options == "sqc")//separated & quoted & ending with a comma
  165. $file_content = preg_replace("/$default/m", "$var \"$info_param\",", $file_content, 1);
  166. elseif ($options == "key-regex")//replace %key% in <var> and use a regular expression
  167. {
  168. $var = str_replace("%key%", $info_param, $var);
  169. $file_content = preg_replace("/$default/m", "$var", $file_content, 1);
  170. }
  171. else
  172. $file_content = preg_replace("/$default/m", "$var$info_param", $file_content, 1);
  173. }
  174. if ( get_magic_quotes_gpc() )
  175. $file_content=stripslashes($file_content);
  176. }
  177. //echo "<xmp>".$file_content."</xmp>";
  178. $remote->remote_writefile($server_home['home_path'] . "/" . $filepath, $file_content);
  179. }
  180. ?>