|
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
|
|
|
class Template { |
|
20
|
|
|
|
|
21
|
|
|
protected $template = null; |
|
22
|
|
|
protected static $registeredTemplate = array(); |
|
23
|
|
|
|
|
24
|
|
|
public function __construct ($file, Registry &$registry = null) { |
|
25
|
|
|
if ($registry == null) { |
|
26
|
|
|
$registry = Registry::singleton(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (!class_exists("XTemplate", false)) { |
|
30
|
|
|
require_once(ROOT_PATH . "system/packages/com.jukusoft.cms.xtpl/xtpl/xtemplate.class.php"); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if (isset(self::$registeredTemplate[$file])) { |
|
34
|
|
|
$file = self::$registeredTemplate[$file]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
//find file |
|
38
|
|
|
$file = self::findTemplate($file, $registry); |
|
39
|
|
|
|
|
40
|
|
|
$this->template = new XTemplate($file); |
|
41
|
|
|
$this->template->assign("REGISTRY", $registry->listSettings()); |
|
42
|
|
|
|
|
43
|
|
|
//set CSRF token |
|
44
|
|
|
$this->template->assign("CSRF_TOKEN", Security::getCSRFToken()); |
|
45
|
|
|
|
|
46
|
|
|
//set domain, current page and so on |
|
47
|
|
|
$this->template->assign("DOMAIN", DomainUtils::getCurrentDomain()); |
|
48
|
|
|
$this->template->assign("BASE_URL", DomainUtils::getBaseURL()); |
|
49
|
|
|
$this->template->assign("CURRENT_URL", DomainUtils::getURL()); |
|
50
|
|
|
$this->template->assign("FOLDER", $registry->getSetting("folder")); |
|
51
|
|
|
|
|
52
|
|
|
//set language settings |
|
53
|
|
|
$this->template->assign("PREF_LANG", $registry->getSetting("pref_lang")); |
|
54
|
|
|
$this->template->assign("LANG_TOKEN", $registry->getSetting("lang_token")); |
|
55
|
|
|
|
|
56
|
|
|
$redirect_url = urlencode(DomainUtils::getURL()); |
|
57
|
|
|
|
|
58
|
|
|
if (isset($_REQUEST['redirect_url']) && !empty($_REQUEST['redirect_url'])) { |
|
59
|
|
|
$redirect_url = $_REQUEST['redirect_url']; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$domain = $registry->getObject("domain"); |
|
63
|
|
|
$this->template->assign("HOME_PAGE", $domain->getHomePage()); |
|
64
|
|
|
$this->template->assign("LOGIN_PAGE", Settings::get("login_page", "login")); |
|
65
|
|
|
$this->template->assign("LOGIN_URL", DomainUtils::getBaseURL() . "/" . Settings::get("login_page", "login") . "?action=login&redirect_url=" . $redirect_url); |
|
66
|
|
|
$this->template->assign("LOGOUT_PAGE", Settings::get("logout_page", "logout")); |
|
67
|
|
|
$this->template->assign("LOGOUT_URL", DomainUtils::getBaseURL() . "/" . Settings::get("logout_page", "logout") . "?csrf_token=" . urlencode(Security::getCSRFToken())); |
|
68
|
|
|
|
|
69
|
|
|
//set user variables |
|
70
|
|
|
$this->template->assign("USERID", User::current()->getID()); |
|
71
|
|
|
$this->template->assign("USERNAME", User::current()->getUsername()); |
|
72
|
|
|
|
|
73
|
|
|
$style_name = $registry->getSetting("current_style_name"); |
|
74
|
|
|
$this->template->assign("STYLE_PATH",DomainUtils::getBaseURL() . "/styles/" . $style_name . "/"); |
|
75
|
|
|
|
|
76
|
|
|
Events::throwEvent("init_template", array( |
|
77
|
|
|
'file' => &$file, |
|
78
|
|
|
'template' => &$this, |
|
79
|
|
|
'template_instance' => &$this->template, |
|
80
|
|
|
'registry' => &$registry |
|
81
|
|
|
)); |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function assign ($var, $value) { |
|
86
|
|
|
$this->template->assign($var, $value); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function parse ($name = "main") { |
|
90
|
|
|
$this->template->parse($name); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getCode ($name = "main") { |
|
94
|
|
|
return $this->template->text($name); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function registerTemplate ($template, $file) { |
|
98
|
|
|
self::$registeredTemplate[$template] = $file; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function clearTemplates () { |
|
102
|
|
|
self::$registeredTemplate = array(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function createTemplate ($file) { |
|
106
|
|
|
$class = (String) __CLASS__; |
|
107
|
|
|
return new $class($file); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public static function getName () { |
|
111
|
|
|
return __CLASS__; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function findTemplate (string $tpl_name, Registry &$registry) : string { |
|
115
|
|
|
if (strpos($tpl_name, ".tpl") !== FALSE) { |
|
116
|
|
|
//remove file extension |
|
117
|
|
|
$tpl_name = str_replace(".tpl", "", $tpl_name); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
//check, if file path was set |
|
121
|
|
|
if (file_exists($tpl_name . ".tpl")) { |
|
122
|
|
|
return $tpl_name . ".tpl"; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
//find file |
|
126
|
|
|
$current_style = $registry->getSetting("current_style_name"); |
|
127
|
|
|
$style_path = STYLE_PATH . $current_style . "/"; |
|
128
|
|
|
|
|
129
|
|
|
$array = explode("_", $tpl_name); |
|
130
|
|
|
|
|
131
|
|
|
if (sizeof($array) == 3) { |
|
132
|
|
|
//plugin or style template |
|
133
|
|
|
if ($array[0] === "plugin") { |
|
134
|
|
|
return PLUGIN_PATH . $array[1] . "/templates/" . $array[2] . ".tpl"; |
|
135
|
|
|
} else { |
|
136
|
|
|
throw new Exception("templates with 2 '_' (expect 'plugin') arent supported yet."); |
|
137
|
|
|
} |
|
138
|
|
|
} else if (sizeof($array) == 1) { |
|
139
|
|
|
//search in style path |
|
140
|
|
|
if (file_exists($style_path . $tpl_name . ".tpl")) { |
|
141
|
|
|
return $style_path . $tpl_name . ".tpl"; |
|
142
|
|
|
} else if (file_exists(STYLE_PATH . "default/" . $tpl_name . ".tpl")) { |
|
143
|
|
|
//use default template |
|
144
|
|
|
return STYLE_PATH . "default/" . $tpl_name . ".tpl"; |
|
145
|
|
|
} else { |
|
146
|
|
|
throw new Exception("Coulnd't found template '" . $tpl_name . "' (search path: '" . $style_path . $tpl_name . ".tpl" . "'!"); |
|
147
|
|
|
} |
|
148
|
|
|
} else { |
|
149
|
|
|
throw new IllegalStateException("Coulndt found template file '" . $tpl_name . "', because unknown array size: " . sizeof($array)); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
?> |
|
|
|
|
|
|
156
|
|
|
|
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.