From 92718c4769eace1aacbb0e0d2d9fe6f498f4a2fb Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 16 Apr 2001 17:51:52 +0000 Subject: [PATCH] Added fix_vars.php script. --- misc/fix_vars.php | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 misc/fix_vars.php diff --git a/misc/fix_vars.php b/misc/fix_vars.php new file mode 100644 index 00000000..f531e89e --- /dev/null +++ b/misc/fix_vars.php @@ -0,0 +1,61 @@ +\n\n"); +} + +$qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''; +$pwd = $HTTP_ENV_VARS['PWD']; + +foreach (array_slice($argv, 1) as $template) { + $template = $pwd . '/' . $template; + if (!is_file($template)) continue; + + $input = implode('', file($template)); + $fp = fopen($template.'.out', 'w'); + if (!$fp) { + die("\nError: could not open $template.out for writing\n\n"); + } + + if (strnatcmp(PHP_VERSION, '4.0.5') > 0) + $output = preg_replace_callback('!\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*!', 'fix_var', $input); + else + $output = preg_replace('!\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*!F', 'fix_var', $input); + + fwrite($fp, $output); + fclose($fp); + copy($template.'.out', $template); + unlink($template.'.out'); + + print "Fixed $template.\n"; +} + +function fix_var($match) +{ + $var_expr = $match[0]; + + list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2); + + $sections = explode('/', $var_ref); + $props = explode('.', array_pop($sections)); + $var_name = array_shift($props); + + $output = "\$$var_name"; + + foreach ($sections as $section_ref) { + $output .= "[$section_ref]"; + } + if (count($props)) + $output .= ".".implode('.', $props); + + return $output; +} + +?>