89 $MESSAGE = LEPTON_core::getGlobal(
"MESSAGE");
91 if (NULL === self::$instance)
93 self::$instance = $this;
98 if (isset($config_array[
'USERNAME_FIELDNAME']))
100 $this->username_fieldname = $config_array[
'USERNAME_FIELDNAME'];
103 if (isset($config_array[
'PASSWORD_FIELDNAME']))
105 $this->password_fieldname = $config_array[
'PASSWORD_FIELDNAME'];
108 if (isset($config_array[
'WARNING_URL']))
110 $this->warning_url = $config_array[
'WARNING_URL'];
113 if (isset($config_array[
'LOGIN_URL']))
115 $this->login_url = $config_array[
'LOGIN_URL'];
118 if (isset($config_array[
'TEMPLATE_DIR']))
120 $this->template_dir = $config_array[
'TEMPLATE_DIR'];
123 if (isset($config_array[
'TEMPLATE_FILE']))
125 $this->template_file = $config_array[
'TEMPLATE_FILE'];
128 if (isset($config_array[
'frontend']))
130 $this->frontend = $config_array[
'frontend'];
133 if (isset($config_array[
'FORGOTTEN_URL']))
135 $this->forgotten_url = $config_array[
'FORGOTTEN_URL'];
138 if (isset($config_array[
'REDIRECT_URL']))
140 $this->redirect_url = $config_array[
'REDIRECT_URL'];
144 $this->redirect_url = LEPTON_core::getValue(
'redirect',
'string_clean',
'request') ??
"";
148 if (!is_null(LEPTON_core::getValue(
'username_fieldname',
'username',
'post')))
150 $username_fieldname = LEPTON_core::getValue(
'username_fieldname',
'username',
'post');
151 $password_fieldname = LEPTON_core::getValue(
'password_fieldname',
'username',
'post');
155 $username_fieldname =
'username';
156 $password_fieldname =
'password';
159 $this->username = LEPTON_core::getValue($username_fieldname,
'clean_string',
'post') ??
"";
161 $this->password = LEPTON_core::getValue($password_fieldname,
'password',
'post') ??
"";
164 $this->username_len = strlen($this->username);
165 $this->password_len = strlen($this->password);
168 if ($this->redirect_url !=
'')
174 $this->url = LEPTON_core::getValue(
'url') ??
'';
177 if (strlen($this->url) < 2)
179 $token = (!LEPTOKEN_LIFETIME) ?
'' :
'?leptoken=' . $this->createLepToken();
180 $this->url = ($config_array[
'DEFAULT_URL'] ??
"") . $token;
183 if ($this->is_authenticated() ===
true)
186 header(
'Location: '.$this->url);
189 elseif ($this->username ==
'')
191 $this->message = $MESSAGE[
'LOGIN_USERNAME_BLANK'];
194 elseif ($this->password ==
'')
196 $this->message = $MESSAGE[
'LOGIN_PASSWORD_BLANK'];
199 elseif ($this->username_len < AUTH_MIN_LOGIN_LENGTH)
201 $this->message = $MESSAGE[
'LOGIN_USERNAME_TOO_SHORT'];
204 elseif ($this->password_len < AUTH_MIN_PASS_LENGTH)
206 $this->message = $MESSAGE[
'LOGIN_PASSWORD_TOO_SHORT'];
214 $token = (!LEPTOKEN_LIFETIME) ?
'' :
'?leptoken=' . $this->createLepToken();
219 $browser_fingerprint = hash( self::FINGERPRINT_HASH_ALGO, $_SERVER[
'HTTP_USER_AGENT'] );
220 $ip_fingerprint = hash(self::FINGERPRINT_HASH_ALGO, $_SERVER[
'REMOTE_ADDR'] );
225 'temp_time' => TIME()
232 "temp_ip = '".$ip_fingerprint.
"' AND temp_browser='".$browser_fingerprint.
"'"
238 if (TFA ===
'local' && LEPTOKEN_LIFETIME > 0)
241 $oTFA->initialize(intval($_SESSION[
'USER_ID']));
243 if (isset($_REQUEST[
'redirect']))
245 if ($oTFA->key_new ===
true)
247 if (isset($_POST[
'submit']) )
249 $oTFA->set_fe_pin(
'create');
254 header(
'Location: '.LEPTON_URL.
'/account/logout.php');
260 if (isset($_POST[
'submit']))
262 $oTFA->display_fe_pin(
'display');
267 header(
'Location: '.LEPTON_URL.
'/account/logout.php');
274 if ($oTFA->key_new ===
true)
276 if (isset($_POST[
'submit']))
278 $oTFA->set_be_pin(
'create');
283 header(
'Location: '.ADMIN_URL.
'/logout/index.php');
289 if (!isset($_POST[
'lkey']))
291 $oTFA->display_be_pin(
'display');
296 header(
'Location: '.ADMIN_URL.
'/logout/index.php');
305 if (TFA ===
'mail' && LEPTOKEN_LIFETIME > 0)
308 $oTFA->initialize(intval($_SESSION[
'USER_ID']));
309 if(isset($_REQUEST[
'redirect']))
312 $oTFA->display_fe_pin(
'display');
317 if (!isset($_POST[
'lkey']) )
320 $oTFA->display_be_pin(
'display');
325 header(
'Location: '.ADMIN_URL.
'/logout/index.php');
333 header(
"Location: ".$this->url . $token);
338 $this->message = $MESSAGE[
'LOGIN_AUTHENTICATION_FAILED'];
354 $loginName = (preg_match(
'/[\;\=\&\|<> ]/', $this->username) ?
'' : $this->username);
357 'SELECT `password` FROM `'.$this->USERS_TABLE.
'` WHERE `username` = "'.$loginName.
'" AND `active` = 1',
363 if (!empty($results_array))
365 $check = password_verify($this->password,$results_array[
'password']);
372 $authenticated_user = [];
374 'SELECT * FROM `'.$this->USERS_TABLE.
'` WHERE `username` = "'.$loginName.
'" AND `active` = 1',
380 $this->user_id = intval($authenticated_user[
'user_id']);
381 $_SESSION[
'USER_ID'] =$this->user_id;
382 $_SESSION[
'GROUPS_ID'] = $authenticated_user[
'groups_id'];
383 $_SESSION[
'USERNAME'] = $authenticated_user[
'username'];
384 $_SESSION[
'DISPLAY_NAME'] = $authenticated_user[
'display_name'];
385 $_SESSION[
'EMAIL'] = $authenticated_user[
'email'];
386 $_SESSION[
'HOME_FOLDER'] = $authenticated_user[
'home_folder'];
389 if ($authenticated_user[
'language'] !=
'')
391 $_SESSION[
'LANGUAGE'] = $authenticated_user[
'language'];
395 if ($authenticated_user[
'timezone_string'] !=
'')
397 $_SESSION[
'TIMEZONE_STRING'] = $authenticated_user[
'timezone_string'];
399 $timezone_string = ($_SESSION[
'TIMEZONE_STRING'] ?? DEFAULT_TIMEZONE_STRING);
400 date_default_timezone_set($timezone_string);
403 if ($authenticated_user[
'date_format'] !=
'')
405 $_SESSION[
'DATE_FORMAT'] = $authenticated_user[
'date_format'];
410 $_SESSION[
'USE_DEFAULT_DATE_FORMAT'] =
true;
413 if ($authenticated_user[
'time_format'] !=
'')
415 $_SESSION[
'TIME_FORMAT'] = $authenticated_user[
'time_format'];
420 $_SESSION[
'USE_DEFAULT_TIME_FORMAT'] =
true;
424 $_SESSION[
'SYSTEM_PERMISSIONS'] = [];
425 $_SESSION[
'MODULE_PERMISSIONS'] = [];
427 $_SESSION[
'GROUP_NAME'] = [];
430 foreach (LEPTON_core::getValue(
'groups_id',
'string',
'session',
',') as $cur_group_id)
432 $results_array_2 = [];
434 "SELECT * FROM ".$this->GROUPS_TABLE.
" WHERE group_id = ".$cur_group_id,
440 if (empty($results_array_2))
445 $_SESSION[
'GROUP_NAME'][$cur_group_id] = $results_array_2[
'name'];
448 $_SESSION[
'SYSTEM_PERMISSIONS'] = array_merge($_SESSION[
'SYSTEM_PERMISSIONS'], explode(
',', $results_array_2[
'system_permissions']));
453 $_SESSION[
'MODULE_PERMISSIONS'] = explode(
',', $results_array_2[
'module_permissions']);
457 $_SESSION[
'MODULE_PERMISSIONS'] = array_merge($_SESSION[
'MODULE_PERMISSIONS'], explode(
',', $results_array_2[
'module_permissions']));
463 if (
false === $this->frontend)
466 $aTempGroups = explode(
",",$authenticated_user[
'groups_id']);
467 $bGotBackendAccess =
false;
468 foreach($aTempGroups as $tempGroupId)
470 $check_backend = intval(
$database->get_one(
"SELECT `backend_access` FROM `".$this->GROUPS_TABLE.
"` WHERE `group_id` = ".$tempGroupId ) );
471 if( 1 === $check_backend)
473 $bGotBackendAccess =
true;
474 $_SESSION[
'BACKEND_ACCESS'] = $check_backend;
478 if (
false === $bGotBackendAccess)
482 header(
'Location:'.LEPTON_URL);
490 "login_when" => time(),
491 "login_ip" => $_SERVER[
'REMOTE_ADDR']
498 "user_id = ".$authenticated_user[
'user_id']
543 if ($_SESSION[
'ATTEMPS'] > $this->max_attempts)
549 if ($this->frontend ===
false)
552 'ACTION_URL' => $this->login_url,
553 'ATTEMPS' => LEPTON_core::getValue(
'ATTEMPS',
'integer',
'session'),
554 'USERNAME' => $this->username,
555 'USERNAME_FIELDNAME' => $this->username_fieldname,
556 'PASSWORD_FIELDNAME' => $this->password_fieldname,
558 'LEPTON_URL' => LEPTON_URL,
559 'THEME_URL' => THEME_URL,
560 'LEPTON_VERSION' => LEPTON_VERSION,
561 'LANGUAGE' => strtolower(LANGUAGE),
562 'FORGOTTEN_URL' => $this->forgotten_url,
563 'TEXT_FORGOTTEN_DETAILS' => $TEXT[
'FORGOTTEN_DETAILS'],
564 'TEXT_USERNAME' => $TEXT[
'USERNAME'],
565 'TEXT_PASSWORD' => $TEXT[
'PASSWORD'],
566 'TEXT_LOGIN' => $MENU[
'LOGIN'],
567 'TEXT_HOME' => $TEXT[
'HOME'],
568 'PAGES_DIRECTORY' => PAGES_DIRECTORY,
569 'SECTION_LOGIN' => $MENU[
'LOGIN'],
570 'CHARSET' => DEFAULT_CHARSET
573 $oTWIG = lib_twig_box::getInstance();
574 $oTWIG->loader->prependPath( $this->template_dir,
"backend" );
577 "@backend/".$this->template_file,
583 header(
'Location:'.LEPTON_URL);