LEPTON CMS 7.0.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 $sql_where_language = (PAGE_LANGUAGES) ? " AND language = '" . LANGUAGE . "'" : "";
96
97 // Get default page
98 $now = time();
99 $query_default = "
100 SELECT *
101 FROM `".TABLE_PREFIX . "pages` AS `p`
102 INNER JOIN `".TABLE_PREFIX . "sections`
103
104 USING(`page_id`)
105
106 WHERE `parent` = '0'
107
108 AND `visibility` = 'public'
109
110 AND (
111 (".$now." >= `publ_start` OR `publ_start` = 0)
112 AND
113 (".$now." <= `publ_end` OR `publ_end` = 0)
114 )
115 ".$sql_where_language."
116 ORDER BY
117 `p`.`position`
118
119 ASC LIMIT 1
120 ";
121
122 $fetch_default = [];
123 $this->database->execute_query(
124 $query_default,
125 true,
126 $fetch_default,
127 false
128 );
129
130 if ( !isset( $page_id ) || !is_numeric( $page_id ) )
131 {
132 // Display default page
133 if (!empty($fetch_default))
134 {
135 $this->default_link = $fetch_default[ 'link' ];
136 $this->default_page_id = intval($fetch_default[ 'page_id' ]);
137
138 // Check if we should redirect or include page inline
139 if ( HOMEPAGE_REDIRECTION )
140 {
141 // Redirect to page
142 header( "Location: " . $this->buildPageLink( $this->default_link ) );
143 exit();
144 }
145 else
146 {
147 // Include page inline
148 $this->page_id = $this->default_page_id;
149 }
150 }
151 else
152 {
153 exit();
154 }
155 }
156 else
157 {
158 if(!isset($fetch_default[ 'link' ]))
159 {
160 die(LEPTON_tools::display('This installation has no content yet', 'pre','ui red message'));
161 }
162
163 $this->page_id = $page_id;
164 $this->default_link = $fetch_default[ 'link' ];
165 $this->default_page_id = intval($fetch_default[ 'page_id' ]);
166 $this->page = $fetch_default;
167
168 }
169
170 return true;
171 }
172
173 public function get_page_details()
174 {
175 if ($this->page_id != 0)
176 {
177 $this->page = [];
178 $query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = ".$this->page_id;
179 $this->database->execute_query(
180 $query_page,
181 true,
182 $this->page,
183 false
184 );
185
186 // Make sure page was found in database
187 if (empty($this->page))
188 {
189 // Print page not found message
190 exit( "Page not found." );
191 }
192 else
193 {
194 foreach ($this->page as $key => $value)
195 {
196 // set members of array to constants
197 $key = strtoupper($key);
198 if (!defined($key))
199 {
200 if ($key === 'TEMPLATE' && empty($value))
201 {
202 $value = $this->database->get_one("SELECT value FROM ".TABLE_PREFIX."settings WHERE name = 'default_template' ");
203 }
204
205 if ($key === 'DESCRIPTION' && empty($value))
206 {
207 $value = WEBSITE_DESCRIPTION;
208 }
209
210 if ($key === 'KEYWORDS' && empty($value))
211 {
212 $value = WEBSITE_KEYWORDS;
213 }
214
215 define($key, $value);
216 }
217 }
218 }
219
220//@DEPRECATED_TEMP: see method getPageLanguage() $this->getPageLanguage();
221
222 // Check if the page language is also the selected language. If not, send headers again.
223 if ( $this->page[ 'language' ] != LANGUAGE )
224 {
225 if ( isset( $_SERVER[ 'QUERY_STRING' ] ) && $_SERVER[ 'QUERY_STRING' ] != '' ) // check if there is a query-string
226 {
227 header( 'Location: ' . $this->buildPageLink( $this->page[ 'link' ] ) . '?' . $_SERVER[ 'QUERY_STRING' ] . '&lang=' . $this->page[ 'language' ] );
228 }
229 else
230 {
231 header( 'Location: ' . $this->buildPageLink( $this->page[ 'link' ] ) . '?lang=' . $this->page[ 'language' ] );
232 }
233 exit();
234 }
235
236 // Page trail
237 foreach ( explode( ',', $this->page[ 'page_trail' ] ) AS $pid )
238 {
239 $this->page_trail[ $pid ] = $pid;
240 }
241 }
242
243
244 // Set the template dir
245 if(!defined('TEMPLATE'))
246 {
247 define('TEMPLATE', DEFAULT_TEMPLATE);
248 }
249 define( 'TEMPLATE_DIR', LEPTON_URL . '/templates/' . TEMPLATE );
250 // Check if user is allowed to view this page
251 if ($this->page_is_visible($this->page) === false)
252 {
253 if ( VISIBILITY == 'deleted' || VISIBILITY == 'none' )
254 {
255 // User isn't allowed on this page so tell them
256 $this->page_access_denied = true;
257 }
258 elseif ( VISIBILITY == 'private' || VISIBILITY == 'registered' )
259 {
260 // Check if the user is authenticated
261 if ( $this->is_authenticated() === false )
262 {
263 // User needs to log-in first
264 header( "Location: " . LEPTON_URL . "/account/login.php?redirect=" . $this->buildPageLink($this->page['link']) );
265 exit( 0 );
266 }
267 else
268 {
269 $temp_array = explode(',',$this->page['viewing_groups']);
270 if(in_array( $_SESSION['GROUP_ID'],$temp_array)) {
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
284 $this->maintainConstants();
285
286 // check if there is at least one active section
287 if ($this->page_is_active($this->page) === false)
288 {
289 $this->page_no_active_sections = true;
290 }
291 }
292
293 public function get_website_settings()
294 {
295 // Work-out if any possible in-line search boxes should be shown
296 if ( SEARCH == 'public' )
297 {
298 define( 'SHOW_SEARCH', true );
299 }
300 elseif ( SEARCH == 'private' && VISIBILITY == 'private' )
301 {
302 define( 'SHOW_SEARCH', true );
303 }
304 elseif ( SEARCH == 'private' && $this->is_authenticated() === true )
305 {
306 define( 'SHOW_SEARCH', true );
307 }
308 elseif ( SEARCH == 'registered' && $this->is_authenticated() === true )
309 {
310 define( 'SHOW_SEARCH', true );
311 }
312 else
313 {
314 define( 'SHOW_SEARCH', false );
315 }
316 // Work-out if menu should be shown
317 if ( !defined( 'SHOW_MENU' ) )
318 {
319 define( 'SHOW_MENU', true );
320 }
321 // Work-out if login menu constants should be set
322 if ( FRONTEND_LOGIN )
323 {
324 // Set login menu constants
325 define( 'LOGIN_URL', LEPTON_URL . '/account/login.php' );
326 define( 'LOGOUT_URL', LEPTON_URL . '/account/logout.php' );
327 define( 'FORGOT_URL', LEPTON_URL . '/account/forgot.php' );
328 define( 'PREFERENCES_URL', LEPTON_URL . '/account/preferences.php' );
329 define( 'SIGNUP_URL', LEPTON_URL . '/account/signup.php' );
330 }
331 }
332
338 public function preprocess(string &$content): void
339 {
340 $content = str_ireplace( ["%5B","%5D"], ["[", "]"], $content);
341
342 // starting with L*5 LEPTONlink replaces wblink
343 if (preg_match_all('/\[LEPTONlink([0-9]+)\]/isU', $content, $ids))
344 {
345 $new_ids = array_unique( $ids[ 1 ] );
346 foreach ($new_ids as $key => $temp_page_id)
347 {
348 $link = $this->database->get_one( "SELECT `link` FROM `" . TABLE_PREFIX . "pages` WHERE `page_id` = " . $temp_page_id );
349 if (!is_null($link))
350 {
351 $content = str_replace($ids[0][$key], $this->buildPageLink($link), $content);
352 }
353 }
354 unset($temp_page_id);
355 }
356 }
357
363 public function maintainConstants(): void
364 {
365 $lookFor = [
366 "DESCRIPTION" => WEBSITE_DESCRIPTION,
367 "KEYWORDS" => WEBSITE_KEYWORDS
368 ];
369
370 foreach ($lookFor as $key => $value)
371 {
372 if (!defined($key))
373 {
374 define($key, $value);
375 }
376 }
377 }
378
379
385 public function getPageLanguage(): void
386 {
387 if(!defined ('LANGUAGE'))
388 {
389 if (isset($_GET['lang']) && ($_GET['lang'] != '') && (!is_numeric($_GET['lang'])) && (strlen($_GET['lang']) == 2))
390 {
391 if (0 != PAGE_ID)
392 {
393 $sTempLang = $this->database->get_one("SELECT language FROM ".TABLE_PREFIX."pages WHERE page_id = ".PAGE_ID);
394 if (null != $sTempLang)
395 {
396 define( 'LANGUAGE', $sTempLang );
397 }
398 else
399 {
400 // more or less a theoretical case
401 define('LANGUAGE', DEFAULT_LANGUAGE);
402 }
403 }
404 else
405 {
406 if (isset($_SESSION['USER_ID']))
407 {
408 $sTempLang = $this->database->get_one("SELECT language FROM ".TABLE_PREFIX."users WHERE user_id = ".$_SESSION['USER_ID']);
409 if (null != $sTempLang)
410 {
411 define('LANGUAGE', $sTempLang);
412 }
413 else
414 {
415 define('LANGUAGE', strtoupper($_GET['lang']));
416 }
417 }
418 else
419 {
420 define('LANGUAGE', strtoupper($_GET['lang']));
421 }
422 }
423
424 $_SESSION['LANGUAGE'] = LANGUAGE;
425 }
426 }
427 }
428}
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)
trait LEPTON_singleton