1 <?php
3 /**
4 * Smarty Internal Plugin Resource File
5 *
6 * Implements the file system as resource for Smarty templates
7 *
8 * @package Smarty
9 * @subpackage TemplateResources
10 * @author Uwe Tews
11 */
12 /**
13 * Smarty Internal Plugin Resource File
14 */
15 class Smarty_Internal_Resource_File {
16 public function __construct($smarty)
17 {
18 $this->smarty = $smarty;
19 }
20 // classes used for compiling Smarty templates from file resource
21 public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
22 public $template_lexer_class = 'Smarty_Internal_Templatelexer';
23 public $template_parser_class = 'Smarty_Internal_Templateparser';
24 // properties
25 public $usesCompiler = true;
26 public $isEvaluated = false;
28 /**
29 * Return flag if template source is existing
30 *
31 * @return boolean true
32 */
33 public function isExisting($template)
34 {
35 if ($template->getTemplateFilepath() === false) {
36 return false;
37 } else {
38 return true;
39 }
40 }
42 /**
43 * Get filepath to template source
44 *
45 * @param object $_template template object
46 * @return string filepath to template source file
47 */
48 public function getTemplateFilepath($_template)
49 {
50 $_filepath = $_template->buildTemplateFilepath ();
52 if ($_filepath !== false) {
53 if ($_template->security) {
54 $_template->smarty->security_handler->isTrustedResourceDir($_filepath);
55 }
56 }
57 $_template->templateUid = sha1($_filepath);
58 return $_filepath;
59 }
61 /**
62 * Get timestamp to template source
63 *
64 * @param object $_template template object
65 * @return integer timestamp of template source file
66 */
67 public function getTemplateTimestamp($_template)
68 {
69 return filemtime($_template->getTemplateFilepath());
70 }
72 /**
73 * Read template source from file
74 *
75 * @param object $_template template object
76 * @return string content of template source file
77 */
78 public function getTemplateSource($_template)
79 {
80 // read template file
81 if (file_exists($_template->getTemplateFilepath())) {
82 $_template->template_source = file_get_contents($_template->getTemplateFilepath());
83 return true;
84 } else {
85 return false;
86 }
87 }
89 /**
90 * Get filepath to compiled template
91 *
92 * @param object $_template template object
93 * @return string return path to compiled template
94 */
95 public function getCompiledFilepath($_template)
96 {
97 $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
98 // calculate Uid if not already done
99 if ($_template->templateUid == '') {
100 $_template->getTemplateFilepath();
101 }
102 $_filepath = $_template->templateUid;
103 // if use_sub_dirs, break file into directories
104 if ($_template->smarty->use_sub_dirs) {
105 $_filepath = substr($_filepath, 0, 2) . DS
106 . substr($_filepath, 2, 2) . DS
107 . substr($_filepath, 4, 2) . DS
108 . $_filepath;
109 }
110 $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
111 if (isset($_compile_id)) {
112 $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
113 }
114 if ($_template->caching) {
115 $_cache = '.cache';
116 } else {
117 $_cache = '';
118 }
119 $_compile_dir = $_template->smarty->compile_dir;
120 if (strpos('/\\', substr($_compile_dir, -1)) === false) {
121 $_compile_dir .= DS;
122 }
123 return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_template->resource_name) . $_cache . '.php';
124 }
125 }
127 ?>