* @version 1.0.3 (February 02, 2005)
* @package AutoIndex
*/
class Template
{
/**
* @var string The final output
*/
protected $out;
/**
* @param array $m The array given by preg_replace_callback()
* @return string Looks up $m[1] in word list and returns match
*/
private static function callback_words($m)
{
global $words;
return $words->__get(strtolower($m[1]));
}
/**
* @param array $m The array given by preg_replace_callback()
* @return string The parsed template of filename $m[1]
*/
private static function callback_include($m)
{
$temp = new Template($m[1]);
return $temp->__toString();
}
/**
* @param array $m The array given by preg_replace_callback()
* @return string The setting for the config value $m[1]
*/
private static function callback_config($m)
{
global $config;
return $config->__get(strtolower($m[1]));
}
/**
* Parses the text in $filename and sets the result to $out. We cannot
* use ExceptionDisplay here if there is an error, since it uses the
* template system.
*
* Steps to parse the template:
* - remove comments
* - replace {info} variables
* - replace {words} strings
* - replace {config} variables
* - include other files when we see the {include} statement
*
* @param string $filename The name of the file to parse
*/
public function __construct($filename)
{
global $config, $dir, $subdir, $words, $mobile_device_detect;
$full_filename = $config ->__get('template') . $filename;
if (!@is_file($full_filename))
{
throw new ExceptionFatal('Template file ' . Url::html_output($full_filename) . ' cannot be found.');
}
//read raw file contents
$contents = file_get_contents($full_filename);
if ($contents === false)
{
throw new ExceptionFatal('Template file ' . Url::html_output($full_filename) . ' could not be opened for reading.');
}
//remove comments
$contents = preg_replace('#/\*.*?\*/#s', '', $contents);
//replace info variables and word strings from language file
$tr = array(
'{info:dir}' => (isset($dir) ? Url::html_output($dir) : ''),
'{info:subdir}' => (isset($subdir) ? Url::html_output($subdir) : ''),
'{info:version}' => VERSION,
'{info:page_time}' => round((microtime(true) - START_TIME) * 1000, 1),
'{info:statinfo}' => $mobile_device_detect->detect()->getInfo(),
'{info:message}' => $words->__get('COOKIE_CONSENT_MSG'),
'{info:dismiss}' => $words->__get('COOKIE_CONSENT_OK'),
'{info:link}' => $words->__get('COOKIE_CONSENT_INFO'),
'{info:href}' => $words->__get('PRIVACY')
);
$contents = preg_replace_callback('/\{\s*words?\s*:\s*(.+)\s*\}/Ui',
array('self', 'callback_words'), strtr($contents, $tr));
//replace {config} variables
$contents = preg_replace_callback('/\{\s*config\s*:\s*(.+)\s*\}/Ui',
array('self', 'callback_config'), $contents);
//parse includes
$this -> out = preg_replace_callback('/\{\s*include\s*:\s*(.+)\s*\}/Ui',
array('self', 'callback_include'), $contents);
}
/**
* @return string The HTML text of the parsed template
*/
public function __toString()
{
return $this -> out;
}
}
?>