03e87ac0302c8be50130117ae13d233ceb1ec1a5
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 $dbObj = null;
24 protected static $sessionObj = null;
31 protected static $tplObj = null;
38 protected $language = array();
43 public function __construct() {
44 spl_autoload_register(array('dns\system\DNS', 'autoload'));
45 set_exception_handler(array('dns\system\DNS', 'handleException'));
46 set_error_handler(array('dns\system\DNS', 'handleError'), E_ALL
);
47 /*register_shutdown_function(array('dns\system\DNS', 'destruct'));*/
52 $this->initLanguage();
58 public static function destruct() {
59 $error = error_get_last();
61 self::handleException(new SystemException($error['message'], $error['line']));
70 public static function getDB() {
77 protected function initDB() {
78 require(DNS_DIR
.'/config.inc.php');
79 self
::$dbObj = new DB($driver, $host, $user, $pass, $db, $port);
85 protected function initSession() {
86 self
::$sessionObj = new SessionHandler();
90 * return session object
92 public static function getSession() {
93 return self
::$sessionObj;
97 * autoload class files from namespace uses
99 * @param string $className
101 public static function autoload ($className) {
102 $namespaces = explode('\\', $className);
103 if (count($namespaces) > 1) {
104 array_shift($namespaces);
105 $classPath = DNS_DIR
.'/lib/'.implode('/', $namespaces).'.class.php';
106 if (file_exists($classPath)) {
107 require_once($classPath);
113 * Calls the show method on the given exception.
115 * @param \Exception $e
117 public static final function handleException(\Exception
$e) {
119 if ($e instanceof SystemException
) {
125 self
::handleException(new SystemException($e->getMessage(), $e->getCode(), '', $e));
127 catch (\Exception
$exception) {
128 die("<pre>DNS::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString());
133 * Catches php errors and throws instead a system exception.
135 * @param integer $errorNo
136 * @param string $message
137 * @param string $filename
138 * @param integer $lineNo
140 public static final function handleError($errorNo, $message, $filename, $lineNo) {
141 if (error_reporting() != 0) {
144 case 2: $type = 'warning';
146 case 8: $type = 'notice';
150 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
155 * Returns true if the debug mode is enabled, otherwise false.
159 public static function debugModeIsEnabled() {
160 // try to load constants
161 if (!defined('ENABLE_DEBUG')) {
162 self
::buildOptions();
165 if (defined('ENABLE_DEBUG') && ENABLE_DEBUG
) {
173 * init language system
175 protected function initLanguage () {
176 $availableLanguages = array("de", "en");
177 $languageCode = 'de';
178 $basedir = DNS_DIR
.'/lang/';
179 if (isset($_GET['l'])) {
180 $code = strtolower($_GET['l']);
181 if (in_array($code, $availableLanguages)) {
182 $languageCode = $code;
184 else if (array_key_exists($code, $availableLanguages)) {
185 $languageCode = $availableLanguages[$code];
188 else if (self
::getSession()->language
!== null) {
189 $code = strtolower(self
::getSession()->language
);
190 if (in_array($code, $availableLanguages)) {
191 $languageCode = $code;
193 else if (array_key_exists($code, $availableLanguages)) {
194 $languageCode = $availableLanguages[$code];
197 else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) {
198 $acceptedLanguages = explode(',', str_replace('_', '-', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
199 foreach ($acceptedLanguages as $acceptedLanguage) {
200 $code = strtolower(preg_replace('%^([a-z]{2}).*$%i', '$1', $acceptedLanguage));
201 if (in_array($code, $availableLanguages)) {
202 $languageCode = $code;
208 $file = $basedir.$languageCode.'.lang.php';
209 self
::getSession()->register('language', $languageCode);
211 if (file_exists($file)) {
213 if (isset($lang) && !empty($lang) && is_array($lang)) {
214 $this->language
= array_merge($this->language
, $lang);
222 * Executes template scripting in a language variable.
224 * @param string $item
225 * @param array $variables
226 * @return string result
228 public static function getLanguageVariable ($item, array $variables = array()) {
229 $lang = self
::getTPL()->getTemplateVars('language');
235 if (!empty($variables)) {
236 self
::getTPL()->assign($variables);
239 if (isset($lang[$item])) {
240 if (strpos($lang[$item], self
::getTPL()->left_delimiter
) !== false && strpos($lang[$item], self
::getTPL()->right_delimiter
) !== false) {
241 $data = str_replace("\$", '$', $lang[$item]);
242 $template_class = self
::getTPL()->template_class
;
243 $template = new $template_class('eval:'.$data, self
::getTPL(), self
::getTPL());
244 return $template->fetch();
254 * init template engine
256 protected function initTPL () {
257 require(DNS_DIR
.'/config.inc.php');
259 if (self
::getSession()->tpl
!== null && !empty(self
::getSession()->tpl
)) {
260 $tpl = self
::getSession()->tpl
;
263 require_once(DNS_DIR
.'/lib/system/api/smarty/libs/Smarty.class.php');
264 self
::$tplObj = new \Smarty
;
266 self
::getTPL()->setTemplateDir(DNS_DIR
."/templates/".$tpl);
267 self
::getTPL()->setCompileDir(DNS_DIR
."/templates/compiled/".$tpl);
268 self
::getTPL()->setPluginsDir(array(
269 DNS_DIR
."/lib/system/api/smarty/libs/plugins",
270 DNS_DIR
."/lib/template/plugins"
272 self
::getTPL()->loadFilter('pre', 'hascontent');
275 self
::getTPL()->loadFilter('output', 'trimwhitespace');
278 /* assign language variables */
279 self
::getTPL()->assign(array(
280 "language" => $this->language
,
281 "isReseller" => User
::isReseller(),
282 "isAdmin" => User
::isAdmin()
285 /*self::getTPL()->assign("version", mb_substr(sha1(DNS_VERSION), 0, 8));*/
290 * get template engine
292 public static function getTPL () {
293 return self
::$tplObj;
297 * Creates a random hash.
301 public static function generateRandomID() {
302 return sha1(microtime() . uniqid(mt_rand(), true));
310 public static function generateUUID() {
311 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)));
315 * build options from database
317 * @param boolean $force
319 public static function buildOptions ($force = false) {
320 $file = DNS_DIR
."/options.inc.php";
321 if (!file_exists($file) ||
(filemtime($file) +
86400) < time() ||
$force === true) {
322 if (file_exists($file)) {
327 $options = self
::getDB()->query("select * from dns_options");
328 $content = "<?php\n/* generated at ".gmdate('r')." */\n";
330 while ($row = self
::getDB()->fetch_array($options)) {
331 $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";
334 $handler = fOpen($file, "a+");
335 fWrite($handler, $content);