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;
220 public static function getValue(
222 string $type =
"string_clean",
223 string $requestFrom =
"post",
224 string $dividerString =
"X"
225 ): string|int|array|null
228 if ($requestFrom ==
'session')
230 $lookForName = strtoupper($lookForName);
233 LEPTON_request::getInstance()->setStrictLookInside($requestFrom);
234 $aField = [ $lookForName => [
'type' => $type,
'default' =>
null] ];
235 $sTested = LEPTON_request::getInstance()->testValues($aField);
238 if ($dividerString !=
'X')
240 if (!is_array($sTested[$lookForName]))
242 $sTested[$lookForName] = explode($dividerString, $sTested[$lookForName] ??
"");
244 foreach ($sTested[$lookForName] as &$item)
246 $item = explode($dividerString, $item ??
"");
250 return $sTested[$lookForName];
261 public function is_group_match(
int|
string|array $groups_list1 =
'',
int|
string|array $groups_list2 =
''): bool
263 if ($groups_list1 ==
'')
267 if ($groups_list2 ==
'')
271 if (!is_array($groups_list1))
273 $groups_list1 = explode(
',', $groups_list1 );
275 if (!is_array($groups_list2))
277 $groups_list2 = explode(
',', $groups_list2 );
280 return (
sizeof(array_intersect($groups_list1, $groups_list2)) != 0);
291 public static function addItems(
string $for =
'frontend',
string $path = LEPTON_PATH,
bool $footer =
false): void
293 $trail = explode(
'/', $path);
294 $subdir = array_pop($trail);
301 $add_to = &self::$FOOTERS;
302 $to_load =
'footers.inc.php';
306 $add_to = &self::$HEADERS;
307 $to_load =
'headers.inc.php';
310 require $path.
'/'.$to_load;
312 if (
true === $footer)
314 $aRefArray = &$mod_footers;
318 $aRefArray = &$mod_headers;
321 if (count($aRefArray))
323 foreach ([
'css',
'js'] as $key)
325 if (!isset($aRefArray[$for][$key]))
329 foreach ($aRefArray[$for][$key] as &$item)
332 if ((isset($item[
'file']))
333 && (!preg_match(
"#/$subdir/#", $item[
'file']))
334 && (file_exists($path.
'/'.$item[
'file']))
338 $item[
'file'] = str_ireplace(LEPTON_PATH,
'', $path).
'/'.$item[
'file'];
342 if ($key ===
"css") {
343 foreach ($add_to[$for][$key] as $temp_ref)
345 if ($temp_ref[
'file'] == $item[
'file'])
350 } elseif ($key ===
"js")
352 foreach ($add_to[$for][$key] as $temp_ref)
354 if ($item === $temp_ref)
364 $add_to[$for][$key][] = $item;
370 if ($footer && file_exists($path.$for.
'_body.js'))
372 $add_to[$for][
'js'][] =
'/modules/'.$subdir.
'_body.js';
382 public function getProtectedFunctions(
string &$html,
object &$oReference): void
384 $this->addLepToken($html, $oReference);
391 protected function addLepToken(
string &$html,
object &$oReference): void
396 if (!LEPTOKEN_LIFETIME)
401 $token = $oReference->createLepToken();
403 $token1 =
"$1?leptoken=".$token.
"$3";
404 $token2 =
"leptoken=".$token;
405 $token3 =
"$1&leptoken=".$token.
"$3";
406 $token4 =
"$1?leptoken=".$token.
"$2";
407 $hiddentoken =
"$1\n<span><input type='hidden' name='leptoken' value='".$token.
"' /></span>\n";
410 $qs =
'~((href|action|window\.location)\s?=\s?[\'"]' . LEPTON_URL .
'[\w\-\./]+\.php\?[\w\-\.=&%;/]+)([#[\w]*]?[\'"])~';
411 $html = preg_replace( $qs, $token3, $html, -1 );
414 $qs =
'~((href|action|ajaxfilemanagerurl|window\.location)\s?=\s?[\'"]' . LEPTON_URL .
'[\w\-\./]+\.php)([#[\w]*]?[\'"])~';
415 $html = preg_replace( $qs, $token1, $html, -1 );
418 $qs =
'~((href|action|window\.location)\s?=\s?[\'"][\w/]+\.php\?[\w\-\.=%&;/]+)([#[\w]*]?[\'"])~';
419 $html = preg_replace( $qs, $token3, $html, -1 );
422 $qs =
'~((href|action|window\.location)\s?=\s?[\'"][\w/]+\.php)([#[\w]*]?[\'"])~';
423 $html = preg_replace( $qs, $token1, $html, -1 );
426 $qs =
'~(href\s?=\s?[\'"]' . LEPTON_URL .
')([\'"])~';
427 $html = preg_replace( $qs, $token4, $html, -1 );
430 $qs =
'~(send_testmail\(\'' . ADMIN_URL .
'/settings/ajax_testmail\.php)(\'\))~';
431 $html = preg_replace( $qs, $token4, $html, -1 );
434 $qs =
'~(<form\s+action=[\'"][\w:\.\?/]+leptoken=\w{32}[\'"]\s+method=[\'"]get[\'"]\s*>)~';
435 $html = preg_replace( $qs, $hiddentoken, $html, -1 );
438 $qs =
'~leptokh=#-!leptoken-!#~';
439 $html = preg_replace( $qs, $token2, $html, -1 );
450 static public function make_dir(
string $dir_name,
string $dir_mode = NULL): bool
452 if ($dir_mode == NULL)
454 $dir_mode = (int) octdec( STRING_DIR_MODE );
457 if (!is_dir($dir_name))
460 mkdir($dir_name, $dir_mode,
true);
475 static public function change_mode(
string $name ): bool
477 if (OPERATING_SYSTEM !=
'windows')
479 $mode = (is_dir($name)) ? (
int)octdec(STRING_DIR_MODE) : (int)octdec(STRING_FILE_MODE);
480 if (file_exists($name))
506 static public function create_access_file(
string $filename,
int $page_id): bool
508 global $admin, $MESSAGE;
509 $pages_path = LEPTON_PATH . PAGES_DIRECTORY;
510 $rel_pages_dir = str_replace($pages_path,
'', dirname($filename));
511 $rel_filename = str_replace($pages_path,
'', $filename);
514 if (PAGES_DIRECTORY ==
'')
532 $search = explode(
'/', $rel_filename);
534 $denied = in_array($search[1], $forbidden);
536 if ((
true === is_writable($pages_path)) && (
false === $denied))
539 $parent_folders = explode(
'/', $rel_pages_dir);
541 foreach ($parent_folders as $parent_folder)
543 if ($parent_folder !=
'/' && $parent_folder !=
'')
545 $parents .=
'/' . $parent_folder;
546 if (!file_exists($pages_path . $parents))
548 LEPTON_core::make_dir( $pages_path . $parents );
549 LEPTON_core::change_mode( $pages_path . $parents );
553 $step_back = str_repeat(
'../', substr_count($rel_pages_dir,
'/') + (PAGES_DIRECTORY ==
"" ? 0 : 1));
554 $content =
'<?php' .
"\n";
555 $content .=
"/**\n *\tThis file is autogenerated by LEPTON - Version: ".LEPTON_VERSION.
"\n";
556 $content .=
" *\tDo not modify this file!\n */\n";
557 $content .=
"\t" .
'$page_id = ' . $page_id .
';' .
"\n";
558 $content .=
"\t" .
'require_once(\'' . $step_back .
'index.php\');' .
"\n";
564 $fp = fopen($filename,
'w');
567 fwrite($fp, $content, strlen($content));
573 LEPTON_core::change_mode($filename);
579 $temp_index_path = dirname($filename).
"/index.php";
580 if (!file_exists($temp_index_path))
582 $origin = ADMIN_PATH.
"/pages/master_index.php";
583 if (file_exists($origin))
585 copy($origin, $temp_index_path);
591 $admin->print_error($MESSAGE[
'PAGES_CANNOT_CREATE_ACCESS_FILE'].
"<br />Problems while trying to open the file!");
598 $admin->print_error($MESSAGE[
'PAGES_CANNOT_CREATE_ACCESS_FILE']);
611 static public function level_count(
int $iPageId ): int
615 $iParent =
$database->get_one(
'SELECT `parent` FROM `'.TABLE_PREFIX.
'pages` WHERE `page_id` = '.$iPageId);
619 $iLevel =
$database->get_one(
'SELECT `level` FROM `'.TABLE_PREFIX.
'pages` WHERE `page_id` = '.$iParent);
644 static public function get_subs(
int $parent, array &$subs): void
651 "SELECT page_id FROM ".TABLE_PREFIX.
"pages WHERE parent = ".$parent.
" ORDER BY position",
657 foreach ($all as &$fetch)
659 $subs[] = $fetch[
'page_id'];
662 self::get_subs($fetch[
'page_id'], $subs);
675 static public function delete_page(
int $page_id): void
678 $admin = self::getGlobal(
'admin');
679 $MESSAGE = self::getGlobal(
'MESSAGE');
680 $section_id = self::getGlobal(
'section_id');
687 'SELECT link, parent FROM '.TABLE_PREFIX.
'pages WHERE page_id = '.$page_id,
693 if (empty($page_info))
695 $admin->print_error($MESSAGE[
'PAGES_NOT_FOUND']);
701 'SELECT section_id, module FROM '.TABLE_PREFIX.
'sections WHERE page_id = '.$page_id,
706 foreach($all_sections as &$section)
709 $section_id = $section[
'section_id'];
712 if (file_exists(LEPTON_PATH.
'/modules/'.$section[
'module'].
'/delete.php'))
714 include LEPTON_PATH.
'/modules/'.$section[
'module'].
'/delete.php';
719 $database->simple_query(
"DELETE FROM ".TABLE_PREFIX.
"pages WHERE page_id = ".$page_id);
722 $database->simple_query(
"DELETE FROM ".TABLE_PREFIX.
"sections WHERE page_id = ".$page_id);
726 $order->clean($page_info[
'parent']);
729 $directory = LEPTON_PATH . PAGES_DIRECTORY . $page_info[
'link'];
730 $filename = $directory . PAGE_EXTENSION;
732 if (file_exists($filename))
734 if (!is_writable(LEPTON_PATH . PAGES_DIRECTORY .
'/'))
736 $admin->print_error($MESSAGE[
'PAGES_CANNOT_DELETE_ACCESS_FILE']);
741 if (file_exists($directory) && (rtrim($directory,
'/') != LEPTON_PATH . PAGES_DIRECTORY) && ($page_info[
'link'][0] !=
'.'))
753 static public function getGlobal(
string $name): null|int|string|array|object
756 if (isset($GLOBALS[$name]))
758 $returnValue = &$GLOBALS[$name];
768 static function registerBasicFunctions(): void
770 $functionListToRegister = [
784 static function loadCodeSnippets(): void
788 "SELECT `directory` FROM `".TABLE_PREFIX.
"addons` WHERE `function` = 'snippet'",
794 foreach ($snippets as $snippet)
796 $tempPath = LEPTON_PATH.
"/modules/".$snippet[
'directory'].
"/include.php";
797 if (file_exists($tempPath))
810 static public function check_entry(
string $value): bool
815 "SELECT * FROM ".TABLE_PREFIX.
"keepout ORDER BY id DESC",
821 if (!empty($all_entries))
823 foreach ($all_entries as $check)
825 if (str_contains($check[
'email'],
'*'))
827 $check_email = str_replace(
'*',
'',$check[
'email']);
828 if (str_contains($value, $check_email))
834 if ($value == $check[
'ip'] || $value == $check[
'email'])
849 static public function userHasAdminRights(): bool
851 if (self::$bUserGotAdminRights ==
false)
854 $aUser = explode(
",", ($_SESSION[
'GROUPS_ID'] ??
""));
855 self::$bUserGotAdminRights = (in_array(1, $aUser));
858 return self::$bUserGotAdminRights;
866 static public function imageTypesAllowed(): array
868 $aWhiteList = explode(
',', UPLOAD_WHITELIST);
869 $aImageTypes = lib_r_filemanager::allowed_image_types;
871 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)