77// Include the actual smarty class file
88include (SMARTY_DIR . 'Smarty.class.php ' );
99
10- /**
11- * Custom Smarty Template Resource for Pages
12- * Get templates from Database
13- * Allow admin to manage his templates from Backoffice
14- */
15- class Smarty_Resource_Database extends Smarty_Resource_Custom {
16- protected $ template ;
17-
18- public function __construct ($ template ) {
19- $ this ->template = $ template ;
20- }
21- /**
22- * Fetch a template and its modification time from database
23- *
24- * @param string $name template name
25- * @param string $source template source
26- * @param integer $mtime template modification timestamp (epoch)
27- * @return void
28- */
29- protected function fetch ($ name , &$ source , &$ mtime ) {
30- $ oTemplate = $ this ->template ->getEntry ($ this ->fullTemplateName ($ name ));
31- if ( $ oTemplate && $ oTemplate ['active ' ] ) {
32- $ source = $ oTemplate ['content ' ];
33- $ mtime = strtotime ($ oTemplate ['modified_at ' ]);
34- } else {
35- $ source = null ;
36- $ mtime = null ;
37- }
38- }
39-
40- /**
41- * Fetch a template's modification time from database
42- *
43- * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
44- * @param string $name template name
45- * @return integer timestamp (epoch) the template was modified
46- */
47- protected function fetchTimestamp ($ name ) {
48- $ templates = $ this ->template ->cachedGetActiveTemplates ();
49- $ mtime = @$ templates [$ this ->fullTemplateName ($ name )];
50- return $ mtime ? $ mtime : false ;
51- }
52-
53- /**
54- * Prepend THEME name to template name to get valid DB primary key
55- *
56- * @param string $name template name
57- */
58- protected function fullTemplateName ($ name ) {
59- return $ this ->normalisePath (THEME . "/ " . $ name );
60- }
61-
62- /**
63- * Normalise a file path string so that it can be checked safely.
64- *
65- * Attempt to avoid invalid encoding bugs by transcoding the path. Then
66- * remove any unnecessary path components including '.', '..' and ''.
67- *
68- * @param $path string
69- * The path to normalise.
70- * @return string
71- * The path, normalised.
72- * @see https://gist.github.com/thsutton/772287
73- */
74- protected function normalisePath ($ path ) {
75- // Process the components
76- $ parts = explode ('/ ' , $ path );
77- $ safe = array ();
78- foreach ($ parts as $ idx => $ part ) {
79- if (empty ($ part ) || ('. ' == $ part )) {
80- continue ;
81- } elseif ('.. ' == $ part ) {
82- array_pop ($ safe );
83- continue ;
84- } else {
85- $ safe [] = $ part ;
86- }
87- }
88- // Return the "clean" path
89- $ path = implode (DIRECTORY_SEPARATOR , $ safe );
90- return $ path ;
91- }
92-
93- }
94-
95- class Smarty_Resource_Hybrid extends Smarty_Resource {
96-
97- protected $ databaseResource ;
98-
99- protected $ fileResource ;
100-
101- public function __construct ($ dbResource , $ fileResource ) {
102- $ this ->databaseResource = $ dbResource ;
103- $ this ->fileResource = $ fileResource ;
104- }
105-
106- /**
107- * populate Source Object with meta data from Resource
108- *
109- * @param Smarty_Template_Source $source source object
110- * @param Smarty_Internal_Template $_template template object
111- */
112- public function populate (Smarty_Template_Source $ source , Smarty_Internal_Template $ _template =null ) {
113- if ( !@$ _REQUEST ['disable_template_override ' ] ) {
114- $ this ->databaseResource ->populate ($ source , $ _template );
115- if ( $ source ->exists )
116- return ;
117- }
118- $ source ->type = 'file ' ;
119- return $ this ->fileResource ->populate ($ source , $ _template );
120- }
121-
122- /**
123- * Load template's source into current template object
124- *
125- * @param Smarty_Template_Source $source source object
126- * @return string template source
127- * @throws SmartyException if source cannot be loaded
128- */
129- public function getContent (Smarty_Template_Source $ source ) {
130- try {
131- return $ this ->databaseResource ->getContent ($ source );
132- } catch (SmartyException $ e ) {
133- return $ this ->fileResource ->getContent ($ source );
134- }
135- }
136-
137- /**
138- * Determine basename for compiled filename
139- *
140- * @param Smarty_Template_Source $source source object
141- * @return string resource's basename
142- */
143- public function getBasename (Smarty_Template_Source $ source ) {
144- return $ this ->fileResource ->getBasename ($ source );
145- }
146-
147- }
148-
14910// We initialize smarty here
15011$ debug ->append ('Instantiating Smarty Object ' , 3 );
15112$ smarty = new Smarty ;
@@ -154,11 +15,6 @@ public function getBasename(Smarty_Template_Source $source) {
15415$ debug ->append ('Define Smarty Paths ' , 3 );
15516$ smarty ->template_dir = BASEPATH . 'templates/ ' . THEME . '/ ' ;
15617$ smarty ->compile_dir = BASEPATH . 'templates/compile/ ' . THEME . '/ ' ;
157- $ smarty ->registerResource ('hybrid ' , new Smarty_Resource_Hybrid (
158- new Smarty_Resource_Database ($ template ),
159- new Smarty_Internal_Resource_File ()
160- ));
161- $ smarty ->default_resource_type = "hybrid " ;
16218$ smarty_cache_key = md5 (serialize ($ _REQUEST ) . serialize (@$ _SESSION ['USERDATA ' ]['id ' ]));
16319
16420// Optional smarty caching, check Smarty documentation for details
0 commit comments