Permalink
Browse files

Make it possible to edit config options in adm_config_report.php

Use CONFIG_TYPE_xxx constants instead of magic strings to define the
type of config value to process.

Added code for FLOAT type which was previously handled through COMPLEX.

Improve handling of INT (and FLOAT) by calling constant_replace(),
allowing user to specify a defined constant instead of a numeric value.

The 'Username', 'Project Name' and 'Configuration Option' fields in the
'Set Configuration Option' form are preset to the corresponding value
from the filter or defaulting to ALL_USERS, ALL_PROJECTS and blank
respectively if the filter is not defined or set to '[any]'. This allows
easier definition of related config, e.g. for a given project or user.

Port of 1.2.x commits
- 8890b21
- 8b426cf
- 5858a65

Fixes #7586, #15416
  • Loading branch information...
1 parent 0c81929 commit 57f574098ee6a852e5546923b72fbfcfc56a5904 @dregad dregad committed Mar 8, 2013
Showing with 157 additions and 82 deletions.
  1. +97 −40 adm_config_report.php
  2. +57 −41 adm_config_set.php
  3. +1 −0 core/constant_inc.php
  4. +2 −1 core/print_api.php
View
@@ -63,37 +63,41 @@
print_manage_menu( 'adm_config_report.php' );
print_manage_config_menu( 'adm_config_report.php' );
+$t_config_types = array(
+ CONFIG_TYPE_DEFAULT => 'default',
+ CONFIG_TYPE_INT => 'integer',
+ CONFIG_TYPE_FLOAT => 'float',
+ CONFIG_TYPE_COMPLEX => 'complex',
+ CONFIG_TYPE_STRING => 'string',
+);
+
function get_config_type( $p_type ) {
- switch( $p_type ) {
- case CONFIG_TYPE_INT:
- return "integer";
- case CONFIG_TYPE_FLOAT:
- return "float";
- case CONFIG_TYPE_COMPLEX:
- return "complex";
- case CONFIG_TYPE_STRING:
- default:
- return "string";
+ global $t_config_types;
+ if( array_key_exists( $p_type, $t_config_types ) ) {
+ return $t_config_types[$p_type];
+ } else {
+ return $t_config_types[CONFIG_TYPE_DEFAULT];
}
}
-function print_config_value_as_string( $p_type, $p_value ) {
+function print_config_value_as_string( $p_type, $p_value, $p_for_display = true ) {
$t_corrupted = false;
switch( $p_type ) {
+ case CONFIG_TYPE_DEFAULT:
+ return;
case CONFIG_TYPE_FLOAT:
- $t_value = (float)$p_value;
- echo $t_value;
+ echo (float)$p_value;
return;
case CONFIG_TYPE_INT:
- $t_value = (integer)$p_value;
- echo $t_value;
+ echo (integer)$p_value;
return;
case CONFIG_TYPE_STRING:
- $t_value = config_eval( $p_value );
- echo '<p id="adm-config-value">';
- echo string_nl2br( string_html_specialchars( "'$t_value'" ) );
- echo '</p>';
+ $t_value = string_nl2br( string_html_specialchars( config_eval( $p_value ) ) );
+ if( $p_for_display ) {
+ $t_value = '<p id="adm-config-value">' . "'$t_value'" . '</p>';
+ }
+ echo $t_value;
return;
case CONFIG_TYPE_COMPLEX:
$t_value = @unserialize( $p_value );
@@ -106,18 +110,17 @@ function print_config_value_as_string( $p_type, $p_value ) {
break;
}
- echo '<pre id="adm-config-value">';
-
if ( $t_corrupted ) {
+ $t_output = $p_for_display ? lang_get( 'configuration_corrupted' ) : '';
} else {
- if ( function_exists( 'var_export' ) ) {
- var_export( $t_value );
- } else {
- print_r( $t_value );
- }
+ $t_output = var_export( $t_value, true );
}
- echo '</pre>';
+ if( $p_for_display ) {
+ echo '<pre id="adm-config-value">' . string_attribute( $t_output ) . '</pre>';
+ } else {
+ echo $t_output;
+ }
}
function print_option_list_from_array( $p_array, $p_filter_value ) {
@@ -175,6 +178,13 @@ function print_option_list_from_array( $p_array, $p_filter_value ) {
}
}
+# Get config edit values
+$t_edit_user_id = gpc_get_int( 'user_id', $t_filter_user_value == META_FILTER_NONE ? ALL_USERS : $t_filter_user_value );
+$t_edit_project_id = gpc_get_int( 'project_id', $t_filter_project_value == META_FILTER_NONE ? ALL_PROJECTS : $t_filter_project_value );
+$t_edit_option = gpc_get_string( 'config_option', $t_filter_config_value == META_FILTER_NONE ? '' : $t_filter_config_value );
+$t_edit_type = gpc_get_string( 'type', CONFIG_TYPE_DEFAULT );
+$t_edit_value = gpc_get_string( 'value', '' );
+
# Apply filters
$t_config_table = db_get_table( 'config' );
$t_project_table = db_get_table( 'project' );
@@ -344,24 +354,56 @@ function print_option_list_from_array( $p_array, $p_filter_value ) {
<?php if ( $t_read_write_access ): ?>
<td >
<?php
- if( config_can_delete( $v_config_id ) ) {
- print_button( "adm_config_delete.php?user_id=$v_user_id&project_id=$v_project_id&config_option=$v_config_id", lang_get( 'delete_link' ) );
+ if (
+ config_can_delete( $v_config_id )
+ && access_has_global_level( config_get( 'set_configuration_threshold' ) )
+ ) {
+ # Update button (will populate edit form at page bottom)
+ print_button(
+ '#config_set_form',
+ lang_get( 'edit_link' ),
+ array(
+ 'filter_user_id' => $t_filter_user_value,
+ 'filter_project_id' => $t_filter_project_value,
+ 'filter_config_id' => $t_filter_config_value,
+ 'user_id' => $v_user_id,
+ 'project_id' => $v_project_id,
+ 'config_option' => $v_config_id,
+ 'type' => $v_type,
+ 'value' => $v_value,
+ )
+ );
+
+ # Delete button
+ print_button(
+ 'adm_config_delete.php',
+ lang_get( 'delete_link' ),
+ array(
+ 'user_id' => $v_user_id,
+ 'project_id' => $v_project_id,
+ 'config_option' => $v_config_id,
+ )
+ );
} else {
echo '&#160;';
}
?>
</td>
<?php endif; ?>
- </tr><?php
- } # end for loop ?>
+ </tr>
+<?php
+ } # end for loop
+?>
</table>
</div>
</div>
+
<?php
# Only display the edit form if user is authorized to change configuration
if ( $t_read_write_access ) { ?>
<div id="config-edit-div" class="form-container">
- <form method="post" action="adm_config_set.php">
+ <!-- Config Set Form -->
+ <form id="config_set_form" method="post" action="adm_config_set.php">
<fieldset>
<legend><span><?php echo lang_get( 'set_configuration_option' ) ?></span></legend>
<?php echo form_security_field( 'adm_config_set' ) ?>
@@ -371,7 +413,10 @@ function print_option_list_from_array( $p_array, $p_filter_value ) {
<label for="config-user-id"><span><?php echo lang_get( 'username' ) ?></span></label>
<span class="select">
<select id="config-user-id" name="user_id">
- <option value="0" selected="selected"><?php echo lang_get( 'all_users' ); ?></option>
+ <option value="<?php echo ALL_USERS; ?>"
+ <?php check_selected( $t_edit_user_id, ALL_USERS ) ?>>
+ <?php echo lang_get( 'all_users' ); ?>
+ </option>
<?php print_user_option_list( 0 ) ?>
</select>
</span>
@@ -383,7 +428,10 @@ function print_option_list_from_array( $p_array, $p_filter_value ) {
<label for="config-project-id"><span><?php echo lang_get( 'project_name' ) ?></span></label>
<span class="select">
<select id="config-project-id" name="project_id">
- <option value="0" selected="selected"><?php echo lang_get( 'all_projects' ); ?></option>
+ <option value="<?php echo ALL_PROJECTS; ?>"
+ <?php check_selected( $t_edit_project_id, ALL_PROJECTS ); ?>>
+ <?php echo lang_get( 'all_projects' ); ?>
+ </option>
<?php print_project_option_list( ALL_PROJECTS, false ) ?>
</select>
</span>
@@ -393,7 +441,11 @@ function print_option_list_from_array( $p_array, $p_filter_value ) {
<!-- Config option name -->
<div class="field-container <?php echo helper_alternate_class_no_attribute(); ?>">
<label for="config-option"><span><?php echo lang_get( 'configuration_option' ) ?></span></label>
- <span class="input"><input type="text" id="config-option" name="config_option" value="" size="64" maxlength="64" /></span>
+ <span class="input">
+ <input type="text" name="config_option"
+ value="<?php echo $t_edit_option; ?>"
+ size="64" maxlength="64" />
+ </span>
<span class="label-style"></span>
</div>
@@ -402,10 +454,13 @@ function print_option_list_from_array( $p_array, $p_filter_value ) {
<label for="config-type"><span><?php echo lang_get( 'configuration_option_type' ) ?></span></label>
<span class="select">
<select id="config-type" name="type">
- <option value="default" selected="selected">default</option>
- <option value="string">string</option>
- <option value="integer">integer</option>
- <option value="complex">complex</option>
+ <?php
+ foreach( $t_config_types as $t_key => $t_type ) {
+ echo '<option value="' . $t_key . '" ';
+ check_selected( $t_key, $t_edit_type );
+ echo ">$t_type</option>";
+ }
+ ?>
</select>
</span>
<span class="label-style"></span>
@@ -415,7 +470,9 @@ function print_option_list_from_array( $p_array, $p_filter_value ) {
<div class="field-container <?php echo helper_alternate_class_no_attribute(); ?>">
<label for="config-value"><span><?php echo lang_get( 'configuration_option_value' ) ?></span></label>
<span class="textarea">
- <textarea id="config-value" name="value" cols="80" rows="10"></textarea>
+ <textarea name="value" cols="80" rows="10"><?php
+ echo print_config_value_as_string( $t_edit_type, $t_edit_value, false );
+ ?></textarea>
</span>
<span class="label-style"></span>
</div>
View
@@ -80,52 +80,66 @@
trigger_error( ERROR_CONFIG_OPT_CANT_BE_SET_IN_DB, ERROR );
}
-if ( $f_type === 'default' ) {
+# For 'default', behavior is based on the global variable's type
+if( $f_type == CONFIG_TYPE_DEFAULT ) {
$t_config_global_value = config_get_global( $f_config_option );
if ( is_string( $t_config_global_value ) ) {
- $t_type = 'string';
+ $t_type = CONFIG_TYPE_STRING;
} else if ( is_int( $t_config_global_value ) ) {
- $t_type = 'integer';
- } else { # note that we consider bool and float as complex. We use ON/OFF for bools which map to numeric.
- $t_type = 'complex';
+ $t_type = CONFIG_TYPE_INT;
+ } else if( is_float( $t_config_global_value ) ) {
+ $t_type = CONFIG_TYPE_FLOAT;
+ } else {
+ # note that we consider bool and float as complex.
+ # We use ON/OFF for bools which map to numeric.
+ $t_type = CONFIG_TYPE_COMPLEX;
}
} else {
$t_type = $f_type;
}
-if ( $t_type === 'string' ) {
- $t_value = $f_value;
-} else if ( $t_type === 'integer' ) {
- $t_value = (integer)$f_value;
-} else {
- # We support these kind of variables here:
- # 1. constant values (like the ON/OFF switches): they are defined as constants mapping to numeric values
- # 2. simple arrays with the form: array( a, b, c, d )
- # 3. associative arrays with the form: array( a=>1, b=>2, c=>3, d=>4 )
- # TODO: allow multi-dimensional arrays, allow commas and => within strings
- $t_full_string = trim( $f_value );
- if ( preg_match('/array[\s]*\((.*)\)/s', $t_full_string, $t_match ) === 1 ) {
- // we have an array here
- $t_values = explode( ',', trim( $t_match[1] ) );
- foreach ( $t_values as $key => $value ) {
- if ( !trim( $value ) ) {
- continue;
- }
- $t_split = explode( '=>', $value, 2 );
- if ( count( $t_split ) == 2 ) {
- // associative array
- $t_new_key = constant_replace( trim( $t_split[0], " \t\n\r\0\x0B\"'" ) );
- $t_new_value = constant_replace( trim( $t_split[1], " \t\n\r\0\x0B\"'" ) );
- $t_value[ $t_new_key ] = $t_new_value;
- } else {
- // regular array
- $t_value[ $key ] = constant_replace( trim( $value, " \t\n\r\0\x0B\"'" ) );
+switch( $t_type ) {
+ case CONFIG_TYPE_STRING:
+ $t_value = $f_value;
+ break;
+ case CONFIG_TYPE_INT:
+ $t_value = (integer) constant_replace( trim( $f_value ) );
+ break;
+ case CONFIG_TYPE_FLOAT:
+ $t_value = (float) constant_replace( trim( $f_value ) );
+ break;
+ case CONFIG_TYPE_COMPLEX:
+ default:
+ # We support these kind of variables here:
+ # 1. constant values (like the ON/OFF switches): they are
+ # defined as constants mapping to numeric values
+ # 2. simple arrays with the form: array( a, b, c )
+ # 3. associative arrays with the form: array( a=>1, b=>2, c=>3 )
+ # @TODO: allow multi-dimensional arrays, allow commas and => within strings (see #13298)
+ $t_full_string = trim( $f_value );
+ if ( preg_match('/array[\s]*\((.*)\)/s', $t_full_string, $t_match ) === 1 ) {
+ # we have an array here
+ $t_values = explode( ',', trim( $t_match[1] ) );
+ foreach ( $t_values as $key => $value ) {
+ if ( !trim( $value ) ) {
+ continue;
+ }
+ $t_split = explode( '=>', $value, 2 );
+ if ( count( $t_split ) == 2 ) {
+ # associative array
+ $t_new_key = constant_replace( trim( $t_split[0], " \t\n\r\0\x0B\"'" ) );
+ $t_new_value = constant_replace( trim( $t_split[1], " \t\n\r\0\x0B\"'" ) );
+ $t_value[ $t_new_key ] = $t_new_value;
+ } else {
+ # regular array
+ $t_value[ $key ] = constant_replace( trim( $value, " \t\n\r\0\x0B\"'" ) );
+ }
}
+ } else {
+ # scalar value
+ $t_value = constant_replace( $t_full_string );
}
- } else {
- // scalar value
- $t_value = constant_replace( trim( $t_full_string ) );
- }
+ break;
}
config_set( $f_config_option, $t_value, $f_user_id, $f_project_id );
@@ -136,13 +150,15 @@
/**
- * Check if the passed string is a constant and return its value
+ * Check if the passed string is a constant and returns its value
+ * if yes, or the string itself if not
+ * @param $p_name string to check
+ * @return mixed|string value of constant $p_name, or $p_name itself
*/
function constant_replace( $p_name ) {
- $t_result = $p_name;
if ( is_string( $p_name ) && defined( $p_name ) ) {
- // we have a constant
- $t_result = constant( $p_name );
+ # we have a constant
+ return constant( $p_name );
}
- return $t_result;
+ return $p_name;
}
View
@@ -475,6 +475,7 @@
define( 'TOKEN_EXPIRY_COLLAPSE', 365 * 24 * 60 * 60 );
# config types
+define( 'CONFIG_TYPE_DEFAULT', 0 );
define( 'CONFIG_TYPE_INT', 1 );
define( 'CONFIG_TYPE_STRING', 2 );
define( 'CONFIG_TYPE_COMPLEX', 3 );
View
@@ -1177,7 +1177,8 @@ function print_button( $p_action_page, $p_label, $p_args_to_post = null ) {
if( $p_args_to_post !== null ) {
foreach( $p_args_to_post as $t_var => $t_value ) {
- echo "<input type=\"hidden\" name=\"$t_var\" value=\"$t_value\" />";
+ echo '<input type="hidden" name="' . $t_var .
+ '" value="' . htmlentities( $t_value ) . '" />';
}
}

0 comments on commit 57f5740

Please sign in to comment.