Commit | Line | Data |
---|---|---|
2aa91ff2 S |
1 | <?php |
2 | namespace dns\system; | |
3 | ||
2c424371 S |
4 | if (!defined('DNS_VERSION')) define('DNS_VERSION', '3.0.0 Beta'); |
5 | ||
2aa91ff2 S |
6 | /** |
7 | * @author Jan Altensen (Stricted) | |
8 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
9 | * @copyright 2014-2015 Jan Altensen (Stricted) | |
10 | */ | |
11 | class DNS { | |
d3147195 S |
12 | /** |
13 | * module name | |
14 | * | |
15 | * @var string | |
16 | */ | |
17 | protected static $module = ''; | |
18 | ||
2aa91ff2 S |
19 | /** |
20 | * database object | |
21 | * | |
22 | * @var object | |
23 | */ | |
24 | protected static $dbObj = null; | |
25 | ||
5a33cd73 S |
26 | /** |
27 | * session object | |
28 | * | |
29 | * @var object | |
30 | */ | |
31 | protected static $sessionObj = null; | |
32 | ||
2aa91ff2 S |
33 | /** |
34 | * template object | |
35 | * | |
36 | * @var object | |
37 | */ | |
38 | protected static $tplObj = null; | |
39 | ||
40 | /** | |
41 | * language array | |
42 | * | |
43 | * @var array | |
44 | */ | |
45 | protected $language = array(); | |
46 | ||
47 | /** | |
48 | * init main system | |
49 | */ | |
d3147195 S |
50 | public function __construct($module = '') { |
51 | self::$module = $module; | |
19018596 S |
52 | spl_autoload_register(array('dns\system\DNS', 'autoload')); |
53 | set_exception_handler(array('dns\system\DNS', 'handleException')); | |
54 | set_error_handler(array('dns\system\DNS', 'handleError'), E_ALL); | |
2aa91ff2 S |
55 | |
56 | $this->initDB(); | |
57 | self::buildOptions(); | |
5a33cd73 | 58 | $this->initSession(); |
2aa91ff2 S |
59 | $this->initLanguage(); |
60 | $this->initTPL(); | |
d3147195 | 61 | new RequestHandler(self::$module); |
2aa91ff2 S |
62 | } |
63 | ||
64 | /** | |
65 | * get database | |
66 | */ | |
67 | public static function getDB() { | |
68 | return self::$dbObj; | |
69 | } | |
70 | ||
71 | /** | |
72 | * init database | |
73 | */ | |
74 | protected function initDB() { | |
75 | require(DNS_DIR.'/config.inc.php'); | |
76 | self::$dbObj = new DB($driver, $host, $user, $pass, $db, $port); | |
77 | } | |
78 | ||
5a33cd73 S |
79 | /** |
80 | * init session system | |
81 | */ | |
82 | protected function initSession() { | |
83 | self::$sessionObj = new SessionHandler(); | |
84 | } | |
85 | ||
86 | /** | |
87 | * return session object | |
88 | */ | |
89 | public static function getSession() { | |
90 | return self::$sessionObj; | |
91 | } | |
92 | ||
2aa91ff2 S |
93 | /* |
94 | * autoload class files from namespace uses | |
95 | * | |
96 | * @param string $className | |
97 | */ | |
98 | public static function autoload ($className) { | |
99 | $namespaces = explode('\\', $className); | |
100 | if (count($namespaces) > 1) { | |
101 | array_shift($namespaces); | |
102 | $classPath = DNS_DIR.'/lib/'.implode('/', $namespaces).'.class.php'; | |
103 | if (file_exists($classPath)) { | |
104 | require_once($classPath); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
19018596 S |
109 | /** |
110 | * Calls the show method on the given exception. | |
111 | * | |
112 | * @param \Exception $e | |
113 | */ | |
114 | public static final function handleException(\Exception $e) { | |
115 | try { | |
116 | if ($e instanceof SystemException) { | |
117 | $e->show(); | |
118 | exit; | |
119 | } | |
120 | ||
121 | // repack Exception | |
122 | self::handleException(new SystemException($e->getMessage(), $e->getCode(), '', $e)); | |
123 | } | |
124 | catch (\Exception $exception) { | |
125 | die("<pre>DNS::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString()); | |
126 | } | |
127 | } | |
128 | ||
129 | /** | |
130 | * Catches php errors and throws instead a system exception. | |
131 | * | |
132 | * @param integer $errorNo | |
133 | * @param string $message | |
134 | * @param string $filename | |
135 | * @param integer $lineNo | |
136 | */ | |
137 | public static final function handleError($errorNo, $message, $filename, $lineNo) { | |
138 | if (error_reporting() != 0) { | |
139 | $type = 'error'; | |
140 | switch ($errorNo) { | |
141 | case 2: $type = 'warning'; | |
142 | break; | |
143 | case 8: $type = 'notice'; | |
144 | break; | |
145 | } | |
146 | ||
147 | throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0); | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * Returns true if the debug mode is enabled, otherwise false. | |
153 | * | |
154 | * @return boolean | |
155 | */ | |
156 | public static function debugModeIsEnabled() { | |
157 | // try to load constants | |
158 | if (!defined('ENABLE_DEBUG')) { | |
159 | self::buildOptions(); | |
160 | } | |
161 | ||
162 | if (defined('ENABLE_DEBUG') && ENABLE_DEBUG) { | |
163 | return true; | |
164 | } | |
165 | ||
166 | return false; | |
167 | } | |
168 | ||
2aa91ff2 S |
169 | /** |
170 | * init language system | |
171 | */ | |
172 | protected function initLanguage () { | |
d3147195 S |
173 | /* |
174 | * @TODO: activate this later | |
175 | * self::buildlanguage(); | |
176 | */ | |
2aa91ff2 S |
177 | $availableLanguages = array("de", "en"); |
178 | $languageCode = 'de'; | |
d3147195 | 179 | |
2aa91ff2 S |
180 | if (isset($_GET['l'])) { |
181 | $code = strtolower($_GET['l']); | |
182 | if (in_array($code, $availableLanguages)) { | |
183 | $languageCode = $code; | |
184 | } | |
185 | else if (array_key_exists($code, $availableLanguages)) { | |
186 | $languageCode = $availableLanguages[$code]; | |
187 | } | |
188 | } | |
b8e7e273 S |
189 | else if (self::getSession()->language !== null) { |
190 | $code = strtolower(self::getSession()->language); | |
2aa91ff2 S |
191 | if (in_array($code, $availableLanguages)) { |
192 | $languageCode = $code; | |
193 | } | |
194 | else if (array_key_exists($code, $availableLanguages)) { | |
195 | $languageCode = $availableLanguages[$code]; | |
196 | } | |
197 | } | |
198 | else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) { | |
199 | $acceptedLanguages = explode(',', str_replace('_', '-', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']))); | |
200 | foreach ($acceptedLanguages as $acceptedLanguage) { | |
201 | $code = strtolower(preg_replace('%^([a-z]{2}).*$%i', '$1', $acceptedLanguage)); | |
202 | if (in_array($code, $availableLanguages)) { | |
203 | $languageCode = $code; | |
204 | break; | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
d3147195 S |
209 | // @TODO: remove this later |
210 | /* try to load module language files */ | |
211 | if (!empty(self::$module)) { | |
212 | $basedir = DNS_DIR.'/'.self::$module.'/lang/'; | |
213 | $file = $basedir.$languageCode.'.lang.php'; | |
214 | self::getSession()->register('language', $languageCode); | |
215 | ||
216 | if (file_exists($file)) { | |
217 | require_once($file); | |
218 | if (isset($lang) && !empty($lang) && is_array($lang)) { | |
219 | $this->language = array_merge($this->language, $lang); | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
224 | /* load default language files */ | |
225 | $basedir = DNS_DIR.'/lang/'; | |
2aa91ff2 | 226 | $file = $basedir.$languageCode.'.lang.php'; |
b8e7e273 | 227 | self::getSession()->register('language', $languageCode); |
2aa91ff2 S |
228 | |
229 | if (file_exists($file)) { | |
230 | require_once($file); | |
231 | if (isset($lang) && !empty($lang) && is_array($lang)) { | |
232 | $this->language = array_merge($this->language, $lang); | |
233 | } | |
234 | } | |
235 | ||
236 | return; | |
237 | } | |
238 | ||
239 | /** | |
240 | * Executes template scripting in a language variable. | |
241 | * | |
242 | * @param string $item | |
243 | * @param array $variables | |
244 | * @return string result | |
245 | */ | |
246 | public static function getLanguageVariable ($item, array $variables = array()) { | |
247 | $lang = self::getTPL()->getTemplateVars('language'); | |
248 | ||
249 | if ($lang == null) { | |
250 | return $item; | |
251 | } | |
252 | ||
253 | if (!empty($variables)) { | |
254 | self::getTPL()->assign($variables); | |
255 | } | |
256 | ||
257 | if (isset($lang[$item])) { | |
258 | if (strpos($lang[$item], self::getTPL()->left_delimiter) !== false && strpos($lang[$item], self::getTPL()->right_delimiter) !== false) { | |
259 | $data = str_replace("\$", '$', $lang[$item]); | |
260 | $template_class = self::getTPL()->template_class; | |
261 | $template = new $template_class('eval:'.$data, self::getTPL(), self::getTPL()); | |
262 | return $template->fetch(); | |
263 | } | |
264 | ||
265 | return $lang[$item]; | |
266 | } | |
267 | ||
268 | return $item; | |
269 | } | |
270 | ||
271 | /** | |
272 | * init template engine | |
273 | */ | |
274 | protected function initTPL () { | |
275 | require(DNS_DIR.'/config.inc.php'); | |
276 | ||
b8e7e273 S |
277 | if (self::getSession()->tpl !== null && !empty(self::getSession()->tpl)) { |
278 | $tpl = self::getSession()->tpl; | |
2aa91ff2 S |
279 | } |
280 | ||
97c78a9a S |
281 | if (!file_exists(DNS_DIR.'/lib/system/api/smarty/libs/Smarty.class.php')) { |
282 | throw new SystemException('Unable to find Smarty'); | |
283 | } | |
284 | ||
01c0ad42 | 285 | require_once(DNS_DIR.'/lib/system/api/smarty/libs/Smarty.class.php'); |
2aa91ff2 S |
286 | self::$tplObj = new \Smarty; |
287 | ||
d3147195 S |
288 | self::getTPL()->setTemplateDir(DNS_DIR.(empty(self::$module) ? '' : '/'.self::$module)."/templates/".$tpl); |
289 | self::getTPL()->setCompileDir(DNS_DIR.(empty(self::$module) ? '' : '/'.self::$module)."/templates/compiled/".$tpl); | |
8c6420e2 | 290 | self::getTPL()->setPluginsDir(array( |
01c0ad42 | 291 | DNS_DIR."/lib/system/api/smarty/libs/plugins", |
64ff9617 | 292 | DNS_DIR."/lib/system/template/plugins" |
8c6420e2 | 293 | )); |
2aa91ff2 S |
294 | self::getTPL()->loadFilter('pre', 'hascontent'); |
295 | ||
d8929762 | 296 | if (!ENABLE_DEBUG) { |
2aa91ff2 S |
297 | self::getTPL()->loadFilter('output', 'trimwhitespace'); |
298 | } | |
299 | ||
2aa91ff2 | 300 | /* assign language variables */ |
e7216dc0 | 301 | self::getTPL()->assign(array( |
cd67909e S |
302 | "language" => $this->language, |
303 | "isReseller" => User::isReseller(), | |
304 | "isAdmin" => User::isAdmin() | |
305 | )); | |
306 | ||
307 | /*self::getTPL()->assign("version", mb_substr(sha1(DNS_VERSION), 0, 8));*/ | |
308 | ||
2aa91ff2 S |
309 | } |
310 | ||
311 | /** | |
312 | * get template engine | |
313 | */ | |
314 | public static function getTPL () { | |
315 | return self::$tplObj; | |
316 | } | |
317 | ||
e7216dc0 S |
318 | /** |
319 | * Creates a random hash. | |
320 | * | |
321 | * @return string | |
322 | */ | |
323 | public static function generateRandomID() { | |
324 | return sha1(microtime() . uniqid(mt_rand(), true)); | |
325 | } | |
326 | ||
d8929762 S |
327 | /** |
328 | * Creates an UUID. | |
329 | * | |
330 | * @return string | |
331 | */ | |
332 | public static function generateUUID() { | |
333 | return strtoupper(sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535))); | |
334 | } | |
335 | ||
2aa91ff2 S |
336 | /** |
337 | * build options from database | |
338 | * | |
339 | * @param boolean $force | |
340 | */ | |
341 | public static function buildOptions ($force = false) { | |
342 | $file = DNS_DIR."/options.inc.php"; | |
343 | if (!file_exists($file) || (filemtime($file) + 86400) < time() || $force === true) { | |
344 | if (file_exists($file)) { | |
345 | @unlink($file); | |
346 | } | |
347 | ||
348 | @touch($file); | |
349 | $options = self::getDB()->query("select * from dns_options"); | |
350 | $content = "<?php\n/* generated at ".gmdate('r')." */\n"; | |
351 | ||
352 | while ($row = self::getDB()->fetch_array($options)) { | |
6d8f93a7 | 353 | $content .= "if (!defined('".strtoupper($row['optionName'])."')) define('".strtoupper($row['optionName'])."', ".((is_bool($row['optionValue']) || is_numeric($row['optionValue'])) ? intval($row['optionValue']) : "'".addcslashes($row['optionValue'], "'\\")."'").");\n"; |
2aa91ff2 S |
354 | } |
355 | ||
356 | $handler = fOpen($file, "a+"); | |
357 | fWrite($handler, $content); | |
358 | fClose($handler); | |
359 | } | |
360 | ||
361 | require_once($file); | |
362 | } | |
d3147195 S |
363 | |
364 | /** | |
365 | * build language files from database | |
366 | * | |
367 | * @param boolean $force | |
368 | */ | |
369 | public static function buildlanguage ($force = false) { | |
370 | $availableLanguages = array("de", "en"); | |
371 | foreach ($availableLanguages as $languageID => $languageCode) { | |
372 | ||
373 | $file = DNS_DIR."/lang/".$languageCode.".lang.php"; | |
374 | if (!file_exists($file) || (filemtime($file) + 86400) < time() || $force === true) { | |
375 | if (file_exists($file)) { | |
376 | @unlink($file); | |
377 | } | |
378 | ||
379 | @touch($file); | |
380 | ||
381 | $items = self::getDB()->query("select * from dns_language where languageID = ?", array($languageID)); | |
382 | $content = "<?php\n/**\n* language: ".$languageCode."\n* encoding: UTF-8\n* generated at ".gmdate("r")."\n* \n* DO NOT EDIT THIS FILE\n*/\n"; | |
383 | $content .= "\$lang = array();\n"; | |
384 | while ($row = self::getDB()->fetch_array($items)) { | |
385 | print_r($row); | |
386 | $content .= "\$lang['".$row['languageItem']."'] = '".str_replace("\$", '$', $row['languageValue'])."';\n"; | |
387 | } | |
388 | ||
389 | $handler = fOpen($file, "a+"); | |
390 | fWrite($handler, $content); | |
391 | fClose($handler); | |
392 | } | |
393 | } | |
394 | } | |
2aa91ff2 | 395 | } |