LEPTON CMS 7.4.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
lepton_frontend.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
25class LEPTON_frontend extends LEPTON_core
26{
28
30 public static $instance;
31
32 // [1] Defaults
33 public string $default_link = "/";
34 public int $default_page_id = 1;
35
36 // [2] Page details
37 public array $page = [];
38 public int $page_id = 0;
39
40 public array $page_trail = [];
41
42 public bool $page_access_denied = false;
43 public bool $page_no_active_sections = false;
44
45 public function __construct()
46 {
47 global $TEXT;
48
49 LEPTON_core::registerBasicFunctions();
50 LEPTON_core::loadCodeSnippets();
51 $this->database = LEPTON_database::getInstance();
52
53 // @ADD_cronjob 20230727, include cronjob file for external call
54 if(CRONJOB == 1 || CRONJOB == 3)
55 {
56 $_POST['ikey'] = LEPTON_cronjob::getInstance()->cj_key;
57 LEPTON_handle::include_files("/modules/cronjob.php");
58 }
59
60 self::$instance = $this;
61 if (TFA != 'none') // first step in process to display page and set vars
62 {
63 if (!isset($_SESSION['USER_ID']))
64 {
65 $pin_set = -1;
66 }
67 else
68 {
69 $pin_set = $this->database->get_one("SELECT pin_set FROM ".TABLE_PREFIX."users WHERE user_id = '".$_SESSION['USER_ID']."' ");
70
71 }
72
73 switch ($pin_set )
74 {
75 case 0:
76 case 1:
77 header('Location: '.LEPTON_URL.'/account/logout.php');
78 break;
79
80 case -1:
81 case 2:
82 LEPTON_SecureCMS::clearLepTokens();
83 break;
84
85 default:
86 LEPTON_SecureCMS::clearLepTokens();
87 header('Location: '.LEPTON_URL.'/account/logout.php');
88 }
89 }
90 }
91
92 public function page_select()
93 {
94 global $page_id;
95
96 // Check if we should add page language sql code
97 if (PAGE_LANGUAGES == true)
98 {
99 $sql_where_language = " AND p.language = '".LANGUAGE."'";
100 }
101 else
102 {
103 $sql_where_language = "";
104 }
105
106 // Get default page
107 $now = time();
108 $query_default = "
109 SELECT *
110 FROM ".TABLE_PREFIX . "pages AS p
111 INNER JOIN ".TABLE_PREFIX . "sections AS s
112
113 ON (s.page_id = p.page_id)
114
115 WHERE p.parent = 0
116
117 AND p.visibility = 'public'
118
119 AND (
120 ((".$now." >= s.publ_start) OR (s.publ_start = 0))
121 AND
122 ((".$now." <= s.publ_end) OR (s.publ_end = 0))
123 )
124 ".$sql_where_language."
125 ORDER BY
126 p.position
127
128 ASC LIMIT 1
129 ";
130
131 $fetch_default = [];
132 $this->database->execute_query(
133 $query_default,
134 true,
135 $fetch_default,
136 false
137 );
138
139 if (!isset($page_id) || !is_numeric($page_id))
140 {
141 // Display default page
142 if (!empty($fetch_default))
143 {
144 $this->default_link = $fetch_default[ 'link' ];
145 $this->default_page_id = intval($fetch_default[ 'page_id' ]);
146
147 // Check if we should redirect or include page inline
148 if (HOMEPAGE_REDIRECTION)
149 {
150 // Redirect to page
151 header("Location: ".$this->buildPageLink($this->default_link));
152 exit();
153 }
154 else
155 {
156 // Include page inline
157 $this->page_id = $this->default_page_id;
158 }
159 }
160 else
161 {
162 // PAGE_LANGUAGES == true, therefore you want to have pages with different languages. In this case there is no page in "your" language available!
163 die(LEPTON_tools::display_dev('[300]:Please check if you have pages in your language!', 'pre','ui blue message'));
164 }
165 }
166 else
167 {
168 if (!isset($fetch_default[ 'link' ]))
169 {
170 die(LEPTON_tools::display('This installation has no content yet', 'pre','ui red message'));
171 }
172
173 $this->page_id = intval($page_id);
174 $this->default_link = $fetch_default[ 'link' ];
175 $this->default_page_id = intval($fetch_default[ 'page_id' ]);
176 $this->page = $fetch_default;
177
178 }
179
180 return true;
181 }
182
183 public function get_page_details()
184 {
185 $this->page_id = intval($this->page_id);
186 if ($this->page_id != 0)
187 {
188 $this->page = [];
189 $query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = ".$this->page_id;
190 $this->database->execute_query(
191 $query_page,
192 true,
193 $this->page,
194 false
195 );
196
197 // Make sure page was found in database
198 if (empty($this->page))
199 {
200 // Print page not found message
201 exit( "Page not found." );
202 }
203 else
204 {
205 foreach ($this->page as $key => $value)
206 {
207 // set members of array to constants
208 $key = strtoupper($key);
209 if (!defined($key))
210 {
211 if ($key === 'TEMPLATE' && empty($value))
212 {
213 $value = $this->database->get_one("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'default_template' ");
214 }
215
216 if ($key === 'DESCRIPTION' && empty($value))
217 {
218 $value = WEBSITE_DESCRIPTION;
219 }
220
221 if ($key === 'KEYWORDS' && empty($value))
222 {
223 $value = WEBSITE_KEYWORDS;
224 }
225
226 define($key, $value);
227 }
228 }
229 }
230
231
232 // Page trail
233 foreach ( explode( ',', $this->page[ 'page_trail' ] ) AS $pid )
234 {
235 $this->page_trail[ $pid ] = $pid;
236 }
237 }
238
239
240 // Set the template dir
241 if(!defined('TEMPLATE'))
242 {
243 define('TEMPLATE', DEFAULT_TEMPLATE);
244 }
245 define( 'TEMPLATE_DIR', LEPTON_URL . '/templates/' . TEMPLATE );
246 // Check if user is allowed to view this page
247 if ($this->page_is_visible($this->page) === false)
248 {
249 if ( VISIBILITY == 'deleted' || VISIBILITY == 'none' )
250 {
251 // User isn't allowed on this page so tell them
252 $this->page_access_denied = true;
253 }
254 elseif ( VISIBILITY == 'private' || VISIBILITY == 'registered' )
255 {
256 // Check if the user is authenticated
257 if ( $this->is_authenticated() === false )
258 {
259 // User needs to log-in first
260 header( "Location: " . LEPTON_URL . "/account/login.php?redirect=" . $this->buildPageLink($this->page['link']) );
261 exit( 0 );
262 }
263 else
264 {
265 $aAllowedGroupsId = explode(',',$this->page['viewing_groups']);
266 $aSessionGroupsId = LEPTON_core::getValue('groups_id','string_clean','session',',');
267 $result = array_intersect( $aSessionGroupsId,$aAllowedGroupsId);
268 if(!empty($result))
269 {
270
271 $this->page_access_denied = false;
272 }
273 else
274 {
275 // User is not allowed on this page so tell them
276 $this->page_access_denied = true;
277 }
278 }
279 }
280 }
281
282 $this->maintainConstants();
283
284 // check if there is at least one active section
285 if ($this->page_is_active($this->page) === false)
286 {
287 $this->page_no_active_sections = true;
288 }
289 }
290
291 public function get_website_settings()
292 {
293 // Work-out if any possible in-line search boxes should be shown
294 if ( SEARCH == 'public' )
295 {
296 define( 'SHOW_SEARCH', true );
297 }
298 elseif ( SEARCH == 'private' && VISIBILITY == 'private' )
299 {
300 define( 'SHOW_SEARCH', true );
301 }
302 elseif ( SEARCH == 'private' && $this->is_authenticated() === true )
303 {
304 define( 'SHOW_SEARCH', true );
305 }
306 elseif ( SEARCH == 'registered' && $this->is_authenticated() === true )
307 {
308 define( 'SHOW_SEARCH', true );
309 }
310 else
311 {
312 define( 'SHOW_SEARCH', false );
313 }
314 // Work-out if menu should be shown
315 if ( !defined( 'SHOW_MENU' ) )
316 {
317 define( 'SHOW_MENU', true );
318 }
319 // Work-out if login menu constants should be set
320 if ( FRONTEND_LOGIN )
321 {
322 // Set login menu constants
323 define( 'LOGIN_URL', LEPTON_URL . '/account/login.php' );
324 define( 'LOGOUT_URL', LEPTON_URL . '/account/logout.php' );
325 define( 'FORGOT_URL', LEPTON_URL . '/account/forgot.php' );
326 define( 'PREFERENCES_URL', LEPTON_URL . '/account/preferences.php' );
327 define( 'SIGNUP_URL', LEPTON_URL . '/account/signup.php' );
328 }
329 }
330
336 public function preprocess(string &$content): void
337 {
338 $content = str_ireplace( ["%5B","%5D"], ["[", "]"], $content);
339
340 // starting with L*5 LEPTONlink replaces wblink
341 if (preg_match_all('/\[LEPTONlink([0-9]+)\]/isU', $content, $ids))
342 {
343 $new_ids = array_unique( $ids[ 1 ] );
344 foreach ($new_ids as $key => $temp_page_id)
345 {
346 $link = $this->database->get_one( "SELECT `link` FROM `" . TABLE_PREFIX . "pages` WHERE `page_id` = " . $temp_page_id );
347 if (!is_null($link))
348 {
349 $content = str_replace($ids[0][$key], $this->buildPageLink($link), $content);
350 }
351 }
352 unset($temp_page_id);
353 }
354 }
355
361 public function maintainConstants(): void
362 {
363 $lookFor = [
364 "DESCRIPTION" => WEBSITE_DESCRIPTION,
365 "KEYWORDS" => WEBSITE_KEYWORDS
366 ];
367
368 foreach ($lookFor as $key => $value)
369 {
370 if (!defined($key))
371 {
372 define($key, $value);
373 }
374 }
375 }
376
397 public static function printFEMessage(string $sType ="success", string $sMessage="", string $sRedirect="", int $iRedirectTime = 3000): void
398 {
399 $info = '@DEPRECATED_TEMP 20250407: this method will be removed in L* > 7.4.0,<br> use LEPTON_frontend::displayFEMessage instead';
400 echo(LEPTON_tools::display($info, 'pre','ui orange message'));
401 die (LEPTON_tools::display(debug_print_backtrace(), 'pre','ui message'));
402
403 if (empty($sRedirect))
404 {
405 // Try to get a valid full link to the current page
406 $link = self::$instance->page['link'];
407 $sRedirect = LEPTON_core::getInstance()->buildPageLink($link);
408 }
409
410 $_SESSION['FRONTEND_MESSAGE'] = $sMessage;
411
412 // @internal marker - used in function.page_content.php - see details in function
413 $_SESSION["PAGE_CONTENT"] = "@LEPTON_FE_MESSAGE";
414
415 $_SESSION["FRONTEND_MESSAGE_TYPE"] = $sType;
416 $_SESSION["FRONTEND_MESSAGE_REDIRECT"] = $sRedirect;
417 $_SESSION["FRONTEND_MESSAGE_REDIRECT_TIME"] = $iRedirectTime;
418
419 header('Location: '.$sRedirect);
420 }
421
426 public static function displayMessage(): void
427 {
428 $info = '@DEPRECATED_TEMP 20250407: this method will be removed in L* > 7.4.0,<br> use LEPTON_frontend::displayFEMessage instead';
429 echo(LEPTON_tools::display($info, 'pre','ui orange message'));
430 die (LEPTON_tools::display(debug_print_backtrace(), 'pre','ui message'));
431 $sMessage = self::getSessionAndClean('FRONTEND_MESSAGE');
432
433 $sType = self::getSessionAndClean('FRONTEND_MESSAGE_TYPE');
434
435 $sRedirect = self::getSessionAndClean('FRONTEND_MESSAGE_REDIRECT');
436
437 $iRedirectTime = self::getSessionAndClean("FRONTEND_MESSAGE_REDIRECT_TIME", "integer");
438
439 $oTWIG = lib_twig_box::getInstance();
440
441 // [2.1] Template file from the currend theme.
442 $oTWIG->registerPath(LEPTON_PATH."/templates/".DEFAULT_THEME."/templates/", "core");
443
444 // [2.2] Template file in the Frontend-template?
445 $tempTemplate = self::$instance->page['template'];
446 $lookUpTemplate = (empty($tempTemplate))
447 ? DEFAULT_TEMPLATE
448 : $tempTemplate
449 ;
450
451 $lookUpPath = LEPTON_PATH."/templates/".$lookUpTemplate."/frontend/core/";
452
453 if (file_exists($lookUpPath))
454 {
455 $oTWIG->registerPath($lookUpPath, "core");
456 }
457
458 $data = [
459 'MESSAGE' => $sMessage,
460 'TYPE' => $sType,
461 'REDIRECT' => $sRedirect,
462 'REDIRECT_TIMER' => $iRedirectTime
463 ];
464
465 echo( $oTWIG->render(
466 '@core/message.lte',
467 $data
468 ));
469 }
470
471
472
493 public static function displayFEMessage(
494 string $sType ="success",
495 array|string $aMessage = [],
496 string $sRedirect = "",
497 int $iRedirectTime = -1,
498 string $sTemplateName = "message.lte",
499 bool $bDirectOutput = true
500 ): string
501 {
502 $oTwig = lib_twig_box::getInstance();
503
504 if (!is_array($aMessage))
505 {
506 $aMessage = [$aMessage];
507 }
508
509 if (empty($sRedirect))
510 {
511 // Try to get a valid full link to the current page
512 $link = self::$instance->page['link'];
513 $sRedirect = LEPTON_core::getInstance()->buildPageLink($link);
514 }
515
516 // [1] Is there a lte file inside the frontend-template dir?
517 if (file_exists(LEPTON_PATH."/templates/".DEFAULT_TEMPLATE."/frontend/core/message.lte"))
518 {
519 // [1.1] File is inside the frontend-template - so we are using this path.
520 $oTwig->registerPath(LEPTON_PATH."/templates/".DEFAULT_TEMPLATE."/frontend/core/", "core");
521 }
522 else
523 {
524 // [1.2] File not found so we are using the "default" path inside the "account" directory.
525 die(LEPTON_tools::display('There is no message file in your FE-Template available!', 'pre','ui orange message'));
526 }
527
528 $data = [
529 'type' => $sType,
530 'message' => $aMessage,
531 'redirect' => $sRedirect,
532 'redirect_time' => $iRedirectTime
533 ];
534
535 $sHTML_rendered = $oTwig->render(
536 "@core/".$sTemplateName,
537 $data
538 );
539
540 if ($bDirectOutput == true)
541 {
542 echo $sHTML_rendered;
543 return "";
544 }
545
546 return $sHTML_rendered;
547 }
548
549
550 public static function getSessionAndClean(string $sKey, string $sType = "string"): string|int
551 {
552 $returnValue = LEPTON_core::getValue($sKey, $sType, "session") ?? "";
553 unset($_SESSION[$sKey]);
554 return $returnValue;
555 }
556}
static getInstance(array &$settings=[])
LEPTON_database $database
preprocess(string &$content)
static include_files(array|string $file_names=[], bool $interrupt=true)
static display(mixed $something_to_display="", string $tag="pre", string|null $css_class=null, bool|null $useVarDump=null)
static display_dev(mixed $something_to_display="", string $tag="pre", string|null $css_class=null, bool|null $useVarDump=null)
trait LEPTON_singleton