11<?php
2- require_once __DIR__ . "/../lib/orm.php " ;
32require_once __DIR__ . "/../conf/config.php " ;
4-
53session_start ();
64
75if (isset ($ _SESSION ['user ' ]) && !empty ($ _SESSION ['user ' ])) {
86 header ('Location: /home.php ' );
97 exit ;
108}
119
12- // default template variables used when rendering the page (GET)
13- $ login_error = false ;
14-
15- // If this is a POST -> handle login API and return JSON
16- if ($ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ) {
17-
18- // basic JSON response helper
19- function json_response (array $ data , int $ status = 200 ) {
20- http_response_code ($ status );
21- header ('Content-Type: application/json; charset=utf-8 ' );
22- echo json_encode ($ data );
23- exit ;
24- }
25-
26- // read password from POST
27- $ pwd = isset ($ _POST ['password ' ]) ? (string ) $ _POST ['password ' ] : '' ;
28-
29- // simple brute-force protection stored in session
30- if (!isset ($ _SESSION ['login_attempts ' ])) {
31- $ _SESSION ['login_attempts ' ] = 0 ;
32- $ _SESSION ['lockout_until ' ] = 0 ;
33- }
34-
35- $ now = time ();
36- $ maxAttempts = 5 ;
37- $ lockoutSeconds = 10 ; // lockout length after max attempts
38-
39- if ($ _SESSION ['lockout_until ' ] > $ now ) {
40- $ remaining = $ _SESSION ['lockout_until ' ] - $ now ;
41- json_response (['ok ' => false , 'error ' => 'locked ' , 'locked_for ' => $ remaining ], 429 );
42- }
43-
44- // load password from settings table (your ORM usage)
45- $ settingsManager = new PersistentEntityManager (KeyValue::class, $ logger , DB , 'settings ' );
46- $ passwordObject = $ settingsManager ->find (["key " => "passwordHash " ]);
47- $ passwordConfig = $ passwordObject ->value ?? '' ;
48-
49-
50- $ ok = false ;
51- $ ok = password_verify ($ pwd , $ passwordConfig );
52-
53- if (! $ ok ) {
54- $ _SESSION ['login_attempts ' ] += 1 ;
55- if ($ _SESSION ['login_attempts ' ] >= $ maxAttempts ) {
56- $ _SESSION ['lockout_until ' ] = $ now + $ lockoutSeconds ;
57- $ _SESSION ['login_attempts ' ] = 0 ; // reset attempts after enforcing lockout
58- json_response (['ok ' => false , 'error ' => 'locked ' , 'locked_for ' => $ lockoutSeconds ], 429 );
59- } else {
60- json_response (['ok ' => false , 'error ' => 'invalid ' , 'attempts_left ' => $ maxAttempts - $ _SESSION ['login_attempts ' ]], 401 );
61- }
62- }
63-
64- // success: start session (already started) and set session vars
65- session_regenerate_id (true );
66- $ _SESSION ['user ' ] = [
67- 'username ' => 'admin ' ,
68- 'logged_in_at ' => $ now
69- ];
70-
71- // reset attempts on success
72- $ _SESSION ['login_attempts ' ] = 0 ;
73- $ _SESSION ['lockout_until ' ] = 0 ;
74-
75- // respond with success
76- json_response (['ok ' => true , 'redirect ' => '/home.php ' ]);
77- // json_response already exits
78- }
79-
80- // If it's not POST, execution continues and your template is rendered.
81- // You can set $login_error here based on query params or session if you want:
82- if (isset ($ _GET ['error ' ]) && $ _GET ['error ' ] === '1 ' ) {
83- $ login_error = true ;
84- }
85-
10+ $ login_error = isset ($ _GET ['error ' ]) && $ _GET ['error ' ] === '1 ' ;
8611?>
8712{% extends 'templates/base.j2' %}
8813{% block title %}CronDNS Login{% endblock %}
@@ -114,18 +39,15 @@ function json_response(array $data, int $status = 200) {
11439{% endblock %}
11540
11641{% block scripts %}
117- /* ---------- Password visibility toggle ---------- */
11842const pwdInput = document.getElementById('pwd');
11943const toggleBtn = document.getElementById('togglePwd');
120-
12144toggleBtn.addEventListener('click', () => {
12245 const type = pwdInput.type === 'password' ? 'text' : 'password';
12346 pwdInput.type = type;
12447 toggleBtn.classList.toggle('ti-eye-off', type === 'text');
12548 toggleBtn.classList.toggle('ti-eye', type === 'password');
12649});
12750
128- /* ---------- Login: replace simulated handler with real fetch ---------- */
12951const loginBtn = document.getElementById('loginBtn');
13052const loginForm = document.getElementById('loginForm');
13153let lockoutTimer = null;
@@ -143,7 +65,7 @@ function json_response(array $data, int $status = 200) {
14365 const form = new FormData();
14466 form.append('password', pwd);
14567
146- const resp = await fetch('/login.php', {
68+ const resp = await fetch('/api/ login.php', {
14769 method: 'POST',
14870 body: form,
14971 credentials: 'same-origin'
@@ -152,7 +74,7 @@ function json_response(array $data, int $status = 200) {
15274 const data = await resp.json();
15375
15476 if (resp.ok && data.ok) {
155- window.location.href = data.redirect || '/dashboard ';
77+ window.location.href = data.redirect || '/home.php ';
15678 return;
15779 }
15880
0 commit comments