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;
61 public static array $aPageVisibility = [
'public',
'private',
'registered',
'hidden',
'none'];
66 public function __construct()
79 public function page_is_visible( $page ): bool
84 switch ($page[
'visibility'])
93 if ($this->is_authenticated() ===
true)
95 $show_it = ($this->is_group_match($this->getValue(
'groups_id',
'string',
'session',
','), $page[
'viewing_groups']) );
116 public function section_is_active(
int $section_id): bool
121 FROM `" . TABLE_PREFIX .
"sections`
122 WHERE (" . $now .
" BETWEEN `publ_start` AND `publ_end`)
123 OR (" . $now .
" > `publ_start` AND `publ_end`=0)
124 AND `section_id`=" . $section_id;
136 public function page_is_active(array $page ): bool
141 FROM `".TABLE_PREFIX.
"sections`
142 WHERE `page_id` = ".$page[
'page_id'].
"
143 AND (".$now.
" BETWEEN `publ_start` AND `publ_end` OR (".$now.
" > `publ_start` AND `publ_end`= 0))
155 public function show_page(array|
null $page ): bool
157 if (!is_array($page))
161 "SELECT `page_id`, `visibility` FROM `" . TABLE_PREFIX .
"pages` WHERE `page_id`=" . (
int) $page,
172 return ($this->page_is_visible($page) && $this->page_is_active($page));
176 public function is_authenticated(): bool
178 return ( (isset( $_SESSION[
'USER_ID' ] ) )
179 && ($_SESSION[
'USER_ID' ] !=
"")
180 && (is_numeric( $_SESSION[
'USER_ID' ] ) )
191 public function buildPageLink(
string $link): string
194 if (strstr($link,
'://') ==
'' && !str_starts_with($link,
'mailto:'))
196 return LEPTON_URL . PAGES_DIRECTORY . $link . PAGE_EXTENSION;
231 public static function getValue(
233 string $type =
"string_clean",
234 string $requestFrom =
"post",
235 string $dividerString =
"X",
236 string|
int|array|
null $default =
null,
237 string|
int|array|
null $range =
null
238 ): string|int|array|null
241 if ($requestFrom ==
'session')
243 $lookForName = strtoupper($lookForName);
246 LEPTON_request::getInstance()->setStrictLookInside($requestFrom);
247 $aField = [ $lookForName => [
249 'default' => $default,
253 $sTested = LEPTON_request::getInstance()->testValues($aField);
256 if ((is_null($sTested[$lookForName]) || empty($sTested[$lookForName])) && (!empty($default)))
258 $sTested[$lookForName] = $default;
261 if ($dividerString !=
'X')
263 if (!is_array($sTested[$lookForName]))
265 $sTested[$lookForName] = explode($dividerString, $sTested[$lookForName] ??
"");
267 foreach ($sTested[$lookForName] as &$item)
269 $item = explode($dividerString, $item ??
"");
273 return $sTested[$lookForName];
284 public function is_group_match(
int|
string|array $groups_list1 =
'',
int|
string|array $groups_list2 =
''): bool
286 if ($groups_list1 ==
'')
290 if ($groups_list2 ==
'')
294 if (!is_array($groups_list1))
296 $groups_list1 = explode(
',', $groups_list1 );
298 if (!is_array($groups_list2))
300 $groups_list2 = explode(
',', $groups_list2 );
303 return (
sizeof(array_intersect($groups_list1, $groups_list2)) != 0);
314 public static function addItems(
string $for =
'frontend',
string $path = LEPTON_PATH,
bool $footer =
false): void
316 $trail = explode(
'/', $path);
317 $subdir = array_pop($trail);
324 $add_to = &self::$FOOTERS;
325 $to_load =
'footers.inc.php';
329 $add_to = &self::$HEADERS;
330 $to_load =
'headers.inc.php';
333 require $path.
'/'.$to_load;
335 if (
true === $footer)
337 $aRefArray = &$mod_footers;
341 $aRefArray = &$mod_headers;
344 if (count($aRefArray))
346 foreach ([
'css',
'js'] as $key)
348 if (!isset($aRefArray[$for][$key]))
352 foreach ($aRefArray[$for][$key] as &$item)
355 if ((isset($item[
'file']))
356 && (!preg_match(
"#/$subdir/#", $item[
'file']))
357 && (file_exists($path.
'/'.$item[
'file']))
361 $item[
'file'] = str_ireplace(LEPTON_PATH,
'', $path).
'/'.$item[
'file'];
365 if ($key ===
"css") {
366 foreach ($add_to[$for][$key] as $temp_ref)
368 if ($temp_ref[
'file'] == $item[
'file'])
373 } elseif ($key ===
"js")
375 foreach ($add_to[$for][$key] as $temp_ref)
377 if ($item === $temp_ref)
387 $add_to[$for][$key][] = $item;
393 if ($footer && file_exists($path.$for.
'_body.js'))
395 $add_to[$for][
'js'][] =
'/modules/'.$subdir.
'_body.js';
405 public function getProtectedFunctions(
string &$html,
object &$oReference): void
407 $this->addLepToken($html, $oReference);
414 protected function addLepToken(
string &$html,
object &$oReference): void
419 if (!LEPTOKEN_LIFETIME)
424 $token = $oReference->createLepToken();
426 $token1 =
"$1?leptoken=".$token.
"$3";
427 $token2 =
"leptoken=".$token;
428 $token3 =
"$1&leptoken=".$token.
"$3";
429 $token4 =
"$1?leptoken=".$token.
"$2";
430 $hiddentoken =
"$1\n<span><input type='hidden' name='leptoken' value='".$token.
"' /></span>\n";
433 $qs =
'~((href|action|window\.location)\s?=\s?[\'"]' . LEPTON_URL .
'[\w\-\./]+\.php\?[\w\-\.=&%;/]+)([#[\w]*]?[\'"])~';
434 $html = preg_replace( $qs, $token3, $html, -1 );
437 $qs =
'~((href|action|ajaxfilemanagerurl|window\.location)\s?=\s?[\'"]' . LEPTON_URL .
'[\w\-\./]+\.php)([#[\w]*]?[\'"])~';
438 $html = preg_replace( $qs, $token1, $html, -1 );
441 $qs =
'~((href|action|window\.location)\s?=\s?[\'"][\w/]+\.php\?[\w\-\.=%&;/]+)([#[\w]*]?[\'"])~';
442 $html = preg_replace( $qs, $token3, $html, -1 );
445 $qs =
'~((href|action|window\.location)\s?=\s?[\'"][\w/]+\.php)([#[\w]*]?[\'"])~';
446 $html = preg_replace( $qs, $token1, $html, -1 );
449 $qs =
'~(href\s?=\s?[\'"]' . LEPTON_URL .
')([\'"])~';
450 $html = preg_replace( $qs, $token4, $html, -1 );
453 $qs =
'~(send_testmail\(\'' . ADMIN_URL .
'/settings/ajax_testmail\.php)(\'\))~';
454 $html = preg_replace( $qs, $token4, $html, -1 );
457 $qs =
'~(<form\s+action=[\'"][\w:\.\?/]+leptoken=\w{32}[\'"]\s+method=[\'"]get[\'"]\s*>)~';
458 $html = preg_replace( $qs, $hiddentoken, $html, -1 );
461 $qs =
'~leptokh=#-!leptoken-!#~';
462 $html = preg_replace( $qs, $token2, $html, -1 );
473 static public function make_dir(
string $dir_name,
string|
null $dir_mode = NULL): bool
475 if ($dir_mode == NULL)
477 $dir_mode = (int) octdec( STRING_DIR_MODE );
480 if (!is_dir($dir_name))
483 $result = mkdir($dir_name, $dir_mode,
true);
506 static public function change_mode(
string $name ): bool
508 if (OPERATING_SYSTEM !=
'windows')
510 $mode = (is_dir($name)) ? (
int)octdec(STRING_DIR_MODE) : (int)octdec(STRING_FILE_MODE);
511 if (file_exists($name))
537 static public function create_access_file(
string $filename,
int $page_id): bool
539 global $admin, $MESSAGE;
540 $pages_path = LEPTON_PATH . PAGES_DIRECTORY;
541 $rel_pages_dir = str_replace($pages_path,
'', dirname($filename));
542 $rel_filename = str_replace($pages_path,
'', $filename);
545 if (PAGES_DIRECTORY ==
'')
563 $search = explode(
'/', $rel_filename);
565 $denied = in_array($search[1], $forbidden);
567 if ((
true === is_writable($pages_path)) && (
false === $denied))
570 $parent_folders = explode(
'/', $rel_pages_dir);
572 foreach ($parent_folders as $parent_folder)
574 if ($parent_folder !=
'/' && $parent_folder !=
'')
576 $parents .=
'/' . $parent_folder;
577 if (!file_exists($pages_path . $parents))
579 LEPTON_core::make_dir( $pages_path . $parents );
580 LEPTON_core::change_mode( $pages_path . $parents );
584 $step_back = str_repeat(
'../', substr_count($rel_pages_dir,
'/') + (PAGES_DIRECTORY ==
"" ? 0 : 1));
585 $content =
'<?php'.
"\n";
586 $content .=
"/**\n * This file is autogenerated by LEPTON - Version: ".LEPTON_VERSION.
"\n";
587 $content .=
" * Do not modify this file!\n */\n";
588 $content .=
" ".
'$page_id = '.$page_id.
';'.
"\n";
589 $content .=
" ".
'require_once \''.$step_back.
'index.php\';'.
"\n";
595 $fp = fopen($filename,
'w');
598 fwrite($fp, $content, strlen($content));
604 LEPTON_core::change_mode($filename);
610 $temp_index_path = dirname($filename).
"/index.php";
611 if (!file_exists($temp_index_path))
613 $origin = ADMIN_PATH.
"/pages/master_index.php";
614 if (file_exists($origin))
616 copy($origin, $temp_index_path);
622 $admin->print_error($MESSAGE[
'PAGES_CANNOT_CREATE_ACCESS_FILE'].
"<br />Problems while trying to open the file!");
629 $admin->print_error($MESSAGE[
'PAGES_CANNOT_CREATE_ACCESS_FILE']);
642 static public function level_count(
int $iPageId ): int
646 $iParent =
$database->get_one(
'SELECT `parent` FROM `'.TABLE_PREFIX.
'pages` WHERE `page_id` = '.$iPageId);
650 $iLevel =
$database->get_one(
'SELECT `level` FROM `'.TABLE_PREFIX.
'pages` WHERE `page_id` = '.$iParent);
675 static public function get_subs(
int $parent, array &$subs): void
682 "SELECT `page_id` FROM `".TABLE_PREFIX.
"pages` WHERE `parent` = ".$parent.
" ORDER BY position",
688 foreach ($all as &$aTempPageRef)
690 $subs[] = $aTempPageRef[
'page_id'];
693 self::get_subs($aTempPageRef[
'page_id'], $subs);
706 static public function delete_page(
int $page_id): void
709 $admin = self::getGlobal(
'admin');
710 $MESSAGE = self::getGlobal(
'MESSAGE');
711 $section_id = self::getGlobal(
'section_id');
718 'SELECT link, parent FROM '.TABLE_PREFIX.
'pages WHERE page_id = '.$page_id,
724 if (empty($page_info))
726 $admin->print_error($MESSAGE[
'PAGES_NOT_FOUND']);
732 'SELECT section_id, module FROM '.TABLE_PREFIX.
'sections WHERE page_id = '.$page_id,
737 foreach($all_sections as &$section)
740 $section_id = $section[
'section_id'];
743 if (file_exists(LEPTON_PATH.
'/modules/'.$section[
'module'].
'/delete.php'))
745 include LEPTON_PATH.
'/modules/'.$section[
'module'].
'/delete.php';
750 $database->simple_query(
"DELETE FROM ".TABLE_PREFIX.
"pages WHERE page_id = ".$page_id);
753 $database->simple_query(
"DELETE FROM ".TABLE_PREFIX.
"sections WHERE page_id = ".$page_id);
757 $order->clean($page_info[
'parent']);
760 $directory = LEPTON_PATH . PAGES_DIRECTORY . $page_info[
'link'];
761 $filename = $directory . PAGE_EXTENSION;
763 if (file_exists($filename))
765 if (!is_writable(LEPTON_PATH . PAGES_DIRECTORY .
'/'))
767 $admin->print_error($MESSAGE[
'PAGES_CANNOT_DELETE_ACCESS_FILE']);
772 if (file_exists($directory) && (rtrim($directory,
'/') != LEPTON_PATH . PAGES_DIRECTORY) && ($page_info[
'link'][0] !=
'.'))
784 static public function getGlobal(
string $name): null|int|string|array|object
787 if (isset($GLOBALS[$name]))
789 $returnValue = &$GLOBALS[$name];
799 static function registerBasicFunctions(): void
801 $functionListToRegister = [
815 static function loadCodeSnippets(): void
819 "SELECT `directory` FROM `".TABLE_PREFIX.
"addons` WHERE `function` = 'snippet'",
825 foreach ($snippets as $snippet)
827 $tempPath = LEPTON_PATH.
"/modules/".$snippet[
'directory'].
"/include.php";
828 if (file_exists($tempPath))
841 static public function check_entry(
string $value): bool
846 "SELECT * FROM ".TABLE_PREFIX.
"keepout ORDER BY id DESC",
852 if (!empty($all_entries))
854 foreach ($all_entries as $check)
856 if (str_contains($check[
'email'],
'*'))
858 $check_email = str_replace(
'*',
'',$check[
'email']);
859 if (str_contains($value, $check_email))
865 if ($value == $check[
'ip'] || $value == $check[
'email'])
880 static public function userHasAdminRights(): bool
882 if (self::$bUserGotAdminRights ==
false)
885 $aUser = explode(
",", ($_SESSION[
'GROUPS_ID'] ??
""));
886 self::$bUserGotAdminRights = (in_array(1, $aUser));
889 return self::$bUserGotAdminRights;
897 static public function imageTypesAllowed(): array
899 $aWhiteList = explode(
',', UPLOAD_WHITELIST);
900 $aImageTypes = lib_r_filemanager::allowed_image_types;
902 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)