1ca2bb26ca4bbec68622c78ff5863b73d579e6bd
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
);
59 $this->initLanguage();
61 new RequestHandler(self
::$module);
67 public static function getDB() {
74 protected function initDB() {
75 require(DNS_DIR
.'/config.inc.php');
76 self
::$dbObj = new DB($driver, $host, $user, $pass, $db, $port);
82 protected function initSession() {
83 self
::$sessionObj = new SessionHandler();
87 * return session object
89 public static function getSession() {
90 return self
::$sessionObj;
94 * autoload class files from namespace uses
96 * @param string $className
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);
110 * Calls the show method on the given exception.
112 * @param \Exception $e
114 public static final function handleException(\Exception
$e) {
116 if ($e instanceof SystemException
) {
122 self
::handleException(new SystemException($e->getMessage(), $e->getCode(), '', $e));
124 catch (\Exception
$exception) {
125 die("<pre>DNS::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString());
130 * Catches php errors and throws instead a system exception.
132 * @param integer $errorNo
133 * @param string $message
134 * @param string $filename
135 * @param integer $lineNo
137 public static final function handleError($errorNo, $message, $filename, $lineNo) {
138 if (error_reporting() != 0) {
141 case 2: $type = 'warning';
143 case 8: $type = 'notice';
147 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
152 * Returns true if the debug mode is enabled, otherwise false.
156 public static function debugModeIsEnabled() {
157 // try to load constants
158 if (!defined('ENABLE_DEBUG')) {
159 self
::buildOptions();
162 if (defined('ENABLE_DEBUG') && ENABLE_DEBUG
) {
170 * init language system
172 protected function initLanguage () {
174 * @TODO: activate this later
175 * self::buildlanguage();
177 $availableLanguages = array("de", "en");
178 $languageCode = 'de';
180 if (isset($_GET['l'])) {
181 $code = strtolower($_GET['l']);
182 if (in_array($code, $availableLanguages)) {
183 $languageCode = $code;
185 else if (array_key_exists($code, $availableLanguages)) {
186 $languageCode = $availableLanguages[$code];
189 else if (self
::getSession()->language
!== null) {
190 $code = strtolower(self
::getSession()->language
);
191 if (in_array($code, $availableLanguages)) {
192 $languageCode = $code;
194 else if (array_key_exists($code, $availableLanguages)) {
195 $languageCode = $availableLanguages[$code];
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;
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);
216 if (file_exists($file)) {
218 if (isset($lang) && !empty($lang) && is_array($lang)) {
219 $this->language
= array_merge($this->language
, $lang);
224 /* load default language files */
225 $basedir = DNS_DIR
.'/lang/';
226 $file = $basedir.$languageCode.'.lang.php';
227 self
::getSession()->register('language', $languageCode);
229 if (file_exists($file)) {
231 if (isset($lang) && !empty($lang) && is_array($lang)) {
232 $this->language
= array_merge($this->language
, $lang);
240 * Executes template scripting in a language variable.
242 * @param string $item
243 * @param array $variables
244 * @return string result
246 public static function getLanguageVariable ($item, array $variables = array()) {
247 $lang = self
::getTPL()->getTemplateVars('language');
253 if (!empty($variables)) {
254 self
::getTPL()->assign($variables);
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();
272 * init template engine
274 protected function initTPL () {
275 require(DNS_DIR
.'/config.inc.php');
277 if (self
::getSession()->tpl
!== null && !empty(self
::getSession()->tpl
)) {
278 $tpl = self
::getSession()->tpl
;
281 if (!file_exists(DNS_DIR
.'/lib/system/api/smarty/libs/Smarty.class.php')) {
282 throw new SystemException('Unable to find Smarty');
285 require_once(DNS_DIR
.'/lib/system/api/smarty/libs/Smarty.class.php');
286 self
::$tplObj = new \Smarty
;
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);
290 self
::getTPL()->setPluginsDir(array(
291 DNS_DIR
."/lib/system/api/smarty/libs/plugins",
292 DNS_DIR
."/lib/template/plugins"
294 self
::getTPL()->loadFilter('pre', 'hascontent');
297 self
::getTPL()->loadFilter('output', 'trimwhitespace');
300 /* assign language variables */
301 self
::getTPL()->assign(array(
302 "language" => $this->language
,
303 "isReseller" => User
::isReseller(),
304 "isAdmin" => User
::isAdmin()
307 /*self::getTPL()->assign("version", mb_substr(sha1(DNS_VERSION), 0, 8));*/
312 * get template engine
314 public static function getTPL () {
315 return self
::$tplObj;
319 * Creates a random hash.
323 public static function generateRandomID() {
324 return sha1(microtime() . uniqid(mt_rand(), true));
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)));
337 * build options from database
339 * @param boolean $force
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)) {
349 $options = self
::getDB()->query("select * from dns_options");
350 $content = "<?php\n/* generated at ".gmdate('r')." */\n";
352 while ($row = self
::getDB()->fetch_array($options)) {
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";
356 $handler = fOpen($file, "a+");
357 fWrite($handler, $content);
365 * build language files from database
367 * @param boolean $force
369 public static function buildlanguage ($force = false) {
370 $availableLanguages = array("de", "en");
371 foreach ($availableLanguages as $languageID => $languageCode) {
373 $file = DNS_DIR
."/lang/".$languageCode.".lang.php";
374 if (!file_exists($file) ||
(filemtime($file) +
86400) < time() ||
$force === true) {
375 if (file_exists($file)) {
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)) {
386 $content .= "\$lang['".$row['languageItem']."'] = '".str_replace("\$", '$', $row['languageValue'])."';\n";
389 $handler = fOpen($file, "a+");
390 fWrite($handler, $content);