Skip to content

Commit

Permalink
Fix config parsing issues and improve unit tests
Browse files Browse the repository at this point in the history
PR #796

Fixes #21124, #21136
  • Loading branch information
dregad committed Jul 3, 2016
2 parents 4dcb16c + 5484d10 commit 9ed0b9f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 195 deletions.
4 changes: 2 additions & 2 deletions adm_config_report.php
Expand Up @@ -104,9 +104,9 @@ function print_config_value_as_string( $p_type, $p_value, $p_for_display = true
echo (integer)$p_value;
return;
case CONFIG_TYPE_STRING:
$t_value = string_nl2br( string_html_specialchars( config_eval( $p_value ) ) );
$t_value = string_html_specialchars( config_eval( $p_value ) );
if( $p_for_display ) {
$t_value = '<p id="adm-config-value">\'' . $t_value . '\'</p>';
$t_value = '<p id="adm-config-value">\'' . string_nl2br( $t_value ) . '\'</p>';
}
echo $t_value;
return;
Expand Down
17 changes: 10 additions & 7 deletions adm_config_set.php
Expand Up @@ -90,7 +90,8 @@


# For 'default', behavior is based on the global variable's type
if( $f_type == CONFIG_TYPE_DEFAULT ) {
# If value is empty, process as per default to ensure proper typecast
if( $f_type == CONFIG_TYPE_DEFAULT || empty( $f_value ) ) {
$t_config_global_value = config_get_global( $f_config_option );
if( is_string( $t_config_global_value ) ) {
$t_type = CONFIG_TYPE_STRING;
Expand All @@ -108,13 +109,15 @@
}

# Parse the value
if( $t_type == CONFIG_TYPE_STRING ) {
# Return strings as is
$t_value = $f_value;
} else {
# - Strings are returned as-is
# - Empty values are typecast as appropriate
$t_value = $f_value;
if( $t_type != CONFIG_TYPE_STRING ) {
try {
$t_parser = new ConfigParser( $f_value );
$t_value = $t_parser->parse( ConfigParser::EXTRA_TOKENS_IGNORE );
if( !empty( $f_value ) ) {
$t_parser = new ConfigParser( $f_value );
$t_value = $t_parser->parse( ConfigParser::EXTRA_TOKENS_IGNORE );
}

switch( $t_type ) {
case CONFIG_TYPE_INT:
Expand Down
4 changes: 3 additions & 1 deletion core/classes/ConfigParser.class.php
Expand Up @@ -27,7 +27,7 @@
* Configuration Parser class
*
* Simple PHP code parser for scalar and array types
*
*
* @package MantisBT
* @subpackage classes
*
Expand Down Expand Up @@ -76,6 +76,8 @@ public function parse( $p_extra_tokens = self::EXTRA_TOKENS_ERROR ) {
case T_STRING:
case T_LNUMBER:
case T_DNUMBER:
case '-':
case '+':
$t_result = $this->process_value();
break;

Expand Down
11 changes: 3 additions & 8 deletions core/classes/Tokenizer.class.php
Expand Up @@ -25,10 +25,10 @@

/**
* Tokenizer class
*
*
* Uses PHP's internal token_get_all() function to parse a piece of code
* into tokens
*
*
* @package MantisBT
* @subpackage classes
*/
Expand All @@ -44,15 +44,10 @@ class Tokenizer
* Builds the token array from given code, discarding whitespace and
* trailing semicolons
* @param string $p_code PHP code to tokenize
* @throws Exception if there are no tokens to process
* @throws Exception if given code is not valid
*/
public function __construct( $p_code )
{
if( empty( $p_code ) ) {
throw new Exception( 'No more tokens' );
}

# Check syntax to make sure we get valid PHP code
# prepend 'return' statement to ensure the code is not actually executed
$t_code = 'return; ' . $p_code . ';';
Expand Down Expand Up @@ -182,7 +177,7 @@ public function ensure_matches( $p_value ) {
$p_value = token_name( $p_value );
}
throw new Exception(
'Invalid token: got "' . $this->value() . '", expected "$p_value"'
'Invalid token: got "' . $this->value() . '", expected "' . $p_value . '"'
);
}
$this->pop();
Expand Down

0 comments on commit 9ed0b9f

Please sign in to comment.