3declare(strict_types=1);
25 public static $instance;
27 public string $string_secure =
'a-zA-Z0-9\-_\.';
29 public string $password_chars =
'a-zA-Z0-9\_\-\!\$\ย\ง\#\*\+';
31 public string $email_chars =
'a-zA-Z0-9@\_\-\.';
33 public string $username_chars =
'a-zA-Z0-9@ \_\-,\.';
35 public string $hex_chars =
'a-fA-F0-9\-';
37 public static array $HEADERS = [
48 public static array $FOOTERS = [
59 public static bool $bUserGotAdminRights =
false;
64 public function __construct()
77 public function page_is_visible( $page ): bool
82 switch ($page[
'visibility'])
91 if ($this->is_authenticated() ===
true)
93 $show_it = ($this->is_group_match($this->getValue(
'groups_id',
'string',
'session',
','), $page[
'viewing_groups']) );
114 public function section_is_active(
int $section_id): bool
119 FROM `" . TABLE_PREFIX .
"sections`
120 WHERE (" . $now .
" BETWEEN `publ_start` AND `publ_end`)
121 OR (" . $now .
" > `publ_start` AND `publ_end`=0)
122 AND `section_id`=" . $section_id;
134 public function page_is_active(array $page ): bool
139 FROM `".TABLE_PREFIX.
"sections`
140 WHERE `page_id` = ".$page[
'page_id'].
"
141 AND (".$now.
" BETWEEN `publ_start` AND `publ_end` OR (".$now.
" > `publ_start` AND `publ_end`= 0))
153 public function show_page(array|
null $page ): bool
155 if (!is_array($page))
159 "SELECT `page_id`, `visibility` FROM `" . TABLE_PREFIX .
"pages` WHERE `page_id`=" . (
int) $page,
170 return ($this->page_is_visible($page) && $this->page_is_active($page));
174 public function is_authenticated(): bool
176 return ( (isset( $_SESSION[
'USER_ID' ] ) )
177 && ($_SESSION[
'USER_ID' ] !=
"")
178 && (is_numeric( $_SESSION[
'USER_ID' ] ) )
189 public function buildPageLink(
string $link): string
192 if (strstr($link,
'://') ==
'' && !str_starts_with($link,
'mailto:'))
194 return LEPTON_URL . PAGES_DIRECTORY . $link . PAGE_EXTENSION;
229 public static function getValue(
231 string $type =
"string_clean",
232 string $requestFrom =
"post",
233 string $dividerString =
"X",
234 string|
int|array|
null $default =
null,
235 string|
int|array|
null $range =
null
236 ): string|int|array|null
239 if ($requestFrom ==
'session')
241 $lookForName = strtoupper($lookForName);
244 LEPTON_request::getInstance()->setStrictLookInside($requestFrom);
245 $aField = [ $lookForName => [
247 'default' => $default,
251 $sTested = LEPTON_request::getInstance()->testValues($aField);
254 if ((is_null($sTested[$lookForName]) || empty($sTested[$lookForName])) && (!empty($default)))
256 $sTested[$lookForName] = $default;
259 if ($dividerString !=
'X')
261 if (!is_array($sTested[$lookForName]))
263 $sTested[$lookForName] = explode($dividerString, $sTested[$lookForName] ??
"");
265 foreach ($sTested[$lookForName] as &$item)
267 $item = explode($dividerString, $item ??
"");
271 return $sTested[$lookForName];
282 public function is_group_match(
int|
string|array $groups_list1 =
'',
int|
string|array $groups_list2 =
''): bool
284 if ($groups_list1 ==
'')
288 if ($groups_list2 ==
'')
292 if (!is_array($groups_list1))
294 $groups_list1 = explode(
',', $groups_list1 );
296 if (!is_array($groups_list2))
298 $groups_list2 = explode(
',', $groups_list2 );
301 return (
sizeof(array_intersect($groups_list1, $groups_list2)) != 0);
312 public static function addItems(
string $for =
'frontend',
string $path = LEPTON_PATH,
bool $footer =
false): void
314 $trail = explode(
'/', $path);
315 $subdir = array_pop($trail);
322 $add_to = &self::$FOOTERS;
323 $to_load =
'footers.inc.php';
327 $add_to = &self::$HEADERS;
328 $to_load =
'headers.inc.php';
331 require $path.
'/'.$to_load;
333 if (
true === $footer)
335 $aRefArray = &$mod_footers;
339 $aRefArray = &$mod_headers;
342 if (count($aRefArray))
344 foreach ([
'css',
'js'] as $key)
346 if (!isset($aRefArray[$for][$key]))
350 foreach ($aRefArray[$for][$key] as &$item)
353 if ((isset($item[
'file']))
354 && (!preg_match(
"#/$subdir/#", $item[
'file']))
355 && (file_exists($path.
'/'.$item[
'file']))
359 $item[
'file'] = str_ireplace(LEPTON_PATH,
'', $path).
'/'.$item[
'file'];
363 if ($key ===
"css") {
364 foreach ($add_to[$for][$key] as $temp_ref)
366 if ($temp_ref[
'file'] == $item[
'file'])
371 } elseif ($key ===
"js")
373 foreach ($add_to[$for][$key] as $temp_ref)
375 if ($item === $temp_ref)
385 $add_to[$for][$key][] = $item;
391 if ($footer && file_exists($path.$for.
'_body.js'))
393 $add_to[$for][
'js'][] =
'/modules/'.$subdir.
'_body.js';
403 public function getProtectedFunctions(
string &$html,
object &$oReference): void
405 $this->addLepToken($html, $oReference);
412 protected function addLepToken(
string &$html,
object &$oReference): void
417 if (!LEPTOKEN_LIFETIME)
422 $token = $oReference->createLepToken();
424 $token1 =
"$1?leptoken=".$token.
"$3";
425 $token2 =
"leptoken=".$token;
426 $token3 =
"$1&leptoken=".$token.
"$3";
427 $token4 =
"$1?leptoken=".$token.
"$2";
428 $hiddentoken =
"$1\n<span><input type='hidden' name='leptoken' value='".$token.
"' /></span>\n";
431 $qs =
'~((href|action|window\.location)\s?=\s?[\'"]' . LEPTON_URL .
'[\w\-\./]+\.php\?[\w\-\.=&%;/]+)([#[\w]*]?[\'"])~';
432 $html = preg_replace( $qs, $token3, $html, -1 );
435 $qs =
'~((href|action|ajaxfilemanagerurl|window\.location)\s?=\s?[\'"]' . LEPTON_URL .
'[\w\-\./]+\.php)([#[\w]*]?[\'"])~';
436 $html = preg_replace( $qs, $token1, $html, -1 );
439 $qs =
'~((href|action|window\.location)\s?=\s?[\'"][\w/]+\.php\?[\w\-\.=%&;/]+)([#[\w]*]?[\'"])~';
440 $html = preg_replace( $qs, $token3, $html, -1 );
443 $qs =
'~((href|action|window\.location)\s?=\s?[\'"][\w/]+\.php)([#[\w]*]?[\'"])~';
444 $html = preg_replace( $qs, $token1, $html, -1 );
447 $qs =
'~(href\s?=\s?[\'"]' . LEPTON_URL .
')([\'"])~';
448 $html = preg_replace( $qs, $token4, $html, -1 );
451 $qs =
'~(send_testmail\(\'' . ADMIN_URL .
'/settings/ajax_testmail\.php)(\'\))~';
452 $html = preg_replace( $qs, $token4, $html, -1 );
455 $qs =
'~(<form\s+action=[\'"][\w:\.\?/]+leptoken=\w{32}[\'"]\s+method=[\'"]get[\'"]\s*>)~';
456 $html = preg_replace( $qs, $hiddentoken, $html, -1 );
459 $qs =
'~leptokh=#-!leptoken-!#~';
460 $html = preg_replace( $qs, $token2, $html, -1 );
471 static public function make_dir(
string $dir_name,
string|
null $dir_mode = NULL): bool
473 if ($dir_mode == NULL)
475 $dir_mode = (int) octdec( STRING_DIR_MODE );
478 if (!is_dir($dir_name))
481 $result = mkdir($dir_name, $dir_mode,
true);
501 static public function change_mode(
string $name ): bool
503 if (OPERATING_SYSTEM !=
'windows')
505 $mode = (is_dir($name)) ? (
int)octdec(STRING_DIR_MODE) : (int)octdec(STRING_FILE_MODE);
506 if (file_exists($name))
532 static public function create_access_file(
string $filename,
int $page_id): bool
534 global $admin, $MESSAGE;
535 $pages_path = LEPTON_PATH . PAGES_DIRECTORY;
536 $rel_pages_dir = str_replace($pages_path,
'', dirname($filename));
537 $rel_filename = str_replace($pages_path,
'', $filename);
540 if (PAGES_DIRECTORY ==
'')
558 $search = explode(
'/', $rel_filename);
560 $denied = in_array($search[1], $forbidden);
562 if ((
true === is_writable($pages_path)) && (
false === $denied))
565 $parent_folders = explode(
'/', $rel_pages_dir);
567 foreach ($parent_folders as $parent_folder)
569 if ($parent_folder !=
'/' && $parent_folder !=
'')
571 $parents .=
'/' . $parent_folder;
572 if (!file_exists($pages_path . $parents))
574 LEPTON_core::make_dir( $pages_path . $parents );
575 LEPTON_core::change_mode( $pages_path . $parents );
579 $step_back = str_repeat(
'../', substr_count($rel_pages_dir,
'/') + (PAGES_DIRECTORY ==
"" ? 0 : 1));
580 $content =
'<?php' .
"\n";
581 $content .=
"/**\n *\tThis file is autogenerated by LEPTON - Version: ".LEPTON_VERSION.
"\n";
582 $content .=
" *\tDo not modify this file!\n */\n";
583 $content .=
"\t" .
'$page_id = ' . $page_id .
';' .
"\n";
584 $content .=
"\t" .
'require_once(\'' . $step_back .
'index.php\');' .
"\n";
590 $fp = fopen($filename,
'w');
593 fwrite($fp, $content, strlen($content));
599 LEPTON_core::change_mode($filename);
605 $temp_index_path = dirname($filename).
"/index.php";
606 if (!file_exists($temp_index_path))
608 $origin = ADMIN_PATH.
"/pages/master_index.php";
609 if (file_exists($origin))
611 copy($origin, $temp_index_path);
617 $admin->print_error($MESSAGE[
'PAGES_CANNOT_CREATE_ACCESS_FILE'].
"<br />Problems while trying to open the file!");
624 $admin->print_error($MESSAGE[
'PAGES_CANNOT_CREATE_ACCESS_FILE']);
637 static public function level_count(
int $iPageId ): int
641 $iParent =
$database->get_one(
'SELECT `parent` FROM `'.TABLE_PREFIX.
'pages` WHERE `page_id` = '.$iPageId);
645 $iLevel =
$database->get_one(
'SELECT `level` FROM `'.TABLE_PREFIX.
'pages` WHERE `page_id` = '.$iParent);
670 static public function get_subs(
int $parent, array &$subs): void
677 "SELECT page_id FROM ".TABLE_PREFIX.
"pages WHERE parent = ".$parent.
" ORDER BY position",
683 foreach ($all as &$fetch)
685 $subs[] = $fetch[
'page_id'];
688 self::get_subs($fetch[
'page_id'], $subs);
701 static public function delete_page(
int $page_id): void
704 $admin = self::getGlobal(
'admin');
705 $MESSAGE = self::getGlobal(
'MESSAGE');
706 $section_id = self::getGlobal(
'section_id');
713 'SELECT link, parent FROM '.TABLE_PREFIX.
'pages WHERE page_id = '.$page_id,
719 if (empty($page_info))
721 $admin->print_error($MESSAGE[
'PAGES_NOT_FOUND']);
727 'SELECT section_id, module FROM '.TABLE_PREFIX.
'sections WHERE page_id = '.$page_id,
732 foreach($all_sections as &$section)
735 $section_id = $section[
'section_id'];
738 if (file_exists(LEPTON_PATH.
'/modules/'.$section[
'module'].
'/delete.php'))
740 include LEPTON_PATH.
'/modules/'.$section[
'module'].
'/delete.php';
745 $database->simple_query(
"DELETE FROM ".TABLE_PREFIX.
"pages WHERE page_id = ".$page_id);
748 $database->simple_query(
"DELETE FROM ".TABLE_PREFIX.
"sections WHERE page_id = ".$page_id);
752 $order->clean($page_info[
'parent']);
755 $directory = LEPTON_PATH . PAGES_DIRECTORY . $page_info[
'link'];
756 $filename = $directory . PAGE_EXTENSION;
758 if (file_exists($filename))
760 if (!is_writable(LEPTON_PATH . PAGES_DIRECTORY .
'/'))
762 $admin->print_error($MESSAGE[
'PAGES_CANNOT_DELETE_ACCESS_FILE']);
767 if (file_exists($directory) && (rtrim($directory,
'/') != LEPTON_PATH . PAGES_DIRECTORY) && ($page_info[
'link'][0] !=
'.'))
779 static public function getGlobal(
string $name): null|int|string|array|object
782 if (isset($GLOBALS[$name]))
784 $returnValue = &$GLOBALS[$name];
794 static function registerBasicFunctions(): void
796 $functionListToRegister = [
810 static function loadCodeSnippets(): void
814 "SELECT `directory` FROM `".TABLE_PREFIX.
"addons` WHERE `function` = 'snippet'",
820 foreach ($snippets as $snippet)
822 $tempPath = LEPTON_PATH.
"/modules/".$snippet[
'directory'].
"/include.php";
823 if (file_exists($tempPath))
836 static public function check_entry(
string $value): bool
841 "SELECT * FROM ".TABLE_PREFIX.
"keepout ORDER BY id DESC",
847 if (!empty($all_entries))
849 foreach ($all_entries as $check)
851 if (str_contains($check[
'email'],
'*'))
853 $check_email = str_replace(
'*',
'',$check[
'email']);
854 if (str_contains($value, $check_email))
860 if ($value == $check[
'ip'] || $value == $check[
'email'])
875 static public function userHasAdminRights(): bool
877 if (self::$bUserGotAdminRights ==
false)
880 $aUser = explode(
",", ($_SESSION[
'GROUPS_ID'] ??
""));
881 self::$bUserGotAdminRights = (in_array(1, $aUser));
884 return self::$bUserGotAdminRights;
892 static public function imageTypesAllowed(): array
894 $aWhiteList = explode(
',', UPLOAD_WHITELIST);
895 $aImageTypes = lib_r_filemanager::allowed_image_types;
897 return array_merge(array_intersect($aWhiteList, $aImageTypes), []);
static getInstance(array &$settings=[])
static getInstance(string $table, string $order_field='position', string $id_field='id', string $common_field='')
rm_full_dir(string $directory)