PHP Code:
function gettemplate($templatename, $escapecontents = true,
$gethtmlcomments = true, $parsephp = true)
{
// gets a template from the db or from the local cache
global $templatecache, $DB_site, $templatesetid, $addtemplatename;
$templatesetid = intval($templatesetid);
if (isset($templatecache[$templatename]))
{
$toreturn = $templatecache[$templatename];
}
else
{
$template = $DB_site->query_first("SELECT template FROM template
WHERE title = \"" . addslashes($templatename) . "\"
AND (templatesetid = -1 OR templatesetid = $templatesetid)
ORDER BY templatesetid DESC LIMIT 1");
$toreturn = $template['template'];
$templatecache[$templatename] = $toreturn;
}
if (substr_count($template,'?'.'>')>=1 or substr_count($template,'php?'.'>')>=1 or substr_count($template,'</php>')>=1) {
while (preg_match("/(\<\?php|\<\?|\<php\> )(\r\n)*(.*)(\r\n)*(php\?"."\>|\?"."\>|\<\/php\> )/siU", $template, $matches)) {
if ($parsephp) {
preg_match_all("/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/", $matches[3], $varnames);
$varnames = implode(", ", $varnames[0]);
if (!empty($varnames)) {
@eval("global $varnames;");
}
$eval_result = @eval(stripslashes($matches[3]));
} else $eval_result = "";
$template = str_replace($matches[0], $eval_result, $template);
}
}
if ($parsephp) // is this needed still?
{
// regex is mangled to avoid breaking stupid ides
$count = preg_match_all("/<\?(.*)\?" . ">/siU", $toreturn, $matches);
foreach ($matches[1] as $match)
{
// put vars in the global scope
preg_match_all("/\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/",
$match, $varnames);
$varnames = implode(", ", $varnames[0]);
if (!empty($varnames)) eval("global $varnames;");
eval($match);
}
str_replace($matches[0], "", $toreturn);
}
if ($escapecontents)
{
$toreturn = addslashes($toreturn);
$toreturn = str_replace("\'", "'", $toreturn);
}
if ($gethtmlcomments and $addtemplatename)
{
$charcount = strlen($templatecache[$templatename]) .
" chars originally";
$phpcount = ($parsephp ? ", $count embedded PHP tags" : "");
return "<!-- begin template \"$templatename\" ($charcount$phpcount) " .
"-->\n$toreturn\n<!-- END TEMPLATE: $templatename -->";
}
return $toreturn;