Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // easyIT - André Barros - drewvirtual.space
- require("config.php");
- require("connectDB.php");
- function createUser() {
- global $connectDB;
- if (isset($_POST["registerUsername"]) && isset($_POST["registerName"]) && isset($_POST["registerPassword"]) && isset($_POST["registerPasswordConfirmation"])) {
- $username = htmlspecialchars($_POST["registerUsername"]);
- $name = $_POST["registerName"];
- $pw1 = $_POST["registerPassword"];
- $pw2 = $_POST["registerPasswordConfirmation"];
- if ($pw1 === $pw2) {
- $password = password_hash($pw1, PASSWORD_DEFAULT);
- if ($connectDB->connect_error) {
- die("<p>Serviço indisponível. Por favor tente mais tarde</p>");
- }
- $inst = $connectDB->prepare("INSERT INTO app_users(user_Username, user_Name, user_Hash) VALUES(?, ?, ?)");
- $inst->bind_param("sss", $username, $name, $password);
- if ($inst->execute()) {
- echo json_encode("true");
- } else {
- echo json_encode("used");
- }
- } else {
- echo json_encode("errorpw");
- }
- } else {
- echo json_encode("false");
- }
- }
- function userLogin() {
- session_start();
- global $connectDB;
- if (isset($_POST["loginUsername"]) && isset($_POST["loginPassword"])) {
- if ($connectDB->connect_error) {
- http_response_code(503);
- die(json_encode("false"));
- }
- $username = htmlspecialchars($_POST["loginUsername"]);
- $inst = $connectDB->prepare("SELECT user_Id, user_Hash, user_Username, user_Previleges FROM app_users WHERE user_Username=?");
- $inst->bind_param("s", $username);
- $inst->execute();
- $res = $inst->get_result();
- if ($res->num_rows === 0) {
- http_response_code(200);
- $connectDB->close();
- die(json_encode("false"));
- }
- $linha = $res->fetch_assoc();
- http_response_code(200);
- $connectDB->close();
- if (password_verify($_POST["loginPassword"], $linha["user_Hash"])) {
- if ($linha["user_Previleges"] === "1") {
- $_SESSION['userAuth'] = "Yes";
- $_SESSION['userID'] = $linha["user_Id"];
- $_SESSION['userType'] = "Admin";
- $_SESSION['userName'] = $linha["user_Username"];
- echo json_encode("trueAdmin");
- } else {
- $_SESSION['userAuth'] = "Yes";
- $_SESSION['userID'] = $linha["user_Id"];
- $_SESSION['userType'] = "User";
- $_SESSION['userName'] = $linha["user_Username"];
- echo json_encode("trueUser");
- }
- } else {
- echo json_encode("false");
- }
- } else {
- echo json_encode("false");
- }
- }
- function userLogout() {
- session_start();
- session_destroy();
- //BASE URL
- header("Location: ../../../../index.php");
- }
- function checkAuth() {
- if (($_SESSION['userType']) != "Admin") {
- //BASE URL
- header("Location: ../../../../../index.php");
- exit;
- }
- }
- ?>
- function showRegisterForm() {
- $('.loginBox').fadeOut('fast', function () {
- $('.registerBox').fadeIn('fast');
- $('.login-footer').fadeOut('fast', function () {
- $('.register-footer').fadeIn('fast');
- });
- $('.modal-title').html('easyIT - Register');
- });
- $('.error').removeClass('alert alert-danger').html('');
- }
- function showLoginForm() {
- $('#loginModal .registerBox').fadeOut('fast', function () {
- $('.loginBox').fadeIn('fast');
- $('.register-footer').fadeOut('fast', function () {
- $('.login-footer').fadeIn('fast');
- });
- $('.modal-title').html('easyIT - Login');
- });
- $('.error').removeClass('alert alert-danger').html('');
- }
- function openLoginModal() {
- showLoginForm();
- setTimeout(function () {
- $('#loginModal').modal('show');
- }, 230);
- }
- function openRegisterModal() {
- showRegisterForm();
- setTimeout(function () {
- $('#loginModal').modal('show');
- }, 230);
- }
- $('#loginUser').on('click', function () {
- $.ajax({
- //Define server side authentication function
- url: "assets/php/includes/login/userLogin.php",
- type: "POST",
- dataType: "json",
- data: {
- loginUsername: $("#loginUsername").val(),
- loginPassword: $("#loginPassword").val()
- },
- success: function (resposta) {
- if (resposta === "trueAdmin") {
- window.location.href = 'dashboard.php';
- } else if (resposta === "trueUser") {
- window.location.href = 'index.php';
- } else if (resposta === "false") {
- shakeModal();
- }
- },
- error: function (xhr, status, error) {
- }
- });
- });
- $('#registerUser').on('click', function () {
- $.ajax({
- url: "assets/php/includes/login/createUser.php",
- type: "POST",
- dataType: "json",
- data: {
- registerUsername: $("#registerUsername").val(),
- registerName: $("#registerName").val(),
- registerPassword: $("#registerPassword").val(),
- registerPasswordConfirmation: $("#registerPasswordConfirmation").val(),
- },
- success: function (resposta) {
- if (resposta === "true") {
- openLoginModal();
- }
- else{
- shakeModal2();
- }
- },
- error: function (xhr, status, error) {
- }
- });
- });
- function shakeModal() {
- $('#loginModal .modal-dialog').addClass('shake');
- $('.error').addClass('alert alert-danger').html("Invalid username/password combination");
- $('input[type="password"]').val('');
- setTimeout(function () {
- $('#loginModal .modal-dialog').removeClass('shake');
- }, 1000);
- }
- function shakeModal2() {
- $('#loginModal .modal-dialog').addClass('shake');
- $('.error').addClass('alert alert-danger').html("Fill all the fields/username already taken");
- $('input[type="password"]').val('');
- setTimeout(function () {
- $('#loginModal .modal-dialog').removeClass('shake');
- }, 1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement