This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/pages/login/controller.php

99 lines
2.6 KiB
PHP
Raw Normal View History

2023-10-21 20:47:11 +02:00
<?php
class Login extends Page {
2023-10-22 01:46:22 +02:00
function setMenu()
{
if($_SESSION['user'])
2023-10-23 13:59:26 +02:00
{
2023-10-22 01:46:22 +02:00
$this->menu_text = $_SESSION['userid'];
2023-10-23 13:59:26 +02:00
}
2023-10-22 01:46:22 +02:00
else
$this->menu_text = 'Login';
$this->menu_image = 'far fa-user';
$this->menu_priority = 99;
}
2023-10-22 09:57:29 +02:00
function setSubmenu()
{
if($_SESSION['user'])
{
$this->addSubmenuItem('Settings', '/settings', 'fas fa-cog');
$this->addSubmenuItem('Logout', '/login/logout', 'fas fa-sign-out-alt', 'bg-red-500');
}
}
2023-10-21 20:47:11 +02:00
function index()
{
2023-10-23 13:59:26 +02:00
2023-10-21 20:47:11 +02:00
$this->set('template', 'login.html');
//return print_r($_REQUEST, true);
}
2023-10-21 22:12:56 +02:00
function test()
{
return nl2br(print_r([
'uuid' => gen_ulid(),
'timestamp' => ulid_to_timestamp("01HD9XN98F8SGT01X527KBNHRN"),
'freshtime' => ulid_to_timestamp(gen_ulid())
],true));
}
2023-10-22 09:57:29 +02:00
function logout()
{
session_destroy();
$this->redirect('/');
}
2023-10-21 21:30:20 +02:00
function validate()
{
2023-10-23 13:59:26 +02:00
$email = trim($_REQUEST['email']);
$password = trim($_REQUEST['password']);
2023-10-21 21:30:20 +02:00
$remember = $_REQUEST['remember'];
2023-10-23 13:59:26 +02:00
$error = false;
$u = new User();
if(!$email || !$password)
$error = 'Bitte gib deine E-Mail-Adresse und dein Passwort ein';
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
$error = 'Bitte gib eine gültige E-Mail-Adresse ein';
else if(!$u->exists($email))
$error = 'Benutzer nicht gefunden. Schon registriert?';
else {
try{
$u->load($email);
}
catch(Exception $e){
$error = $e->getMessage();
}
if(!password_verify($password, $u->data['password']))
$error = 'E-Mail-Adresse oder Passwort falsch';
else if($u->data['active'] == 0)
$error = 'Dein Account ist noch nicht aktiviert';
else
{
$u->login();
2023-10-26 22:04:05 +02:00
if($_SERVER['HTTP_HX_CURRENT_URL'] && !endsWith($_SERVER['HTTP_HX_CURRENT_URL'],'/login'))
$this->redirect($_SERVER['HTTP_HX_CURRENT_URL']);
else
$this->redirect('/');
2023-10-23 13:59:26 +02:00
}
}
if($error)
{
$this->set('template', '/templates/partials/error.html');
$this->set('errorTitle', 'Error');
$this->set('errorMessage', $error);
}
//return print_r(['email'=>$email,'password'=>$password,'remember'=>$remember], true);
2023-10-21 21:30:20 +02:00
}
2023-10-21 20:47:11 +02:00
}