f0e111b4ef980dab557341012be7b561aa942bed
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('self', 'autoload'));
49 $this->initLanguage();
57 public static function getDB() {
64 protected function initDB() {
65 require(DNS_DIR
.'/config.inc.php');
66 self
::$dbObj = new DB($driver, $host, $user, $pass, $db, $port);
72 protected function initSession() {
73 self
::$sessionObj = new SessionHandler();
77 * return session object
79 public static function getSession() {
80 return self
::$sessionObj;
84 * autoload class files from namespace uses
86 * @param string $className
88 public static function autoload ($className) {
89 $namespaces = explode('\\', $className);
90 if (count($namespaces) > 1) {
91 array_shift($namespaces);
92 $classPath = DNS_DIR
.'/lib/'.implode('/', $namespaces).'.class.php';
93 if (file_exists($classPath)) {
94 require_once($classPath);
100 * init language system
102 protected function initLanguage () {
103 $availableLanguages = array("de", "en");
104 $languageCode = 'de';
105 $basedir = DNS_DIR
.'/lang/';
106 if (isset($_GET['l'])) {
107 $code = strtolower($_GET['l']);
108 if (in_array($code, $availableLanguages)) {
109 $languageCode = $code;
111 else if (array_key_exists($code, $availableLanguages)) {
112 $languageCode = $availableLanguages[$code];
115 else if (DNS
::getSession()->language
!== null) {
116 $code = strtolower(DNS
::getSession()->language
);
117 if (in_array($code, $availableLanguages)) {
118 $languageCode = $code;
120 else if (array_key_exists($code, $availableLanguages)) {
121 $languageCode = $availableLanguages[$code];
124 else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) {
125 $acceptedLanguages = explode(',', str_replace('_', '-', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
126 foreach ($acceptedLanguages as $acceptedLanguage) {
127 $code = strtolower(preg_replace('%^([a-z]{2}).*$%i', '$1', $acceptedLanguage));
128 if (in_array($code, $availableLanguages)) {
129 $languageCode = $code;
135 $file = $basedir.$languageCode.'.lang.php';
136 DNS
::getSession()->register('language', $languageCode);
138 if (file_exists($file)) {
140 if (isset($lang) && !empty($lang) && is_array($lang)) {
141 $this->language
= array_merge($this->language
, $lang);
149 * Executes template scripting in a language variable.
151 * @param string $item
152 * @param array $variables
153 * @return string result
155 public static function getLanguageVariable ($item, array $variables = array()) {
156 $lang = self
::getTPL()->getTemplateVars('language');
162 if (!empty($variables)) {
163 self
::getTPL()->assign($variables);
166 if (isset($lang[$item])) {
167 if (strpos($lang[$item], self
::getTPL()->left_delimiter
) !== false && strpos($lang[$item], self
::getTPL()->right_delimiter
) !== false) {
168 $data = str_replace("\$", '$', $lang[$item]);
169 $template_class = self
::getTPL()->template_class
;
170 $template = new $template_class('eval:'.$data, self
::getTPL(), self
::getTPL());
171 return $template->fetch();
181 * init template engine
183 protected function initTPL () {
184 require(DNS_DIR
.'/config.inc.php');
186 if (DNS
::getSession()->tpl
!== null && !empty(DNS
::getSession()->tpl
)) {
187 $tpl = DNS
::getSession()->tpl
;
190 require_once(DNS_DIR
.'/lib/api/smarty/Smarty.class.php');
191 self
::$tplObj = new \Smarty
;
193 self
::getTPL()->setTemplateDir(DNS_DIR
."/templates/".$tpl);
194 self
::getTPL()->setCompileDir(DNS_DIR
."/templates/compiled/".$tpl);
195 self
::getTPL()->setPluginsDir(DNS_DIR
."/lib/api/smarty/plugins");
196 self
::getTPL()->loadFilter('pre', 'hascontent');
199 self
::getTPL()->loadFilter('output', 'trimwhitespace');
202 /* assign language variables */
203 self
::getTPL()->assign(array(
204 "language" => $this->language
,
205 "isReseller" => User
::isReseller(),
206 "isAdmin" => User
::isAdmin()
209 /*self::getTPL()->assign("version", mb_substr(sha1(DNS_VERSION), 0, 8));*/
214 * get template engine
216 public static function getTPL () {
217 return self
::$tplObj;
221 * Creates a random hash.
225 public static function generateRandomID() {
226 return sha1(microtime() . uniqid(mt_rand(), true));
234 public static function generateUUID() {
235 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)));
239 * build options from database
241 * @param boolean $force
243 public static function buildOptions ($force = false) {
244 $file = DNS_DIR
."/options.inc.php";
245 if (!file_exists($file) ||
(filemtime($file) +
86400) < time() ||
$force === true) {
246 if (file_exists($file)) {
251 $options = self
::getDB()->query("select * from dns_options");
252 $content = "<?php\n/* generated at ".gmdate('r')." */\n";
254 while ($row = self
::getDB()->fetch_array($options)) {
255 $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";
258 $handler = fOpen($file, "a+");
259 fWrite($handler, $content);