f0e111b4ef980dab557341012be7b561aa942bed
[GitHub/Stricted/Domain-Control-Panel.git] / lib / system / DNS.class.php
1 <?php
2 namespace dns\system;
3
4 if (!defined('DNS_VERSION')) define('DNS_VERSION', '3.0.0 Beta');
5
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 {
12 /**
13 * database object
14 *
15 * @var object
16 */
17 protected static $dbObj = null;
18
19 /**
20 * session object
21 *
22 * @var object
23 */
24 protected static $sessionObj = null;
25
26 /**
27 * template object
28 *
29 * @var object
30 */
31 protected static $tplObj = null;
32
33 /**
34 * language array
35 *
36 * @var array
37 */
38 protected $language = array();
39
40 /**
41 * init main system
42 */
43 public function __construct() {
44 spl_autoload_register(array('self', 'autoload'));
45
46 $this->initDB();
47 self::buildOptions();
48 $this->initSession();
49 $this->initLanguage();
50 $this->initTPL();
51 new RequestHandler();
52 }
53
54 /**
55 * get database
56 */
57 public static function getDB() {
58 return self::$dbObj;
59 }
60
61 /**
62 * init database
63 */
64 protected function initDB() {
65 require(DNS_DIR.'/config.inc.php');
66 self::$dbObj = new DB($driver, $host, $user, $pass, $db, $port);
67 }
68
69 /**
70 * init session system
71 */
72 protected function initSession() {
73 self::$sessionObj = new SessionHandler();
74 }
75
76 /**
77 * return session object
78 */
79 public static function getSession() {
80 return self::$sessionObj;
81 }
82
83 /*
84 * autoload class files from namespace uses
85 *
86 * @param string $className
87 */
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);
95 }
96 }
97 }
98
99 /**
100 * init language system
101 */
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;
110 }
111 else if (array_key_exists($code, $availableLanguages)) {
112 $languageCode = $availableLanguages[$code];
113 }
114 }
115 else if (DNS::getSession()->language !== null) {
116 $code = strtolower(DNS::getSession()->language);
117 if (in_array($code, $availableLanguages)) {
118 $languageCode = $code;
119 }
120 else if (array_key_exists($code, $availableLanguages)) {
121 $languageCode = $availableLanguages[$code];
122 }
123 }
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;
130 break;
131 }
132 }
133 }
134
135 $file = $basedir.$languageCode.'.lang.php';
136 DNS::getSession()->register('language', $languageCode);
137
138 if (file_exists($file)) {
139 require_once($file);
140 if (isset($lang) && !empty($lang) && is_array($lang)) {
141 $this->language = array_merge($this->language, $lang);
142 }
143 }
144
145 return;
146 }
147
148 /**
149 * Executes template scripting in a language variable.
150 *
151 * @param string $item
152 * @param array $variables
153 * @return string result
154 */
155 public static function getLanguageVariable ($item, array $variables = array()) {
156 $lang = self::getTPL()->getTemplateVars('language');
157
158 if ($lang == null) {
159 return $item;
160 }
161
162 if (!empty($variables)) {
163 self::getTPL()->assign($variables);
164 }
165
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();
172 }
173
174 return $lang[$item];
175 }
176
177 return $item;
178 }
179
180 /**
181 * init template engine
182 */
183 protected function initTPL () {
184 require(DNS_DIR.'/config.inc.php');
185
186 if (DNS::getSession()->tpl !== null && !empty(DNS::getSession()->tpl)) {
187 $tpl = DNS::getSession()->tpl;
188 }
189
190 require_once(DNS_DIR.'/lib/api/smarty/Smarty.class.php');
191 self::$tplObj = new \Smarty;
192
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');
197
198 if (!ENABLE_DEBUG) {
199 self::getTPL()->loadFilter('output', 'trimwhitespace');
200 }
201
202 /* assign language variables */
203 self::getTPL()->assign(array(
204 "language" => $this->language,
205 "isReseller" => User::isReseller(),
206 "isAdmin" => User::isAdmin()
207 ));
208
209 /*self::getTPL()->assign("version", mb_substr(sha1(DNS_VERSION), 0, 8));*/
210
211 }
212
213 /**
214 * get template engine
215 */
216 public static function getTPL () {
217 return self::$tplObj;
218 }
219
220 /**
221 * Creates a random hash.
222 *
223 * @return string
224 */
225 public static function generateRandomID() {
226 return sha1(microtime() . uniqid(mt_rand(), true));
227 }
228
229 /**
230 * Creates an UUID.
231 *
232 * @return string
233 */
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)));
236 }
237
238 /**
239 * build options from database
240 *
241 * @param boolean $force
242 */
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)) {
247 @unlink($file);
248 }
249
250 @touch($file);
251 $options = self::getDB()->query("select * from dns_options");
252 $content = "<?php\n/* generated at ".gmdate('r')." */\n";
253
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";
256 }
257
258 $handler = fOpen($file, "a+");
259 fWrite($handler, $content);
260 fClose($handler);
261 }
262
263 require_once($file);
264 }
265 }