|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2018 Justin Kuenzel (jukusoft.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Project: JuKuCMS |
|
22
|
|
|
* License: Apache 2.0 license |
|
23
|
|
|
* User: Justin |
|
24
|
|
|
* Date: 03.04.2018 |
|
25
|
|
|
* Time: 20:07 |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class DwooTemplate extends Template { |
|
29
|
|
|
|
|
30
|
|
|
protected static $core = null; |
|
31
|
|
|
|
|
32
|
|
|
protected static $files = array(); |
|
33
|
|
|
|
|
34
|
|
|
protected static $benchmark = array(); |
|
35
|
|
|
|
|
36
|
|
|
//variables |
|
37
|
|
|
//protected $vars = array(); |
|
38
|
|
|
protected $data = null; |
|
39
|
|
|
|
|
40
|
|
|
protected $file = ""; |
|
41
|
|
|
protected $template = null; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct($file, Registry $registry = null) { |
|
44
|
|
|
if ($registry == null) { |
|
45
|
|
|
$registry = Registry::singleton(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
//initialize core if neccessary |
|
49
|
|
|
self::initCoreIfAbsent(); |
|
50
|
|
|
|
|
51
|
|
|
//find file |
|
52
|
|
|
$this->file = Template::findTemplate($file, $registry); |
|
53
|
|
|
|
|
54
|
|
|
//load a template file, this is reusable if you want to render multiple times the same template with different data |
|
55
|
|
|
if (isset(self::$files[$file])) { |
|
56
|
|
|
$this->template = self::$files[$this->file]; |
|
57
|
|
|
} else { |
|
58
|
|
|
$this->template = new Dwoo\Template\File($this->file); |
|
59
|
|
|
self::$files[$file] = $this->template; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
//create a data set, this data set can be reused to render multiple templates if it contains enough data to fill them all |
|
63
|
|
|
$this->data = new Dwoo\Data(); |
|
64
|
|
|
|
|
65
|
|
|
//set default values |
|
66
|
|
|
$this->assign("REGISTRY", $registry->listSettings()); |
|
67
|
|
|
|
|
68
|
|
|
//set CSRF token |
|
69
|
|
|
$this->assign("CSRF_TOKEN", Security::getCSRFToken()); |
|
70
|
|
|
|
|
71
|
|
|
//set domain, current page and so on |
|
72
|
|
|
$this->assign("DOMAIN", DomainUtils::getCurrentDomain()); |
|
73
|
|
|
$this->assign("BASE_URL", DomainUtils::getBaseURL()); |
|
74
|
|
|
$this->assign("CURRENT_URL", DomainUtils::getURL()); |
|
75
|
|
|
$this->assign("FOLDER", $registry->getSetting("folder")); |
|
76
|
|
|
|
|
77
|
|
|
//set language settings |
|
78
|
|
|
$this->assign("PREF_LANG", $registry->getSetting("pref_lang")); |
|
79
|
|
|
$this->assign("LANG_TOKEN", $registry->getSetting("lang_token")); |
|
80
|
|
|
|
|
81
|
|
|
$domain = $registry->getObject("domain"); |
|
82
|
|
|
$this->assign("HOME_PAGE", $domain->getHomePage()); |
|
83
|
|
|
$this->assign("LOGIN_PAGE", Settings::get("login_page", "login")); |
|
84
|
|
|
$this->assign("LOGIN_URL", $registry->getSetting("login_url")); |
|
85
|
|
|
$this->assign("LOGOUT_PAGE", Settings::get("logout_page", "logout")); |
|
86
|
|
|
$this->assign("LOGOUT_URL", $registry->getSetting("logout_url")); |
|
87
|
|
|
|
|
88
|
|
|
//set user variables |
|
89
|
|
|
$this->assign("USERID", User::current()->getID()); |
|
90
|
|
|
$this->assign("USERNAME", User::current()->getUsername()); |
|
91
|
|
|
|
|
92
|
|
|
$style_name = $registry->getSetting("current_style_name"); |
|
93
|
|
|
$this->assign("STYLE_PATH",DomainUtils::getBaseURL() . "/styles/" . $style_name . "/"); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function assign ($var, $value) { |
|
97
|
|
|
//$this->vars[$var] = $value; |
|
98
|
|
|
$this->data->assign($var, $value); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function parse ($name = "main") { |
|
102
|
|
|
throw new Exception("Method DwooTemplate::parse() is not supported from Dwoo template engine."); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getCode ($name = "main") { |
|
106
|
|
|
$start_time = microtime(true); |
|
107
|
|
|
|
|
108
|
|
|
// Output the result |
|
109
|
|
|
$html = self::$core->get($this->template, $this->data); |
|
110
|
|
|
|
|
111
|
|
|
$end_time = microtime(true); |
|
112
|
|
|
$exec_time = $end_time - $start_time; |
|
113
|
|
|
|
|
114
|
|
|
//store benchmark |
|
115
|
|
|
self::$benchmark[$this->file] = $exec_time; |
|
116
|
|
|
|
|
117
|
|
|
return $html; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected static function initCoreIfAbsent () { |
|
121
|
|
|
if (self::$core == null) { |
|
122
|
|
|
self::$core = new Dwoo\Core(); |
|
123
|
|
|
|
|
124
|
|
|
$cache_dir = CACHE_PATH . "dwoo/"; |
|
125
|
|
|
$compile_dir = CACHE_PATH . "dwoo-compile/"; |
|
126
|
|
|
|
|
127
|
|
|
//check, if cache dir exists |
|
128
|
|
|
if (!file_exists($cache_dir)) { |
|
129
|
|
|
mkdir($cache_dir); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
//check, if compile dir exists |
|
133
|
|
|
if (!file_exists($compile_dir)) { |
|
134
|
|
|
mkdir($compile_dir); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
//set cache dir |
|
138
|
|
|
self::$core->setCacheDir($cache_dir); |
|
139
|
|
|
self::$core->setCompileDir($compile_dir); |
|
140
|
|
|
|
|
141
|
|
|
//allow some php functions |
|
142
|
|
|
//self::$core->setSecurityPolicy(new \Dwoo\Security\Policy()); |
|
143
|
|
|
//self::$core->getSecurityPolicy()->allowPhpFunction("count"); |
|
144
|
|
|
//self::$core->getSecurityPolicy()->allowPhpFunction("sizeof"); |
|
145
|
|
|
|
|
146
|
|
|
//add plugins |
|
147
|
|
|
//self::$core->addPlugin("if"); |
|
148
|
|
|
|
|
149
|
|
|
Events::throwEvent("init_dwoo", array( |
|
150
|
|
|
'core' => &self::$core, |
|
151
|
|
|
'cache_path' => CACHE_PATH, |
|
152
|
|
|
'cache_dir' => $cache_dir |
|
153
|
|
|
)); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public static function listFileBenchmark () { |
|
158
|
|
|
return self::$benchmark; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
?> |
|
|
|
|
|
|
164
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.