$t_values = split(',', trim($t_match[1])); foreach ($t_values as $key => $value) { $t_split = split('=>', $value, 2); if (count($t_split) == 2) { // associative array $t_new_key = constant_replace(trim($t_split[0])); $t_new_value = constant_replace(trim($t_split[1])); $t_value[$t_new_key] = $t_new_value; } else { // regular array $t_value[$key] = constant_replace(trim($value)); } } } else { // scalar value $t_value = constant_replace(trim($t_full_string)); } } } config_set($f_config_option, $t_value, $f_user_id, $f_project_id); print_successful_redirect('adm_config_report.php'); /** * Check if the passed string is a constant and return its value */ 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); }
/** * Helper function to recursively process complex types * We support the following 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 ) * 4. multi-dimensional arrays * commas and '=>' within strings are handled * * @param string $p_value Complex value to process. * @param boolean $p_trim_quotes Whether to trim quotes. * @return parsed variable */ function process_complex_value($p_value, $p_trim_quotes = false) { static $s_regex_array = null; static $s_regex_string = null; static $s_regex_element = null; $t_value = trim($p_value); # Parsing regex initialization if (is_null($s_regex_array)) { $s_regex_array = '^array[\\s]*\\((.*)\\)[;]*$'; $s_regex_string = '[\\w]+' . '|' . "'(?:[^'\\\\]|\\\\.)*'" . '|' . '"(?:[^"\\\\]|\\\\.)*"'; # The following complex regex will parse individual array elements, # taking into consideration sub-arrays, associative arrays and single, # double and un-quoted strings # @TODO dregad reverse pattern logic for sub-array to avoid match on array(xxx)=>array(xxx) $s_regex_element = '(' . '(' . '(?:(?iU:array\\s*(?:\\((?:(?>[^()]+)|(?1))*\\))))' . '|' . $s_regex_string . ')' . '(?:\\s*=>\\s*(?2))?' . ')'; } if (preg_match('/' . $s_regex_array . '/s', $t_value, $t_match) === 1) { # It's an array - process each element $t_processed = array(); if (preg_match_all('/' . $s_regex_element . '/', $t_match[1], $t_elements)) { foreach ($t_elements[0] as $t_key => $t_element) { if (!trim($t_element)) { # Empty element - skip it continue; } # Check if element is associative array preg_match_all('/(' . $s_regex_string . ')\\s*=>\\s*(.*)/', $t_element, $t_split); if (!empty($t_split[0])) { # associative array $t_new_key = constant_replace(trim($t_split[1][0], " \t\n\r\v\"'")); $t_new_value = process_complex_value($t_split[2][0], true); $t_processed[$t_new_key] = $t_new_value; } else { # regular array $t_new_value = process_complex_value($t_element); $t_processed[$t_key] = $t_new_value; } } } return $t_processed; } else { # Scalar value $t_value = trim($t_value, " \t\n\r\v"); if (is_numeric($t_value)) { return (int) $t_value; } # if has quotation marks if (strpos($t_value, "'") !== false || strpos($t_value, '"') !== false) { if ($p_trim_quotes) { $t_value = trim($t_value, "\"'"); } } else { # Only replace constants when no quotation marks exist $t_value = constant_replace($t_value); } return $t_value; } }