LEPTON CMS 7.5.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
19
20
25class LEPTON_frontend extends LEPTON_core
26{
27 use LEPTON_singleton;
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 // include cronjob file for external call
54 // @DEPRECATED_TEMP 20251010: this class will be removed in L* > 7.5.0, use external cronjobs instead
55 if(CRONJOB == 1 || CRONJOB == 3)
56 {
57 $_POST['ikey'] = LEPTON_cronjob::getInstance()->cj_key;
58 LEPTON_handle::include_files("/modules/cronjob.php");
59 }
60
61 self::$instance = $this;
62 if (TFA != 'none') // first step in process to display page and set vars
63 {
64 if (!isset($_SESSION['USER_ID']))
65 {
66 $pin_set = -1;
67 }
68 else
69 {
70 $pin_set = $this->database->get_one("SELECT pin_set FROM ".TABLE_PREFIX."users WHERE user_id = '".$_SESSION['USER_ID']."' ");
71
72 }
73
74 switch ($pin_set )
75 {
76 case 0:
77 case 1:
78 header('Location: '.LEPTON_URL.'/account/logout.php');
79 break;
80
81 case -1:
82 case 2:
83 LEPTON_SecureCMS::clearLepTokens();
84 break;
85
86 default:
87 LEPTON_SecureCMS::clearLepTokens();
88 header('Location: '.LEPTON_URL.'/account/logout.php');
89 }
90 }
91 }
92
93 public function page_select()
94 {
95 global $page_id;
96
97 // Check if we should add page language sql code
98 if (PAGE_LANGUAGES == true)
99 {
100 $sql_where_language = " AND p.language = '".LANGUAGE."'";
101 }
102 else
103 {
104 $sql_where_language = "";
105 }
106
107 // Get default page
108 $now = time();
109 $query_default = "
110 SELECT *
111 FROM ".TABLE_PREFIX . "pages AS p
112 INNER JOIN ".TABLE_PREFIX . "sections AS s
113
114 ON (s.page_id = p.page_id)
115
116 WHERE p.parent = 0
117
118 AND p.visibility = 'public'
119
120 AND (
121 ((".$now." >= s.publ_start) OR (s.publ_start = 0))
122 AND
123 ((".$now." <= s.publ_end) OR (s.publ_end = 0))
124 )
125 ".$sql_where_language."
126 ORDER BY
127 p.position
128
129 ASC LIMIT 1
130 ";
131
132 $fetch_default = [];
133 $this->database->execute_query(
134 $query_default,
135 true,
136 $fetch_default,
137 false
138 );
139
140 if (!isset($page_id) || !is_numeric($page_id))
141 {
142 // Display default page
143 if (!empty($fetch_default))
144 {
145 $this->default_link = $fetch_default[ 'link' ];
146 $this->default_page_id = intval($fetch_default[ 'page_id' ]);
147
148 // Check if we should redirect or include page inline
149 if (HOMEPAGE_REDIRECTION)
150 {
151 // Redirect to page
152 header("Location: ".$this->buildPageLink($this->default_link));
153 exit();
154 }
155 else
156 {
157 // Include page inline
158 $this->page_id = $this->default_page_id;
159 }
160 }
161 else
162 {
163 // PAGE_LANGUAGES == true, therefore you want to have pages with different languages. In this case there is no page in "your" language available!
164 die(LEPTON_tools::display_dev('[300]:Please check if you have pages in your language!', 'pre','ui blue message'));
165 }
166 }
167 else
168 {
169 if (!isset($fetch_default[ 'link' ]))
170 {
171 die(LEPTON_tools::display('This installation has no content yet', 'pre','ui red message'));
172 }
173
174 $this->page_id = intval($page_id);
175 $this->default_link = $fetch_default[ 'link' ];
176 $this->default_page_id = intval($fetch_default[ 'page_id' ]);
177 $this->page = $fetch_default;
178
179 }
180
181 return true;
182 }
183
184 public function get_page_details()
185 {
186 $this->page_id = intval($this->page_id);
187 if ($this->page_id != 0)
188 {
189 $this->page = [];
190 $query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = ".$this->page_id;
191 $this->database->execute_query(
192 $query_page,
193 true,
194 $this->page,
195 false
196 );
197
198 // Make sure page was found in database
199 if (empty($this->page))
200 {
201 // Print page not found message
202 exit( "Page not found." );
203 }
204 else
205 {
206 foreach ($this->page as $key => $value)
207 {
208 // set members of array to constants
209 $key = strtoupper($key);
210 if (!defined($key))
211 {
212 if ($key === 'TEMPLATE' && empty($value))
213 {
214 $value = $this->database->get_one("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'default_template' ");
215 }
216
217 if ($key === 'DESCRIPTION' && empty($value))
218 {
219 $value = WEBSITE_DESCRIPTION;
220 }
221
222 if ($key === 'KEYWORDS' && empty($value))
223 {
224 $value = WEBSITE_KEYWORDS;
225 }
226
227 define($key, $value);
228 }
229 }
230 }
231
232
233 // Page trail
234 foreach ( explode( ',', $this->page[ 'page_trail' ] ) AS $pid )
235 {
236 $this->page_trail[ $pid ] = $pid;
237 }
238 }
239
240
241 // Set the template dir
242 if(!defined('TEMPLATE'))
243 {
244 define('TEMPLATE', DEFAULT_TEMPLATE);
245 }
246 define( 'TEMPLATE_DIR', LEPTON_URL . '/templates/' . TEMPLATE );
247 // Check if user is allowed to view this page
248 if ($this->page_is_visible($this->page) === false)
249 {
250 if ( VISIBILITY == 'deleted' || VISIBILITY == 'none' )
251 {
252 // User isn't allowed on this page so tell them
253 $this->page_access_denied = true;
254 }
255 elseif ( VISIBILITY == 'private' || VISIBILITY == 'registered' )
256 {
257 // Check if the user is authenticated
258 if ( $this->is_authenticated() === false )
259 {
260 // User needs to log-in first
261 header( "Location: " . LEPTON_URL . "/account/login.php?redirect=" . $this->buildPageLink($this->page['link']) );
262 exit( 0 );
263 }
264 else
265 {
266 $aAllowedGroupsId = explode(',',$this->page['viewing_groups']);
267 $aSessionGroupsId = LEPTON_core::getValue('groups_id','string_clean','session',',');
268 $result = array_intersect( $aSessionGroupsId,$aAllowedGroupsId);
269 if(!empty($result))
270 {
271
272 $this->page_access_denied = false;
273 }
274 else
275 {
276 // User is not allowed on this page so tell them
277 $this->page_access_denied = true;
278 }
279 }
280 }
281 }
282
283 $this->maintainConstants();
284
285 // check if there is at least one active section
286 if ($this->page_is_active($this->page) === false)
287 {
288 $this->page_no_active_sections = true;
289 }
290 }
291
292 public function get_website_settings()
293 {
294 // Work-out if any possible in-line search boxes should be shown
295 if ( SEARCH == 'public' )
296 {
297 define( 'SHOW_SEARCH', true );
298 }
299 elseif ( SEARCH == 'private' && VISIBILITY == 'private' )
300 {
301 define( 'SHOW_SEARCH', true );
302 }
303 elseif ( SEARCH == 'private' && $this->is_authenticated() === true )
304 {
305 define( 'SHOW_SEARCH', true );
306 }
307 elseif ( SEARCH == 'registered' && $this->is_authenticated() === true )
308 {
309 define( 'SHOW_SEARCH', true );
310 }
311 else
312 {
313 define( 'SHOW_SEARCH', false );
314 }
315 // Work-out if menu should be shown
316 if ( !defined( 'SHOW_MENU' ) )
317 {
318 define( 'SHOW_MENU', true );
319 }
320 // Work-out if login menu constants should be set
321 if ( FRONTEND_LOGIN )
322 {
323 // Set login menu constants
324 define( 'LOGIN_URL', LEPTON_URL . '/account/login.php' );
325 define( 'LOGOUT_URL', LEPTON_URL . '/account/logout.php' );
326 define( 'FORGOT_URL', LEPTON_URL . '/account/forgot.php' );
327 define( 'PREFERENCES_URL', LEPTON_URL . '/account/preferences.php' );
328 define( 'SIGNUP_URL', LEPTON_URL . '/account/signup.php' );
329 }
330 }
331
337 public function preprocess(string &$content): void
338 {
339 $content = str_ireplace( ["%5B","%5D"], ["[", "]"], $content);
340
341 // starting with L*5 LEPTONlink replaces wblink
342 if (preg_match_all('/\[LEPTONlink([0-9]+)\]/isU', $content, $ids))
343 {
344 $new_ids = array_unique( $ids[ 1 ] );
345 foreach ($new_ids as $key => $temp_page_id)
346 {
347 $link = $this->database->get_one( "SELECT `link` FROM `" . TABLE_PREFIX . "pages` WHERE `page_id` = " . $temp_page_id );
348 if (!is_null($link))
349 {
350 $content = str_replace($ids[0][$key], $this->buildPageLink($link), $content);
351 }
352 }
353 unset($temp_page_id);
354 }
355 }
356
362 public function maintainConstants(): void
363 {
364 $lookFor = [
365 "DESCRIPTION" => WEBSITE_DESCRIPTION,
366 "KEYWORDS" => WEBSITE_KEYWORDS
367 ];
368
369 foreach ($lookFor as $key => $value)
370 {
371 if (!defined($key))
372 {
373 define($key, $value);
374 }
375 }
376 }
377
378
399 public static function displayFEMessage(
400 string $sType ="success",
401 array|string $aMessage = [],
402 string $sRedirect = "",
403 int $iRedirectTime = -1,
404 string $sTemplateName = "message.lte",
405 bool $bDirectOutput = true
406 ): string
407 {
408 $oTwig = lib_twig_box::getInstance();
409
410 if (!is_array($aMessage))
411 {
412 $aMessage = [$aMessage];
413 }
414
415 if (empty($sRedirect))
416 {
417 // Try to get a valid full link to the current page
418 $link = self::$instance->page['link'];
419 $sRedirect = LEPTON_core::getInstance()->buildPageLink($link);
420 }
421
422 // [1] Is there a lte file inside the frontend-template dir?
423 if (file_exists(LEPTON_PATH."/templates/".DEFAULT_TEMPLATE."/frontend/core/message.lte"))
424 {
425 // [1.1] File is inside the frontend-template - so we are using this path.
426 $oTwig->registerPath(LEPTON_PATH."/templates/".DEFAULT_TEMPLATE."/frontend/core/", "core");
427 }
428 else
429 {
430 // [1.2] File not found so we are using the "default" path inside the "account" directory.
431 die(LEPTON_tools::display('There is no message file in your FE-Template available!', 'pre','ui orange message'));
432 }
433
434 $data = [
435 'type' => $sType,
436 'message' => $aMessage,
437 'redirect' => $sRedirect,
438 'redirect_time' => $iRedirectTime
439 ];
440
441 $sHTML_rendered = $oTwig->render(
442 "@core/".$sTemplateName,
443 $data
444 );
445
446 if ($bDirectOutput == true)
447 {
448 echo $sHTML_rendered;
449 return "";
450 }
451
452 return $sHTML_rendered;
453 }
454
455
456 public static function getSessionAndClean(string $sKey, string $sType = "string"): string|int
457 {
458 $returnValue = LEPTON_core::getValue($sKey, $sType, "session") ?? "";
459 unset($_SESSION[$sKey]);
460 return $returnValue;
461 }
462}
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)