custom_fields.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 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. require_once("modules/config_games/server_config_parser.php");
  25. require_once('includes/form_table_class.php');
  26. $custom_fields = array();
  27. $isAdmin = false;
  28. function renderCustomFields($field, $home_id)
  29. {
  30. global $db, $custom_fields, $isAdmin;
  31. $attributesString = "";
  32. $disabledString = ((!property_exists($field, 'access') || $field->access != "admin") || $isAdmin) ? "" : "disabled ";
  33. foreach ($field->attribute as $attribute)
  34. $attributesString .= $attribute['key']. "='$attribute' ";
  35. if (is_array($custom_fields) and array_key_exists((string)$field['key'], $custom_fields))
  36. $fieldValue = (string)$custom_fields[(string)$field['key']];
  37. else
  38. $fieldValue = (string)$field->default_value;
  39. $idString = "id='".clean_id_string($field['key'])."'";
  40. $nameString = "name='fields[".$field['key']."]'";
  41. $fieldType = $field['type'];
  42. if ($fieldType == "select")
  43. {
  44. $inputElementString = "<select $idString $nameString $disabledString>";
  45. foreach ($field->option as $option)
  46. {
  47. $optionValue = (string)($option['value']);
  48. $selectedString = ($optionValue == $fieldValue) ? "selected='selected'" : "";
  49. $valueString = "value=\"".str_replace('"', "&quot;", strip_real_escape_string($optionValue))."\"";
  50. $inputElementString .= "<option $selectedString $valueString>$option</option>";
  51. }
  52. $inputElementString .="</select>";
  53. } else
  54. {
  55. if ($fieldType == "checkbox_key_value") {
  56. if ($fieldValue) // convert the XML object to string
  57. $attributesString .= "checked='checked' ";
  58. $fieldValue = $field['key'];
  59. $fieldType = "checkbox";
  60. }
  61. else if ($fieldType == "checkbox")
  62. {
  63. if ($fieldValue) // convert the XML object to string
  64. $attributesString .= "checked='checked' ";
  65. }
  66. $inputElementString = "<input $idString $nameString ".
  67. "type='$fieldType' value=\"".str_replace('"', "&quot;", strip_real_escape_string($fieldValue))."\" $attributesString $disabledString/>";
  68. }
  69. echo "<tr><td class='right'><label for='".clean_id_string($field['key'])."'>".$field['key'].
  70. ":</label></td><td class='left'>$inputElementString<label for='".clean_id_string($field['key'])."'>";
  71. if ( !empty($field->caption) )
  72. echo $field->caption;
  73. if ( !empty($field->desc) )
  74. echo "<br/><span class='info'>(".$field->desc.")</span>";
  75. echo "</label></td></tr>\n";
  76. }
  77. function exec_ogp_module()
  78. {
  79. global $db,$view,$custom_fields,$isAdmin;
  80. $home_id = $_GET['home_id'];
  81. $isAdmin = $db->isAdmin( $_SESSION['user_id'] );
  82. if( $isAdmin )
  83. {
  84. $home_info = $db->getGameHome($home_id);
  85. $custom_fileds_access_enabled = TRUE;
  86. }
  87. else
  88. {
  89. $home_info = $db->getUserGameHome($_SESSION['user_id'],$home_id);
  90. $custom_fileds_access_enabled = preg_match("/c/",$home_info['access_rights']) > 0 ? TRUE : FALSE;
  91. }
  92. if( !$home_info OR !$custom_fileds_access_enabled )
  93. return;
  94. //get used custom value or get default
  95. $customFieldsFromDB = $db->getCustomFields($home_id);
  96. $custom_fields = $customFieldsFromDB !== false ? json_decode($customFieldsFromDB, True) : array();
  97. $server_xml = read_server_config(SERVER_CONFIG_LOCATION.$home_info['home_cfg_file']);
  98. include_once('includes/lib_remote.php');
  99. $remote = new OGPRemoteLibrary($home_info['agent_ip'],$home_info['agent_port'],$home_info['encryption_key'],$home_info['timeout']);
  100. echo "<h2>".get_lang('editing_home_called')." \"".htmlentities($home_info['home_name'])."\"</h2>";
  101. echo "<p>";
  102. echo "<a href='?m=gamemanager&p=game_monitor&home_id=$home_id'>&lt;&lt; ".get_lang('back_to_game_monitor')."</a>";
  103. echo "</p>";
  104. if(isset($_POST['update_settings']))
  105. {
  106. $save_field = $_POST['fields'];
  107. $updatedSettings = array();
  108. foreach($server_xml->custom_fields->field as $field)
  109. {
  110. if (array_key_exists((string)$field['key'], (array)$custom_fields)){
  111. $origValue = (string)$custom_fields[(string)$field['key']];
  112. }else{
  113. $origValue = "";
  114. }
  115. $found = 0;
  116. foreach ($save_field as $key => $value )
  117. {
  118. if($key == (string)$field['key']){
  119. $found++;
  120. // If locked by an admin, ignore the value posted by the user
  121. $lockedByAdmin = false;
  122. if(property_exists($field, 'access') && $field->access == "admin")
  123. {
  124. $lockedByAdmin = true;
  125. if(!$isAdmin){
  126. $value = $origValue; // Set it to the old saved value (which was last set by an admin) or set it to its default value
  127. }
  128. }
  129. if (strlen(trim($value)) > 0) {
  130. $updatedSettings[$key] = $value;
  131. }
  132. break;
  133. }
  134. }
  135. if($found == 0 && !empty($origValue)){
  136. $updatedSettings[(string)$field['key']] = $origValue;
  137. }
  138. }
  139. if(is_array($updatedSettings) && count($updatedSettings) > 0){
  140. $db->changeCustomFields($home_info['home_id'],json_encode($updatedSettings));
  141. }else{
  142. $db->changeCustomFields($home_info['home_id'],"");
  143. }
  144. print_success(get_lang('settings_updated'));
  145. $view->refresh("?m=user_games&p=custom_fields&home_id=".$home_id);
  146. }
  147. $ft = new FormTable();
  148. $ft->start_form("", "post", "autocomplete=\"off\"");
  149. $ft->start_table();
  150. foreach($server_xml->custom_fields->field as $field)
  151. {
  152. renderCustomFields($field, $home_info['home_id']);
  153. }
  154. $ft->end_table();
  155. $ft->add_button("submit","update_settings",get_lang('update_settings'));
  156. $ft->end_form();
  157. }
  158. ?>