function xt_process_template(&$xt, $str) { // parse template file tag by tag $start = 0; $literal = false; $len = strlen($str); while (true) { $pos = strpos($str, "{", $start); if ($pos === false) { echo substr($str, $start, $len - $start); break; } $section = false; $var = false; $message = false; if (substr($str, $pos + 1, 6) == "BEGIN ") { $section = true; } elseif (substr($str, $pos + 1, 1) == '$') { $var = true; } elseif (substr($str, $pos + 1, 14) == 'mlang_message ') { $message = true; } else { // no tag, just '{' char echo substr($str, $start, $pos - $start + 1); $start = $pos + 1; continue; } echo substr($str, $start, $pos - $start); if ($section) { // section $endpos = strpos($str, "}", $pos); if ($endpos === false) { $xt->report_error("Page is broken"); return; } $section_name = trim(substr($str, $pos + 7, $endpos - $pos - 7)); $endtag = "{END " . $section_name . "}"; $endpos1 = strpos($str, $endtag, $endpos); if ($endpos1 === false) { echo "End tag not found:" . htmlspecialchars($endtag); $xt->report_error("Page is broken"); return; } $section = substr($str, $endpos + 1, $endpos1 - $endpos - 1); $start = $endpos1 + strlen($endtag); $var = xt_getvar($xt, $section_name); if ($var === false) { continue; } $begin = ""; $end = ""; if (is_array($var)) { $begin = @$var["begin"]; $end = @$var["end"]; $var = @$var["data"]; } if (!is_array($var)) { // if section echo $begin; xt_process_template($xt, $section); $xt->processVar($end, $varparams); } else { // foreach section echo $begin; $keys = array_keys($var); foreach ($keys as $i) { $xt->xt_stack[] =& $var[$i]; if (is_array($var[$i]) && array_key_exists("begin", $var[$i])) { echo $var[$i]["begin"]; } xt_process_template($xt, $section); array_pop($xt->xt_stack); if (is_array($var[$i]) && array_key_exists("end", $var[$i])) { echo $var[$i]["end"]; } } $xt->processVar($end, $varparams); } } elseif ($var) { // display a variable or call a function $endpos = strpos($str, "}", $pos); if ($endpos === false) { $xt->report_error("Page is broken"); return; } $varparams = array(); $var_name = trim(substr($str, $pos + 2, $endpos - $pos - 2)); if (strpos($var_name, " ") !== FALSE) { $varparams = explode(" ", $var_name); $var_name = $varparams[0]; unset($varparams[0]); } $start = $endpos + 1; $var = xt_getvar($xt, $var_name); if ($var === false) { continue; } $xt->processVar($var, $varparams); } elseif ($message) { $endpos = strpos($str, "}", $pos); if ($endpos === false) { $xt->report_error("Page is broken"); return; } $tag = trim(substr($str, $pos + 15, $endpos - $pos - 15)); $start = $endpos + 1; echo htmlspecialchars(mlang_message($tag)); } } }
/** * Returns variable by name. * @intellisense */ function getvar($name) { return xt_getvar($this, $name); }