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