@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | // |
51 | 51 | } |
52 | 52 | |
53 | - public function load (int $userID = -1) { |
|
53 | + public function load(int $userID = -1) { |
|
54 | 54 | //check, if user is logged in |
55 | 55 | if ($userID === -1) { |
56 | 56 | if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) { |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | } |
152 | 152 | } |
153 | 153 | |
154 | - public function loginByUsername (string $username, string $password) : array { |
|
154 | + public function loginByUsername(string $username, string $password) : array { |
|
155 | 155 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
156 | 156 | 'username' => &$username |
157 | 157 | )); |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | return $this->loginRow($row, $password); |
176 | 176 | } |
177 | 177 | |
178 | - public function loginByMail (string $mail, string $password) : array { |
|
178 | + public function loginByMail(string $mail, string $password) : array { |
|
179 | 179 | //check, if mail is valide |
180 | 180 | $validator = new Validator_Mail(); |
181 | 181 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | return $this->loginRow($row, $password); |
194 | 194 | } |
195 | 195 | |
196 | - public function loginByID (int $userID) : array { |
|
196 | + public function loginByID(int $userID) : array { |
|
197 | 197 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
198 | 198 | 'userID' => &$userID |
199 | 199 | )); |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | * |
229 | 229 | * @return true, if password is correct |
230 | 230 | */ |
231 | - public function checkPassword (string $password) : bool { |
|
231 | + public function checkPassword(string $password) : bool { |
|
232 | 232 | if ($this->row == null || empty($this->row)) { |
233 | 233 | throw new IllegalStateException("user wasnt loaded."); |
234 | 234 | } |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | return password_verify($password, $row['password']); |
248 | 248 | } |
249 | 249 | |
250 | - public function setPassword (string $password) { |
|
250 | + public function setPassword(string $password) { |
|
251 | 251 | if ($this->row == null || empty($this->row)) { |
252 | 252 | throw new IllegalStateException("user wasnt loaded."); |
253 | 253 | } |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | Cache::clear("user", "user-" . $this->getID()); |
273 | 273 | } |
274 | 274 | |
275 | - protected function loginRow ($row, string $password) : array { |
|
275 | + protected function loginRow($row, string $password) : array { |
|
276 | 276 | $res = array(); |
277 | 277 | |
278 | 278 | if (!$row) { |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | } |
322 | 322 | } |
323 | 323 | |
324 | - protected function setLoggedIn (int $userID, string $username, array $row) { |
|
324 | + protected function setLoggedIn(int $userID, string $username, array $row) { |
|
325 | 325 | $_SESSION['logged-in'] = true; |
326 | 326 | $_SESSION['userID'] = (int) $userID; |
327 | 327 | $_SESSION['username'] = $username; |
@@ -334,7 +334,7 @@ discard block |
||
334 | 334 | $this->row = $row; |
335 | 335 | } |
336 | 336 | |
337 | - public function logout () { |
|
337 | + public function logout() { |
|
338 | 338 | //check, if session was started |
339 | 339 | PHPUtils::checkSessionStarted(); |
340 | 340 | |
@@ -346,13 +346,13 @@ discard block |
||
346 | 346 | $this->setGuest(); |
347 | 347 | } |
348 | 348 | |
349 | - protected function setGuest () { |
|
349 | + protected function setGuest() { |
|
350 | 350 | $this->userID = (int) Settings::get("guest_userid", "-1"); |
351 | 351 | $this->username = Settings::get("guest_username", "Guest"); |
352 | 352 | $this->isLoggedIn = false; |
353 | 353 | } |
354 | 354 | |
355 | - protected static function hashPassword ($password, $salt) { |
|
355 | + protected static function hashPassword($password, $salt) { |
|
356 | 356 | //http://php.net/manual/de/function.password-hash.php |
357 | 357 | |
358 | 358 | //add salt to password |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | * |
377 | 377 | * @return integer userID |
378 | 378 | */ |
379 | - public function getID () : int { |
|
379 | + public function getID() : int { |
|
380 | 380 | return $this->userID; |
381 | 381 | } |
382 | 382 | |
@@ -385,27 +385,27 @@ discard block |
||
385 | 385 | * |
386 | 386 | * @return string username |
387 | 387 | */ |
388 | - public function getUsername () : string { |
|
388 | + public function getUsername() : string { |
|
389 | 389 | return $this->username; |
390 | 390 | } |
391 | 391 | |
392 | - public function getMail () : string { |
|
392 | + public function getMail() : string { |
|
393 | 393 | return $this->row['mail']; |
394 | 394 | } |
395 | 395 | |
396 | - public function isLoggedIn () : bool { |
|
396 | + public function isLoggedIn() : bool { |
|
397 | 397 | return $this->isLoggedIn; |
398 | 398 | } |
399 | 399 | |
400 | - public function getRow () : array { |
|
400 | + public function getRow() : array { |
|
401 | 401 | return $this->row; |
402 | 402 | } |
403 | 403 | |
404 | - public function hasTitle () : bool { |
|
404 | + public function hasTitle() : bool { |
|
405 | 405 | return $this->row['specific_title'] !== "none"; |
406 | 406 | } |
407 | 407 | |
408 | - public function getTitle () : string { |
|
408 | + public function getTitle() : string { |
|
409 | 409 | if (!$this->hasTitle()) { |
410 | 410 | return Settings::get("user_default_title", "User"); |
411 | 411 | } |
@@ -413,7 +413,7 @@ discard block |
||
413 | 413 | return $this->row['specific_title']; |
414 | 414 | } |
415 | 415 | |
416 | - public function setOnline (bool $updateIP = true) { |
|
416 | + public function setOnline(bool $updateIP = true) { |
|
417 | 417 | //get client ip |
418 | 418 | $ip = PHPUtils::getClientIP(); |
419 | 419 | |
@@ -435,7 +435,7 @@ discard block |
||
435 | 435 | } |
436 | 436 | } |
437 | 437 | |
438 | - public function updateOnlineList () { |
|
438 | + public function updateOnlineList() { |
|
439 | 439 | $interval_minutes = (int) Settings::get("online_interval", "5"); |
440 | 440 | |
441 | 441 | Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; "); |
@@ -446,7 +446,7 @@ discard block |
||
446 | 446 | * |
447 | 447 | * Only use this method for installation & upgrade! |
448 | 448 | */ |
449 | - public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
449 | + public static function createIfIdAbsent(int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
450 | 450 | if (self::existsUserID($userID)) { |
451 | 451 | //dont create user, if user already exists |
452 | 452 | return; |
@@ -474,7 +474,7 @@ discard block |
||
474 | 474 | )); |
475 | 475 | } |
476 | 476 | |
477 | - public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1, string $authentificator = "LocalAuthentificator", string $owner = "system") { |
|
477 | + public static function create(string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1, string $authentificator = "LocalAuthentificator", string $owner = "system") { |
|
478 | 478 | if (self::existsUsername($username)) { |
479 | 479 | //dont create user, if username already exists |
480 | 480 | return false; |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | ); |
540 | 540 | } |
541 | 541 | |
542 | - public static function deleteUserID (int $userID) { |
|
542 | + public static function deleteUserID(int $userID) { |
|
543 | 543 | Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
544 | 544 | 'userID' => array( |
545 | 545 | 'type' => PDO::PARAM_INT, |
@@ -551,7 +551,7 @@ discard block |
||
551 | 551 | Cache::clear("user", "user-" . $userID); |
552 | 552 | } |
553 | 553 | |
554 | - public static function existsUserID (int $userID) : bool { |
|
554 | + public static function existsUserID(int $userID) : bool { |
|
555 | 555 | //search for userID in database |
556 | 556 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
557 | 557 | 'userID' => array( |
@@ -563,21 +563,21 @@ discard block |
||
563 | 563 | return $row !== false; |
564 | 564 | } |
565 | 565 | |
566 | - public static function existsUsername (string $username) : bool { |
|
566 | + public static function existsUsername(string $username) : bool { |
|
567 | 567 | //search for username in database, ignore case |
568 | 568 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
569 | 569 | |
570 | 570 | return $row !== false; |
571 | 571 | } |
572 | 572 | |
573 | - public static function existsMail (string $mail) : bool { |
|
573 | + public static function existsMail(string $mail) : bool { |
|
574 | 574 | //search for mail in database, ignore case |
575 | 575 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail)); |
576 | 576 | |
577 | 577 | return $row !== false; |
578 | 578 | } |
579 | 579 | |
580 | - public static function getIDByUsernameFromDB (string $username) : int { |
|
580 | + public static function getIDByUsernameFromDB(string $username) : int { |
|
581 | 581 | //use in-memory cache to avoid a large amount of sql queries on setup scripts |
582 | 582 | if (isset(self::$getIDByUsernameFromDB_cache[$username])) { |
583 | 583 | return self::$getIDByUsernameFromDB_cache[$username]; |
@@ -597,7 +597,7 @@ discard block |
||
597 | 597 | return $userID; |
598 | 598 | } |
599 | 599 | |
600 | - public static function &getAuthentificatorByID (int $userID = -1) { |
|
600 | + public static function &getAuthentificatorByID(int $userID = -1) { |
|
601 | 601 | if ($userID == -1) { |
602 | 602 | //get default authentificator |
603 | 603 | return self::getDefaultAuthentificator(); |
@@ -620,7 +620,7 @@ discard block |
||
620 | 620 | } |
621 | 621 | } |
622 | 622 | |
623 | - public static function &getAuthentificatorByUsername (string $username = "") { |
|
623 | + public static function &getAuthentificatorByUsername(string $username = "") { |
|
624 | 624 | if ($username == null || empty($username)) { |
625 | 625 | //get default authentificator |
626 | 626 | return self::getDefaultAuthentificator(); |
@@ -643,7 +643,7 @@ discard block |
||
643 | 643 | } |
644 | 644 | } |
645 | 645 | |
646 | - public static function &getDefaultAuthentificator () : IAuthentificator { |
|
646 | + public static function &getDefaultAuthentificator() : IAuthentificator { |
|
647 | 647 | if (self::$default_authentificator == null) { |
648 | 648 | $class_name = Settings::get("default_authentificator", "LocalAuthentificator"); |
649 | 649 | $obj = new $class_name(); |
@@ -657,7 +657,7 @@ discard block |
||
657 | 657 | /** |
658 | 658 | * get instance of current (logged in / guest) user |
659 | 659 | */ |
660 | - public static function ¤t () : User { |
|
660 | + public static function ¤t() : User { |
|
661 | 661 | if (self::$instance == null) { |
662 | 662 | self::$instance = new User(); |
663 | 663 | self::$instance->load(); |
@@ -27,646 +27,646 @@ |
||
27 | 27 | |
28 | 28 | class User { |
29 | 29 | |
30 | - //instance of current (logged-in / guest) user |
|
31 | - protected static $instance = null; |
|
32 | - |
|
33 | - //current userID |
|
34 | - protected $userID = -1; |
|
35 | - |
|
36 | - //current username |
|
37 | - protected $username = "Guest"; |
|
38 | - |
|
39 | - //flag, if user is logged in |
|
40 | - protected $isLoggedIn = false; |
|
41 | - |
|
42 | - //current database row |
|
43 | - protected $row = null; |
|
44 | - |
|
45 | - protected static $default_authentificator = null; |
|
46 | - |
|
47 | - protected static $getIDByUsernameFromDB_cache = array(); |
|
48 | - |
|
49 | - public function __construct() { |
|
50 | - // |
|
51 | - } |
|
52 | - |
|
53 | - public function load (int $userID = -1) { |
|
54 | - //check, if user is logged in |
|
55 | - if ($userID === -1) { |
|
56 | - if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) { |
|
57 | - if (!isset($_SESSION['userID']) || empty($_SESSION['userID'])) { |
|
58 | - throw new IllegalStateException("userID is not set in session."); |
|
59 | - } |
|
60 | - |
|
61 | - if (!isset($_SESSION['username']) || empty($_SESSION['username'])) { |
|
62 | - throw new IllegalStateException("username is not set in session."); |
|
63 | - } |
|
64 | - |
|
65 | - $this->userID = (int) $_SESSION['userID']; |
|
66 | - $this->username = $_SESSION['username']; |
|
67 | - $this->isLoggedIn = true; |
|
68 | - |
|
69 | - //TODO: update online state in database |
|
70 | - } else { |
|
71 | - $this->setGuest(); |
|
72 | - } |
|
73 | - } else if ($userID == 0) { |
|
74 | - throw new IllegalArgumentException("userID cannot be 0, userID has to be > 0."); |
|
75 | - } else { |
|
76 | - $this->userID = (int) $userID; |
|
77 | - } |
|
78 | - |
|
79 | - Events::throwEvent("before_load_user", array( |
|
80 | - 'userID' => &$this->userID, |
|
81 | - 'isLoggedIn' => &$this->isLoggedIn, |
|
82 | - 'user' => &$this |
|
83 | - )); |
|
84 | - |
|
85 | - //try to load from cache |
|
86 | - if (Cache::contains("user", "user-" . $this->userID)) { |
|
87 | - $this->row = Cache::get("user", "user-" . $this->userID); |
|
88 | - } else { |
|
89 | - $row = false; |
|
90 | - |
|
91 | - //check, if guest user, because guest user doesnt exists in database |
|
92 | - if ($this->userID !== -1) { |
|
93 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
94 | - 'userID' => array( |
|
95 | - 'type' => PDO::PARAM_INT, |
|
96 | - 'value' => $this->userID |
|
97 | - ) |
|
98 | - )); |
|
99 | - } |
|
100 | - |
|
101 | - if (!$row) { |
|
102 | - $logout_user = true; |
|
103 | - |
|
104 | - //user not found, throw an event, so plugins can handle this (optional) |
|
105 | - Events::throwEvent("user_not_found", array( |
|
106 | - 'userID' => &$this->userID, |
|
107 | - 'username' => &$this->username, |
|
108 | - 'isLoggedIn' => &$this->isLoggedIn, |
|
109 | - 'row' => &$row, |
|
110 | - 'logout_user' => &$logout_user, |
|
111 | - 'user' => &$this |
|
112 | - )); |
|
113 | - |
|
114 | - if ($logout_user && $this->userID == -1) { |
|
115 | - //logout user |
|
116 | - $this->logout(); |
|
117 | - } |
|
118 | - } else { |
|
119 | - //remove password hash from row |
|
120 | - unset($row['password']); |
|
121 | - |
|
122 | - Events::throwEvent("before_cache_user", array( |
|
123 | - 'userID' => &$this->userID, |
|
124 | - 'username' => &$this->username, |
|
125 | - 'isLoggedIn' => &$this->isLoggedIn, |
|
126 | - 'row' => &$row, |
|
127 | - 'user' => &$this |
|
128 | - )); |
|
129 | - |
|
130 | - //cache entry |
|
131 | - Cache::put("user", "user-" . $this->userID, $row); |
|
132 | - |
|
133 | - $this->row = $row; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - if ($this->row !== null) { |
|
138 | - $this->userID = (int) $this->row['userID']; |
|
139 | - $this->username = $this->row['username']; |
|
140 | - } |
|
141 | - |
|
142 | - Events::throwEvent("after_load_user", array( |
|
143 | - 'userID' => &$this->userID, |
|
144 | - 'username' => &$this->username, |
|
145 | - 'isLoggedIn' => &$this->isLoggedIn, |
|
146 | - 'row' => &$row, |
|
147 | - 'user' => &$this |
|
148 | - )); |
|
149 | - |
|
150 | - //TODO: update online state and IP |
|
151 | - if ($userID === -1 && $this->isLoggedIn()) { |
|
152 | - $this->setOnline(); |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - public function loginByUsername (string $username, string $password) : array { |
|
157 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
158 | - 'username' => &$username |
|
159 | - )); |
|
160 | - |
|
161 | - if (!$row) { |
|
162 | - //get default authentificator |
|
163 | - $authentificator = self::getDefaultAuthentificator(); |
|
164 | - |
|
165 | - $userID = $authentificator->checkPasswordAndImport($username, $password); |
|
166 | - |
|
167 | - if ($userID == -1) { |
|
168 | - //user not found |
|
169 | - } else { |
|
170 | - //user was imported now, get user row |
|
171 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
172 | - 'userID' => &$userID |
|
173 | - )); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - return $this->loginRow($row, $password); |
|
178 | - } |
|
179 | - |
|
180 | - public function loginByMail (string $mail, string $password) : array { |
|
181 | - //check, if mail is valide |
|
182 | - $validator = new Validator_Mail(); |
|
183 | - |
|
184 | - if (!$validator->isValide($mail)) { |
|
185 | - return array( |
|
186 | - 'success' => false, |
|
187 | - 'error' => "mail_not_valide" |
|
188 | - ); |
|
189 | - } |
|
190 | - |
|
191 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `mail` = :mail AND `activated` = '1'; ", array( |
|
192 | - 'mail' => &$mail |
|
193 | - )); |
|
194 | - |
|
195 | - return $this->loginRow($row, $password); |
|
196 | - } |
|
197 | - |
|
198 | - public function loginByID (int $userID) : array { |
|
199 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
200 | - 'userID' => &$userID |
|
201 | - )); |
|
202 | - |
|
203 | - $res = array(); |
|
204 | - |
|
205 | - if ($row !== false) { |
|
206 | - //set online state |
|
207 | - $this->setOnline(); |
|
208 | - |
|
209 | - //set logged in |
|
210 | - $this->setLoggedIn($row['userID'], $row['username'], $row); |
|
211 | - |
|
212 | - //login successful |
|
213 | - $res['success'] = true; |
|
214 | - $res['error'] = "none"; |
|
215 | - return $res; |
|
216 | - } else { |
|
217 | - //user doesnt exists |
|
218 | - $res['success'] = false; |
|
219 | - $res['error'] = "user_not_exists"; |
|
220 | - return $res; |
|
221 | - } |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * check password of current user |
|
226 | - * |
|
227 | - * @param $password string password |
|
228 | - * |
|
229 | - * @throws IllegalStateException if user wasnt loaded before |
|
230 | - * |
|
231 | - * @return true, if password is correct |
|
232 | - */ |
|
233 | - public function checkPassword (string $password) : bool { |
|
234 | - if ($this->row == null || empty($this->row)) { |
|
235 | - throw new IllegalStateException("user wasnt loaded."); |
|
236 | - } |
|
237 | - |
|
238 | - //because password is not cached, we have to load it directly from database |
|
239 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
240 | - 'userID' => $this->getID() |
|
241 | - )); |
|
242 | - |
|
243 | - //get salt |
|
244 | - $salt = $row['salt']; |
|
245 | - |
|
246 | - //add salt to password |
|
247 | - $password .= $salt; |
|
248 | - |
|
249 | - return password_verify($password, $row['password']); |
|
250 | - } |
|
251 | - |
|
252 | - public function setPassword (string $password) { |
|
253 | - if ($this->row == null || empty($this->row)) { |
|
254 | - throw new IllegalStateException("user wasnt loaded."); |
|
255 | - } |
|
256 | - |
|
257 | - //validate password |
|
258 | - $password = Validator_Password::get($password); |
|
259 | - |
|
260 | - //create new salt |
|
261 | - $salt = md5(PHPUtils::randomString(50)); |
|
262 | - |
|
263 | - //generate password hash |
|
264 | - $hashed_password = self::hashPassword($password, $salt); |
|
265 | - |
|
266 | - //update database |
|
267 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password, `salt` = :salt WHERE `userID` = :userID; ", array( |
|
268 | - 'password' => $hashed_password, |
|
269 | - 'salt' => $salt, |
|
270 | - 'userID' => $this->getID() |
|
271 | - )); |
|
272 | - |
|
273 | - //clear cache |
|
274 | - Cache::clear("user", "user-" . $this->getID()); |
|
275 | - } |
|
276 | - |
|
277 | - protected function loginRow ($row, string $password) : array { |
|
278 | - $res = array(); |
|
279 | - |
|
280 | - if (!$row) { |
|
281 | - //user doesnt exists |
|
282 | - $res['success'] = false; |
|
283 | - $res['error'] = "user_not_exists"; |
|
284 | - |
|
285 | - return $res; |
|
286 | - } |
|
287 | - |
|
288 | - if ($row['userID'] <= 0) { |
|
289 | - //user doesnt exists |
|
290 | - $res['success'] = false; |
|
291 | - $res['error'] = "not_permitted_user"; |
|
292 | - |
|
293 | - return $res; |
|
294 | - } |
|
295 | - |
|
296 | - //get authentificator |
|
297 | - $authentificator = self::getAuthentificatorByID($row['userID']); |
|
298 | - |
|
299 | - $auth_res = $authentificator->checkPasswordAndImport($row['username'], $password); |
|
300 | - |
|
301 | - //check password |
|
302 | - if ($auth_res !== -1) { |
|
303 | - //password is correct |
|
304 | - |
|
305 | - //set online state |
|
306 | - $this->setOnline(); |
|
307 | - |
|
308 | - //set logged in |
|
309 | - $this->setLoggedIn($row['userID'], $row['username'], $row); |
|
310 | - |
|
311 | - //login successful |
|
312 | - $res['success'] = true; |
|
313 | - $res['error'] = "none"; |
|
314 | - return $res; |
|
315 | - } else { |
|
316 | - //wrong password |
|
317 | - |
|
318 | - //user doesnt exists |
|
319 | - $res['success'] = false; |
|
320 | - $res['error'] = "wrong_password"; |
|
321 | - |
|
322 | - return $res; |
|
323 | - } |
|
324 | - } |
|
325 | - |
|
326 | - protected function setLoggedIn (int $userID, string $username, array $row) { |
|
327 | - $_SESSION['logged-in'] = true; |
|
328 | - $_SESSION['userID'] = (int) $userID; |
|
329 | - $_SESSION['username'] = $username; |
|
330 | - |
|
331 | - //remove password hash from row (so password isnt cached) |
|
332 | - unset($row['password']); |
|
333 | - |
|
334 | - $this->userID = $userID; |
|
335 | - $this->username = $username; |
|
336 | - $this->row = $row; |
|
337 | - } |
|
338 | - |
|
339 | - public function logout () { |
|
340 | - //check, if session was started |
|
341 | - PHPUtils::checkSessionStarted(); |
|
342 | - |
|
343 | - unset($_SESSION['userID']); |
|
344 | - unset($_SESSION['username']); |
|
345 | - |
|
346 | - $_SESSION['logged-in'] = false; |
|
347 | - |
|
348 | - $this->setGuest(); |
|
349 | - } |
|
350 | - |
|
351 | - protected function setGuest () { |
|
352 | - $this->userID = (int) Settings::get("guest_userid", "-1"); |
|
353 | - $this->username = Settings::get("guest_username", "Guest"); |
|
354 | - $this->isLoggedIn = false; |
|
355 | - } |
|
356 | - |
|
357 | - protected static function hashPassword ($password, $salt) { |
|
358 | - //http://php.net/manual/de/function.password-hash.php |
|
359 | - |
|
360 | - //add salt to password |
|
361 | - $password .= $salt; |
|
362 | - |
|
363 | - $options = array( |
|
364 | - 'cost' => (int) Settings::get("password_hash_cost", "10") |
|
365 | - ); |
|
366 | - $algo = PASSWORD_DEFAULT; |
|
367 | - |
|
368 | - Events::throwEvent("hashing_password", array( |
|
369 | - 'options' => &$options, |
|
370 | - 'algo' => &$algo |
|
371 | - )); |
|
372 | - |
|
373 | - return password_hash($password, $algo, $options); |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * get user ID of user |
|
378 | - * |
|
379 | - * @return integer userID |
|
380 | - */ |
|
381 | - public function getID () : int { |
|
382 | - return $this->userID; |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * get username of user |
|
387 | - * |
|
388 | - * @return string username |
|
389 | - */ |
|
390 | - public function getUsername () : string { |
|
391 | - return $this->username; |
|
392 | - } |
|
393 | - |
|
394 | - public function getMail () : string { |
|
395 | - return $this->row['mail']; |
|
396 | - } |
|
397 | - |
|
398 | - public function isLoggedIn () : bool { |
|
399 | - return $this->isLoggedIn; |
|
400 | - } |
|
401 | - |
|
402 | - public function getRow () : array { |
|
403 | - return $this->row; |
|
404 | - } |
|
405 | - |
|
406 | - public function hasTitle () : bool { |
|
407 | - return $this->row['specific_title'] !== "none"; |
|
408 | - } |
|
409 | - |
|
410 | - public function getTitle () : string { |
|
411 | - if (!$this->hasTitle()) { |
|
412 | - return Settings::get("user_default_title", "User"); |
|
413 | - } |
|
414 | - |
|
415 | - return $this->row['specific_title']; |
|
416 | - } |
|
417 | - |
|
418 | - public function setOnline (bool $updateIP = true) { |
|
419 | - //get client ip |
|
420 | - $ip = PHPUtils::getClientIP(); |
|
421 | - |
|
422 | - if ($updateIP) { |
|
423 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array( |
|
424 | - 'userid' => array( |
|
425 | - 'type' => PDO::PARAM_INT, |
|
426 | - 'value' => (int) $this->userID |
|
427 | - ), |
|
428 | - 'ip' => $ip |
|
429 | - )); |
|
430 | - } else { |
|
431 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array( |
|
432 | - 'userid' => array( |
|
433 | - 'type' => PDO::PARAM_INT, |
|
434 | - 'value' => (int) $this->userID |
|
435 | - ) |
|
436 | - )); |
|
437 | - } |
|
438 | - } |
|
439 | - |
|
440 | - public function updateOnlineList () { |
|
441 | - $interval_minutes = (int) Settings::get("online_interval", "5"); |
|
442 | - |
|
443 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; "); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * creates user if userID is absent |
|
448 | - * |
|
449 | - * Only use this method for installation & upgrade! |
|
450 | - */ |
|
451 | - public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
452 | - if (self::existsUserID($userID)) { |
|
453 | - //dont create user, if user already exists |
|
454 | - return; |
|
455 | - } |
|
456 | - |
|
457 | - //create salt |
|
458 | - $salt = md5(PHPUtils::randomString(50)); |
|
459 | - |
|
460 | - //generate password hash |
|
461 | - $hashed_password = self::hashPassword($password, $salt); |
|
462 | - |
|
463 | - Database::getInstance()->execute("INSERT INTO `{praefix}user` ( |
|
30 | + //instance of current (logged-in / guest) user |
|
31 | + protected static $instance = null; |
|
32 | + |
|
33 | + //current userID |
|
34 | + protected $userID = -1; |
|
35 | + |
|
36 | + //current username |
|
37 | + protected $username = "Guest"; |
|
38 | + |
|
39 | + //flag, if user is logged in |
|
40 | + protected $isLoggedIn = false; |
|
41 | + |
|
42 | + //current database row |
|
43 | + protected $row = null; |
|
44 | + |
|
45 | + protected static $default_authentificator = null; |
|
46 | + |
|
47 | + protected static $getIDByUsernameFromDB_cache = array(); |
|
48 | + |
|
49 | + public function __construct() { |
|
50 | + // |
|
51 | + } |
|
52 | + |
|
53 | + public function load (int $userID = -1) { |
|
54 | + //check, if user is logged in |
|
55 | + if ($userID === -1) { |
|
56 | + if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) { |
|
57 | + if (!isset($_SESSION['userID']) || empty($_SESSION['userID'])) { |
|
58 | + throw new IllegalStateException("userID is not set in session."); |
|
59 | + } |
|
60 | + |
|
61 | + if (!isset($_SESSION['username']) || empty($_SESSION['username'])) { |
|
62 | + throw new IllegalStateException("username is not set in session."); |
|
63 | + } |
|
64 | + |
|
65 | + $this->userID = (int) $_SESSION['userID']; |
|
66 | + $this->username = $_SESSION['username']; |
|
67 | + $this->isLoggedIn = true; |
|
68 | + |
|
69 | + //TODO: update online state in database |
|
70 | + } else { |
|
71 | + $this->setGuest(); |
|
72 | + } |
|
73 | + } else if ($userID == 0) { |
|
74 | + throw new IllegalArgumentException("userID cannot be 0, userID has to be > 0."); |
|
75 | + } else { |
|
76 | + $this->userID = (int) $userID; |
|
77 | + } |
|
78 | + |
|
79 | + Events::throwEvent("before_load_user", array( |
|
80 | + 'userID' => &$this->userID, |
|
81 | + 'isLoggedIn' => &$this->isLoggedIn, |
|
82 | + 'user' => &$this |
|
83 | + )); |
|
84 | + |
|
85 | + //try to load from cache |
|
86 | + if (Cache::contains("user", "user-" . $this->userID)) { |
|
87 | + $this->row = Cache::get("user", "user-" . $this->userID); |
|
88 | + } else { |
|
89 | + $row = false; |
|
90 | + |
|
91 | + //check, if guest user, because guest user doesnt exists in database |
|
92 | + if ($this->userID !== -1) { |
|
93 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
94 | + 'userID' => array( |
|
95 | + 'type' => PDO::PARAM_INT, |
|
96 | + 'value' => $this->userID |
|
97 | + ) |
|
98 | + )); |
|
99 | + } |
|
100 | + |
|
101 | + if (!$row) { |
|
102 | + $logout_user = true; |
|
103 | + |
|
104 | + //user not found, throw an event, so plugins can handle this (optional) |
|
105 | + Events::throwEvent("user_not_found", array( |
|
106 | + 'userID' => &$this->userID, |
|
107 | + 'username' => &$this->username, |
|
108 | + 'isLoggedIn' => &$this->isLoggedIn, |
|
109 | + 'row' => &$row, |
|
110 | + 'logout_user' => &$logout_user, |
|
111 | + 'user' => &$this |
|
112 | + )); |
|
113 | + |
|
114 | + if ($logout_user && $this->userID == -1) { |
|
115 | + //logout user |
|
116 | + $this->logout(); |
|
117 | + } |
|
118 | + } else { |
|
119 | + //remove password hash from row |
|
120 | + unset($row['password']); |
|
121 | + |
|
122 | + Events::throwEvent("before_cache_user", array( |
|
123 | + 'userID' => &$this->userID, |
|
124 | + 'username' => &$this->username, |
|
125 | + 'isLoggedIn' => &$this->isLoggedIn, |
|
126 | + 'row' => &$row, |
|
127 | + 'user' => &$this |
|
128 | + )); |
|
129 | + |
|
130 | + //cache entry |
|
131 | + Cache::put("user", "user-" . $this->userID, $row); |
|
132 | + |
|
133 | + $this->row = $row; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + if ($this->row !== null) { |
|
138 | + $this->userID = (int) $this->row['userID']; |
|
139 | + $this->username = $this->row['username']; |
|
140 | + } |
|
141 | + |
|
142 | + Events::throwEvent("after_load_user", array( |
|
143 | + 'userID' => &$this->userID, |
|
144 | + 'username' => &$this->username, |
|
145 | + 'isLoggedIn' => &$this->isLoggedIn, |
|
146 | + 'row' => &$row, |
|
147 | + 'user' => &$this |
|
148 | + )); |
|
149 | + |
|
150 | + //TODO: update online state and IP |
|
151 | + if ($userID === -1 && $this->isLoggedIn()) { |
|
152 | + $this->setOnline(); |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + public function loginByUsername (string $username, string $password) : array { |
|
157 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
158 | + 'username' => &$username |
|
159 | + )); |
|
160 | + |
|
161 | + if (!$row) { |
|
162 | + //get default authentificator |
|
163 | + $authentificator = self::getDefaultAuthentificator(); |
|
164 | + |
|
165 | + $userID = $authentificator->checkPasswordAndImport($username, $password); |
|
166 | + |
|
167 | + if ($userID == -1) { |
|
168 | + //user not found |
|
169 | + } else { |
|
170 | + //user was imported now, get user row |
|
171 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
172 | + 'userID' => &$userID |
|
173 | + )); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + return $this->loginRow($row, $password); |
|
178 | + } |
|
179 | + |
|
180 | + public function loginByMail (string $mail, string $password) : array { |
|
181 | + //check, if mail is valide |
|
182 | + $validator = new Validator_Mail(); |
|
183 | + |
|
184 | + if (!$validator->isValide($mail)) { |
|
185 | + return array( |
|
186 | + 'success' => false, |
|
187 | + 'error' => "mail_not_valide" |
|
188 | + ); |
|
189 | + } |
|
190 | + |
|
191 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `mail` = :mail AND `activated` = '1'; ", array( |
|
192 | + 'mail' => &$mail |
|
193 | + )); |
|
194 | + |
|
195 | + return $this->loginRow($row, $password); |
|
196 | + } |
|
197 | + |
|
198 | + public function loginByID (int $userID) : array { |
|
199 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
200 | + 'userID' => &$userID |
|
201 | + )); |
|
202 | + |
|
203 | + $res = array(); |
|
204 | + |
|
205 | + if ($row !== false) { |
|
206 | + //set online state |
|
207 | + $this->setOnline(); |
|
208 | + |
|
209 | + //set logged in |
|
210 | + $this->setLoggedIn($row['userID'], $row['username'], $row); |
|
211 | + |
|
212 | + //login successful |
|
213 | + $res['success'] = true; |
|
214 | + $res['error'] = "none"; |
|
215 | + return $res; |
|
216 | + } else { |
|
217 | + //user doesnt exists |
|
218 | + $res['success'] = false; |
|
219 | + $res['error'] = "user_not_exists"; |
|
220 | + return $res; |
|
221 | + } |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * check password of current user |
|
226 | + * |
|
227 | + * @param $password string password |
|
228 | + * |
|
229 | + * @throws IllegalStateException if user wasnt loaded before |
|
230 | + * |
|
231 | + * @return true, if password is correct |
|
232 | + */ |
|
233 | + public function checkPassword (string $password) : bool { |
|
234 | + if ($this->row == null || empty($this->row)) { |
|
235 | + throw new IllegalStateException("user wasnt loaded."); |
|
236 | + } |
|
237 | + |
|
238 | + //because password is not cached, we have to load it directly from database |
|
239 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
240 | + 'userID' => $this->getID() |
|
241 | + )); |
|
242 | + |
|
243 | + //get salt |
|
244 | + $salt = $row['salt']; |
|
245 | + |
|
246 | + //add salt to password |
|
247 | + $password .= $salt; |
|
248 | + |
|
249 | + return password_verify($password, $row['password']); |
|
250 | + } |
|
251 | + |
|
252 | + public function setPassword (string $password) { |
|
253 | + if ($this->row == null || empty($this->row)) { |
|
254 | + throw new IllegalStateException("user wasnt loaded."); |
|
255 | + } |
|
256 | + |
|
257 | + //validate password |
|
258 | + $password = Validator_Password::get($password); |
|
259 | + |
|
260 | + //create new salt |
|
261 | + $salt = md5(PHPUtils::randomString(50)); |
|
262 | + |
|
263 | + //generate password hash |
|
264 | + $hashed_password = self::hashPassword($password, $salt); |
|
265 | + |
|
266 | + //update database |
|
267 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password, `salt` = :salt WHERE `userID` = :userID; ", array( |
|
268 | + 'password' => $hashed_password, |
|
269 | + 'salt' => $salt, |
|
270 | + 'userID' => $this->getID() |
|
271 | + )); |
|
272 | + |
|
273 | + //clear cache |
|
274 | + Cache::clear("user", "user-" . $this->getID()); |
|
275 | + } |
|
276 | + |
|
277 | + protected function loginRow ($row, string $password) : array { |
|
278 | + $res = array(); |
|
279 | + |
|
280 | + if (!$row) { |
|
281 | + //user doesnt exists |
|
282 | + $res['success'] = false; |
|
283 | + $res['error'] = "user_not_exists"; |
|
284 | + |
|
285 | + return $res; |
|
286 | + } |
|
287 | + |
|
288 | + if ($row['userID'] <= 0) { |
|
289 | + //user doesnt exists |
|
290 | + $res['success'] = false; |
|
291 | + $res['error'] = "not_permitted_user"; |
|
292 | + |
|
293 | + return $res; |
|
294 | + } |
|
295 | + |
|
296 | + //get authentificator |
|
297 | + $authentificator = self::getAuthentificatorByID($row['userID']); |
|
298 | + |
|
299 | + $auth_res = $authentificator->checkPasswordAndImport($row['username'], $password); |
|
300 | + |
|
301 | + //check password |
|
302 | + if ($auth_res !== -1) { |
|
303 | + //password is correct |
|
304 | + |
|
305 | + //set online state |
|
306 | + $this->setOnline(); |
|
307 | + |
|
308 | + //set logged in |
|
309 | + $this->setLoggedIn($row['userID'], $row['username'], $row); |
|
310 | + |
|
311 | + //login successful |
|
312 | + $res['success'] = true; |
|
313 | + $res['error'] = "none"; |
|
314 | + return $res; |
|
315 | + } else { |
|
316 | + //wrong password |
|
317 | + |
|
318 | + //user doesnt exists |
|
319 | + $res['success'] = false; |
|
320 | + $res['error'] = "wrong_password"; |
|
321 | + |
|
322 | + return $res; |
|
323 | + } |
|
324 | + } |
|
325 | + |
|
326 | + protected function setLoggedIn (int $userID, string $username, array $row) { |
|
327 | + $_SESSION['logged-in'] = true; |
|
328 | + $_SESSION['userID'] = (int) $userID; |
|
329 | + $_SESSION['username'] = $username; |
|
330 | + |
|
331 | + //remove password hash from row (so password isnt cached) |
|
332 | + unset($row['password']); |
|
333 | + |
|
334 | + $this->userID = $userID; |
|
335 | + $this->username = $username; |
|
336 | + $this->row = $row; |
|
337 | + } |
|
338 | + |
|
339 | + public function logout () { |
|
340 | + //check, if session was started |
|
341 | + PHPUtils::checkSessionStarted(); |
|
342 | + |
|
343 | + unset($_SESSION['userID']); |
|
344 | + unset($_SESSION['username']); |
|
345 | + |
|
346 | + $_SESSION['logged-in'] = false; |
|
347 | + |
|
348 | + $this->setGuest(); |
|
349 | + } |
|
350 | + |
|
351 | + protected function setGuest () { |
|
352 | + $this->userID = (int) Settings::get("guest_userid", "-1"); |
|
353 | + $this->username = Settings::get("guest_username", "Guest"); |
|
354 | + $this->isLoggedIn = false; |
|
355 | + } |
|
356 | + |
|
357 | + protected static function hashPassword ($password, $salt) { |
|
358 | + //http://php.net/manual/de/function.password-hash.php |
|
359 | + |
|
360 | + //add salt to password |
|
361 | + $password .= $salt; |
|
362 | + |
|
363 | + $options = array( |
|
364 | + 'cost' => (int) Settings::get("password_hash_cost", "10") |
|
365 | + ); |
|
366 | + $algo = PASSWORD_DEFAULT; |
|
367 | + |
|
368 | + Events::throwEvent("hashing_password", array( |
|
369 | + 'options' => &$options, |
|
370 | + 'algo' => &$algo |
|
371 | + )); |
|
372 | + |
|
373 | + return password_hash($password, $algo, $options); |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * get user ID of user |
|
378 | + * |
|
379 | + * @return integer userID |
|
380 | + */ |
|
381 | + public function getID () : int { |
|
382 | + return $this->userID; |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * get username of user |
|
387 | + * |
|
388 | + * @return string username |
|
389 | + */ |
|
390 | + public function getUsername () : string { |
|
391 | + return $this->username; |
|
392 | + } |
|
393 | + |
|
394 | + public function getMail () : string { |
|
395 | + return $this->row['mail']; |
|
396 | + } |
|
397 | + |
|
398 | + public function isLoggedIn () : bool { |
|
399 | + return $this->isLoggedIn; |
|
400 | + } |
|
401 | + |
|
402 | + public function getRow () : array { |
|
403 | + return $this->row; |
|
404 | + } |
|
405 | + |
|
406 | + public function hasTitle () : bool { |
|
407 | + return $this->row['specific_title'] !== "none"; |
|
408 | + } |
|
409 | + |
|
410 | + public function getTitle () : string { |
|
411 | + if (!$this->hasTitle()) { |
|
412 | + return Settings::get("user_default_title", "User"); |
|
413 | + } |
|
414 | + |
|
415 | + return $this->row['specific_title']; |
|
416 | + } |
|
417 | + |
|
418 | + public function setOnline (bool $updateIP = true) { |
|
419 | + //get client ip |
|
420 | + $ip = PHPUtils::getClientIP(); |
|
421 | + |
|
422 | + if ($updateIP) { |
|
423 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array( |
|
424 | + 'userid' => array( |
|
425 | + 'type' => PDO::PARAM_INT, |
|
426 | + 'value' => (int) $this->userID |
|
427 | + ), |
|
428 | + 'ip' => $ip |
|
429 | + )); |
|
430 | + } else { |
|
431 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array( |
|
432 | + 'userid' => array( |
|
433 | + 'type' => PDO::PARAM_INT, |
|
434 | + 'value' => (int) $this->userID |
|
435 | + ) |
|
436 | + )); |
|
437 | + } |
|
438 | + } |
|
439 | + |
|
440 | + public function updateOnlineList () { |
|
441 | + $interval_minutes = (int) Settings::get("online_interval", "5"); |
|
442 | + |
|
443 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; "); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * creates user if userID is absent |
|
448 | + * |
|
449 | + * Only use this method for installation & upgrade! |
|
450 | + */ |
|
451 | + public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
452 | + if (self::existsUserID($userID)) { |
|
453 | + //dont create user, if user already exists |
|
454 | + return; |
|
455 | + } |
|
456 | + |
|
457 | + //create salt |
|
458 | + $salt = md5(PHPUtils::randomString(50)); |
|
459 | + |
|
460 | + //generate password hash |
|
461 | + $hashed_password = self::hashPassword($password, $salt); |
|
462 | + |
|
463 | + Database::getInstance()->execute("INSERT INTO `{praefix}user` ( |
|
464 | 464 | `userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `registered`, `activated` |
465 | 465 | ) VALUES ( |
466 | 466 | :userID, :username, :password, :salt, :mail, '0.0.0.0', :main_group, :title, '0', '0000-00-00 00:00:00', CURRENT_TIMESTAMP , :activated |
467 | 467 | )", array( |
468 | - 'userID' => $userID, |
|
469 | - 'username' => $username, |
|
470 | - 'password' => $hashed_password, |
|
471 | - 'salt' => $salt, |
|
472 | - 'mail' => $mail, |
|
473 | - 'main_group' => $main_group, |
|
474 | - 'title' => $specific_title, |
|
475 | - 'activated' => $activated |
|
476 | - )); |
|
477 | - } |
|
478 | - |
|
479 | - public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1, string $authentificator = "LocalAuthentificator", string $owner = "system") { |
|
480 | - if (self::existsUsername($username)) { |
|
481 | - //dont create user, if username already exists |
|
482 | - return false; |
|
483 | - } |
|
484 | - |
|
485 | - if (self::existsMail($mail)) { |
|
486 | - //dont create user, if mail already exists |
|
487 | - return false; |
|
488 | - } |
|
489 | - |
|
490 | - if (empty($specific_title)) { |
|
491 | - $specific_title = "none"; |
|
492 | - } |
|
493 | - |
|
494 | - //create salt |
|
495 | - $salt = md5(PHPUtils::randomString(50)); |
|
496 | - |
|
497 | - //generate password hash |
|
498 | - $hashed_password = self::hashPassword($password, $salt); |
|
499 | - |
|
500 | - //create user in database |
|
501 | - Database::getInstance()->execute("INSERT INTO `{praefix}user` ( |
|
468 | + 'userID' => $userID, |
|
469 | + 'username' => $username, |
|
470 | + 'password' => $hashed_password, |
|
471 | + 'salt' => $salt, |
|
472 | + 'mail' => $mail, |
|
473 | + 'main_group' => $main_group, |
|
474 | + 'title' => $specific_title, |
|
475 | + 'activated' => $activated |
|
476 | + )); |
|
477 | + } |
|
478 | + |
|
479 | + public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1, string $authentificator = "LocalAuthentificator", string $owner = "system") { |
|
480 | + if (self::existsUsername($username)) { |
|
481 | + //dont create user, if username already exists |
|
482 | + return false; |
|
483 | + } |
|
484 | + |
|
485 | + if (self::existsMail($mail)) { |
|
486 | + //dont create user, if mail already exists |
|
487 | + return false; |
|
488 | + } |
|
489 | + |
|
490 | + if (empty($specific_title)) { |
|
491 | + $specific_title = "none"; |
|
492 | + } |
|
493 | + |
|
494 | + //create salt |
|
495 | + $salt = md5(PHPUtils::randomString(50)); |
|
496 | + |
|
497 | + //generate password hash |
|
498 | + $hashed_password = self::hashPassword($password, $salt); |
|
499 | + |
|
500 | + //create user in database |
|
501 | + Database::getInstance()->execute("INSERT INTO `{praefix}user` ( |
|
502 | 502 | `userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `authentificator`, `owner`, `registered`, `activated` |
503 | 503 | ) VALUES ( |
504 | 504 | NULL, :username, :password, :salt, :mail, :ip, :main_group, :title, '0', '0000-00-00 00:00:00', :authentificator, :owner, CURRENT_TIMESTAMP , :activated |
505 | 505 | )", array( |
506 | - 'username' => $username, |
|
507 | - 'password' => $hashed_password, |
|
508 | - 'salt' => $salt, |
|
509 | - 'mail' => $mail, |
|
510 | - 'ip' => $ip, |
|
511 | - 'main_group' => $main_group, |
|
512 | - 'title' => $specific_title, |
|
513 | - 'authentificator' => $authentificator, |
|
514 | - 'owner' => $owner, |
|
515 | - 'activated' => $activated |
|
516 | - )); |
|
517 | - |
|
518 | - //get userID |
|
519 | - $userID = self::getIDByUsernameFromDB($username); |
|
520 | - |
|
521 | - if ($userID == Settings::get("guest_userid", -1)) { |
|
522 | - //something went wrong |
|
523 | - return false; |
|
524 | - } |
|
525 | - |
|
526 | - //add user to group "registered users" |
|
527 | - Groups::addGroupToUser(2, $userID, false); |
|
528 | - |
|
529 | - Events::throwEvent("add_user", array( |
|
530 | - 'userID' => $userID, |
|
531 | - 'username' => &$username, |
|
532 | - 'mail' => $mail, |
|
533 | - 'main_group' => $main_group |
|
534 | - )); |
|
535 | - |
|
536 | - return array( |
|
537 | - 'success' => true, |
|
538 | - 'userID' => $userID, |
|
539 | - 'username' => $username, |
|
540 | - 'mail' => $mail |
|
541 | - ); |
|
542 | - } |
|
543 | - |
|
544 | - public static function deleteUserID (int $userID) { |
|
545 | - Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
546 | - 'userID' => array( |
|
547 | - 'type' => PDO::PARAM_INT, |
|
548 | - 'value' => $userID |
|
549 | - ) |
|
550 | - )); |
|
551 | - |
|
552 | - //remove user from cache |
|
553 | - Cache::clear("user", "user-" . $userID); |
|
554 | - } |
|
555 | - |
|
556 | - public static function existsUserID (int $userID) : bool { |
|
557 | - //search for userID in database |
|
558 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
559 | - 'userID' => array( |
|
560 | - 'type' => PDO::PARAM_INT, |
|
561 | - 'value' => $userID |
|
562 | - ) |
|
563 | - )); |
|
564 | - |
|
565 | - return $row !== false; |
|
566 | - } |
|
567 | - |
|
568 | - public static function existsUsername (string $username) : bool { |
|
569 | - //search for username in database, ignore case |
|
570 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
571 | - |
|
572 | - return $row !== false; |
|
573 | - } |
|
574 | - |
|
575 | - public static function existsMail (string $mail) : bool { |
|
576 | - //search for mail in database, ignore case |
|
577 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail)); |
|
578 | - |
|
579 | - return $row !== false; |
|
580 | - } |
|
581 | - |
|
582 | - public static function getIDByUsernameFromDB (string $username) : int { |
|
583 | - //use in-memory cache to avoid a large amount of sql queries on setup scripts |
|
584 | - if (isset(self::$getIDByUsernameFromDB_cache[$username])) { |
|
585 | - return self::$getIDByUsernameFromDB_cache[$username]; |
|
586 | - } |
|
587 | - |
|
588 | - //search for username in database, ignore case |
|
589 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
590 | - |
|
591 | - if ($row === false) { |
|
592 | - //return guest userID |
|
593 | - return Settings::get("guest_userid", -1); |
|
594 | - } |
|
595 | - |
|
596 | - $userID = $row['userID']; |
|
597 | - self::$getIDByUsernameFromDB_cache[$username] = $userID; |
|
598 | - |
|
599 | - return $userID; |
|
600 | - } |
|
601 | - |
|
602 | - public static function &getAuthentificatorByID (int $userID = -1) { |
|
603 | - if ($userID == -1) { |
|
604 | - //get default authentificator |
|
605 | - return self::getDefaultAuthentificator(); |
|
606 | - } else { |
|
607 | - //get authentificator class |
|
608 | - |
|
609 | - //check, if user exists |
|
610 | - if (!self::existsUserID($userID)) { |
|
611 | - throw new IllegalStateException("user with userID '" . $userID . "' doesnt exists."); |
|
612 | - } |
|
613 | - |
|
614 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
615 | - 'userID' => &$userID |
|
616 | - )); |
|
617 | - |
|
618 | - $class_name = $row['authentificator']; |
|
619 | - $obj = new $class_name(); |
|
620 | - |
|
621 | - return $obj; |
|
622 | - } |
|
623 | - } |
|
624 | - |
|
625 | - public static function &getAuthentificatorByUsername (string $username = "") { |
|
626 | - if ($username == null || empty($username)) { |
|
627 | - //get default authentificator |
|
628 | - return self::getDefaultAuthentificator(); |
|
629 | - } else { |
|
630 | - //get authentificator class |
|
631 | - |
|
632 | - //check, if user exists |
|
633 | - if (!self::existsUsername($username)) { |
|
634 | - throw new IllegalStateException("user with username '" . $username . "' doesnt exists."); |
|
635 | - } |
|
636 | - |
|
637 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
638 | - 'username' => &$username |
|
639 | - )); |
|
640 | - |
|
641 | - $class_name = $row['authentificator']; |
|
642 | - $obj = new $class_name(); |
|
643 | - |
|
644 | - return $obj; |
|
645 | - } |
|
646 | - } |
|
647 | - |
|
648 | - public static function &getDefaultAuthentificator () : IAuthentificator { |
|
649 | - if (self::$default_authentificator == null) { |
|
650 | - $class_name = Settings::get("default_authentificator", "LocalAuthentificator"); |
|
651 | - $obj = new $class_name(); |
|
652 | - |
|
653 | - self::$default_authentificator = $obj; |
|
654 | - } |
|
655 | - |
|
656 | - return self::$default_authentificator; |
|
657 | - } |
|
658 | - |
|
659 | - /** |
|
660 | - * get instance of current (logged in / guest) user |
|
661 | - */ |
|
662 | - public static function ¤t () : User { |
|
663 | - if (self::$instance == null) { |
|
664 | - self::$instance = new User(); |
|
665 | - self::$instance->load(); |
|
666 | - } |
|
667 | - |
|
668 | - return self::$instance; |
|
669 | - } |
|
506 | + 'username' => $username, |
|
507 | + 'password' => $hashed_password, |
|
508 | + 'salt' => $salt, |
|
509 | + 'mail' => $mail, |
|
510 | + 'ip' => $ip, |
|
511 | + 'main_group' => $main_group, |
|
512 | + 'title' => $specific_title, |
|
513 | + 'authentificator' => $authentificator, |
|
514 | + 'owner' => $owner, |
|
515 | + 'activated' => $activated |
|
516 | + )); |
|
517 | + |
|
518 | + //get userID |
|
519 | + $userID = self::getIDByUsernameFromDB($username); |
|
520 | + |
|
521 | + if ($userID == Settings::get("guest_userid", -1)) { |
|
522 | + //something went wrong |
|
523 | + return false; |
|
524 | + } |
|
525 | + |
|
526 | + //add user to group "registered users" |
|
527 | + Groups::addGroupToUser(2, $userID, false); |
|
528 | + |
|
529 | + Events::throwEvent("add_user", array( |
|
530 | + 'userID' => $userID, |
|
531 | + 'username' => &$username, |
|
532 | + 'mail' => $mail, |
|
533 | + 'main_group' => $main_group |
|
534 | + )); |
|
535 | + |
|
536 | + return array( |
|
537 | + 'success' => true, |
|
538 | + 'userID' => $userID, |
|
539 | + 'username' => $username, |
|
540 | + 'mail' => $mail |
|
541 | + ); |
|
542 | + } |
|
543 | + |
|
544 | + public static function deleteUserID (int $userID) { |
|
545 | + Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
546 | + 'userID' => array( |
|
547 | + 'type' => PDO::PARAM_INT, |
|
548 | + 'value' => $userID |
|
549 | + ) |
|
550 | + )); |
|
551 | + |
|
552 | + //remove user from cache |
|
553 | + Cache::clear("user", "user-" . $userID); |
|
554 | + } |
|
555 | + |
|
556 | + public static function existsUserID (int $userID) : bool { |
|
557 | + //search for userID in database |
|
558 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
559 | + 'userID' => array( |
|
560 | + 'type' => PDO::PARAM_INT, |
|
561 | + 'value' => $userID |
|
562 | + ) |
|
563 | + )); |
|
564 | + |
|
565 | + return $row !== false; |
|
566 | + } |
|
567 | + |
|
568 | + public static function existsUsername (string $username) : bool { |
|
569 | + //search for username in database, ignore case |
|
570 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
571 | + |
|
572 | + return $row !== false; |
|
573 | + } |
|
574 | + |
|
575 | + public static function existsMail (string $mail) : bool { |
|
576 | + //search for mail in database, ignore case |
|
577 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail)); |
|
578 | + |
|
579 | + return $row !== false; |
|
580 | + } |
|
581 | + |
|
582 | + public static function getIDByUsernameFromDB (string $username) : int { |
|
583 | + //use in-memory cache to avoid a large amount of sql queries on setup scripts |
|
584 | + if (isset(self::$getIDByUsernameFromDB_cache[$username])) { |
|
585 | + return self::$getIDByUsernameFromDB_cache[$username]; |
|
586 | + } |
|
587 | + |
|
588 | + //search for username in database, ignore case |
|
589 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
590 | + |
|
591 | + if ($row === false) { |
|
592 | + //return guest userID |
|
593 | + return Settings::get("guest_userid", -1); |
|
594 | + } |
|
595 | + |
|
596 | + $userID = $row['userID']; |
|
597 | + self::$getIDByUsernameFromDB_cache[$username] = $userID; |
|
598 | + |
|
599 | + return $userID; |
|
600 | + } |
|
601 | + |
|
602 | + public static function &getAuthentificatorByID (int $userID = -1) { |
|
603 | + if ($userID == -1) { |
|
604 | + //get default authentificator |
|
605 | + return self::getDefaultAuthentificator(); |
|
606 | + } else { |
|
607 | + //get authentificator class |
|
608 | + |
|
609 | + //check, if user exists |
|
610 | + if (!self::existsUserID($userID)) { |
|
611 | + throw new IllegalStateException("user with userID '" . $userID . "' doesnt exists."); |
|
612 | + } |
|
613 | + |
|
614 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
615 | + 'userID' => &$userID |
|
616 | + )); |
|
617 | + |
|
618 | + $class_name = $row['authentificator']; |
|
619 | + $obj = new $class_name(); |
|
620 | + |
|
621 | + return $obj; |
|
622 | + } |
|
623 | + } |
|
624 | + |
|
625 | + public static function &getAuthentificatorByUsername (string $username = "") { |
|
626 | + if ($username == null || empty($username)) { |
|
627 | + //get default authentificator |
|
628 | + return self::getDefaultAuthentificator(); |
|
629 | + } else { |
|
630 | + //get authentificator class |
|
631 | + |
|
632 | + //check, if user exists |
|
633 | + if (!self::existsUsername($username)) { |
|
634 | + throw new IllegalStateException("user with username '" . $username . "' doesnt exists."); |
|
635 | + } |
|
636 | + |
|
637 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
638 | + 'username' => &$username |
|
639 | + )); |
|
640 | + |
|
641 | + $class_name = $row['authentificator']; |
|
642 | + $obj = new $class_name(); |
|
643 | + |
|
644 | + return $obj; |
|
645 | + } |
|
646 | + } |
|
647 | + |
|
648 | + public static function &getDefaultAuthentificator () : IAuthentificator { |
|
649 | + if (self::$default_authentificator == null) { |
|
650 | + $class_name = Settings::get("default_authentificator", "LocalAuthentificator"); |
|
651 | + $obj = new $class_name(); |
|
652 | + |
|
653 | + self::$default_authentificator = $obj; |
|
654 | + } |
|
655 | + |
|
656 | + return self::$default_authentificator; |
|
657 | + } |
|
658 | + |
|
659 | + /** |
|
660 | + * get instance of current (logged in / guest) user |
|
661 | + */ |
|
662 | + public static function ¤t () : User { |
|
663 | + if (self::$instance == null) { |
|
664 | + self::$instance = new User(); |
|
665 | + self::$instance->load(); |
|
666 | + } |
|
667 | + |
|
668 | + return self::$instance; |
|
669 | + } |
|
670 | 670 | |
671 | 671 | } |
672 | 672 |
@@ -7,41 +7,41 @@ |
||
7 | 7 | |
8 | 8 | $package_list = array( |
9 | 9 | 'com.jukusoft.cms.db.upgrader' => true, |
10 | - 'com.jukusoft.cms.domain' => true, |
|
11 | - 'com.jukusoft.cms.settings' => true, |
|
12 | - 'com.jukusoft.cms.apimethods' => true, |
|
13 | - 'com.jukusoft.cms.xtpl' => true, |
|
14 | - 'com.jukusoft.cms.robots' => true, |
|
15 | - 'com.jukusoft.cms.style' => true, |
|
16 | - 'com.jukusoft.cms.browser' => true, |
|
17 | - 'com.jukusoft.cms.lang' => true, |
|
18 | - 'com.jukusoft.cms.folder' => true, |
|
19 | - 'com.jukusoft.cms.style' => true, |
|
20 | - 'com.jukusoft.cms.test' => true, |
|
21 | - 'com.jukusoft.cms.page' => true, |
|
22 | - 'com.jukusoft.cms.menu' => true, |
|
23 | - 'com.jukusoft.cms.htmlpage' => true, |
|
24 | - 'com.jukusoft.cms.user' => true, |
|
25 | - 'com.jukusoft.cms.groups' => true, |
|
26 | - 'com.jukusoft.cms.dashboard' => true, |
|
27 | - 'com.jukusoft.cms.cssbuilder' => true, |
|
28 | - 'com.jukusoft.cms.jsbuilder' => true, |
|
29 | - 'com.jukusoft.cms.version' => true, |
|
30 | - 'com.jukusoft.cms.permissions' => true, |
|
31 | - 'com.jukusoft.cms.dwoo' => true, |
|
32 | - 'com.jukusoft.cms.sitemap' => true, |
|
33 | - 'com.jukusoft.cms.tools' => true, |
|
34 | - 'com.jukusoft.cms.captcha' => true, |
|
35 | - 'com.jukusoft.cms.mail' => true, |
|
36 | - 'com.jukusoft.cms.plugin' => true, |
|
37 | - 'com.jukusoft.cms.tasks' => true, |
|
38 | - 'com.jukusoft.cms.preferences' => true, |
|
39 | - 'com.jukusoft.cms.ldap' => true, |
|
40 | - 'com.jukusoft.cms.api' => true, |
|
41 | - 'com.jukusoft.cms.logging' => true, |
|
42 | - 'com.jukusoft.cms.widgets' => true, |
|
43 | - 'com.jukusoft.cms.sidebar' => true, |
|
44 | - 'com.jukusoft.cms.admin' => true, |
|
10 | + 'com.jukusoft.cms.domain' => true, |
|
11 | + 'com.jukusoft.cms.settings' => true, |
|
12 | + 'com.jukusoft.cms.apimethods' => true, |
|
13 | + 'com.jukusoft.cms.xtpl' => true, |
|
14 | + 'com.jukusoft.cms.robots' => true, |
|
15 | + 'com.jukusoft.cms.style' => true, |
|
16 | + 'com.jukusoft.cms.browser' => true, |
|
17 | + 'com.jukusoft.cms.lang' => true, |
|
18 | + 'com.jukusoft.cms.folder' => true, |
|
19 | + 'com.jukusoft.cms.style' => true, |
|
20 | + 'com.jukusoft.cms.test' => true, |
|
21 | + 'com.jukusoft.cms.page' => true, |
|
22 | + 'com.jukusoft.cms.menu' => true, |
|
23 | + 'com.jukusoft.cms.htmlpage' => true, |
|
24 | + 'com.jukusoft.cms.user' => true, |
|
25 | + 'com.jukusoft.cms.groups' => true, |
|
26 | + 'com.jukusoft.cms.dashboard' => true, |
|
27 | + 'com.jukusoft.cms.cssbuilder' => true, |
|
28 | + 'com.jukusoft.cms.jsbuilder' => true, |
|
29 | + 'com.jukusoft.cms.version' => true, |
|
30 | + 'com.jukusoft.cms.permissions' => true, |
|
31 | + 'com.jukusoft.cms.dwoo' => true, |
|
32 | + 'com.jukusoft.cms.sitemap' => true, |
|
33 | + 'com.jukusoft.cms.tools' => true, |
|
34 | + 'com.jukusoft.cms.captcha' => true, |
|
35 | + 'com.jukusoft.cms.mail' => true, |
|
36 | + 'com.jukusoft.cms.plugin' => true, |
|
37 | + 'com.jukusoft.cms.tasks' => true, |
|
38 | + 'com.jukusoft.cms.preferences' => true, |
|
39 | + 'com.jukusoft.cms.ldap' => true, |
|
40 | + 'com.jukusoft.cms.api' => true, |
|
41 | + 'com.jukusoft.cms.logging' => true, |
|
42 | + 'com.jukusoft.cms.widgets' => true, |
|
43 | + 'com.jukusoft.cms.sidebar' => true, |
|
44 | + 'com.jukusoft.cms.admin' => true, |
|
45 | 45 | ); |
46 | 46 | |
47 | 47 | ?> |
@@ -33,27 +33,27 @@ discard block |
||
33 | 33 | // |
34 | 34 | } |
35 | 35 | |
36 | - public function load ($row) { |
|
36 | + public function load($row) { |
|
37 | 37 | $this->row = $row; |
38 | 38 | } |
39 | 39 | |
40 | - public function getCategory () : string { |
|
40 | + public function getCategory() : string { |
|
41 | 41 | return $this->row['category']; |
42 | 42 | } |
43 | 43 | |
44 | - public function getTitle () : string { |
|
44 | + public function getTitle() : string { |
|
45 | 45 | return $this->row['title']; |
46 | 46 | } |
47 | 47 | |
48 | - public function getOwner () : string { |
|
48 | + public function getOwner() : string { |
|
49 | 49 | return $this->row['owner']; |
50 | 50 | } |
51 | 51 | |
52 | - public function getOrder () : int { |
|
52 | + public function getOrder() : int { |
|
53 | 53 | return $this->row['order']; |
54 | 54 | } |
55 | 55 | |
56 | - public static function createIfAbsent (string $category, string $title, int $order = 10, string $owner = "system") { |
|
56 | + public static function createIfAbsent(string $category, string $title, int $order = 10, string $owner = "system") { |
|
57 | 57 | Database::getInstance()->execute("INSERT INTO `{praefix}global_settings_category` ( |
58 | 58 | `category`, `title`, `owner`, `order` |
59 | 59 | ) VALUES ( |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | Cache::clear("setting_categories"); |
70 | 70 | } |
71 | 71 | |
72 | - public static function remove (string $category) { |
|
72 | + public static function remove(string $category) { |
|
73 | 73 | Database::getInstance()->execute("DELETE FROM `{praefix}global_settings_category` WHERE `category` = :category; ", array( |
74 | 74 | 'category' => $category |
75 | 75 | )); |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | Cache::clear("setting_categories"); |
79 | 79 | } |
80 | 80 | |
81 | - public static function removeByOwner (string $owner) { |
|
81 | + public static function removeByOwner(string $owner) { |
|
82 | 82 | Database::getInstance()->execute("DELETE FROM `{praefix}global_settings_category` WHERE `owner` = :owner; ", array( |
83 | 83 | 'owner' => $owner |
84 | 84 | )); |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | Cache::clear("setting_categories"); |
88 | 88 | } |
89 | 89 | |
90 | - public static function listAllCategories () : array { |
|
90 | + public static function listAllCategories() : array { |
|
91 | 91 | $rows = array(); |
92 | 92 | $list = array(); |
93 | 93 | |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | return $list; |
114 | 114 | } |
115 | 115 | |
116 | - public static function cast (SettingsCategory $category) : SettingsCategory { |
|
116 | + public static function cast(SettingsCategory $category) : SettingsCategory { |
|
117 | 117 | return $category; |
118 | 118 | } |
119 | 119 |
@@ -27,95 +27,95 @@ |
||
27 | 27 | |
28 | 28 | class SettingsCategory { |
29 | 29 | |
30 | - protected $row = array(); |
|
30 | + protected $row = array(); |
|
31 | 31 | |
32 | - public function __construct() { |
|
33 | - // |
|
34 | - } |
|
32 | + public function __construct() { |
|
33 | + // |
|
34 | + } |
|
35 | 35 | |
36 | - public function load ($row) { |
|
37 | - $this->row = $row; |
|
38 | - } |
|
36 | + public function load ($row) { |
|
37 | + $this->row = $row; |
|
38 | + } |
|
39 | 39 | |
40 | - public function getCategory () : string { |
|
41 | - return $this->row['category']; |
|
42 | - } |
|
40 | + public function getCategory () : string { |
|
41 | + return $this->row['category']; |
|
42 | + } |
|
43 | 43 | |
44 | - public function getTitle () : string { |
|
45 | - return $this->row['title']; |
|
46 | - } |
|
44 | + public function getTitle () : string { |
|
45 | + return $this->row['title']; |
|
46 | + } |
|
47 | 47 | |
48 | - public function getOwner () : string { |
|
49 | - return $this->row['owner']; |
|
50 | - } |
|
48 | + public function getOwner () : string { |
|
49 | + return $this->row['owner']; |
|
50 | + } |
|
51 | 51 | |
52 | - public function getOrder () : int { |
|
53 | - return $this->row['order']; |
|
54 | - } |
|
52 | + public function getOrder () : int { |
|
53 | + return $this->row['order']; |
|
54 | + } |
|
55 | 55 | |
56 | - public static function createIfAbsent (string $category, string $title, int $order = 10, string $owner = "system") { |
|
57 | - Database::getInstance()->execute("INSERT INTO `{praefix}global_settings_category` ( |
|
56 | + public static function createIfAbsent (string $category, string $title, int $order = 10, string $owner = "system") { |
|
57 | + Database::getInstance()->execute("INSERT INTO `{praefix}global_settings_category` ( |
|
58 | 58 | `category`, `title`, `owner`, `order` |
59 | 59 | ) VALUES ( |
60 | 60 | :category, :title, :owner, :order |
61 | 61 | ) ON DUPLICATE KEY UPDATE `title` = :title, `owner` = :owner, `order` = :order; ", array( |
62 | - 'category' => $category, |
|
63 | - 'title' => $title, |
|
64 | - 'owner' => $owner, |
|
65 | - 'order' => $order |
|
66 | - )); |
|
67 | - |
|
68 | - //clear cache |
|
69 | - Cache::clear("setting_categories"); |
|
70 | - } |
|
71 | - |
|
72 | - public static function remove (string $category) { |
|
73 | - Database::getInstance()->execute("DELETE FROM `{praefix}global_settings_category` WHERE `category` = :category; ", array( |
|
74 | - 'category' => $category |
|
75 | - )); |
|
76 | - |
|
77 | - //clear cache |
|
78 | - Cache::clear("setting_categories"); |
|
79 | - } |
|
80 | - |
|
81 | - public static function removeByOwner (string $owner) { |
|
82 | - Database::getInstance()->execute("DELETE FROM `{praefix}global_settings_category` WHERE `owner` = :owner; ", array( |
|
83 | - 'owner' => $owner |
|
84 | - )); |
|
85 | - |
|
86 | - //clear cache |
|
87 | - Cache::clear("setting_categories"); |
|
88 | - } |
|
89 | - |
|
90 | - public static function listAllCategories () : array { |
|
91 | - $rows = array(); |
|
92 | - $list = array(); |
|
93 | - |
|
94 | - if (Cache::contains("setting_categories", "list")) { |
|
95 | - $rows = Cache::get("setting_categories", "list"); |
|
96 | - } else { |
|
97 | - //get categories from database |
|
98 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings_category` ORDER BY `order`; "); |
|
99 | - |
|
100 | - //store values in cache |
|
101 | - Cache::put("setting_categories", "list", $rows); |
|
102 | - } |
|
103 | - |
|
104 | - foreach ($rows as $row) { |
|
105 | - //create new instance of category and load row |
|
106 | - $category = new SettingsCategory(); |
|
107 | - $category->load($row); |
|
108 | - |
|
109 | - //add category to list |
|
110 | - $list[] = $category; |
|
111 | - } |
|
112 | - |
|
113 | - return $list; |
|
114 | - } |
|
115 | - |
|
116 | - public static function cast (SettingsCategory $category) : SettingsCategory { |
|
117 | - return $category; |
|
118 | - } |
|
62 | + 'category' => $category, |
|
63 | + 'title' => $title, |
|
64 | + 'owner' => $owner, |
|
65 | + 'order' => $order |
|
66 | + )); |
|
67 | + |
|
68 | + //clear cache |
|
69 | + Cache::clear("setting_categories"); |
|
70 | + } |
|
71 | + |
|
72 | + public static function remove (string $category) { |
|
73 | + Database::getInstance()->execute("DELETE FROM `{praefix}global_settings_category` WHERE `category` = :category; ", array( |
|
74 | + 'category' => $category |
|
75 | + )); |
|
76 | + |
|
77 | + //clear cache |
|
78 | + Cache::clear("setting_categories"); |
|
79 | + } |
|
80 | + |
|
81 | + public static function removeByOwner (string $owner) { |
|
82 | + Database::getInstance()->execute("DELETE FROM `{praefix}global_settings_category` WHERE `owner` = :owner; ", array( |
|
83 | + 'owner' => $owner |
|
84 | + )); |
|
85 | + |
|
86 | + //clear cache |
|
87 | + Cache::clear("setting_categories"); |
|
88 | + } |
|
89 | + |
|
90 | + public static function listAllCategories () : array { |
|
91 | + $rows = array(); |
|
92 | + $list = array(); |
|
93 | + |
|
94 | + if (Cache::contains("setting_categories", "list")) { |
|
95 | + $rows = Cache::get("setting_categories", "list"); |
|
96 | + } else { |
|
97 | + //get categories from database |
|
98 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings_category` ORDER BY `order`; "); |
|
99 | + |
|
100 | + //store values in cache |
|
101 | + Cache::put("setting_categories", "list", $rows); |
|
102 | + } |
|
103 | + |
|
104 | + foreach ($rows as $row) { |
|
105 | + //create new instance of category and load row |
|
106 | + $category = new SettingsCategory(); |
|
107 | + $category->load($row); |
|
108 | + |
|
109 | + //add category to list |
|
110 | + $list[] = $category; |
|
111 | + } |
|
112 | + |
|
113 | + return $list; |
|
114 | + } |
|
115 | + |
|
116 | + public static function cast (SettingsCategory $category) : SettingsCategory { |
|
117 | + return $category; |
|
118 | + } |
|
119 | 119 | |
120 | 120 | } |
121 | 121 |
@@ -27,348 +27,348 @@ |
||
27 | 27 | |
28 | 28 | class Menu { |
29 | 29 | |
30 | - //menu ID (table menu_names) |
|
31 | - protected $menuID = -1; |
|
32 | - |
|
33 | - //name of template |
|
34 | - protected $template = ""; |
|
35 | - |
|
36 | - //menu structure |
|
37 | - protected static $menuID_array = array(); |
|
38 | - |
|
39 | - protected $menus = array(); |
|
40 | - |
|
41 | - public function __construct (int $menuID = -1, string $template = "menu") { |
|
42 | - $this->menuID = (int) $menuID; |
|
43 | - $this->template = $template; |
|
44 | - } |
|
45 | - |
|
46 | - public function loadMenu (int $menuID = -1, Folder $folder) { |
|
47 | - if ($menuID == -1) { |
|
48 | - $menuID = $this->menuID; |
|
49 | - } |
|
50 | - |
|
51 | - if ($menuID == -1) { |
|
52 | - //we dont need to load anything, because no menu is selected |
|
53 | - $this->menus = array(); |
|
54 | - } |
|
55 | - |
|
56 | - Events::throwEvent("before_load_menu", array( |
|
57 | - 'menuID' => &$menuID, |
|
58 | - 'instance' => &$this |
|
59 | - )); |
|
60 | - |
|
61 | - //load menuID if absent |
|
62 | - self::loadMenuID($menuID); |
|
63 | - |
|
64 | - if (Cache::contains("menus", "menu_" . $menuID . "_" . User::current()->getID())) { |
|
65 | - $this->menus = Cache::get("menus", "menu_" . $menuID . "_" . User::current()->getID()); |
|
66 | - } else { |
|
67 | - $menu_cache = self::$menuID_array[$menuID]; |
|
68 | - |
|
69 | - //get menu by parent -y, this means root menu |
|
70 | - $this->menus = $this->getMenuByParent($menu_cache, -1, $folder); |
|
71 | - |
|
72 | - Events::throwEvent("after_load_menu", array( |
|
73 | - 'menuID' => &$menuID, |
|
74 | - 'instance' => &$this, |
|
75 | - 'menus' => &$this->menus, |
|
76 | - 'menu_cache' => $menu_cache |
|
77 | - )); |
|
78 | - |
|
79 | - Cache::put("menus", "menu_" . $menuID . "_" . User::current()->getID(), $this->menus); |
|
80 | - } |
|
81 | - |
|
82 | - $this->menuID = $menuID; |
|
83 | - } |
|
84 | - |
|
85 | - protected function getMenuByParent (array &$menu_array, int $parentID, Folder $folder) : array { |
|
86 | - if (!isset($menu_array[$parentID])) { |
|
87 | - //menu doesnt have submenus |
|
88 | - return array(); |
|
89 | - } |
|
90 | - |
|
91 | - $array = array(); |
|
92 | - |
|
93 | - foreach ($menu_array[$parentID] as $row) { |
|
94 | - //check login_required |
|
95 | - if ($row['login_required'] == 1 && !User::current()->isLoggedIn()) { |
|
96 | - //dont show this menu |
|
97 | - continue; |
|
98 | - } |
|
99 | - |
|
100 | - //check permissions |
|
101 | - $permissions = explode("|", $row['permissions']); |
|
102 | - $has_permission = false; |
|
103 | - |
|
104 | - foreach ($permissions as $permission) { |
|
105 | - if (PermissionChecker::current()->hasRight($permission)) { |
|
106 | - $has_permission = true; |
|
107 | - break; |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - if (!$has_permission) { |
|
112 | - //dont show this menu, because user doesnt have permission for this menu |
|
113 | - continue; |
|
114 | - } |
|
115 | - |
|
116 | - $entry = array(); |
|
117 | - |
|
118 | - //translate title |
|
119 | - if ($folder->isTitleTranslationEnabled()) { |
|
120 | - $row['title'] = Translator::translateTitle($row['title']); |
|
121 | - } |
|
122 | - |
|
123 | - $href = ""; |
|
124 | - $entry['append'] = ""; |
|
125 | - $entry['title'] = $row['title']; |
|
126 | - $entry['text'] = $row['title']; |
|
127 | - $entry['icon'] = $row['icon']; |
|
128 | - $entry['icon_class'] = " " . $row['icon']; |
|
129 | - $entry['permissions'] = explode("|", $row['permissions']); |
|
130 | - $entry['append'] = ""; |
|
131 | - $entry['extension_code'] = ""; |
|
132 | - |
|
133 | - if (strpos($row['url'], "LOGIN_URL") !== FALSE) { |
|
134 | - $row['url'] = Registry::singleton()->getSetting("login_url"); |
|
135 | - } |
|
136 | - |
|
137 | - if (strpos($row['url'], "LOGOUT_URL") !== FALSE) { |
|
138 | - $row['url'] = Registry::singleton()->getSetting("logout_url"); |
|
139 | - } |
|
140 | - |
|
141 | - if ($row['type'] == "page") { |
|
142 | - $href = DomainUtils::generateURL($row['url']); |
|
143 | - } else if ($row['type'] == "link") { |
|
144 | - //TODO: add base url |
|
145 | - $href = $row['url']; |
|
146 | - } else if ($row['type'] == "external_link") { |
|
147 | - $href = $row['url']; |
|
148 | - } else if ($row['type'] == "js_link") { |
|
149 | - $href = "#"; |
|
150 | - $entry['append'] = " onclick=\"" . $row['url'] . "\""; |
|
151 | - } else if ($row['type'] == "no_link") { |
|
152 | - $href = "#"; |
|
153 | - } else if ($row['type'] == "dynamic_link") { |
|
154 | - $href = "#"; |
|
155 | - |
|
156 | - if (PHPUtils::startsWith($row['url'], "settings:")) { |
|
157 | - $array1 = explode(":", $row['url']); |
|
158 | - $href = Settings::get($array1[1], "#"); |
|
159 | - } else if (PHPUtils::startsWith($row['url'], "registry:")) { |
|
160 | - $array1 = explode(":", $row['url']); |
|
161 | - $href = Registry::singleton()->getSetting($array1[1], "#"); |
|
162 | - } |
|
163 | - } else { |
|
164 | - throw new IllegalStateException("Unknown menu type: " . $row['type']); |
|
165 | - } |
|
166 | - |
|
167 | - $entry['href'] = $href; |
|
168 | - |
|
169 | - if (!empty($row['icon']) && $row['icon'] != "none") { |
|
170 | - $entry['text'] = "<img src=\"" . $row['icon'] . "\" alt=\"" . $row['title'] . "\" title=\"" . $row['title'] . "\" style=\"max-width:32px; max-height:32px; \" /> " . $row['title']; |
|
171 | - } |
|
172 | - |
|
173 | - //get submenus |
|
174 | - $entry['submenus'] = $this->getMenuByParent($menu_array, $row['id'], $folder); |
|
175 | - $entry['has_submenus'] = sizeof($entry['submenus']) > 0; |
|
176 | - |
|
177 | - $array[] = $entry; |
|
178 | - } |
|
179 | - |
|
180 | - return $array; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * get HTML code of menu |
|
185 | - */ |
|
186 | - public function getCode () : string { |
|
187 | - $template = new DwooTemplate($this->template); |
|
188 | - |
|
189 | - $template->assign("menu_array", $this->menus); |
|
190 | - |
|
191 | - /*$template = new Template($this->template); |
|
30 | + //menu ID (table menu_names) |
|
31 | + protected $menuID = -1; |
|
32 | + |
|
33 | + //name of template |
|
34 | + protected $template = ""; |
|
35 | + |
|
36 | + //menu structure |
|
37 | + protected static $menuID_array = array(); |
|
38 | + |
|
39 | + protected $menus = array(); |
|
40 | + |
|
41 | + public function __construct (int $menuID = -1, string $template = "menu") { |
|
42 | + $this->menuID = (int) $menuID; |
|
43 | + $this->template = $template; |
|
44 | + } |
|
45 | + |
|
46 | + public function loadMenu (int $menuID = -1, Folder $folder) { |
|
47 | + if ($menuID == -1) { |
|
48 | + $menuID = $this->menuID; |
|
49 | + } |
|
50 | + |
|
51 | + if ($menuID == -1) { |
|
52 | + //we dont need to load anything, because no menu is selected |
|
53 | + $this->menus = array(); |
|
54 | + } |
|
55 | + |
|
56 | + Events::throwEvent("before_load_menu", array( |
|
57 | + 'menuID' => &$menuID, |
|
58 | + 'instance' => &$this |
|
59 | + )); |
|
60 | + |
|
61 | + //load menuID if absent |
|
62 | + self::loadMenuID($menuID); |
|
63 | + |
|
64 | + if (Cache::contains("menus", "menu_" . $menuID . "_" . User::current()->getID())) { |
|
65 | + $this->menus = Cache::get("menus", "menu_" . $menuID . "_" . User::current()->getID()); |
|
66 | + } else { |
|
67 | + $menu_cache = self::$menuID_array[$menuID]; |
|
68 | + |
|
69 | + //get menu by parent -y, this means root menu |
|
70 | + $this->menus = $this->getMenuByParent($menu_cache, -1, $folder); |
|
71 | + |
|
72 | + Events::throwEvent("after_load_menu", array( |
|
73 | + 'menuID' => &$menuID, |
|
74 | + 'instance' => &$this, |
|
75 | + 'menus' => &$this->menus, |
|
76 | + 'menu_cache' => $menu_cache |
|
77 | + )); |
|
78 | + |
|
79 | + Cache::put("menus", "menu_" . $menuID . "_" . User::current()->getID(), $this->menus); |
|
80 | + } |
|
81 | + |
|
82 | + $this->menuID = $menuID; |
|
83 | + } |
|
84 | + |
|
85 | + protected function getMenuByParent (array &$menu_array, int $parentID, Folder $folder) : array { |
|
86 | + if (!isset($menu_array[$parentID])) { |
|
87 | + //menu doesnt have submenus |
|
88 | + return array(); |
|
89 | + } |
|
90 | + |
|
91 | + $array = array(); |
|
92 | + |
|
93 | + foreach ($menu_array[$parentID] as $row) { |
|
94 | + //check login_required |
|
95 | + if ($row['login_required'] == 1 && !User::current()->isLoggedIn()) { |
|
96 | + //dont show this menu |
|
97 | + continue; |
|
98 | + } |
|
99 | + |
|
100 | + //check permissions |
|
101 | + $permissions = explode("|", $row['permissions']); |
|
102 | + $has_permission = false; |
|
103 | + |
|
104 | + foreach ($permissions as $permission) { |
|
105 | + if (PermissionChecker::current()->hasRight($permission)) { |
|
106 | + $has_permission = true; |
|
107 | + break; |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + if (!$has_permission) { |
|
112 | + //dont show this menu, because user doesnt have permission for this menu |
|
113 | + continue; |
|
114 | + } |
|
115 | + |
|
116 | + $entry = array(); |
|
117 | + |
|
118 | + //translate title |
|
119 | + if ($folder->isTitleTranslationEnabled()) { |
|
120 | + $row['title'] = Translator::translateTitle($row['title']); |
|
121 | + } |
|
122 | + |
|
123 | + $href = ""; |
|
124 | + $entry['append'] = ""; |
|
125 | + $entry['title'] = $row['title']; |
|
126 | + $entry['text'] = $row['title']; |
|
127 | + $entry['icon'] = $row['icon']; |
|
128 | + $entry['icon_class'] = " " . $row['icon']; |
|
129 | + $entry['permissions'] = explode("|", $row['permissions']); |
|
130 | + $entry['append'] = ""; |
|
131 | + $entry['extension_code'] = ""; |
|
132 | + |
|
133 | + if (strpos($row['url'], "LOGIN_URL") !== FALSE) { |
|
134 | + $row['url'] = Registry::singleton()->getSetting("login_url"); |
|
135 | + } |
|
136 | + |
|
137 | + if (strpos($row['url'], "LOGOUT_URL") !== FALSE) { |
|
138 | + $row['url'] = Registry::singleton()->getSetting("logout_url"); |
|
139 | + } |
|
140 | + |
|
141 | + if ($row['type'] == "page") { |
|
142 | + $href = DomainUtils::generateURL($row['url']); |
|
143 | + } else if ($row['type'] == "link") { |
|
144 | + //TODO: add base url |
|
145 | + $href = $row['url']; |
|
146 | + } else if ($row['type'] == "external_link") { |
|
147 | + $href = $row['url']; |
|
148 | + } else if ($row['type'] == "js_link") { |
|
149 | + $href = "#"; |
|
150 | + $entry['append'] = " onclick=\"" . $row['url'] . "\""; |
|
151 | + } else if ($row['type'] == "no_link") { |
|
152 | + $href = "#"; |
|
153 | + } else if ($row['type'] == "dynamic_link") { |
|
154 | + $href = "#"; |
|
155 | + |
|
156 | + if (PHPUtils::startsWith($row['url'], "settings:")) { |
|
157 | + $array1 = explode(":", $row['url']); |
|
158 | + $href = Settings::get($array1[1], "#"); |
|
159 | + } else if (PHPUtils::startsWith($row['url'], "registry:")) { |
|
160 | + $array1 = explode(":", $row['url']); |
|
161 | + $href = Registry::singleton()->getSetting($array1[1], "#"); |
|
162 | + } |
|
163 | + } else { |
|
164 | + throw new IllegalStateException("Unknown menu type: " . $row['type']); |
|
165 | + } |
|
166 | + |
|
167 | + $entry['href'] = $href; |
|
168 | + |
|
169 | + if (!empty($row['icon']) && $row['icon'] != "none") { |
|
170 | + $entry['text'] = "<img src=\"" . $row['icon'] . "\" alt=\"" . $row['title'] . "\" title=\"" . $row['title'] . "\" style=\"max-width:32px; max-height:32px; \" /> " . $row['title']; |
|
171 | + } |
|
172 | + |
|
173 | + //get submenus |
|
174 | + $entry['submenus'] = $this->getMenuByParent($menu_array, $row['id'], $folder); |
|
175 | + $entry['has_submenus'] = sizeof($entry['submenus']) > 0; |
|
176 | + |
|
177 | + $array[] = $entry; |
|
178 | + } |
|
179 | + |
|
180 | + return $array; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * get HTML code of menu |
|
185 | + */ |
|
186 | + public function getCode () : string { |
|
187 | + $template = new DwooTemplate($this->template); |
|
188 | + |
|
189 | + $template->assign("menu_array", $this->menus); |
|
190 | + |
|
191 | + /*$template = new Template($this->template); |
|
192 | 192 | |
193 | 193 | $this->parseMenu($this->menus, $template); |
194 | 194 | |
195 | 195 | //parse main block |
196 | 196 | $template->parse("main");*/ |
197 | 197 | |
198 | - $html = $template->getCode(); |
|
198 | + $html = $template->getCode(); |
|
199 | 199 | |
200 | - Events::throwEvent("get_menu_code", array( |
|
201 | - 'menuID' => $this->menuID, |
|
202 | - 'menus' => &$this->menus, |
|
203 | - 'html' => &$html |
|
204 | - )); |
|
200 | + Events::throwEvent("get_menu_code", array( |
|
201 | + 'menuID' => $this->menuID, |
|
202 | + 'menus' => &$this->menus, |
|
203 | + 'html' => &$html |
|
204 | + )); |
|
205 | 205 | |
206 | - return $html; |
|
207 | - } |
|
206 | + return $html; |
|
207 | + } |
|
208 | 208 | |
209 | - protected static function loadMenuID (int $menuID) { |
|
210 | - if (isset(self::$menuID_array[$menuID])) { |
|
211 | - return; |
|
212 | - } |
|
209 | + protected static function loadMenuID (int $menuID) { |
|
210 | + if (isset(self::$menuID_array[$menuID])) { |
|
211 | + return; |
|
212 | + } |
|
213 | 213 | |
214 | - if (Cache::contains("menus", "menuID_" . $menuID)) { |
|
215 | - self::$menuID_array[$menuID] = Cache::get("menus", "menuID_" . $menuID); |
|
216 | - } else { |
|
217 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}menu` WHERE `menuID` = :menuID AND `activated` = '1' ORDER BY `order`; ", array('menuID' => array( |
|
218 | - 'type' => PDO::PARAM_INT, |
|
219 | - 'value' => $menuID |
|
220 | - ))); |
|
214 | + if (Cache::contains("menus", "menuID_" . $menuID)) { |
|
215 | + self::$menuID_array[$menuID] = Cache::get("menus", "menuID_" . $menuID); |
|
216 | + } else { |
|
217 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}menu` WHERE `menuID` = :menuID AND `activated` = '1' ORDER BY `order`; ", array('menuID' => array( |
|
218 | + 'type' => PDO::PARAM_INT, |
|
219 | + 'value' => $menuID |
|
220 | + ))); |
|
221 | 221 | |
222 | - $array = array(); |
|
222 | + $array = array(); |
|
223 | 223 | |
224 | - foreach ($rows as $row) { |
|
225 | - $parentID = $row['parent']; |
|
224 | + foreach ($rows as $row) { |
|
225 | + $parentID = $row['parent']; |
|
226 | 226 | |
227 | - if (!isset($array[$parentID])) { |
|
228 | - $array[$parentID] = array(); |
|
229 | - } |
|
227 | + if (!isset($array[$parentID])) { |
|
228 | + $array[$parentID] = array(); |
|
229 | + } |
|
230 | 230 | |
231 | - $array[$parentID][] = $row; |
|
232 | - } |
|
231 | + $array[$parentID][] = $row; |
|
232 | + } |
|
233 | 233 | |
234 | - self::$menuID_array[$menuID] = $array; |
|
234 | + self::$menuID_array[$menuID] = $array; |
|
235 | 235 | |
236 | - Cache::put("menus", "menuID_" . $menuID, $array); |
|
237 | - } |
|
236 | + Cache::put("menus", "menuID_" . $menuID, $array); |
|
237 | + } |
|
238 | 238 | |
239 | - //var_dump(self::$menuID_array); |
|
240 | - } |
|
239 | + //var_dump(self::$menuID_array); |
|
240 | + } |
|
241 | 241 | |
242 | - public static function createMenuName (string $title, string $unique_name = null) : int { |
|
243 | - Events::throwEvent("before_create_menu_name", array( |
|
244 | - 'title' => &$title |
|
245 | - )); |
|
242 | + public static function createMenuName (string $title, string $unique_name = null) : int { |
|
243 | + Events::throwEvent("before_create_menu_name", array( |
|
244 | + 'title' => &$title |
|
245 | + )); |
|
246 | 246 | |
247 | - if ($unique_name == null) { |
|
248 | - $unique_name = md5(PHPUtils::randomString(100)); |
|
249 | - } |
|
247 | + if ($unique_name == null) { |
|
248 | + $unique_name = md5(PHPUtils::randomString(100)); |
|
249 | + } |
|
250 | 250 | |
251 | - $unique_name = Validator_String::get($unique_name); |
|
251 | + $unique_name = Validator_String::get($unique_name); |
|
252 | 252 | |
253 | - Database::getInstance()->execute("INSERT INTO `{praefix}menu_names` ( |
|
253 | + Database::getInstance()->execute("INSERT INTO `{praefix}menu_names` ( |
|
254 | 254 | `menuID`, `title`, `unique_name`, `activated` |
255 | 255 | ) VALUES ( |
256 | 256 | NULL, :title, :name, '1' |
257 | 257 | ) ON DUPLICATE KEY UPDATE `title` = :title, `activated` = '1'; ", array( |
258 | - 'title' => $title, |
|
259 | - 'name' => $unique_name |
|
260 | - )); |
|
261 | - |
|
262 | - Cache::clear("menus"); |
|
263 | - |
|
264 | - //get menuID of inserted menu name |
|
265 | - $menuID = Database::getInstance()->lastInsertId(); |
|
266 | - |
|
267 | - Events::throwEvent("after_created_menu_name", array( |
|
268 | - 'menuID' => $menuID, |
|
269 | - 'title' => &$title |
|
270 | - )); |
|
271 | - |
|
272 | - return $menuID; |
|
273 | - } |
|
274 | - |
|
275 | - public static function deleteMenuName (int $menuID) { |
|
276 | - Database::getInstance()->execute("DELETE FROM `{praefix}menu_names` WHERE `menuID` = :menuID; ", array( |
|
277 | - 'menuID' => array( |
|
278 | - 'type' => PDO::PARAM_INT, |
|
279 | - 'value' => $menuID |
|
280 | - ) |
|
281 | - )); |
|
282 | - |
|
283 | - //clear cache |
|
284 | - Cache::clear("menus", "menuID_" . $menuID); |
|
285 | - } |
|
286 | - |
|
287 | - public static function listMenuNames () : array { |
|
288 | - if (Cache::contains("menus", "menu_names")) { |
|
289 | - return Cache::get("menus", "menu_names"); |
|
290 | - } else { |
|
291 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}menu_names` WHERE `activated` = 1; "); |
|
292 | - |
|
293 | - Cache::put("menus", "menu_names", $rows); |
|
294 | - |
|
295 | - return $rows; |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - public static function createMenu (int $id, int $menuID, string $title, string $url, int $parent = -1, string $unique_name = "", $type = "page", $permissions = array("all"), $login_required = false, string $icon = "none", int $order = 100, string $owner = "user") { |
|
300 | - if (!is_array($permissions)) { |
|
301 | - $permissions = array($permissions); |
|
302 | - } |
|
303 | - |
|
304 | - //validate values |
|
305 | - $id = Validator_Int::get($id); |
|
306 | - $menuID = Validator_Int::get($menuID); |
|
307 | - $title = Validator_String::get($title); |
|
308 | - $parent = Validator_Int::get($parent); |
|
309 | - $type = Validator_String::get($type); |
|
310 | - $login_required = (bool) $login_required; |
|
311 | - |
|
312 | - $permissions = implode("|", $permissions); |
|
313 | - |
|
314 | - if (is_null($unique_name) || empty($unique_name)) { |
|
315 | - $unique_name = md5(PHPUtils::randomString(100) . time()); |
|
316 | - } |
|
317 | - |
|
318 | - if ($id != null && $id != -1) { |
|
319 | - $insertID = Database::getInstance()->execute("INSERT INTO `{praefix}menu` ( |
|
258 | + 'title' => $title, |
|
259 | + 'name' => $unique_name |
|
260 | + )); |
|
261 | + |
|
262 | + Cache::clear("menus"); |
|
263 | + |
|
264 | + //get menuID of inserted menu name |
|
265 | + $menuID = Database::getInstance()->lastInsertId(); |
|
266 | + |
|
267 | + Events::throwEvent("after_created_menu_name", array( |
|
268 | + 'menuID' => $menuID, |
|
269 | + 'title' => &$title |
|
270 | + )); |
|
271 | + |
|
272 | + return $menuID; |
|
273 | + } |
|
274 | + |
|
275 | + public static function deleteMenuName (int $menuID) { |
|
276 | + Database::getInstance()->execute("DELETE FROM `{praefix}menu_names` WHERE `menuID` = :menuID; ", array( |
|
277 | + 'menuID' => array( |
|
278 | + 'type' => PDO::PARAM_INT, |
|
279 | + 'value' => $menuID |
|
280 | + ) |
|
281 | + )); |
|
282 | + |
|
283 | + //clear cache |
|
284 | + Cache::clear("menus", "menuID_" . $menuID); |
|
285 | + } |
|
286 | + |
|
287 | + public static function listMenuNames () : array { |
|
288 | + if (Cache::contains("menus", "menu_names")) { |
|
289 | + return Cache::get("menus", "menu_names"); |
|
290 | + } else { |
|
291 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}menu_names` WHERE `activated` = 1; "); |
|
292 | + |
|
293 | + Cache::put("menus", "menu_names", $rows); |
|
294 | + |
|
295 | + return $rows; |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + public static function createMenu (int $id, int $menuID, string $title, string $url, int $parent = -1, string $unique_name = "", $type = "page", $permissions = array("all"), $login_required = false, string $icon = "none", int $order = 100, string $owner = "user") { |
|
300 | + if (!is_array($permissions)) { |
|
301 | + $permissions = array($permissions); |
|
302 | + } |
|
303 | + |
|
304 | + //validate values |
|
305 | + $id = Validator_Int::get($id); |
|
306 | + $menuID = Validator_Int::get($menuID); |
|
307 | + $title = Validator_String::get($title); |
|
308 | + $parent = Validator_Int::get($parent); |
|
309 | + $type = Validator_String::get($type); |
|
310 | + $login_required = (bool) $login_required; |
|
311 | + |
|
312 | + $permissions = implode("|", $permissions); |
|
313 | + |
|
314 | + if (is_null($unique_name) || empty($unique_name)) { |
|
315 | + $unique_name = md5(PHPUtils::randomString(100) . time()); |
|
316 | + } |
|
317 | + |
|
318 | + if ($id != null && $id != -1) { |
|
319 | + $insertID = Database::getInstance()->execute("INSERT INTO `{praefix}menu` ( |
|
320 | 320 | `id`, `menuID`, `title`, `url`, `type`, `icon`, `permissions`, `login_required`, `parent`, `unique_name`, `extensions`, `order`, `owner`, `activated` |
321 | 321 | ) VALUES ( |
322 | 322 | :id, :menuID, :title, :url, :url_type, :icon, :permissions, :login_required, :parent, :unique_name, :extensions, :menu_order, :owner, '1' |
323 | 323 | ) ON DUPLICATE KEY UPDATE `menuID` = :menuID, `title` = :title, `url` = :url, `type` = :url_type, `permissions` = :permissions, `login_required` = :login_required, `parent` = :parent, `unique_name` = :unique_name, `extensions` = :extensions, `order` = :menu_order, `owner` = :owner, `icon` = :icon, `activated` = '1'; ", array( |
324 | - 'id' => $id, |
|
325 | - 'menuID' => $menuID, |
|
326 | - 'title' => $title, |
|
327 | - 'url' => $url, |
|
328 | - 'url_type' => $type, |
|
329 | - 'icon' => $icon, |
|
330 | - 'permissions' => $permissions, |
|
331 | - 'login_required' => ($login_required ? 1 : 0), |
|
332 | - 'parent' => $parent, |
|
333 | - 'unique_name' => $unique_name, |
|
334 | - 'extensions' => "none", |
|
335 | - 'menu_order' => $order, |
|
336 | - 'owner' => $owner |
|
337 | - )); |
|
338 | - } else { |
|
339 | - $insertID = Database::getInstance()->execute("INSERT INTO `{praefix}menu` ( |
|
324 | + 'id' => $id, |
|
325 | + 'menuID' => $menuID, |
|
326 | + 'title' => $title, |
|
327 | + 'url' => $url, |
|
328 | + 'url_type' => $type, |
|
329 | + 'icon' => $icon, |
|
330 | + 'permissions' => $permissions, |
|
331 | + 'login_required' => ($login_required ? 1 : 0), |
|
332 | + 'parent' => $parent, |
|
333 | + 'unique_name' => $unique_name, |
|
334 | + 'extensions' => "none", |
|
335 | + 'menu_order' => $order, |
|
336 | + 'owner' => $owner |
|
337 | + )); |
|
338 | + } else { |
|
339 | + $insertID = Database::getInstance()->execute("INSERT INTO `{praefix}menu` ( |
|
340 | 340 | `id`, `menuID`, `title`, `url`, `type`, `icon`, `permissions`, `login_required`, `parent`, `unique_name`, `extensions`, `order`, `owner`, `activated` |
341 | 341 | ) VALUES ( |
342 | 342 | NULL, :menuID, :title, :url, :url_type, :icon, :permissions, :login_required, :parent, :unique_name, :extensions, :menu_order, :owner, '1' |
343 | 343 | ) ON DUPLICATE KEY UPDATE `menuID` = :menuID, `title` = :title, `url` = :url, `type` = :url_type, `permissions` = :permissions, `login_required` = :login_required, `parent` = :parent, `unique_name` = :unique_name, `extensions` = :extensions, `order` = :menu_order, `owner` = :owner, `icon` = :icon, `activated` = '1'; ", array( |
344 | - 'menuID' => $menuID, |
|
345 | - 'title' => $title, |
|
346 | - 'url' => $url, |
|
347 | - 'url_type' => $type, |
|
348 | - 'icon' => $icon, |
|
349 | - 'permissions' => $permissions, |
|
350 | - 'login_required' => ($login_required ? 1 : 0), |
|
351 | - 'parent' => $parent, |
|
352 | - 'unique_name' => $unique_name, |
|
353 | - 'extensions' => "none", |
|
354 | - 'menu_order' => $order, |
|
355 | - 'owner' => $owner |
|
356 | - )); |
|
357 | - } |
|
358 | - |
|
359 | - //clear cache |
|
360 | - Cache::clear("menus", "menuID_" . $menuID); |
|
361 | - |
|
362 | - return $insertID; |
|
363 | - } |
|
364 | - |
|
365 | - public static function deleteMenusByOwner (string $owner) { |
|
366 | - //delete menus from database |
|
367 | - Database::getInstance()->execute("DELETE FROM `{praefix}menu` WHERE `owner` = :owner; ", array('owner' => $owner)); |
|
368 | - |
|
369 | - //clear cache |
|
370 | - Cache::clear("menus"); |
|
371 | - } |
|
344 | + 'menuID' => $menuID, |
|
345 | + 'title' => $title, |
|
346 | + 'url' => $url, |
|
347 | + 'url_type' => $type, |
|
348 | + 'icon' => $icon, |
|
349 | + 'permissions' => $permissions, |
|
350 | + 'login_required' => ($login_required ? 1 : 0), |
|
351 | + 'parent' => $parent, |
|
352 | + 'unique_name' => $unique_name, |
|
353 | + 'extensions' => "none", |
|
354 | + 'menu_order' => $order, |
|
355 | + 'owner' => $owner |
|
356 | + )); |
|
357 | + } |
|
358 | + |
|
359 | + //clear cache |
|
360 | + Cache::clear("menus", "menuID_" . $menuID); |
|
361 | + |
|
362 | + return $insertID; |
|
363 | + } |
|
364 | + |
|
365 | + public static function deleteMenusByOwner (string $owner) { |
|
366 | + //delete menus from database |
|
367 | + Database::getInstance()->execute("DELETE FROM `{praefix}menu` WHERE `owner` = :owner; ", array('owner' => $owner)); |
|
368 | + |
|
369 | + //clear cache |
|
370 | + Cache::clear("menus"); |
|
371 | + } |
|
372 | 372 | |
373 | 373 | } |
374 | 374 |
@@ -38,12 +38,12 @@ discard block |
||
38 | 38 | |
39 | 39 | protected $menus = array(); |
40 | 40 | |
41 | - public function __construct (int $menuID = -1, string $template = "menu") { |
|
41 | + public function __construct(int $menuID = -1, string $template = "menu") { |
|
42 | 42 | $this->menuID = (int) $menuID; |
43 | 43 | $this->template = $template; |
44 | 44 | } |
45 | 45 | |
46 | - public function loadMenu (int $menuID = -1, Folder $folder) { |
|
46 | + public function loadMenu(int $menuID = -1, Folder $folder) { |
|
47 | 47 | if ($menuID == -1) { |
48 | 48 | $menuID = $this->menuID; |
49 | 49 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $this->menuID = $menuID; |
83 | 83 | } |
84 | 84 | |
85 | - protected function getMenuByParent (array &$menu_array, int $parentID, Folder $folder) : array { |
|
85 | + protected function getMenuByParent(array &$menu_array, int $parentID, Folder $folder) : array { |
|
86 | 86 | if (!isset($menu_array[$parentID])) { |
87 | 87 | //menu doesnt have submenus |
88 | 88 | return array(); |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | /** |
184 | 184 | * get HTML code of menu |
185 | 185 | */ |
186 | - public function getCode () : string { |
|
186 | + public function getCode() : string { |
|
187 | 187 | $template = new DwooTemplate($this->template); |
188 | 188 | |
189 | 189 | $template->assign("menu_array", $this->menus); |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | return $html; |
207 | 207 | } |
208 | 208 | |
209 | - protected static function loadMenuID (int $menuID) { |
|
209 | + protected static function loadMenuID(int $menuID) { |
|
210 | 210 | if (isset(self::$menuID_array[$menuID])) { |
211 | 211 | return; |
212 | 212 | } |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | //var_dump(self::$menuID_array); |
240 | 240 | } |
241 | 241 | |
242 | - public static function createMenuName (string $title, string $unique_name = null) : int { |
|
242 | + public static function createMenuName(string $title, string $unique_name = null) : int { |
|
243 | 243 | Events::throwEvent("before_create_menu_name", array( |
244 | 244 | 'title' => &$title |
245 | 245 | )); |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | return $menuID; |
273 | 273 | } |
274 | 274 | |
275 | - public static function deleteMenuName (int $menuID) { |
|
275 | + public static function deleteMenuName(int $menuID) { |
|
276 | 276 | Database::getInstance()->execute("DELETE FROM `{praefix}menu_names` WHERE `menuID` = :menuID; ", array( |
277 | 277 | 'menuID' => array( |
278 | 278 | 'type' => PDO::PARAM_INT, |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | Cache::clear("menus", "menuID_" . $menuID); |
285 | 285 | } |
286 | 286 | |
287 | - public static function listMenuNames () : array { |
|
287 | + public static function listMenuNames() : array { |
|
288 | 288 | if (Cache::contains("menus", "menu_names")) { |
289 | 289 | return Cache::get("menus", "menu_names"); |
290 | 290 | } else { |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | } |
297 | 297 | } |
298 | 298 | |
299 | - public static function createMenu (int $id, int $menuID, string $title, string $url, int $parent = -1, string $unique_name = "", $type = "page", $permissions = array("all"), $login_required = false, string $icon = "none", int $order = 100, string $owner = "user") { |
|
299 | + public static function createMenu(int $id, int $menuID, string $title, string $url, int $parent = -1, string $unique_name = "", $type = "page", $permissions = array("all"), $login_required = false, string $icon = "none", int $order = 100, string $owner = "user") { |
|
300 | 300 | if (!is_array($permissions)) { |
301 | 301 | $permissions = array($permissions); |
302 | 302 | } |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | return $insertID; |
363 | 363 | } |
364 | 364 | |
365 | - public static function deleteMenusByOwner (string $owner) { |
|
365 | + public static function deleteMenusByOwner(string $owner) { |
|
366 | 366 | //delete menus from database |
367 | 367 | Database::getInstance()->execute("DELETE FROM `{praefix}menu` WHERE `owner` = :owner; ", array('owner' => $owner)); |
368 | 368 |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * |
26 | 26 | * @return current style as string name |
27 | 27 | */ |
28 | - public static function getCurrentStyle (Registry &$registry, Page &$page, PageType &$page_type) : string { |
|
28 | + public static function getCurrentStyle(Registry &$registry, Page &$page, PageType &$page_type) : string { |
|
29 | 29 | //get default styles |
30 | 30 | $default_style_name = Settings::get("default_style_name"); |
31 | 31 | $default_mobile_style_name = Settings::get("default_mobile_style_name"); |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | return $style_name; |
47 | 47 | } |
48 | 48 | |
49 | - public static function showPage (Registry &$registry, Page &$page, PageType &$page_type) { |
|
49 | + public static function showPage(Registry &$registry, Page &$page, PageType &$page_type) { |
|
50 | 50 | //create new template |
51 | 51 | $template = new DwooTemplate("index"); |
52 | 52 | |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | echo $template->getCode(); |
187 | 187 | } |
188 | 188 | |
189 | - public static function listAllStyles () : array { |
|
189 | + public static function listAllStyles() : array { |
|
190 | 190 | $styles = array(); |
191 | 191 | |
192 | 192 | $file_array = glob(STYLE_PATH . "*"); |
@@ -18,110 +18,110 @@ discard block |
||
18 | 18 | |
19 | 19 | class StyleController { |
20 | 20 | |
21 | - /** |
|
22 | - * get name of current style |
|
23 | - * |
|
24 | - * @param $registry Registry instance |
|
25 | - * |
|
26 | - * @return current style as string name |
|
27 | - */ |
|
28 | - public static function getCurrentStyle (Registry &$registry, Page &$page, PageType &$page_type) : string { |
|
29 | - //get default styles |
|
30 | - $default_style_name = Settings::get("default_style_name"); |
|
31 | - $default_mobile_style_name = Settings::get("default_mobile_style_name"); |
|
32 | - |
|
33 | - $style_name = !Browser::isMobile() ? $default_style_name : $default_mobile_style_name; |
|
34 | - |
|
35 | - //apply style rules |
|
36 | - $style_name = StyleRules::getStyle($registry, $style_name); |
|
37 | - |
|
38 | - //throw event, so plugins can change style |
|
39 | - Events::throwEvent("get_style", array( |
|
40 | - 'default_style_name' => $default_style_name, |
|
41 | - 'default_mobile_style_name' => $default_mobile_style_name, |
|
42 | - 'style_name' => &$style_name, |
|
43 | - 'registry' => $registry |
|
44 | - )); |
|
45 | - |
|
46 | - //check, if page has own style |
|
47 | - if ($page->getStyle() !== "none") { |
|
48 | - return $page->getStyle(); |
|
49 | - } |
|
50 | - |
|
51 | - return $style_name; |
|
52 | - } |
|
53 | - |
|
54 | - public static function showPage (Registry &$registry, Page &$page, PageType &$page_type) { |
|
55 | - //create new template |
|
56 | - $template = new DwooTemplate("index"); |
|
57 | - |
|
58 | - $title_preafix = Settings::get("title_praefix", ""); |
|
59 | - $title_suffix = Settings::get("title_suffix", ""); |
|
60 | - |
|
61 | - //translate title |
|
62 | - $title = Translator::translateTitle($page->getTitle()); |
|
63 | - |
|
64 | - //assign variables |
|
65 | - $template->assign("TITLE", $title_preafix . $title . $title_suffix); |
|
66 | - $template->assign("SHORT_TITLE", $title); |
|
67 | - $template->assign("RAW_TITLE", $page->getTitle()); |
|
68 | - $template->assign("REGISTRY", $registry->listSettings()); |
|
69 | - |
|
70 | - $head_content = ""; |
|
71 | - |
|
72 | - Events::throwEvent("get_head", array( |
|
73 | - 'registry' => &$registry, |
|
74 | - 'page' => &$page, |
|
75 | - 'page_type' => &$page_type, |
|
76 | - 'head_content' => $head_content |
|
77 | - )); |
|
78 | - |
|
79 | - $template->assign("HEAD", $head_content . $page_type->getAdditionalHeaderCode()); |
|
80 | - |
|
81 | - $template->assign("CONTENT", $page_type->getContent()); |
|
82 | - $template->assign("HEADER", $registry->getSetting("header", "")); |
|
83 | - $template->assign("FOOTER", $registry->getSetting("footer", "")); |
|
84 | - $template->assign("COPYRIGHT", Settings::get("copyright", "<strong>Copyright © 2018 <a href=\"http://jukusoft.com\">JuKuSoft.com</a></strong>, All Rights Reserved.")); |
|
85 | - |
|
86 | - $template->assign("FOOTER_SCRIPTS", $page_type->getFooterScripts()); |
|
87 | - |
|
88 | - $template->assign("MY_GROUP_IDS", implode(",", $registry->getObject("groups")->listGroupIDs())); |
|
89 | - $template->assign("CHARSET", $page_type->getCharset()); |
|
90 | - |
|
91 | - //author name |
|
92 | - $author = $page->getAuthor(); |
|
93 | - |
|
94 | - //meta tags |
|
95 | - $meta = array( |
|
96 | - 'description' => $page->getMetaDescription(), |
|
97 | - 'keywords' => $page->getMetaKeywords(), |
|
98 | - 'robots' => $page->getMetaRobotsOptions(), |
|
99 | - 'canoncials' => $page->getMetaCanonicals(), |
|
100 | - 'has_robots' => !empty($page->getMetaRobotsOptions()), |
|
101 | - 'has_canoncials' => !empty($page->getMetaCanonicals()), |
|
102 | - 'author' => array( |
|
103 | - 'userID' => $author->getID(), |
|
104 | - 'username' => $author->getUsername(), |
|
105 | - 'title' => $author->getTitle() |
|
106 | - ) |
|
107 | - ); |
|
108 | - |
|
109 | - Events::throwEvent("meta_tags", array( |
|
110 | - 'page' => &$page, |
|
111 | - 'translated_title' => &$title, |
|
112 | - 'registry' => &$registry, |
|
113 | - 'template' => &$template, |
|
114 | - 'author' => &$author, |
|
115 | - 'meta' => &$meta |
|
116 | - )); |
|
117 | - |
|
118 | - $template->assign("meta", $meta); |
|
119 | - |
|
120 | - //set opengraph tags |
|
121 | - $template->assign("OG_TAGS", $page_type->getOgTags()); |
|
122 | - |
|
123 | - //create new css builder |
|
124 | - /*$css_builder = new CSSBuilder(); |
|
21 | + /** |
|
22 | + * get name of current style |
|
23 | + * |
|
24 | + * @param $registry Registry instance |
|
25 | + * |
|
26 | + * @return current style as string name |
|
27 | + */ |
|
28 | + public static function getCurrentStyle (Registry &$registry, Page &$page, PageType &$page_type) : string { |
|
29 | + //get default styles |
|
30 | + $default_style_name = Settings::get("default_style_name"); |
|
31 | + $default_mobile_style_name = Settings::get("default_mobile_style_name"); |
|
32 | + |
|
33 | + $style_name = !Browser::isMobile() ? $default_style_name : $default_mobile_style_name; |
|
34 | + |
|
35 | + //apply style rules |
|
36 | + $style_name = StyleRules::getStyle($registry, $style_name); |
|
37 | + |
|
38 | + //throw event, so plugins can change style |
|
39 | + Events::throwEvent("get_style", array( |
|
40 | + 'default_style_name' => $default_style_name, |
|
41 | + 'default_mobile_style_name' => $default_mobile_style_name, |
|
42 | + 'style_name' => &$style_name, |
|
43 | + 'registry' => $registry |
|
44 | + )); |
|
45 | + |
|
46 | + //check, if page has own style |
|
47 | + if ($page->getStyle() !== "none") { |
|
48 | + return $page->getStyle(); |
|
49 | + } |
|
50 | + |
|
51 | + return $style_name; |
|
52 | + } |
|
53 | + |
|
54 | + public static function showPage (Registry &$registry, Page &$page, PageType &$page_type) { |
|
55 | + //create new template |
|
56 | + $template = new DwooTemplate("index"); |
|
57 | + |
|
58 | + $title_preafix = Settings::get("title_praefix", ""); |
|
59 | + $title_suffix = Settings::get("title_suffix", ""); |
|
60 | + |
|
61 | + //translate title |
|
62 | + $title = Translator::translateTitle($page->getTitle()); |
|
63 | + |
|
64 | + //assign variables |
|
65 | + $template->assign("TITLE", $title_preafix . $title . $title_suffix); |
|
66 | + $template->assign("SHORT_TITLE", $title); |
|
67 | + $template->assign("RAW_TITLE", $page->getTitle()); |
|
68 | + $template->assign("REGISTRY", $registry->listSettings()); |
|
69 | + |
|
70 | + $head_content = ""; |
|
71 | + |
|
72 | + Events::throwEvent("get_head", array( |
|
73 | + 'registry' => &$registry, |
|
74 | + 'page' => &$page, |
|
75 | + 'page_type' => &$page_type, |
|
76 | + 'head_content' => $head_content |
|
77 | + )); |
|
78 | + |
|
79 | + $template->assign("HEAD", $head_content . $page_type->getAdditionalHeaderCode()); |
|
80 | + |
|
81 | + $template->assign("CONTENT", $page_type->getContent()); |
|
82 | + $template->assign("HEADER", $registry->getSetting("header", "")); |
|
83 | + $template->assign("FOOTER", $registry->getSetting("footer", "")); |
|
84 | + $template->assign("COPYRIGHT", Settings::get("copyright", "<strong>Copyright © 2018 <a href=\"http://jukusoft.com\">JuKuSoft.com</a></strong>, All Rights Reserved.")); |
|
85 | + |
|
86 | + $template->assign("FOOTER_SCRIPTS", $page_type->getFooterScripts()); |
|
87 | + |
|
88 | + $template->assign("MY_GROUP_IDS", implode(",", $registry->getObject("groups")->listGroupIDs())); |
|
89 | + $template->assign("CHARSET", $page_type->getCharset()); |
|
90 | + |
|
91 | + //author name |
|
92 | + $author = $page->getAuthor(); |
|
93 | + |
|
94 | + //meta tags |
|
95 | + $meta = array( |
|
96 | + 'description' => $page->getMetaDescription(), |
|
97 | + 'keywords' => $page->getMetaKeywords(), |
|
98 | + 'robots' => $page->getMetaRobotsOptions(), |
|
99 | + 'canoncials' => $page->getMetaCanonicals(), |
|
100 | + 'has_robots' => !empty($page->getMetaRobotsOptions()), |
|
101 | + 'has_canoncials' => !empty($page->getMetaCanonicals()), |
|
102 | + 'author' => array( |
|
103 | + 'userID' => $author->getID(), |
|
104 | + 'username' => $author->getUsername(), |
|
105 | + 'title' => $author->getTitle() |
|
106 | + ) |
|
107 | + ); |
|
108 | + |
|
109 | + Events::throwEvent("meta_tags", array( |
|
110 | + 'page' => &$page, |
|
111 | + 'translated_title' => &$title, |
|
112 | + 'registry' => &$registry, |
|
113 | + 'template' => &$template, |
|
114 | + 'author' => &$author, |
|
115 | + 'meta' => &$meta |
|
116 | + )); |
|
117 | + |
|
118 | + $template->assign("meta", $meta); |
|
119 | + |
|
120 | + //set opengraph tags |
|
121 | + $template->assign("OG_TAGS", $page_type->getOgTags()); |
|
122 | + |
|
123 | + //create new css builder |
|
124 | + /*$css_builder = new CSSBuilder(); |
|
125 | 125 | |
126 | 126 | //create new js builder |
127 | 127 | $js_builder = new JSBuilder(); |
@@ -136,76 +136,76 @@ discard block |
||
136 | 136 | $template->assign("JS_ALL_HEADER_EMPTY", $js_builder->isEmpty($current_style, "ALL", "header")); |
137 | 137 | $template->assign("JS_ALL_FOOTER_EMPTY", $js_builder->isEmpty($current_style, "ALL", "footer"));*/ |
138 | 138 | |
139 | - //set sidebar arrays |
|
140 | - $left_sidebar = Registry::singleton()->getObject("left_sidebar"); |
|
141 | - $right_sidebar = Registry::singleton()->getObject("right_sidebar"); |
|
142 | - $sidebars_var = array( |
|
143 | - 'left_sidebar' => $left_sidebar->listWidgetTplArray(), |
|
144 | - 'right_sidebar' => $right_sidebar->listWidgetTplArray() |
|
145 | - ); |
|
146 | - $template->assign("sidebars", $sidebars_var); |
|
147 | - |
|
148 | - //set version and build number |
|
149 | - if (PermissionChecker::current()->hasRight("can_see_cms_version")) { |
|
150 | - $template->assign("VERSION", Version::current()->getVersion()); |
|
151 | - $template->assign("BUILD", Version::current()->getBuildNumber()); |
|
152 | - } else { |
|
153 | - $template->assign("VERSION", "Unknown"); |
|
154 | - $template->assign("BUILD", "Unknown"); |
|
155 | - } |
|
156 | - |
|
157 | - //userid and username |
|
158 | - $user = User::current(); |
|
159 | - /*$template->assign("USERID", $user->getID()); |
|
139 | + //set sidebar arrays |
|
140 | + $left_sidebar = Registry::singleton()->getObject("left_sidebar"); |
|
141 | + $right_sidebar = Registry::singleton()->getObject("right_sidebar"); |
|
142 | + $sidebars_var = array( |
|
143 | + 'left_sidebar' => $left_sidebar->listWidgetTplArray(), |
|
144 | + 'right_sidebar' => $right_sidebar->listWidgetTplArray() |
|
145 | + ); |
|
146 | + $template->assign("sidebars", $sidebars_var); |
|
147 | + |
|
148 | + //set version and build number |
|
149 | + if (PermissionChecker::current()->hasRight("can_see_cms_version")) { |
|
150 | + $template->assign("VERSION", Version::current()->getVersion()); |
|
151 | + $template->assign("BUILD", Version::current()->getBuildNumber()); |
|
152 | + } else { |
|
153 | + $template->assign("VERSION", "Unknown"); |
|
154 | + $template->assign("BUILD", "Unknown"); |
|
155 | + } |
|
156 | + |
|
157 | + //userid and username |
|
158 | + $user = User::current(); |
|
159 | + /*$template->assign("USERID", $user->getID()); |
|
160 | 160 | $template->assign("USERNAME", $user->getUsername());*/ |
161 | - $template->assign("IS_LOGGED_IN", $user->isLoggedIn()); |
|
162 | - |
|
163 | - //assign menu code |
|
164 | - $globalMenu = $registry->getObject("main_menu"); |
|
165 | - $localMenu = $registry->getObject("local_menu"); |
|
166 | - $template->assign("MENU", $globalMenu->getCode()); |
|
167 | - $template->assign("LOCALMENU", $localMenu->getCode()); |
|
168 | - |
|
169 | - if (User::current()->isLoggedIn()) { |
|
170 | - $template->assign("is_logged_in", true); |
|
171 | - //$template->parse("main.logged_in"); |
|
172 | - } else { |
|
173 | - $template->assign("is_logged_in", false); |
|
174 | - //$template->parse("main.not_logged_in"); |
|
175 | - } |
|
176 | - |
|
177 | - //throw event |
|
178 | - Events::throwEvent("show_page", array( |
|
179 | - 'registry' => &$registry, |
|
180 | - 'page' => &$page, |
|
181 | - 'page_type' => &$page_type, |
|
182 | - 'template' => &$template |
|
183 | - )); |
|
184 | - |
|
185 | - if ($page_type->showFooter()) { |
|
186 | - $template->assign("show_footer", true); |
|
187 | - //$template->parse("main.footer"); |
|
188 | - } else { |
|
189 | - $template->assign("show_footer", false); |
|
190 | - } |
|
191 | - |
|
192 | - //$template->parse(); |
|
193 | - |
|
194 | - echo $template->getCode(); |
|
195 | - } |
|
196 | - |
|
197 | - public static function listAllStyles () : array { |
|
198 | - $styles = array(); |
|
199 | - |
|
200 | - $file_array = glob(STYLE_PATH . "*"); |
|
201 | - |
|
202 | - foreach ($file_array as $dir) { |
|
203 | - $array1 = explode("/", $dir); |
|
204 | - $styles[] = $array1[count($array1) - 1]; |
|
205 | - } |
|
206 | - |
|
207 | - return $styles; |
|
208 | - } |
|
161 | + $template->assign("IS_LOGGED_IN", $user->isLoggedIn()); |
|
162 | + |
|
163 | + //assign menu code |
|
164 | + $globalMenu = $registry->getObject("main_menu"); |
|
165 | + $localMenu = $registry->getObject("local_menu"); |
|
166 | + $template->assign("MENU", $globalMenu->getCode()); |
|
167 | + $template->assign("LOCALMENU", $localMenu->getCode()); |
|
168 | + |
|
169 | + if (User::current()->isLoggedIn()) { |
|
170 | + $template->assign("is_logged_in", true); |
|
171 | + //$template->parse("main.logged_in"); |
|
172 | + } else { |
|
173 | + $template->assign("is_logged_in", false); |
|
174 | + //$template->parse("main.not_logged_in"); |
|
175 | + } |
|
176 | + |
|
177 | + //throw event |
|
178 | + Events::throwEvent("show_page", array( |
|
179 | + 'registry' => &$registry, |
|
180 | + 'page' => &$page, |
|
181 | + 'page_type' => &$page_type, |
|
182 | + 'template' => &$template |
|
183 | + )); |
|
184 | + |
|
185 | + if ($page_type->showFooter()) { |
|
186 | + $template->assign("show_footer", true); |
|
187 | + //$template->parse("main.footer"); |
|
188 | + } else { |
|
189 | + $template->assign("show_footer", false); |
|
190 | + } |
|
191 | + |
|
192 | + //$template->parse(); |
|
193 | + |
|
194 | + echo $template->getCode(); |
|
195 | + } |
|
196 | + |
|
197 | + public static function listAllStyles () : array { |
|
198 | + $styles = array(); |
|
199 | + |
|
200 | + $file_array = glob(STYLE_PATH . "*"); |
|
201 | + |
|
202 | + foreach ($file_array as $dir) { |
|
203 | + $array1 = explode("/", $dir); |
|
204 | + $styles[] = $array1[count($array1) - 1]; |
|
205 | + } |
|
206 | + |
|
207 | + return $styles; |
|
208 | + } |
|
209 | 209 | |
210 | 210 | } |
211 | 211 |
@@ -27,219 +27,219 @@ |
||
27 | 27 | |
28 | 28 | class Sidebar { |
29 | 29 | |
30 | - protected $sidebar_id = -1; |
|
31 | - protected $row = array(); |
|
32 | - |
|
33 | - protected $widget_rows = array(); |
|
34 | - protected $widgets = array(); |
|
35 | - |
|
36 | - protected static $is_initialized = false; |
|
37 | - protected static $all_sidebars = array(); |
|
38 | - |
|
39 | - public function __construct() { |
|
40 | - //load in-memory cache, if neccessary |
|
41 | - if (!self::isInitialized()) { |
|
42 | - self::initialize(); |
|
43 | - } |
|
44 | - } |
|
45 | - |
|
46 | - public function load (int $sidebar_id) { |
|
47 | - if (isset(self::$all_sidebars[$sidebar_id])) { |
|
48 | - $this->row = self::$all_sidebars[$sidebar_id]; |
|
49 | - } else { |
|
50 | - throw new IllegalStateException("sidebar with id '" . $sidebar_id . "' doesnt exists."); |
|
51 | - } |
|
52 | - |
|
53 | - $this->sidebar_id = $sidebar_id; |
|
54 | - } |
|
55 | - |
|
56 | - public function loadByArray (array $row) { |
|
57 | - $this->row = $row; |
|
58 | - $this->sidebar_id = (int) $row['sidebar_id']; |
|
59 | - } |
|
60 | - |
|
61 | - public function loadWidgets () { |
|
62 | - $this->widgets = array(); |
|
63 | - |
|
64 | - //load widgets |
|
65 | - if (Cache::contains("sidebars", "sidebar_widgets_" . $this->sidebar_id)) { |
|
66 | - $this->widget_rows = Cache::get("sidebars", "sidebar_widgets_" . $this->sidebar_id); |
|
67 | - } else { |
|
68 | - //list rows from database |
|
69 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebar_widgets` WHERE `sidebar_id` = :sidebar_id ORDER BY `order`; ", array( |
|
70 | - 'sidebar_id' => $this->sidebar_id |
|
71 | - )); |
|
72 | - |
|
73 | - //cache results |
|
74 | - Cache::put("sidebars", "sidebar_widgets_" . $this->sidebar_id, $rows); |
|
75 | - |
|
76 | - $this->widget_rows = $rows; |
|
77 | - } |
|
78 | - |
|
79 | - foreach ($this->widget_rows as $widget_row) { |
|
80 | - $class_name = $widget_row['class_name']; |
|
81 | - $widget_instance = new $class_name(); |
|
82 | - |
|
83 | - if (!($widget_instance instanceof Widget)) { |
|
84 | - throw new IllegalStateException("instance has to be an instance of class Widget, this means widget types has to extends class Widget."); |
|
85 | - } |
|
86 | - |
|
87 | - //cast widget |
|
88 | - $widget = Widget::castWidget($widget_instance); |
|
89 | - |
|
90 | - //load widget and set row, so widget doesn't needs to load data seperate from database |
|
91 | - $widget->load($widget_row); |
|
92 | - |
|
93 | - //add widget to list |
|
94 | - $this->widgets[] = $widget; |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * list widget instances |
|
100 | - */ |
|
101 | - public function listWidgets () : array { |
|
102 | - return $this->widgets; |
|
103 | - } |
|
104 | - |
|
105 | - public function listWidgetTplArray () : array { |
|
106 | - $array = array(); |
|
107 | - |
|
108 | - foreach ($this->listWidgets() as $widget) { |
|
109 | - $widget = Widget::castWidget($widget); |
|
110 | - |
|
111 | - $array[] = array( |
|
112 | - 'id' => $widget->getId(), |
|
113 | - 'title' => $widget->getTitle(), |
|
114 | - 'code' => $widget->getCode(), |
|
115 | - 'css_id' => $widget->getCSSId(), |
|
116 | - 'css_class' => $widget->getCSSClass(), |
|
117 | - 'use_template' => $widget->useTemplate() |
|
118 | - ); |
|
119 | - } |
|
120 | - |
|
121 | - return $array; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @return int sidebar id |
|
126 | - */ |
|
127 | - public function getSidebarId(): int { |
|
128 | - return $this->sidebar_id; |
|
129 | - } |
|
130 | - |
|
131 | - public function getUniqueName () : string { |
|
132 | - return $this->row['unique_name']; |
|
133 | - } |
|
134 | - |
|
135 | - public function getTitle () : string { |
|
136 | - return $this->row['title']; |
|
137 | - } |
|
138 | - |
|
139 | - public function isDeletable () : bool { |
|
140 | - return $this->row['deletable'] == 1; |
|
141 | - } |
|
142 | - |
|
143 | - public function getRow () : array { |
|
144 | - return $this->row; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @return bool |
|
149 | - */ |
|
150 | - protected static function isInitialized(): bool { |
|
151 | - return self::$is_initialized; |
|
152 | - } |
|
153 | - |
|
154 | - protected static function initialize () { |
|
155 | - if (Cache::contains("sidebars", "all_sidebars")) { |
|
156 | - self::$all_sidebars = Cache::get("sidebars", "all_sidebars"); |
|
157 | - } else { |
|
158 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; "); |
|
159 | - |
|
160 | - //clear in-memory cache |
|
161 | - self::$all_sidebars = array(); |
|
162 | - |
|
163 | - foreach ($rows as $row) { |
|
164 | - self::$all_sidebars[$row['sidebar_id']] = $row; |
|
165 | - } |
|
166 | - |
|
167 | - //cache result |
|
168 | - Cache::put("sidebars", "all_sidebars", self::$all_sidebars); |
|
169 | - } |
|
170 | - |
|
171 | - self::$is_initialized = true; |
|
172 | - } |
|
173 | - |
|
174 | - public static function create (string $title, string $unique_name, bool $deletable = true) : int { |
|
175 | - if (empty($unique_name)) { |
|
176 | - throw new IllegalArgumentException("unique_name cannot be null."); |
|
177 | - } |
|
178 | - |
|
179 | - Database::getInstance()->execute("INSERT INTO `{praefix}sidebars` ( |
|
30 | + protected $sidebar_id = -1; |
|
31 | + protected $row = array(); |
|
32 | + |
|
33 | + protected $widget_rows = array(); |
|
34 | + protected $widgets = array(); |
|
35 | + |
|
36 | + protected static $is_initialized = false; |
|
37 | + protected static $all_sidebars = array(); |
|
38 | + |
|
39 | + public function __construct() { |
|
40 | + //load in-memory cache, if neccessary |
|
41 | + if (!self::isInitialized()) { |
|
42 | + self::initialize(); |
|
43 | + } |
|
44 | + } |
|
45 | + |
|
46 | + public function load (int $sidebar_id) { |
|
47 | + if (isset(self::$all_sidebars[$sidebar_id])) { |
|
48 | + $this->row = self::$all_sidebars[$sidebar_id]; |
|
49 | + } else { |
|
50 | + throw new IllegalStateException("sidebar with id '" . $sidebar_id . "' doesnt exists."); |
|
51 | + } |
|
52 | + |
|
53 | + $this->sidebar_id = $sidebar_id; |
|
54 | + } |
|
55 | + |
|
56 | + public function loadByArray (array $row) { |
|
57 | + $this->row = $row; |
|
58 | + $this->sidebar_id = (int) $row['sidebar_id']; |
|
59 | + } |
|
60 | + |
|
61 | + public function loadWidgets () { |
|
62 | + $this->widgets = array(); |
|
63 | + |
|
64 | + //load widgets |
|
65 | + if (Cache::contains("sidebars", "sidebar_widgets_" . $this->sidebar_id)) { |
|
66 | + $this->widget_rows = Cache::get("sidebars", "sidebar_widgets_" . $this->sidebar_id); |
|
67 | + } else { |
|
68 | + //list rows from database |
|
69 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebar_widgets` WHERE `sidebar_id` = :sidebar_id ORDER BY `order`; ", array( |
|
70 | + 'sidebar_id' => $this->sidebar_id |
|
71 | + )); |
|
72 | + |
|
73 | + //cache results |
|
74 | + Cache::put("sidebars", "sidebar_widgets_" . $this->sidebar_id, $rows); |
|
75 | + |
|
76 | + $this->widget_rows = $rows; |
|
77 | + } |
|
78 | + |
|
79 | + foreach ($this->widget_rows as $widget_row) { |
|
80 | + $class_name = $widget_row['class_name']; |
|
81 | + $widget_instance = new $class_name(); |
|
82 | + |
|
83 | + if (!($widget_instance instanceof Widget)) { |
|
84 | + throw new IllegalStateException("instance has to be an instance of class Widget, this means widget types has to extends class Widget."); |
|
85 | + } |
|
86 | + |
|
87 | + //cast widget |
|
88 | + $widget = Widget::castWidget($widget_instance); |
|
89 | + |
|
90 | + //load widget and set row, so widget doesn't needs to load data seperate from database |
|
91 | + $widget->load($widget_row); |
|
92 | + |
|
93 | + //add widget to list |
|
94 | + $this->widgets[] = $widget; |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * list widget instances |
|
100 | + */ |
|
101 | + public function listWidgets () : array { |
|
102 | + return $this->widgets; |
|
103 | + } |
|
104 | + |
|
105 | + public function listWidgetTplArray () : array { |
|
106 | + $array = array(); |
|
107 | + |
|
108 | + foreach ($this->listWidgets() as $widget) { |
|
109 | + $widget = Widget::castWidget($widget); |
|
110 | + |
|
111 | + $array[] = array( |
|
112 | + 'id' => $widget->getId(), |
|
113 | + 'title' => $widget->getTitle(), |
|
114 | + 'code' => $widget->getCode(), |
|
115 | + 'css_id' => $widget->getCSSId(), |
|
116 | + 'css_class' => $widget->getCSSClass(), |
|
117 | + 'use_template' => $widget->useTemplate() |
|
118 | + ); |
|
119 | + } |
|
120 | + |
|
121 | + return $array; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @return int sidebar id |
|
126 | + */ |
|
127 | + public function getSidebarId(): int { |
|
128 | + return $this->sidebar_id; |
|
129 | + } |
|
130 | + |
|
131 | + public function getUniqueName () : string { |
|
132 | + return $this->row['unique_name']; |
|
133 | + } |
|
134 | + |
|
135 | + public function getTitle () : string { |
|
136 | + return $this->row['title']; |
|
137 | + } |
|
138 | + |
|
139 | + public function isDeletable () : bool { |
|
140 | + return $this->row['deletable'] == 1; |
|
141 | + } |
|
142 | + |
|
143 | + public function getRow () : array { |
|
144 | + return $this->row; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @return bool |
|
149 | + */ |
|
150 | + protected static function isInitialized(): bool { |
|
151 | + return self::$is_initialized; |
|
152 | + } |
|
153 | + |
|
154 | + protected static function initialize () { |
|
155 | + if (Cache::contains("sidebars", "all_sidebars")) { |
|
156 | + self::$all_sidebars = Cache::get("sidebars", "all_sidebars"); |
|
157 | + } else { |
|
158 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; "); |
|
159 | + |
|
160 | + //clear in-memory cache |
|
161 | + self::$all_sidebars = array(); |
|
162 | + |
|
163 | + foreach ($rows as $row) { |
|
164 | + self::$all_sidebars[$row['sidebar_id']] = $row; |
|
165 | + } |
|
166 | + |
|
167 | + //cache result |
|
168 | + Cache::put("sidebars", "all_sidebars", self::$all_sidebars); |
|
169 | + } |
|
170 | + |
|
171 | + self::$is_initialized = true; |
|
172 | + } |
|
173 | + |
|
174 | + public static function create (string $title, string $unique_name, bool $deletable = true) : int { |
|
175 | + if (empty($unique_name)) { |
|
176 | + throw new IllegalArgumentException("unique_name cannot be null."); |
|
177 | + } |
|
178 | + |
|
179 | + Database::getInstance()->execute("INSERT INTO `{praefix}sidebars` ( |
|
180 | 180 | `sidebar_id`, `unique_name`, `title`, `deletable` |
181 | 181 | ) VALUES ( |
182 | 182 | NULL, :unique_name, :title, :deletable |
183 | 183 | ) ON DUPLICATE KEY UPDATE `unique_name` = :unique_name, `title` = :title, `deletable` = :deletable; ", array( |
184 | - 'unique_name' => $unique_name, |
|
185 | - 'title' => $title, |
|
186 | - 'deletable' => ($deletable ? 1 : 0) |
|
187 | - )); |
|
184 | + 'unique_name' => $unique_name, |
|
185 | + 'title' => $title, |
|
186 | + 'deletable' => ($deletable ? 1 : 0) |
|
187 | + )); |
|
188 | 188 | |
189 | - //clear cache |
|
190 | - Cache::clear("sidebars"); |
|
189 | + //clear cache |
|
190 | + Cache::clear("sidebars"); |
|
191 | 191 | |
192 | - return Database::getInstance()->lastInsertId(); |
|
193 | - } |
|
192 | + return Database::getInstance()->lastInsertId(); |
|
193 | + } |
|
194 | 194 | |
195 | - public static function removeById (int $id) { |
|
196 | - Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array( |
|
197 | - 'sidebar_id' => $id |
|
198 | - )); |
|
195 | + public static function removeById (int $id) { |
|
196 | + Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array( |
|
197 | + 'sidebar_id' => $id |
|
198 | + )); |
|
199 | 199 | |
200 | - //clear cache |
|
201 | - Cache::clear("sidebars"); |
|
202 | - } |
|
200 | + //clear cache |
|
201 | + Cache::clear("sidebars"); |
|
202 | + } |
|
203 | 203 | |
204 | - public static function removeByUniqueName (string $unique_name) { |
|
205 | - if (empty($unique_name)) { |
|
206 | - throw new IllegalArgumentException("unique_name cannot be null."); |
|
207 | - } |
|
204 | + public static function removeByUniqueName (string $unique_name) { |
|
205 | + if (empty($unique_name)) { |
|
206 | + throw new IllegalArgumentException("unique_name cannot be null."); |
|
207 | + } |
|
208 | 208 | |
209 | - Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `unique_name` = :unique_name; ", array( |
|
210 | - 'unique_name' => $unique_name |
|
211 | - )); |
|
209 | + Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `unique_name` = :unique_name; ", array( |
|
210 | + 'unique_name' => $unique_name |
|
211 | + )); |
|
212 | 212 | |
213 | - //clear cache |
|
214 | - Cache::clear("sidebars"); |
|
215 | - } |
|
213 | + //clear cache |
|
214 | + Cache::clear("sidebars"); |
|
215 | + } |
|
216 | 216 | |
217 | - public static function listSidebars () : array { |
|
218 | - $rows = array(); |
|
217 | + public static function listSidebars () : array { |
|
218 | + $rows = array(); |
|
219 | 219 | |
220 | - if (Cache::contains("sidebars", "list")) { |
|
221 | - $rows = Cache::get("sidebars", "list"); |
|
222 | - } else { |
|
223 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; "); |
|
220 | + if (Cache::contains("sidebars", "list")) { |
|
221 | + $rows = Cache::get("sidebars", "list"); |
|
222 | + } else { |
|
223 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; "); |
|
224 | 224 | |
225 | - Cache::put("sidebars", "list", $rows); |
|
226 | - } |
|
225 | + Cache::put("sidebars", "list", $rows); |
|
226 | + } |
|
227 | 227 | |
228 | - $list = array(); |
|
228 | + $list = array(); |
|
229 | 229 | |
230 | - foreach ($rows as $row) { |
|
231 | - $obj = new Sidebar(); |
|
232 | - $obj->loadByArray($row); |
|
230 | + foreach ($rows as $row) { |
|
231 | + $obj = new Sidebar(); |
|
232 | + $obj->loadByArray($row); |
|
233 | 233 | |
234 | - $list[] = $obj; |
|
235 | - } |
|
234 | + $list[] = $obj; |
|
235 | + } |
|
236 | 236 | |
237 | - return $list; |
|
238 | - } |
|
237 | + return $list; |
|
238 | + } |
|
239 | 239 | |
240 | - public static function cast (Sidebar $sidebar) : Sidebar { |
|
241 | - return $sidebar; |
|
242 | - } |
|
240 | + public static function cast (Sidebar $sidebar) : Sidebar { |
|
241 | + return $sidebar; |
|
242 | + } |
|
243 | 243 | |
244 | 244 | } |
245 | 245 |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
46 | - public function load (int $sidebar_id) { |
|
46 | + public function load(int $sidebar_id) { |
|
47 | 47 | if (isset(self::$all_sidebars[$sidebar_id])) { |
48 | 48 | $this->row = self::$all_sidebars[$sidebar_id]; |
49 | 49 | } else { |
@@ -53,12 +53,12 @@ discard block |
||
53 | 53 | $this->sidebar_id = $sidebar_id; |
54 | 54 | } |
55 | 55 | |
56 | - public function loadByArray (array $row) { |
|
56 | + public function loadByArray(array $row) { |
|
57 | 57 | $this->row = $row; |
58 | 58 | $this->sidebar_id = (int) $row['sidebar_id']; |
59 | 59 | } |
60 | 60 | |
61 | - public function loadWidgets () { |
|
61 | + public function loadWidgets() { |
|
62 | 62 | $this->widgets = array(); |
63 | 63 | |
64 | 64 | //load widgets |
@@ -98,11 +98,11 @@ discard block |
||
98 | 98 | /** |
99 | 99 | * list widget instances |
100 | 100 | */ |
101 | - public function listWidgets () : array { |
|
101 | + public function listWidgets() : array { |
|
102 | 102 | return $this->widgets; |
103 | 103 | } |
104 | 104 | |
105 | - public function listWidgetTplArray () : array { |
|
105 | + public function listWidgetTplArray() : array { |
|
106 | 106 | $array = array(); |
107 | 107 | |
108 | 108 | foreach ($this->listWidgets() as $widget) { |
@@ -128,19 +128,19 @@ discard block |
||
128 | 128 | return $this->sidebar_id; |
129 | 129 | } |
130 | 130 | |
131 | - public function getUniqueName () : string { |
|
131 | + public function getUniqueName() : string { |
|
132 | 132 | return $this->row['unique_name']; |
133 | 133 | } |
134 | 134 | |
135 | - public function getTitle () : string { |
|
135 | + public function getTitle() : string { |
|
136 | 136 | return $this->row['title']; |
137 | 137 | } |
138 | 138 | |
139 | - public function isDeletable () : bool { |
|
139 | + public function isDeletable() : bool { |
|
140 | 140 | return $this->row['deletable'] == 1; |
141 | 141 | } |
142 | 142 | |
143 | - public function getRow () : array { |
|
143 | + public function getRow() : array { |
|
144 | 144 | return $this->row; |
145 | 145 | } |
146 | 146 | |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | return self::$is_initialized; |
152 | 152 | } |
153 | 153 | |
154 | - protected static function initialize () { |
|
154 | + protected static function initialize() { |
|
155 | 155 | if (Cache::contains("sidebars", "all_sidebars")) { |
156 | 156 | self::$all_sidebars = Cache::get("sidebars", "all_sidebars"); |
157 | 157 | } else { |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | self::$is_initialized = true; |
172 | 172 | } |
173 | 173 | |
174 | - public static function create (string $title, string $unique_name, bool $deletable = true) : int { |
|
174 | + public static function create(string $title, string $unique_name, bool $deletable = true) : int { |
|
175 | 175 | if (empty($unique_name)) { |
176 | 176 | throw new IllegalArgumentException("unique_name cannot be null."); |
177 | 177 | } |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | return Database::getInstance()->lastInsertId(); |
193 | 193 | } |
194 | 194 | |
195 | - public static function removeById (int $id) { |
|
195 | + public static function removeById(int $id) { |
|
196 | 196 | Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array( |
197 | 197 | 'sidebar_id' => $id |
198 | 198 | )); |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | Cache::clear("sidebars"); |
202 | 202 | } |
203 | 203 | |
204 | - public static function removeByUniqueName (string $unique_name) { |
|
204 | + public static function removeByUniqueName(string $unique_name) { |
|
205 | 205 | if (empty($unique_name)) { |
206 | 206 | throw new IllegalArgumentException("unique_name cannot be null."); |
207 | 207 | } |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | Cache::clear("sidebars"); |
215 | 215 | } |
216 | 216 | |
217 | - public static function listSidebars () : array { |
|
217 | + public static function listSidebars() : array { |
|
218 | 218 | $rows = array(); |
219 | 219 | |
220 | 220 | if (Cache::contains("sidebars", "list")) { |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | return $list; |
238 | 238 | } |
239 | 239 | |
240 | - public static function cast (Sidebar $sidebar) : Sidebar { |
|
240 | + public static function cast(Sidebar $sidebar) : Sidebar { |
|
241 | 241 | return $sidebar; |
242 | 242 | } |
243 | 243 |
@@ -27,31 +27,31 @@ |
||
27 | 27 | |
28 | 28 | class DataType_SelectBox extends DataType_Base { |
29 | 29 | |
30 | - public function getFormCode(): string { |
|
31 | - $code = "<select name=\"" . $this->getInputName() . "\" class=\"form-control select2\" style=\"width: 100%;\">"; |
|
32 | - |
|
33 | - foreach ($this->getDatatypeParams() as $option) { |
|
34 | - $code .= "<option" . ($option === $this->getValue() ? " selected=\"selected\"" : "") . ">" . $option . "</option>"; |
|
35 | - } |
|
36 | - |
|
37 | - $code .= "</select>"; |
|
38 | - return $code; |
|
39 | - } |
|
40 | - |
|
41 | - public function validate(string $value): bool { |
|
42 | - foreach ($this->getDatatypeParams() as $option) { |
|
43 | - if ($option === $value) { |
|
44 | - //option exists |
|
45 | - return true; |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - return false; |
|
50 | - } |
|
51 | - |
|
52 | - protected function saveAsync($value) { |
|
53 | - Settings::setAsync($this->getKey(), (string) $value); |
|
54 | - } |
|
30 | + public function getFormCode(): string { |
|
31 | + $code = "<select name=\"" . $this->getInputName() . "\" class=\"form-control select2\" style=\"width: 100%;\">"; |
|
32 | + |
|
33 | + foreach ($this->getDatatypeParams() as $option) { |
|
34 | + $code .= "<option" . ($option === $this->getValue() ? " selected=\"selected\"" : "") . ">" . $option . "</option>"; |
|
35 | + } |
|
36 | + |
|
37 | + $code .= "</select>"; |
|
38 | + return $code; |
|
39 | + } |
|
40 | + |
|
41 | + public function validate(string $value): bool { |
|
42 | + foreach ($this->getDatatypeParams() as $option) { |
|
43 | + if ($option === $value) { |
|
44 | + //option exists |
|
45 | + return true; |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + return false; |
|
50 | + } |
|
51 | + |
|
52 | + protected function saveAsync($value) { |
|
53 | + Settings::setAsync($this->getKey(), (string) $value); |
|
54 | + } |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | ?> |
@@ -27,19 +27,19 @@ |
||
27 | 27 | |
28 | 28 | class DataType_Hidden extends DataType_Base { |
29 | 29 | |
30 | - public function getFormCode(): string { |
|
31 | - //dont show hidden value |
|
32 | - return ""; |
|
33 | - } |
|
34 | - |
|
35 | - public function validate(string $value): bool { |
|
36 | - //value cannot be changed |
|
37 | - return true; |
|
38 | - } |
|
39 | - |
|
40 | - protected function saveAsync($value) { |
|
41 | - //dont do anything, because value cannot be overriden |
|
42 | - } |
|
30 | + public function getFormCode(): string { |
|
31 | + //dont show hidden value |
|
32 | + return ""; |
|
33 | + } |
|
34 | + |
|
35 | + public function validate(string $value): bool { |
|
36 | + //value cannot be changed |
|
37 | + return true; |
|
38 | + } |
|
39 | + |
|
40 | + protected function saveAsync($value) { |
|
41 | + //dont do anything, because value cannot be overriden |
|
42 | + } |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | ?> |
@@ -27,17 +27,17 @@ |
||
27 | 27 | |
28 | 28 | class DataType_HTML extends DataType_Base { |
29 | 29 | |
30 | - public function getFormCode(): string { |
|
31 | - return "<textarea name=\"" . $this->getInputName() . "\" rows=\"4\" cols=\"50\">" . $this->getValue() . "</textarea>"; |
|
32 | - } |
|
30 | + public function getFormCode(): string { |
|
31 | + return "<textarea name=\"" . $this->getInputName() . "\" rows=\"4\" cols=\"50\">" . $this->getValue() . "</textarea>"; |
|
32 | + } |
|
33 | 33 | |
34 | - public function validate(string $value): bool { |
|
35 | - return true; |
|
36 | - } |
|
34 | + public function validate(string $value): bool { |
|
35 | + return true; |
|
36 | + } |
|
37 | 37 | |
38 | - protected function saveAsync($value) { |
|
39 | - Settings::setAsync($this->getKey(), (string) $value); |
|
40 | - } |
|
38 | + protected function saveAsync($value) { |
|
39 | + Settings::setAsync($this->getKey(), (string) $value); |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | ?> |