LEPTON CMS 7.2.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
lepton_admin.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27class LEPTON_admin extends LEPTON_core
28{
29
36 private LEPTON_database $database;
37
44 public array $header_storage = [
45 'css' => [],
46 'js' => [],
47 'html' => [],
48 'modules' => []
49 ];
50
57 private string $html_output_storage = "";
58
65 private bool $droplets_ok = false;
66
67 private array $adminTools = [];
68
69 protected string $section_name = "";
70 protected string $section_permission = "";
71
77 public ?lib_twig_box $oTWIG = null;
78
83 public static $instance;
84
96 #[\Override]
97 public static function getInstance(): object
98 {
99 if (null === static::$instance)
100 {
101 $section_name = "Pages";
102 $section_permission = "start";
103 $auto_header = true;
104 $auto_auth = true;
105
106 switch( func_num_args() )
107 {
108 case 1:
109 $section_name = func_get_arg(0);
110 break;
111 case 2:
112 $section_name = func_get_arg(0);
113 $section_permission = func_get_arg(1);
114 break;
115 case 3:
116 $section_name = func_get_arg(0);
117 $section_permission = func_get_arg(1);
118 $auto_header = func_get_arg(2);
119 break;
120 case 4:
121 $section_name = func_get_arg(0);
122 $section_permission = func_get_arg(1);
123 $auto_header = func_get_arg(2);
124 $auto_auth = func_get_arg(3);
125 break;
126 default:
127 // nothing
128 break;
129 }
130 static::$instance = new static($section_name, $section_permission, $auto_header, $auto_auth);
131 }
132 return static::$instance;
133 }
134
146 public function __construct(
147 string $section_name = "Pages",
148 string $section_permission = 'start',
149 bool $auto_header = true,
150 bool $auto_auth = true
151 )
152 {
153 global $database, $MESSAGE, $section_id, $page_id;
154
155 parent::__construct();
156
157 static::$instance = $this;
158
159 $section_id = (isset ($_POST['section_id']) ? intval($_POST['section_id']) : 0);
160 if ($section_id == 0 )
161 {
162 $section_id = (isset ($_GET['section_id'])? intval($_GET['section_id']): 0);
163 }
164
165 $page_id = (isset ($_POST['page_id']) ? intval($_POST['page_id']) : 0);
166 if ($page_id == 0 )
167 {
168 $page_id = (isset ($_GET['page_id']) ? intval($_GET['page_id']) : 0);
169 }
170
174 if (is_null($this->oTWIG))
175 {
176 $this->oTWIG = lib_twig_box::getInstance();
177 $this->oTWIG->loader->prependPath( THEME_PATH."/templates/", "theme" );
178 }
179
184 if (true === $auto_auth)
185 {
186 ob_start();
187 }
188
189 $this->database = LEPTON_database::getInstance();
190
191 // Specify the current applications name
192 $this->section_name = $section_name;
193 $this->section_permission = $section_permission;
194 // Authenticate the user for this application
195 if ($auto_auth === true)
196 {
197 // [a1] First check if the user is logged-in
198 if ($this->is_authenticated() === false)
199 {
200 header('Location:' . ADMIN_URL . '/login/index.php');
201 exit();
202 }
203
204 // [a2] Now check whether he has a valid token
205 if (!$this->checkLepToken())
206 {
207 $pin_set = $this->database->get_one("SELECT `pin_set` FROM `".TABLE_PREFIX."users` WHERE `user_id` = '".$_SESSION['USER_ID']."' ");
208 if ($pin_set == 2)
209 {
210 $this->database->simple_query("UPDATE `" . TABLE_PREFIX . "users` SET `pin_set` = 1 WHERE user_id = '" . $_SESSION['USER_ID'] . "' ");
211 }
212 unset($_SESSION['USER_ID']);
213 header('Location:' . ADMIN_URL . '/login/index.php');
214 exit();
215 }
216
217 // [a3] Now check if they are allowed in this section
218 if ($this->get_permission($section_permission) === false)
219 {
220 if ($section_permission === "admintools")
221 {
222 if (false == $this->userHasAdminToolPermission())
223 {
224 die($MESSAGE['ADMIN_INSUFFICIENT_PRIVILEGES']." [007-002]");
225 }
226
227 } else {
228 die($MESSAGE['ADMIN_INSUFFICIENT_PRIVILEGES']." [007-001]");
229 }
230 }
231 }
232
233 // Check if the backend language is also the selected language. If not, send headers again.
234 $user_language = [];
235 $this->database->execute_query(
236 "SELECT `language` FROM `" . TABLE_PREFIX . "users` WHERE `user_id` = '" . (int) $this->getValue('user_id', 'integer', 'session') . "'",
237 true,
238 $user_language,
239 false
240 );
241 // prevent infinite loop if language file is not XX.php (e.g. DE_de.php)
242 $user_language = (!isset($user_language['language']))
243 ? ""
244 : substr($user_language['language'], 0,2)
245 ;
246
247 // obtain the admin folder (e.g. /admin)
248 $admin_folder = str_replace(LEPTON_PATH, '', ADMIN_PATH);
249 if ((LANGUAGE != $user_language) && file_exists(LEPTON_PATH . '/languages/' . $user_language . '.php') && strpos($_SERVER['SCRIPT_NAME'], $admin_folder . '/') !== false)
250 {
251 // check if page_id is set
252 $page_id_url = (isset($_GET['page_id'])) ? '&page_id=' . (int) $_GET['page_id'] : '';
253 $section_id_url = (isset($_GET['section_id'])) ? '&section_id=' . (int) $_GET['section_id'] : '';
254 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') // check if there is an query-string
255 {
256 header('Location: ' . $_SERVER['SCRIPT_NAME'] . '?lang=' . $user_language . $page_id_url . $section_id_url . '&' . $_SERVER['QUERY_STRING']);
257 }
258 else
259 {
260 header('Location: ' . $_SERVER['SCRIPT_NAME'] . '?lang=' . $user_language . $page_id_url . $section_id_url);
261 }
262 exit();
263 }
264
265 // [a2.1] Auto header code
266 if ($auto_header === true)
267 {
268 $this->print_header();
269 }
270
271 // @ADD_cronjob 20230727, include cronjob file for external call
272 if(CRONJOB == 2 || CRONJOB == 3)
273 {
274 $_POST['ikey'] = LEPTON_cronjob::getInstance()->cj_key;
275 LEPTON_handle::include_files("/modules/cronjob.php");
276 }
277 }
278
286 public function get_permission(string $name, string $type = 'system'): bool
287 {
288 // [p.1] Append to permission type
289 $type .= '_permissions';
290 // [p.2] Check if we have a section to check for
291 if ($name === 'start')
292 {
293 return true;
294 }
295 else
296 {
297 if (true === self::userHasAdminRights())
298 {
299 return true;
300 }
301
302 $aTemp = match (strtolower($type))
303 {
304 "system_permissions" => ($this->getValue('system_permissions', 'string', 'session') ?? []),
305 "module_permissions" => ($this->getValue('module_permissions', 'string', 'session') ?? []),
306 default => []
307 };
308 return in_array($name, $aTemp);
309 }
310 }
311
318 public static function get_user_details(int $user_id): array
319 {
320 $user = [];
321 LEPTON_database::getInstance()->execute_query(
322 "SELECT `username`,`display_name` FROM `".TABLE_PREFIX."users` WHERE `user_id` = ".$user_id,
323 true,
324 $user,
325 false
326 );
327
328 if (empty($user))
329 {
330 $user['display_name'] = 'Unknown';
331 $user['username'] = 'unknown';
332 }
333 return $user;
334 }
335
343 public function get_page_details(int $page_id): array
344 {
345 $aResults = [];
346 $this->database->execute_query(
347 "SELECT * from ".TABLE_PREFIX."pages WHERE page_id = ".$page_id,
348 true,
349 $aResults,
350 false
351 );
352
353 if (empty($aResults))
354 {
355 $this->print_header();
356 $this->print_error($GLOBALS['MESSAGE']['PAGES_NOT_FOUND']);
357 }
358 return $aResults;
359 }
360
371 public function get_page_permission(int $page_id, string $action = 'admin'): bool
372 {
373 $action_groups = $action.'_groups';
374
375 $sGroups = $this->database->get_one("SELECT ".$action_groups." FROM ".TABLE_PREFIX."pages WHERE page_id = ".$page_id);
376
377 $aGroups = explode(',',$sGroups);
378
379 $aUserPermissions = $this->getValue('groups_id', 'string', 'session',',');
380
381 $in_group = !empty(array_intersect($aGroups,$aUserPermissions));
382
383 return $in_group;
384 }
385
394 public function get_link_permission(string $title): bool
395 {
396 if (true === self::userHasAdminRights())
397 {
398 return true;
399 }
400
401 $titleLower = strtolower(str_replace('_blank', '', $title));
402
403 // Set system permissions var
404 $system_permissions = $this->getValue('system_permissions', 'string_clean', 'session');
405
406 // Return true if system perm = 1
407 return (is_numeric(array_search($titleLower, $system_permissions)));
408 }
415 public function getGroupsPermissions(string $what): bool
416 {
417 if (self::userHasAdminRights())
418 {
419 return true;
420 }
421
422 $lookFor = strtolower($what);
423
424 switch ($lookFor)
425 {
426 case '*':
427 $terms = ["groups", "groups_view", "groups_add", "groups_modify", "groups_delete"];
428 break;
429
430 case 'view':
431 case 'add':
432 case 'modify':
433 case 'delete':
434 $terms = ["groups_".$lookFor];
435 break;
436
437 default:
438 echo LEPTON_tools::display_dev("[1023] No valid group key!", "pre", "ui message red");
439 $terms = [];
440 break;
441 }
442 return !empty(array_intersect($terms, $_SESSION['SYSTEM_PERMISSIONS']));
443 }
444
452 public function getUsersPermissions(string $what): bool
453 {
454 if (self::userHasAdminRights())
455 {
456 return true;
457 }
458
459 $lookFor = strtolower($what);
460
461 switch ($lookFor)
462 {
463 case '*':
464 $terms = ["users", "users_view", "users_add", "users_modify", "users_delete"];
465 break;
466
467 case 'view':
468 case 'add':
469 case 'modify':
470 case 'delete':
471 $terms = ["users_".$lookFor];
472 break;
473
474 default:
475 echo LEPTON_tools::display_dev("[1024] No valid users key!", "pre", "ui message red");
476 $terms = [];
477 break;
478 }
479 return !empty(array_intersect($terms, $_SESSION['SYSTEM_PERMISSIONS']));
480 }
481
489 public function getPagesPermissions(string $what): bool
490 {
491 if (self::userHasAdminRights())
492 {
493 return true;
494 }
495
496 $lookFor = strtolower($what);
497
498 switch ($lookFor)
499 {
500 case '*':
501 $terms = ["pages", "pages_settings", "pages_view", "pages_add", "pages_modify", "pages_delete"];
502 break;
503
504 case 'view':
505 case 'add':
506 case 'modify':
507 case 'settings':
508 case 'delete':
509 $terms = ["pages_".$lookFor];
510 break;
511
512 default:
513 echo LEPTON_tools::display_dev("[1025] No valid pages key!", "pre", "ui message red");
514 $terms = [];
515 break;
516 }
517 return !empty(array_intersect($terms, $_SESSION['SYSTEM_PERMISSIONS']));
518 }
519
524 public function print_header(): void
525 {
526 LEPTON_handle::register("get_page_headers");
527 // Get vars from the language file
528 global $MENU;
529 global $MESSAGE;
530 global $TEXT;
531
532 // Get website title
533 $title = $this->database->get_one("SELECT `value` FROM `".TABLE_PREFIX."settings` WHERE `name`='website_title'");
534
535 $charset = (true === defined('DEFAULT_CHARSET')) ? DEFAULT_CHARSET : 'utf-8';
536
537 // Work out the URL for the 'View menu' link in the WB backend
538 // if the page_id is set, show this page otherwise show the root directory of WB
539 $view_url = LEPTON_URL;
540 if (isset($_GET['page_id']))
541 {
542 // Extract page link from the database
543 $result = $this->database->get_one("SELECT `link` FROM `" . TABLE_PREFIX . "pages` WHERE `page_id`= '" . (int) addslashes($_GET['page_id']) . "'");
544 if (!is_null($result))
545 {
546 $view_url .= PAGES_DIRECTORY.$result.PAGE_EXTENSION;
547 }
548 }
549
554 $backend_theme_version = "";
555 if (defined('DEFAULT_THEME'))
556 {
557 $backend_theme_version = $this->database->get_one("SELECT `version` from `" . TABLE_PREFIX . "addons` where `directory`='" . DEFAULT_THEME . "'");
558 }
559
560 $header_vars = [
561 'SECTION_NAME' => $MENU[strtoupper($this->section_name)],
562 'WEBSITE_TITLE' => $title,
563 'BACKEND_TITLE' => BACKEND_TITLE,
564 'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
565 'CURRENT_USER' => $MESSAGE['START_CURRENT_USER'],
566 'DISPLAY_NAME' => $this->getValue('display_name', 'string', 'session'),
567 'CHARSET' => $charset,
568 'LANGUAGE' => strtolower(LANGUAGE),
569 'LEPTON_VERSION' => LEPTON_VERSION,
570 'SUBVERSION' => SUBVERSION,
571 'LEPTON_URL' => LEPTON_URL,
572 'ADMIN_URL' => ADMIN_URL,
573 'THEME_URL' => THEME_URL,
574 'TITLE_START' => $MENU['START'],
575 'TITLE_VIEW' => $MENU['VIEW'],
576 'TITLE_HELP' => $MENU['HELP'],
577 'TITLE_LOGOUT' => $MENU['LOGOUT'],
578// additional marker links/text in semantic BE-header
579 //'PAGES' => $MENU['PAGES'],
580 //'MEDIA' => $MENU['MEDIA'],
581 //'ADDONS' => $MENU['ADDONS'],
582 //'PREFERENCES' => $MENU['PREFERENCES'],
583 //'SETTINGS' => $MENU['SETTINGS'],
584 //'ADMINTOOLS' => $MENU['ADMINTOOLS'],
585 //'ACCESS' => $MENU['ACCESS'],
586// end additional marks
587 'URL_VIEW' => $view_url,
588 'URL_HELP' => ' https://lepton-cms.org/',
589 'GET_PAGE_HEADERS' => get_page_headers('backend', false),
590 'THEME_VERSION' => $backend_theme_version,
591 'THEME_NAME' => DEFAULT_THEME,
592 // permissions
593 'p_pages' => $this->get_link_permission('pages'),
594 'p_pages_settings' => $this->getUserPermission('pages_settings'), // 1
595 'p_pages_add' => $this->getUserPermission('pages_add'), // 2
596
597 'p_media' => $this->get_link_permission('media'),
598 'p_addons' => $this->get_link_permission('addons'),
599 'p_preferences' => $this->getUserPermission('preferences'), // 1
600 'p_settings' => $this->get_link_permission('settings'),
601 'p_admintools' => $this->userHasAdminToolPermission(), // $this->get_link_permission('admintools'),
602 'p_access' => $this->get_link_permission('access'),
603 // -- [groups]
604 'p_groups' => $this->getGroupsPermissions("*"),
605 'p_groups_view' => $this->getGroupsPermissions("view"),
606 'p_groups_add' => $this->getGroupsPermissions("add"),
607 'p_groups_moddify' => $this->getGroupsPermissions("modify"),
608 'p_groups_delete' => $this->getGroupsPermissions("delete"),
609 // -- [users]
610 'p_users' => $this->getUsersPermissions("*"),
611 'p_users_view' => $this->getUsersPermissions("view"),
612 'p_users_add' => $this->getUsersPermissions("add"),
613 'p_users_moddify' => $this->getUsersPermissions("modify"),
614 'p_users_delete' => $this->getUsersPermissions("delete")
615 ];
616
617 echo $this->oTWIG->render(
618 '@theme/header.lte',
619 $header_vars
620 );
621 }
622
629 public function print_footer(): void
630 {
631 LEPTON_handle::register("get_page_footers");
632 $footer_vars = [
633 'GET_PAGE_FOOTERS' => get_page_footers('backend'),
634// 'LEPTON_URL' => LEPTON_URL,
635// 'LEPTON_PATH' => LEPTON_PATH,
636// 'ADMIN_URL' => ADMIN_URL,
637// 'THEME_URL' => THEME_URL
638 ];
639
640 echo $this->oTWIG->render(
641 "@theme/footer.lte",
642 $footer_vars
643 );
644
653 $this->html_output_storage = ob_get_clean();
654 if (true === $this->droplets_ok)
655 {
656 evalDroplets($this->html_output_storage);
657 }
658
659 // CSRF protection - add tokens to internal links
660 if ($this->is_authenticated() )
661 {
662 LEPTON_core::getInstance()->getProtectedFunctions($this->html_output_storage, $this);
663 }
664
665 echo $this->html_output_storage;
666 }
667
676 public function print_success(string|array $message, string $redirect = 'index.php', bool $auto_footer = true): void
677 {
678 global $TEXT;
679 global $section_id;
680
682
683 if (true === is_array($message))
684 {
685 $message = implode("<br />", $message);
686 }
687
688 // add template variables
689 $page_vars = [
690 'NEXT' => $TEXT['NEXT'],
691 'BACK' => $TEXT['BACK'],
692 'MESSAGE' => $message,
693 'THEME_URL' => THEME_URL,
694 'REDIRECT' => $redirect,
695 'REDIRECT_TIMER' => REDIRECT_TIMER
696 ];
697
698 echo $this->oTWIG->render(
699 '@theme/success.lte',
700 $page_vars
701 );
702
703 if (true === $auto_footer)
704 {
705 $this->print_footer();
706 }
707 exit();
708 }
709
718 public function print_error(string|array $message, string $link = 'index.php', bool $auto_footer = true): void
719 {
720 global $TEXT;
721
723
724 if (true === is_array($message))
725 {
726 $message = implode("<br />", $message);
727 }
728
729 $page_vars = [
730 'MESSAGE' => $message,
731 'LINK' => $link,
732 'BACK' => $TEXT['BACK'],
733 'THEME_URL' => THEME_URL
734 ];
735
736 echo $this->oTWIG->render(
737 '@theme/error.lte',
738 $page_vars
739 );
740
741 if (true === $auto_footer && method_exists($this, "print_footer"))
742 {
743 $this->print_footer();
744 }
745 exit();
746 }
747
755 static public function getUserPermission(string $sPermissionName = "" ): bool
756 {
757 if (self::userHasAdminRights())
758 {
759 return true;
760 }
761
762 if (!isset($_SESSION['SYSTEM_PERMISSIONS']))
763 {
764 return false;
765 }
766
767 return (in_array($sPermissionName, $_SESSION['SYSTEM_PERMISSIONS']));
768 }
769
773 public function getHeaderStorage(): array
774 {
776 }
777
778 public function resetObject()
779 {
780 static::$instance = null;
781 return self::getInstance();
782 }
783
788 public function userHasAdminToolPermission(): bool
789 {
790 if (LEPTON_core::userHasAdminRights() == true)
791 {
792 return true;
793 }
794
795 if (empty($this->adminTools))
796 {
797 $aAllAdminToolsStorage = [];
798 $this->database->execute_query(
799 "SELECT `directory` FROM `".TABLE_PREFIX."addons` WHERE `function` = 'tool'",
800 true,
801 $aAllAdminToolsStorage,
802 true
803 );
804
805 foreach ($aAllAdminToolsStorage as $tempTool)
806 {
807 $this->adminTools[] = $tempTool['directory'];
808 }
809 }
810 // Keep in mind that module_permissions is an array!
811 $aUserModules = LEPTON_core::getValue("module_permissions", "array", "session");
812
813 return !empty(array_intersect($aUserModules, $this->adminTools));
814 }
815}
static saveLastEditSection(int $iSectionID=0)
get_permission(string $name, string $type='system')
get_page_permission(int $page_id, string $action='admin')
array $header_storage
__construct(string $section_name="Pages", string $section_permission='start', bool $auto_header=true, bool $auto_auth=true)
string $section_permission
print_error(string|array $message, string $link='index.php', bool $auto_footer=true)
getPagesPermissions(string $what)
static getUserPermission(string $sPermissionName="")
get_link_permission(string $title)
static get_user_details(int $user_id)
getUsersPermissions(string $what)
print_success(string|array $message, string $redirect='index.php', bool $auto_footer=true)
getGroupsPermissions(string $what)
string $section_name
static getInstance()
lib_twig_box $oTWIG
get_page_details(int $page_id)
static getInstance(array &$settings=[])
static include_files(array|string $file_names=[], bool $interrupt=true)
static display_dev(mixed $something_to_display="", string $tag="pre", string|null $css_class=null, bool|null $useVarDump=null)
const THEME_PATH
$database
Definition constants.php:52
get_page_footers(string $for='frontend')
get_page_headers(string $for='frontend', bool $print_output=true, bool $individual=false)