diff --git a/core/SSViewer.php b/core/SSViewer.php index 4e5f8194f..56dcbe00a 100755 --- a/core/SSViewer.php +++ b/core/SSViewer.php @@ -460,174 +460,7 @@ class SSViewer { } static function parseTemplateContent($content, $template="") { - // Remove UTF-8 byte order mark: - // This is only necessary if you don't have zend-multibyte enabled. - if(substr($content, 0,3) == pack("CCC", 0xef, 0xbb, 0xbf)) { - $content = substr($content, 3); - } - - // Add template filename comments on dev sites - if(Director::isDev() && self::$source_file_comments && $template && stripos($content, "]*>)/i', "\\1", $content); - $content = preg_replace('/(<\/html[^>]*>)/i', "\\1", $content); - } else { - $content = "\n" . $content . "\n"; - } - } - - // $val, $val.property, $val(param), etc. - $replacements = array( - '/<%--.*--%>/U' => '', - '/\$Iteration/' => '', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+), *([^),]+)\\)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)}/' => 'obj("\\1",array("\\2","\\3"),true)->obj("\\4",null,true)->XML_val("\\5",null,true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+), *([^),]+)\\)\\.([A-Za-z0-9_]+)}/' => 'obj("\\1",array("\\2","\\3"),true)->XML_val("\\4",null,true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+), *([^),]+)\\)}/' => 'XML_val("\\1",array("\\2","\\3"),true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+)\\)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)}/' => 'obj("\\1",array("\\2"),true)->obj("\\3",null,true)->XML_val("\\4",null,true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+)\\)\\.([A-Za-z0-9_]+)}/' => 'obj("\\1",array("\\2"),true)->XML_val("\\3",null,true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+)\\)}/' => 'XML_val("\\1",array("\\2"),true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)}/' => 'obj("\\1",null,true)->obj("\\2",null,true)->XML_val("\\3",null,true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z0-9_]+)}/' => 'obj("\\1",null,true)->XML_val("\\2",null,true) ?>', - '/{\\$([A-Za-z_][A-Za-z0-9_]*)}/' => 'XML_val("\\1",null,true) ?>\\2', - - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z0-9_]+)\\(([^),]+)\\)([^A-Za-z0-9]|$)/' => 'obj("\\1")->XML_val("\\2",array("\\3"),true) ?>\\4', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z0-9_]+)\\(([^),]+), *([^),]+)\\)([^A-Za-z0-9]|$)/' => 'obj("\\1")->XML_val("\\2",array("\\3", "\\4"),true) ?>\\5', - - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+), *([^),]+)\\)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)([^A-Za-z0-9]|$)/' => 'obj("\\1",array("\\2","\\3"),true)->obj("\\4",null,true)->XML_val("\\5",null,true) ?>\\6', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+), *([^),]+)\\)\\.([A-Za-z0-9_]+)([^A-Za-z0-9]|$)/' => 'obj("\\1",array("\\2","\\3"),true)->XML_val("\\4",null,true) ?>\\5', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+), *([^),]+)\\)([^A-Za-z0-9]|$)/' => 'XML_val("\\1",array("\\2","\\3"),true) ?>\\4', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+)\\)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)([^A-Za-z0-9]|$)/' => 'obj("\\1",array("\\2"),true)->obj("\\3",null,true)->XML_val("\\4",null,true) ?>\\5', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+)\\)\\.([A-Za-z0-9_]+)([^A-Za-z0-9]|$)/' => 'obj("\\1",array("\\2"),true)->XML_val("\\3",null,true) ?>\\4', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\(([^),]+)\\)([^A-Za-z0-9]|$)/' => 'XML_val("\\1",array("\\2"),true) ?>\\3', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)([^A-Za-z0-9]|$)/' => 'obj("\\1",null,true)->obj("\\2",null,true)->XML_val("\\3",null,true) ?>\\4', - '/\\$([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z0-9_]+)([^A-Za-z0-9]|$)/' => 'obj("\\1",null,true)->XML_val("\\2",null,true) ?>\\3', - '/\\$([A-Za-z_][A-Za-z0-9_]*)([^A-Za-z0-9]|$)/' => 'XML_val("\\1",null,true) ?>\\2', - ); - - $content = preg_replace(array_keys($replacements), array_values($replacements), $content); - $content = str_replace('{dlr}','$',$content); - - // Cache block - $content = SSViewer_PartialParser::process($template, $content); - - // legacy - $content = ereg_replace('', '<' . '% control \\1 %' . '>', $content); - $content = ereg_replace('', '<' . '% end_control %' . '>', $content); - - // < % control Foo % > - $content = ereg_replace('<' . '% +control +([A-Za-z0-9_]+) +%' . '>', 'obj("\\1")) foreach($loop as $key => $item) { ?>', $content); - // < % control Foo.Bar % > - $content = ereg_replace('<' . '% +control +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', 'obj("\\1")) && ($loop = $loop->obj("\\2"))) foreach($loop as $key => $item) { ?>', $content); - // < % control Foo.Bar(Baz) % > - $content = ereg_replace('<' . '% +control +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)\\(([^),]+)\\) +%' . '>', 'obj("\\1")) && ($loop = $loop->obj("\\2", array("\\3")))) foreach($loop as $key => $item) { ?>', $content); - // < % control Foo(Bar) % > - $content = ereg_replace('<' . '% +control +([A-Za-z0-9_]+)\\(([^),]+)\\) +%' . '>', 'obj("\\1", array("\\2"))) foreach($loop as $key => $item) { ?>', $content); - // < % control Foo(Bar, Baz) % > - $content = ereg_replace('<' . '% +control +([A-Za-z0-9_]+)\\(([^),]+), *([^),]+)\\) +%' . '>', 'obj("\\1", array("\\2","\\3"))) foreach($loop as $key => $item) { ?>', $content); - // < % control Foo(Bar, Baz, Buz) % > - $content = ereg_replace('<' . '% +control +([A-Za-z0-9_]+)\\(([^),]+), *([^),]+), *([^),]+)\\) +%' . '>', 'obj("\\1", array("\\2", "\\3", "\\4"))) foreach($loop as $key => $item) { ?>', $content); - $content = ereg_replace('<' . '% +end_control +%' . '>', '', $content); - $content = ereg_replace('<' . '% +debug +%' . '>', '', $content); - $content = ereg_replace('<' . '% +debug +([A-Za-z0-9_]+) +%' . '>', 'cachedCall("\\1")) ?>', $content); - - // < % if val1.property % > - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', 'obj("\\1",null,true)->hasValue("\\2")) { ?>', $content); - - // < % if val1(parameter) % > - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+)\\(([A-Za-z0-9_-]+)\\) +%' . '>', 'hasValue("\\1",array("\\2"))) { ?>', $content); - - // < % if val1 % > - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+) +%' . '>', 'hasValue("\\1")) { ?>', $content); - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) +%' . '>', 'hasValue("\\1")) { ?>', $content); - - // < % if val1 || val2 % > - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+) *\\|\\|? *([A-Za-z0-9_]+) +%' . '>', 'hasValue("\\1") || $item->hasValue("\\2")) { ?>', $content); - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *\\|\\|? *([A-Za-z0-9_]+) +%' . '>', 'hasValue("\\1") || $item->hasValue("\\2")) { ?>', $content); - - // < % if val1 && val2 % > - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+) *&&? *([A-Za-z0-9_]+) +%' . '>', 'hasValue("\\1") && $item->hasValue("\\2")) { ?>', $content); - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *&&? *([A-Za-z0-9_]+) +%' . '>', 'hasValue("\\1") && $item->hasValue("\\2")) { ?>', $content); - - // < % if val1 == val2 % > - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+) *==? *"?([A-Za-z0-9_-]+)"? +%' . '>', 'XML_val("\\1",null,true) == "\\2") { ?>', $content); - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *==? *"?([A-Za-z0-9_-]+)"? +%' . '>', 'XML_val("\\1",null,true) == "\\2") { ?>', $content); - - // < % if val1 != val2 % > - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+) *!= *"?([A-Za-z0-9_-]+)"? +%' . '>', 'XML_val("\\1",null,true) != "\\2") { ?>', $content); - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *!= *"?([A-Za-z0-9_-]+)"? +%' . '>', 'XML_val("\\1",null,true) != "\\2") { ?>', $content); - - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) +%' . '>', 'cachedCall("\\1")) && ((!is_object($test) && $test) || ($test && $test->exists()) )) { ?>', $content); - - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', 'obj("\\1",null,true)->cachedCall("\\2"); if((!is_object($test) && $test) || ($test && $test->exists())) { ?>', $content); - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', 'obj("\\1",null,true)->cachedCall("\\2")) && ((!is_object($test) && $test) || ($test && $test->exists()) )) { ?>', $content); - - $content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', 'obj("\\1",null,true)->obj("\\2",null,true)->cachedCall("\\3"); if((!is_object($test) && $test) || ($test && $test->exists())) { ?>', $content); - $content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', 'obj("\\1",null,true)->obj("\\2",null,true)->cachedCall("\\3")) && ((!is_object($test) && $test) || ($test && $test->exists()) )) { ?>', $content); - - $content = ereg_replace('<' . '% +else +%' . '>', '', $content); - $content = ereg_replace('<' . '% +end_if +%' . '>', '', $content); - - // i18n - get filename of currently parsed template - // CAUTION: No spaces allowed between arguments for all i18n calls! - ereg('.*[\/](.*)',$template,$path); - - // i18n _t(...) - with entity only (no dots in namespace), - // meaning the current template filename will be added as a namespace. - // This applies only to "root" templates, not includes which should always have their namespace set already. - // See getTemplateContent() for more information. - $content = ereg_replace('<' . '% +_t\((\'([^\.\']*)\'|"([^\."]*)")(([^)]|\)[^ ]|\) +[^% ])*)\) +%' . '>', '', $content); - // i18n _t(...) - $content = ereg_replace('<' . '% +_t\((\'([^\']*)\'|"([^"]*)")(([^)]|\)[^ ]|\) +[^% ])*)\) +%' . '>', '', $content); - - // i18n sprintf(_t(...),$argument) with entity only (no dots in namespace), meaning the current template filename will be added as a namespace - $content = ereg_replace('<' . '% +sprintf\(_t\((\'([^\.\']*)\'|"([^\."]*)")(([^)]|\)[^ ]|\) +[^% ])*)\),\<\?= +([^\?]*) +\?\>) +%' . '>', '', $content); - // i18n sprintf(_t(...),$argument) - $content = ereg_replace('<' . '% +sprintf\(_t\((\'([^\']*)\'|"([^"]*)")(([^)]|\)[^ ]|\) +[^% ])*)\),\<\?= +([^\?]*) +\?\>) +%' . '>', '', $content); - - // isnt valid html? !? - $content = ereg_replace('<' . '% +base_tag +%' . '>', '', $content); - - $content = ereg_replace('<' . '% +current_page +%' . '>', '', $content); - - // change < % require x() % > calls to corresponding Requirement::x() ones, including 0, 1 or 2 options - $content = preg_replace('/<% +require +([a-zA-Z]+)(?:\(([^),]+)\))? +%>/', '', $content); - $content = preg_replace('/<% +require +([a-zA-Z]+)\(([^),]+), *([^),]+)\) +%>/', '', $content); - - - // Add include filename comments on dev sites - if(Director::isDev() && self::$source_file_comments) $replacementCode = 'return "\n" - . "" - . "\n";'; - else $replacementCode = 'return "";'; - - $content = preg_replace_callback('/<' . '% include +([A-Za-z0-9_]+) +%' . '>/', create_function( - '$matches', $replacementCode - ), $content); - - // legacy - $content = ereg_replace('', 'cachedCall("\\1")) { ?>', $content); - $content = ereg_replace('', '', $content); - $content = ereg_replace('', '', $content); - - // Fix link stuff - $content = ereg_replace('href *= *"#', 'href="#', $content); - - // Protect xml header - $content = ereg_replace('<\?xml([^>]+)\?' . '>', '<##xml\\1##>', $content); - - // Turn PHP file into string definition - $content = str_replace('',";\n \$val .= <<]+)##>', '<' . '?xml\\1?' . '>', $output); - - return $output; + return SSTemplateParser::compileString($content, $template, Director::isDev() && self::$source_file_comments); } /**