From 03a75d6497585a5790732f766f88d406ea26a415 Mon Sep 17 00:00:00 2001 From: Florin-Ciprian Bodin Date: Tue, 31 Oct 2023 21:47:52 +0200 Subject: [PATCH] Update TemplateFiles.php Assign informations for the footer. --- classes/TemplateFiles.php | 276 +++++++++++++------------------------- 1 file changed, 90 insertions(+), 186 deletions(-) diff --git a/classes/TemplateFiles.php b/classes/TemplateFiles.php index f93fb42..7e8cb28 100644 --- a/classes/TemplateFiles.php +++ b/classes/TemplateFiles.php @@ -1,9 +1,8 @@ - * @version 1.0.1 (July 09, 2004) + * @version 1.0.3 (February 02, 2005) * @package AutoIndex */ -class TemplateFiles extends TemplateInfo +class TemplateIndexer { /** - * @var Item The file or folder we're currently processing + * @var string The final output */ - private $temp_item; - - /** - * @var bool Is the current user an admin - */ - private $is_admin; - - /** - * @var bool Is the current user a moderator - */ - private $is_mod; - - /** - * @var int The number of the file we're currently processing - */ - private $i; - - /** - * @var int The total number of files to process - */ - private $length; + protected $out; /** * @param array $m The array given by preg_replace_callback() - * @return string Property is gotten from temp_item + * @return string Looks up $m[1] in word list and returns match */ - private function callback_file($m) + private static function callback_words($m) { - global $words, $subdir; - switch (strtolower($m[1])) - { - case 'tr_class': - { - return (($this -> i % 2) ? 'dark_row' : 'light_row'); - } - case 'filename': - { - return Url::html_output($this -> temp_item -> __get('filename')); - } - case 'file_ext': - { - return $this -> temp_item -> file_ext(); - } - case 'size': - { - return $this -> temp_item -> __get('size') -> formatted(); - } - case 'bytes': - { - return $this -> temp_item -> __get('size') -> __get('bytes'); - } - case 'date': - case 'time': - case 'm_time': - { - return $this -> temp_item -> format_m_time(); - } - case 'a_time': - { - return $this -> temp_item -> format_a_time(); - } - case 'thumbnail': - { - return $this -> temp_item -> __get('thumb_link'); - } - case 'num_subfiles': - { - return (($this -> temp_item instanceof DirItem - && !$this -> temp_item -> __get('is_parent_dir')) ? $this -> temp_item -> num_subfiles() : ''); - } - case 'delete_link': - { - return (($this -> is_admin && !$this -> temp_item -> __get('is_parent_dir')) ? - ' [' . $words -> __get('delete') . ']' : ''); - } - case 'rename_link': - { - return (($this -> is_admin && !$this -> temp_item -> __get('is_parent_dir')) ? - ' [' . $words -> __get('rename') . ']' : ''); - } - case 'edit_description_link': - { - $slash = (($this -> temp_item instanceof DirItem) ? '/' : ''); - return (($this -> is_mod && DESCRIPTION_FILE && !$this -> temp_item -> __get('is_parent_dir')) ? - ' [' - . $words -> __get('edit description') . ']' : ''); - } - case 'ftp_upload_link': - { - if (!$this -> is_mod || !$this -> temp_item instanceof FileItem || !isset($_SESSION['ftp'])) - { - return ''; - } - return ' [' . $words->__get('upload to ftp') . ']'; - } - default: - { - return $this -> temp_item -> __get($m[1]); - } - } + global $words; + return $words->__get(strtolower($m[1])); } /** - * Either the HTML text is returned, or an empty string is returned, - * depending on if the if-statement passed. + * @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 TemplateIndexer($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. * - * @param array $m The array given by preg_replace_callback() - * @return string The result to insert into the HTML - */ - private function callback_type($m) - { - switch (strtolower($m[1])) - { - case 'is_file': //file - { - return (($this -> temp_item instanceof FileItem) ? $m[2] : ''); - } - case 'is_dir': //folder or link to parent directory - { - return (($this -> temp_item instanceof DirItem) ? $m[2] : ''); - } - case 'is_real_dir': //folder - { - return (($this -> temp_item instanceof DirItem - && !$this -> temp_item -> __get('is_parent_dir')) ? $m[2] : ''); - } - case 'is_parent_dir': //link to parent directory - { - return (($this -> temp_item instanceof DirItem - && $this -> temp_item -> __get('is_parent_dir')) ? $m[2] : ''); - } - default: - { - throw new ExceptionDisplay('Invalid file:if statement in ' - . Url::html_output(EACH_FILE) . ''); - } - } - } - - /** - * Either the HTML text is returned or an empty string is returned, - * depending on if temp_item is the ith file parsed. - * - * @param array $m The array given by preg_replace_callback() - * @return string The result to insert into the HTML output - */ - private function callback_do_every($m) - { - $num = $this -> i + 1; - return (($num % (int)$m[1] === 0 && $this -> length !== $num) ? $m[2] : ''); - } - - - /** - * Parses info for each file in the directory. Order of elements to - * replace is: - * - file:if - * - do_every - * - file + * 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 - * @param DirectoryListDetailed $list */ - public function __construct($filename, DirectoryListDetailed $list) + public function __construct($filename) { - parent::__construct($filename, $list); - global $you; - $this -> is_admin = ($you -> level >= ADMIN); - $this -> is_mod = ($you -> level >= MODERATOR); - $final_file_line = ''; - $this -> length = (int)$list -> __get('list_count'); - foreach ($list as $i => $item) + global $config, $dir, $subdir, $words, $mobile_device_detect; + $full_filename = $config->__get('template') . $filename; + if (!is_file($full_filename)) { - $this -> i = (int)$i; - $this -> temp_item = $item; - $temp_line = preg_replace_callback('/\{\s*file\s*:\s*if\s*:\s*(\w+)\s*\}(.*)\{\s*end\s*if\s*\}/Uis', - array($this, 'callback_type'), $this -> out); - $temp_line = preg_replace_callback('/\{\s*do_every\s*:\s*(\d+)\s*\}(.*)\{\s*end\s*do_every\s*\}/Uis', - array($this, 'callback_do_every'), $temp_line); - $final_file_line .= preg_replace_callback('/\{\s*file\s*:\s*(\w+)\s*\}/Ui', - array($this, 'callback_file'), $temp_line); + throw new ExceptionFatal('Template file ' . Url::html_output($full_filename) . ' cannot be found.'); } - $this -> out = $final_file_line; + + //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; } } -?> \ No newline at end of file +?>