<?
//
// qTPL - the simplest template engine yet
//
// quick_template
// param: $tpl = variable containing template structure
// $txt = array to be displayed in the template.
//
function quick_tpl ($tpl, $txt)
{
global $lang, $link;
$escape[] = '{{'; $esc[] = '__l__'; $es[] = '{';
$escape[] = '}}'; $esc[] = '__r__'; $es[] = '}';
$tpl = str_replace ($escape, $esc, $tpl);
$tpl = preg_replace ("/{\\$(l_[a-zA-Z0-9\-_]+)}/e", "\$lang['\\1']", $tpl);
$tpl = preg_replace ("/{\\$([a-zA-Z0-9\-_]+)}/e", "\$txt['\\1']", $tpl);
$tpl = str_replace ($esc, $es, $tpl);
return $tpl;
}
// load sections from a template file
// section is simply a part of template. it is still in raw. it's like loading some small templates in one file.
// section is very useful if you want to use multiple classes/instances in looping.
// BEGINIF won't work in BEGINBLOCK, and viceversa.
function load_section ($path)
{
global $tpl_section;
$tpl = array ();
if (strpos (Cur_Url (), 'includes%2F') OR strpos (Cur_Url (), 'admin%2F')) $path = '../'.$path;
if (!file_exists ($path)) die ("<B>Template $path not found! Contact webmaster.</B>");
$fp = fopen($path,'r');
while(!feof($fp)) $tpl .= fgets($fp,4096);
fclose ($fp);
$tpl = preg_replace_callback ("/<!-- BEGIN(SECTION) (.*?) -->(.*?)<!-- ENDSECTION -->/is", "compile_tpl", $tpl);
return TRUE;
}
// load_tpl
// loding a template file into a varible.
// use quick_tpl to display template
function load_tpl ($path)
{
$tpl = '';
global $tpl_block;
if (substr ($path, -4) == '.tpl')
{
if (strpos (Cur_Url (), 'includes%2F') OR strpos (Cur_Url (), 'admin%2F') OR strpos (Cur_Url (), 'members%2F')) $path = '../'.$path;
if (!file_exists ($path)) die ("<B>Template $path not found! Contact webmaster.</B>");
$fp = fopen($path,'r');
while(!feof($fp)) $tpl .= fgets($fp,4096);
fclose ($fp);
}
else
{
$tpl = $path;
}
$tpl = preg_replace_callback ("/<!-- BEGIN(IF) (.*?) -->(.*?)<!-- ENDIF -->/is", "compile_tpl", $tpl);
$tpl = preg_replace_callback ("/<!-- BEGIN(BLOCK) (.*?) -->(.*?)<!-- ENDBLOCK -->/is", "compile_tpl", $tpl);
return $tpl;
}
function compile_tpl ($args)
{
global $tpl_block, $tpl_section;
$args[1] = strtolower ($args[1]);
switch ($args[1])
{
case 'section':
$tpl_section[$args[2]] = $args[3];
return TRUE;
break;
case 'block':
$tpl_block[$args[2]] = $args[3];
return '{$block_'.$args[2].'}';
break;
case 'if':
if ($p = strpos($args[3], '<!-- ELSE -->')) { $args[4] = substr ($args[3], $p+13); $args[3] = substr ($args[3], 0, $p); }
// useful for BEGINIF - ENDIF & BEGINIF - ELSE - ENDIF
if (empty ($args[4])) $args[4] = '';
$vars = (explode (' ', $args[2]));
$vars[0] = substr ($vars[0], 1);
global $$vars[0];
if (empty ($$vars[0]))
$cmd = 0;
else
$cmd = eval ("if ($args[2]) return 1; else return 0;");
if ($cmd) return $args[3]; else return $args[4];
break;
}
}
// to display/flush a template file
// this is different than quick_tpl, because it has default file & variables
function flush_tpl ()
{
global $txt, $lang, $config, $time_start, $user_logged, $tpl_block;
// end stopwatch
$time_end = getmicrotime();
$content = $enc = '';
$txt['total_mysql_query'] = num_format ($config['total_mysql_query']);
$txt['process_time'] = num_format ($time_end - $time_start, 3);
if (strpos (Cur_Url (), 'includes%2F') OR strpos (Cur_Url (), 'admin%2F') OR strpos (Cur_Url (), 'members%2F'))
$txt['css'] = '../';
else
$txt['css'] = '';
$path = load_tpl ($config['skin'].'/outline.tpl');
// enable ADP?
if (!empty ($txt['main_body']) && is_array ($txt))
{
if (empty ($txt['site_description'])) generate_html_header ();
if ($config['enable_adp'])
$content = adp_convert (quick_tpl ($path, $txt));
else
$content = quick_tpl ($path, $txt);
}
if (is_integer (strpos ($_SERVER["HTTP_ACCEPT_ENCODING"], 'x-gzip'))) $enc = 'x-gzip';
if (is_integer (strpos ($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip'))) $enc = 'gzip';
if ($enc && $config['enable_gzip'] && !headers_sent ())
{
$ori = strlen ($content);
$content = gzencode ($content."<!-- GZIP enabled -->");
header ("Content-Encoding: ".$enc);
echo $content;
}
else
{
echo $content;
}
die;
}
function adp_convert ($tpl)
{
global $config;
if ($config['enable_adp'] == 1)
$search = '/([<a|<img|<script|<link|<form][^>]+)(href=|src=|action=)["|\']([^"|^\']+)["|\'^]([^\>]*)>/i';
elseif ($config['enable_adp'] == 2)
$search = '/([<a][^>]+)(href=)["|\']([^"|^\']+)["|\'^]([^\>]*)>/i';
$tp = preg_replace_callback ($search, 'adp_convert_support', $tpl);
return $tp;
}
// support function for adp_convert
function adp_convert_support ()
{
global $config, $adp2_prg_call, $adp2_prg_alias;
$abs_path = $config['site_url'];
$query_str = array ('?', '=', '&', '&');
$args = func_get_args();
if ((strpos ($args[0][3], 'http://') === false) && (strpos ($args[0][3], 'mailto:') === false))
{
if ($config['enable_adp'] == 1)
{
$args[0][3] = str_replace ($query_str, '/', $args[0][3]);
return $args[0][1].$args[0][2].'"'.$abs_path.'/'.$args[0][3].'"'.$args[0][4].'>';
}
elseif ($config['enable_adp'] == 2)
{
if (is_integer (strpos ($args[0][1], '<a')))
{
$fn = substr ($args[0][3], 0, strpos ($args[0][3], '?'));
if (empty ($fn)) $fn = $args[0][3];
if (in_array ($fn, $adp2_prg_call))
{
$args[0][3] = str_replace ($adp2_prg_call, $adp2_prg_alias, $args[0][3]);
$args[0][3] = str_replace ($query_str, '-', $args[0][3]);
}
}
return $args[0][1].$args[0][2].'"'.$args[0][3].'"'.$args[0][4].'>';
}
}
else
{
return $args[0][0];
}
}
// this will convert ADP path to regular $_GET
function adp_get_param ()
{
global $config, $_GET, $_HTTP_GET_VARS;
if (!empty ($config['adp_get_param'])) return;
$config['adp_get_param'] = 1;
if (isset ($_SERVER['REQUEST_URI']))
{
if ($config['enable_adp'] == 1)
$vardata = explode ('/', $_SERVER['REQUEST_URI']);
elseif ($config['enable_adp'] == 2)
$vardata = explode ('-', $_SERVER['REQUEST_URI']);
$num_param = count ($vardata);
// inject for short_query;
if ($config['short_query']) $_SERVER['QUERY_STRING'] = $vardata[$num_param-1];
if ($num_param % 2 == 0) // $vardata[0] = selalu blank, jadi jk jumlah param ganjil, vardata genap!
{
array_unshift ($vardata, '_TEMP_');
$num_param++;
}
for ($i = 1; $i < $num_param; $i += 2)
{
$_GET[$vardata[$i]] = $HTTP_GET_VARS[$vardata[$i]] = $vardata[$i+1];
// echo $_GET[$vardata[$i]];
}
}
}
?>