Skip to content

Commit

Permalink
Don't show complex values in adm_config_report
Browse files Browse the repository at this point in the history
To save page space, don't show complex values in adm_config_report page.
Use an on-demand retrieval of the content through a rest api call.
  • Loading branch information
cproensa authored and dregad committed Jul 15, 2019
1 parent 98080bb commit 7924e4c
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 52 deletions.
66 changes: 14 additions & 52 deletions adm_config_report.php
Expand Up @@ -56,6 +56,7 @@

$t_read_write_access = access_has_global_level( config_get( 'set_configuration_threshold' ) );

require_js( 'adm_config_report.js' );
layout_page_header( lang_get( 'configuration_report' ) );
layout_page_begin( 'manage_overview_page.php' );

Expand Down Expand Up @@ -85,56 +86,6 @@ function get_config_type( $p_type ) {
}
}

/**
* Display a given config value appropriately
* @param integer $p_type Configuration type id.
* @param mixed $p_value Configuration value.
* @param boolean $p_for_display Whether to pass the value via string attribute for web browser display.
* @return void
*/
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:
echo (float)$p_value;
return;
case CONFIG_TYPE_INT:
echo (integer)$p_value;
return;
case CONFIG_TYPE_STRING:
$t_value = string_html_specialchars( config_eval( $p_value ) );
if( $p_for_display ) {
$t_value = '<p id="adm-config-value">\'' . string_nl2br( $t_value ) . '\'</p>';
}
echo $t_value;
return;
case CONFIG_TYPE_COMPLEX:
$t_value = @json_decode( $p_value, true );
if( $t_value === false ) {
$t_corrupted = true;
}
break;
default:
$t_value = config_eval( $p_value );
break;
}

if( $t_corrupted ) {
$t_output = $p_for_display ? lang_get( 'configuration_corrupted' ) : '';
} else {
$t_output = var_export( $t_value, true );
}

if( $p_for_display ) {
echo '<pre id="adm-config-value">' . string_attribute( $t_output ) . '</pre>';
} else {
echo string_attribute( $t_output );
}
}

/**
* Generate an html option list for the given array
* @param array $p_array Array.
Expand Down Expand Up @@ -446,6 +397,17 @@ function check_config_value( $p_config ) {
while( $t_row = $t_config_query->fetch() ) {
extract( $t_row, EXTR_PREFIX_ALL, 'v' );

if( CONFIG_TYPE_COMPLEX == $v_type ) {
$t_html_value = '<div class="adm_config_expand" data-config_id="' . $v_config_id . '"'
. ' data-project_id="' . $v_project_id . '" data-user_id="' . $v_user_id . '">'
. '<span class ="expand_show"><a href="#" class="toggle small">[' . lang_get( 'show_content' ) . ']</a></span>'
. '<span class ="expand_hide hidden"><a href="#" class="toggle small">[' . lang_get( 'hide_content' ) . ']</a></span>'
. '<div class="expand_content"></div>'
. '</div>';
} else {
$t_html_value = config_get_value_as_string( $v_type, $v_value );
}

?>
<!-- Repeated Info Rows -->
<tr class="visible-on-hover-toggle">
Expand All @@ -455,7 +417,7 @@ function check_config_value( $p_config ) {
<td><?php echo string_display_line( project_get_name( $v_project_id, false ) ) ?></td>
<td><?php echo string_display_line( $v_config_id ) ?></td>
<td><?php echo string_display_line( get_config_type( $v_type ) ) ?></td>
<td style="overflow-x:auto;"><?php print_config_value_as_string( $v_type, $v_value ) ?></td>
<td style="overflow-x:auto;"><?php echo $t_html_value ?></td>
<td><?php echo get_enum_element( 'access_levels', $v_access_reqd ) ?></td>
<?php
if( $t_read_write_access ) {
Expand Down Expand Up @@ -629,7 +591,7 @@ function check_config_value( $p_config ) {
</td>
<td>
<textarea class="form-control" name="value" cols="80" rows="10"><?php
print_config_value_as_string( $t_edit_type, $t_edit_value, false );
echo config_get_value_as_string( $t_edit_type, $t_edit_value, false );
?></textarea>
</td>
</tr>
Expand Down
34 changes: 34 additions & 0 deletions api/rest/restcore/internal_rest.php
Expand Up @@ -29,6 +29,7 @@
*/
$g_app->group('/internal', function() use ( $g_app ) {
$g_app->any( '/autocomplete', 'rest_internal_autocomplete' );
$g_app->any( '/config_display', 'rest_internal_config_display' );
});

/**
Expand Down Expand Up @@ -62,3 +63,36 @@ function rest_internal_autocomplete( \Slim\Http\Request $p_request, \Slim\Http\R

return $p_response->withStatus( HTTP_STATUS_SUCCESS )->withJson( $t_matches );
}

function rest_internal_config_display( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_config_id = $p_request->getParam( 'config_id' );
$t_user_id = $p_request->getParam( 'user_id' );
$t_project_id = $p_request->getParam( 'project_id' );

if( !access_has_global_level( config_get( 'view_configuration_threshold' ), auth_get_current_user_id() ) ) {
return $p_response->withStatus( HTTP_STATUS_FORBIDDEN );
}
if( null === $t_user_id || null === $t_project_id || null === $t_config_id ) {
$t_message = "Missing parameters";
return $p_response->withStatus( HTTP_STATUS_BAD_REQUEST, $t_message );
}

$t_sql = 'SELECT config_id, user_id, project_id, type, value, access_reqd FROM {config}'
. ' WHERE user_id = :user_id AND project_id = :project_id AND config_id = :config_id';
$t_params = array(
'user_id' => $t_user_id,
'project_id' => $t_project_id,
'config_id' => $t_config_id
);
$t_query = new DbQuery( $t_sql, $t_params );
$t_row = $t_query->fetch();

if( !$t_row ) {
$t_message = "Requested option/project/user doesn't exist";
return $p_response->withStatus( HTTP_STATUS_NOT_FOUND, $t_message );
}

$t_output = config_get_value_as_string( $t_row['type'], $t_row['value'], true );

return $p_response->withStatus( HTTP_STATUS_SUCCESS )->write( $t_output );
}
47 changes: 47 additions & 0 deletions core/config_api.php
Expand Up @@ -738,4 +738,51 @@ function config_cache_all() {
$g_cache_config[$t_config][$t_user][$t_project] = $t_row['type'] . ';' . $t_row['value'];
$g_cache_config_access[$t_config][$t_user][$t_project] = $t_row['access_reqd'];
}
}

/**
* Display a given config value appropriately
* @param integer $p_type Configuration type id.
* @param mixed $p_value Configuration value.
* @param boolean $p_for_display Whether to pass the value via string attribute for web browser display.
* @return void
*/
function config_get_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:
return (string)(float)$p_value;
case CONFIG_TYPE_INT:
return (string)(integer)$p_value;
case CONFIG_TYPE_STRING:
$t_value = string_html_specialchars( config_eval( $p_value ) );
if( $p_for_display ) {
$t_value = '<p id="adm-config-value">\'' . string_nl2br( $t_value ) . '\'</p>';
}
return $t_value;
case CONFIG_TYPE_COMPLEX:
$t_value = @json_decode( $p_value, true );
if( $t_value === false ) {
$t_corrupted = true;
}
break;
default:
$t_value = config_eval( $p_value );
break;
}

if( $t_corrupted ) {
$t_output = $p_for_display ? lang_get( 'configuration_corrupted' ) : '';
} else {
$t_output = var_export( $t_value, true );
}

if( $p_for_display ) {
return '<pre id="adm-config-value">' . string_attribute( $t_output ) . '</pre>';
} else {
return string_attribute( $t_output );
}
}
49 changes: 49 additions & 0 deletions js/adm_config_report.js
@@ -0,0 +1,49 @@
/*
# Mantis - a php based bugtracking system
# Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
# Mantis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Mantis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mantis. If not, see <http://www.gnu.org/licenses/>.
*/

$(document).ready( function() {
$('.adm_config_expand .expand_hide').hide().removeClass('hidden');
$('.adm_config_expand .expand_hide .toggle')
.click(function(event) {
event.preventDefault();
var container = $(this).closest('.adm_config_expand');
container.find('.expand_hide').hide();
container.find('.expand_show').show();
container.find('.expand_content').hide();
});
$('.adm_config_expand .expand_show .toggle')
.click(function(event) {
event.preventDefault();
var container = $(this).closest('.adm_config_expand');
container.find('.expand_hide').show();
container.find('.expand_show').hide();
// Load contents if not already done and display
var content = container.find('.expand_content');
if (content[0].childElementCount == 0) {
var req_data = {
'config_id': container.data('config_id'),
'project_id': container.data('project_id'),
'user_id': container.data('user_id')
};
content.load('api/rest/internal/config_display',req_data);
}
content.show();
});
});

0 comments on commit 7924e4c

Please sign in to comment.