4 if (!defined('DNS_VERSION')) define('DNS_VERSION', '3.0.0 Beta');
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)
17 protected static $module = '';
24 protected static $dbObj = null;
31 protected static $sessionObj = null;
38 protected static $tplObj = null;
45 protected $language = array();
50 public function __construct($module = '') {
51 self
::$module = $module;
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
);
55 /*register_shutdown_function(array('dns\system\DNS', 'destruct'));*/
60 $this->initLanguage();
62 new RequestHandler(self
::$module);
66 public static function destruct() {
67 $error = error_get_last();
69 self::handleException(new SystemException($error['message'], $error['line']));
78 public static function getDB() {
85 protected function initDB() {
86 require(DNS_DIR
.'/config.inc.php');
87 self
::$dbObj = new DB($driver, $host, $user, $pass, $db, $port);
93 protected function initSession() {
94 self
::$sessionObj = new SessionHandler();
98 * return session object
100 public static function getSession() {
101 return self
::$sessionObj;
105 * autoload class files from namespace uses
107 * @param string $className
109 public static function autoload ($className) {
110 $namespaces = explode('\\', $className);
111 if (count($namespaces) > 1) {
112 array_shift($namespaces);
113 $classPath = DNS_DIR
.'/lib/'.implode('/', $namespaces).'.class.php';
114 if (file_exists($classPath)) {
115 require_once($classPath);
121 * Calls the show method on the given exception.
123 * @param \Exception $e
125 public static final function handleException(\Exception
$e) {
127 if ($e instanceof SystemException
) {
133 self
::handleException(new SystemException($e->getMessage(), $e->getCode(), '', $e));
135 catch (\Exception
$exception) {
136 die("<pre>DNS::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString());
141 * Catches php errors and throws instead a system exception.
143 * @param integer $errorNo
144 * @param string $message
145 * @param string $filename
146 * @param integer $lineNo
148 public static final function handleError($errorNo, $message, $filename, $lineNo) {
149 if (error_reporting() != 0) {
152 case 2: $type = 'warning';
154 case 8: $type = 'notice';
158 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
163 * Returns true if the debug mode is enabled, otherwise false.
167 public static function debugModeIsEnabled() {
168 // try to load constants
169 if (!defined('ENABLE_DEBUG')) {
170 self
::buildOptions();
173 if (defined('ENABLE_DEBUG') && ENABLE_DEBUG
) {
181 * init language system
183 protected function initLanguage () {
185 * @TODO: activate this later
186 * self::buildlanguage();
188 $availableLanguages = array("de", "en");
189 $languageCode = 'de';
191 if (isset($_GET['l'])) {
192 $code = strtolower($_GET['l']);
193 if (in_array($code, $availableLanguages)) {
194 $languageCode = $code;
196 else if (array_key_exists($code, $availableLanguages)) {
197 $languageCode = $availableLanguages[$code];
200 else if (self
::getSession()->language
!== null) {
201 $code = strtolower(self
::getSession()->language
);
202 if (in_array($code, $availableLanguages)) {
203 $languageCode = $code;
205 else if (array_key_exists($code, $availableLanguages)) {
206 $languageCode = $availableLanguages[$code];
209 else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) {
210 $acceptedLanguages = explode(',', str_replace('_', '-', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
211 foreach ($acceptedLanguages as $acceptedLanguage) {
212 $code = strtolower(preg_replace('%^([a-z]{2}).*$%i', '$1', $acceptedLanguage));
213 if (in_array($code, $availableLanguages)) {
214 $languageCode = $code;
220 // @TODO: remove this later
221 /* try to load module language files */
222 if (!empty(self
::$module)) {
223 $basedir = DNS_DIR
.'/'.self
::$module.'/lang/';
224 $file = $basedir.$languageCode.'.lang.php';
225 self
::getSession()->register('language', $languageCode);
227 if (file_exists($file)) {
229 if (isset($lang) && !empty($lang) && is_array($lang)) {
230 $this->language
= array_merge($this->language
, $lang);
235 /* load default language files */
236 $basedir = DNS_DIR
.'/lang/';
237 $file = $basedir.$languageCode.'.lang.php';
238 self
::getSession()->register('language', $languageCode);
240 if (file_exists($file)) {
242 if (isset($lang) && !empty($lang) && is_array($lang)) {
243 $this->language
= array_merge($this->language
, $lang);
251 * Executes template scripting in a language variable.
253 * @param string $item
254 * @param array $variables
255 * @return string result
257 public static function getLanguageVariable ($item, array $variables = array()) {
258 $lang = self
::getTPL()->getTemplateVars('language');
264 if (!empty($variables)) {
265 self
::getTPL()->assign($variables);
268 if (isset($lang[$item])) {
269 if (strpos($lang[$item], self
::getTPL()->left_delimiter
) !== false && strpos($lang[$item], self
::getTPL()->right_delimiter
) !== false) {
270 $data = str_replace("\$", '$', $lang[$item]);
271 $template_class = self
::getTPL()->template_class
;
272 $template = new $template_class('eval:'.$data, self
::getTPL(), self
::getTPL());
273 return $template->fetch();
283 * init template engine
285 protected function initTPL () {
286 require(DNS_DIR
.'/config.inc.php');
288 if (self
::getSession()->tpl
!== null && !empty(self
::getSession()->tpl
)) {
289 $tpl = self
::getSession()->tpl
;
292 require_once(DNS_DIR
.'/lib/system/api/smarty/libs/Smarty.class.php');
293 self
::$tplObj = new \Smarty
;
295 self
::getTPL()->setTemplateDir(DNS_DIR
.(empty(self
::$module) ?
'' : '/'.self
::$module)."/templates/".$tpl);
296 self
::getTPL()->setCompileDir(DNS_DIR
.(empty(self
::$module) ?
'' : '/'.self
::$module)."/templates/compiled/".$tpl);
297 self
::getTPL()->setPluginsDir(array(
298 DNS_DIR
."/lib/system/api/smarty/libs/plugins",
299 DNS_DIR
."/lib/template/plugins"
301 self
::getTPL()->loadFilter('pre', 'hascontent');
304 self
::getTPL()->loadFilter('output', 'trimwhitespace');
307 /* assign language variables */
308 self
::getTPL()->assign(array(
309 "language" => $this->language
,
310 "isReseller" => User
::isReseller(),
311 "isAdmin" => User
::isAdmin()
314 /*self::getTPL()->assign("version", mb_substr(sha1(DNS_VERSION), 0, 8));*/
319 * get template engine
321 public static function getTPL () {
322 return self
::$tplObj;
326 * Creates a random hash.
330 public static function generateRandomID() {
331 return sha1(microtime() . uniqid(mt_rand(), true));
339 public static function generateUUID() {
340 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)));
344 * build options from database
346 * @param boolean $force
348 public static function buildOptions ($force = false) {
349 $file = DNS_DIR
."/options.inc.php";
350 if (!file_exists($file) ||
(filemtime($file) +
86400) < time() ||
$force === true) {
351 if (file_exists($file)) {
356 $options = self
::getDB()->query("select * from dns_options");
357 $content = "<?php\n/* generated at ".gmdate('r')." */\n";
359 while ($row = self
::getDB()->fetch_array($options)) {
360 $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";
363 $handler = fOpen($file, "a+");
364 fWrite($handler, $content);
372 * build language files from database
374 * @param boolean $force
376 public static function buildlanguage ($force = false) {
377 $availableLanguages = array("de", "en");
378 foreach ($availableLanguages as $languageID => $languageCode) {
380 $file = DNS_DIR
."/lang/".$languageCode.".lang.php";
381 if (!file_exists($file) ||
(filemtime($file) +
86400) < time() ||
$force === true) {
382 if (file_exists($file)) {
388 $items = self
::getDB()->query("select * from dns_language where languageID = ?", array($languageID));
389 $content = "<?php\n/**\n* language: ".$languageCode."\n* encoding: UTF-8\n* generated at ".gmdate("r")."\n* \n* DO NOT EDIT THIS FILE\n*/\n";
390 $content .= "\$lang = array();\n";
391 while ($row = self
::getDB()->fetch_array($items)) {
393 $content .= "\$lang['".$row['languageItem']."'] = '".str_replace("\$", '$', $row['languageValue'])."';\n";
396 $handler = fOpen($file, "a+");
397 fWrite($handler, $content);