]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - login/index.php
Move some PHP files from ./ to ./inc: common, connection, header, footer
[irc/unrealircd/unrealircd-webpanel.git] / login / index.php
index 178b1eefca1cb1d06ce462311cd9beb53d1f4957..56dc0a72612cd979ff29d7cf8c9ab37630a3de7a 100644 (file)
@@ -1,18 +1,17 @@
-
 <?php
-require_once "../common.php";
+require_once "../inc/common.php";
 
 $logout = false;
 
-$redirect = BASE_URL;
+$redirect = get_config("base_url");
 if (!empty($_GET['redirect']))
 {
        $str = urldecode($_GET['redirect']);
-       if (str_starts_with($str, BASE_URL)) // prevent redirects to like https://othersite/
+       if (str_starts_with($str, get_config("base_url"))) // prevent redirects to like https://othersite/
                $redirect = $_GET['redirect'];
 }
 
-$redirect = (isset($_GET['redirect'])) ? $_GET['redirect'] : BASE_URL;
+$redirect = (isset($_GET['redirect'])) ? $_GET['redirect'] : get_config("base_url");
 if (!empty($_GET['logout']))
 {
        if (!isset($_SESSION['id']))
@@ -23,25 +22,45 @@ if (!empty($_GET['logout']))
                $logout = true;
        }
 }
+if (!empty($_GET['timeout']))
+{
+       $failmsg = "Your session has timed out. Please login again to continue";
+       $_SESSION = NULL;
+       session_destroy();
+}
 if (!empty($_POST))
 {
        if ($_POST['username'] && $_POST['password'])
        {
-               
-               /* securitah */
-               security_check();
                $user = new PanelUser($_POST['username']);
                /* not being too informative with the login error in case of attackers */
-               if (isset($user->id) && $user->password_verify($_POST['password']))
+               $hash_needs_updating = false;
+               if (isset($user->id) && $user->password_verify($_POST['password'], $hash_needs_updating))
                {
+                       /* SUCCESSFUL LOGIN */
+                       if ($hash_needs_updating)
+                       {
+                               /* Set password again so it is freshly hashed */
+                               $hash = PanelUser::password_hash($_POST['password']);
+                               $ar = ["update_pass_conf"=>$hash];
+                               $user->update_core_info($ar);
+                               unset($ar);
+                               unset($hash);
+                       }
+                       panel_start_session($user);
                        $_SESSION['id'] = $user->id;
-                       header('Location: ' . $redirect);
                        $user->add_meta("last_login", date("Y-m-d H:i:s"));
                        Hook::run(HOOKTYPE_USER_LOGIN, $user);
+
+                       /* Middle of install? Override redirect: */
+                       if (!isset($config['unrealircd']) || empty($config['unrealircd']['host']))
+                               $redirect = get_config("base_url")."settings/install2.php";
+                       header('Location: ' . $redirect);
                        die();
                }
                else
                {
+                       /* LOGIN FAILED */
                        $fail = [
                                "login" => htmlspecialchars($_POST['username']),
                                "IP" => $_SERVER['REMOTE_ADDR']
@@ -57,8 +76,8 @@ if (!empty($_POST))
 
 ?><!DOCTYPE html>
 <head>
-<link href="<?php echo BASE_URL; ?>css/unrealircd-admin.css" rel="stylesheet">
-<script src="<?php echo BASE_URL; ?>js/unrealircd-admin.js"></script>
+<link href="<?php echo get_config("base_url"); ?>css/unrealircd-admin.css" rel="stylesheet">
+<script src="<?php echo get_config("base_url"); ?>js/unrealircd-admin.js"></script>
  <!-- Latest compiled and minified CSS -->
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
 
@@ -74,7 +93,7 @@ if (!empty($_POST))
 <!-- Font Awesome icons -->
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css">
 
-<link rel="icon" type="image/x-icon" href="<?php echo BASE_URL; ?>img/favicon.ico">
+<link rel="icon" type="image/x-icon" href="<?php echo get_config("base_url"); ?>img/favicon.ico">
 <title>UnrealIRCd Panel</title>
 </head>
 <section class="vh-100">
@@ -83,8 +102,8 @@ if (!empty($_POST))
          <div class="col-12 col-md-8 col-lg-6 col-xl-5">
                <div class="card shadow-2-strong" style="border-radius: 1rem;">
                  <div class="card-body p-5 text-center">
-                       <form method="post" action="index.php?redirect=<?php echo $redirect; ?>">
-                               <h3><img src="<?php echo BASE_URL; ?>img/favicon.ico">  Log in to use Admin Panel</h3>
+                       <form id="login" method="post" action="index.php?redirect=<?php echo $redirect; ?>">
+                               <h3><img src="<?php echo get_config("base_url"); ?>img/favicon.ico">    Log in to use Admin Panel</h3>
                                
                                        <?php 
                                        if (isset($failmsg)) Message::Fail($failmsg);
@@ -92,15 +111,23 @@ if (!empty($_POST))
                                                Message::Success("You have been logged out");
                                        ?>
                                        <div class="input-group">
-                                       <div class="input-group mb-3">
+                                       <div id="username" class="input-group mb-3">
                                                <div class="input-group-prepend">
                                                        <span class="input-group-text" id="basic-addon1"><i class="fa-solid fa-user"></i></span>
-                                               </div><input type="text" class="form-control" name="username" id="username" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
+                                               </div><input type="text" id="userinp" class="form-control" name="username" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
+                                               <div id="user_inv" class="invalid-feedback">
+                                                       Username cannot be empty.
+                                               </div>
+
                                        </div>
-                                       <div class="input-group mb-3">
+                                       <div id="password" class="input-group mb-3">
                                                <div class="input-group-prepend">
                                                        <span class="input-group-text" id="basic-addon1"><i class="fa-solid fa-key"></i></span>
-                                               </div><input type="password" class="form-control" name="password" id="password" placeholder="Password">
+                                               </div><input type="password" id="passinp" class="form-control" name="password" placeholder="Password">
+                                               <div id="pass_inv" class="invalid-feedback">
+                                               Password cannot be empty.
+                                               </div>
+
                                        </div>
 
                                </div>
@@ -111,4 +138,31 @@ if (!empty($_POST))
        </div>
 </div>
 </div></section>
-<?php require_once "../footer.php";
\ No newline at end of file
+
+<script>
+       var form = document.getElementById('login');
+       var pinp = document.getElementById('passinp');
+       var uinp = document.getElementById('userinp');
+       
+       form.addEventListener('submit', (event) =>
+       {
+               event.preventDefault();
+               var err = 0;
+               if (uinp.value.length == 0)
+               {
+                       $('#user_inv').show();
+                       err++;
+               }
+               if (pinp.value.length == 0)
+               {
+                       $('#pass_inv').show();
+                       err++;
+               }
+               if (err)
+                       return;
+               else
+                       form.submit();
+       });
+</script>
+
+<?php require_once "../inc/footer.php";
\ No newline at end of file