diff --git a/src/app.php b/src/app.php index 00666354f8aac16058562fb5e4dc1b4a81243a83..86dad5f242fae056960c6bd5806d9fbdc22ead2c 100644 --- a/src/app.php +++ b/src/app.php @@ -18,6 +18,7 @@ class DagoJek { $instance = new DagoJek(); } + include_once 'model/User.php'; $instance->includeAllController(); return $instance; diff --git a/src/controller/EmailValidationController.php b/src/controller/EmailValidationController.php new file mode 100644 index 0000000000000000000000000000000000000000..456f93af7bc75e8e33ce4e8de4139d7644303dbb --- /dev/null +++ b/src/controller/EmailValidationController.php @@ -0,0 +1,25 @@ +<?php +/** + * Created by PhpStorm. + * User: iqbal + * Date: 05/10/17 + * Time: 23:14 + */ + + include_once "Controller.php"; + include_once "../model/User.php"; + + $emailInput = $_REQUEST['q']; + + $pdo = DB::getInstance(); + if ($emailInput !== "") { + if (filter_var($emailInput, FILTER_VALIDATE_EMAIL)) { + if (User::GetUserBy("email", $emailInput, $pdo)) { + echo "unavailable"; + } else { + echo "available"; + } + } else { + echo "unavailable"; + } + } \ No newline at end of file diff --git a/src/controller/LoginController.php b/src/controller/LoginController.php new file mode 100644 index 0000000000000000000000000000000000000000..e4fca67ebe8cf010d70a61c4075ae30d2e5714b0 --- /dev/null +++ b/src/controller/LoginController.php @@ -0,0 +1,31 @@ +<?php +/** + * Created by PhpStorm. + * User: iqbal + * Date: 04/10/17 + * Time: 16:29 + */ + + session_start(); + + include_once "Controller.php"; + include_once "../model/User.php"; + + $pdo = DB::getInstance(); + $userData = User::GetAllUsernameAndPassword($pdo); + + if (isset($_GET['logout'])) { + $_SESSION['username'] = ""; + header("Location: ../view/login.html"); + exit; + } + + if (isset($_POST['username'])) { + if ($userData[$_POST['username']] === md5($_POST['password'])) { + $_SESSION['username'] = $_POST['username']; + //header untuk redirect + echo "<script type='application/javascript'> alert('Login berhasil'); </script>"; + } else { + echo "<script type='application/javascript'> alert('Username atau password salah'); </script>"; + } + } \ No newline at end of file diff --git a/src/controller/MainController.php b/src/controller/MainController.php index c0fa8167c4d80d68f33d6ea3e1411852efec3e28..52c83e7c90cc8ac8fa8c129308584b4a3c1d9f49 100644 --- a/src/controller/MainController.php +++ b/src/controller/MainController.php @@ -3,7 +3,8 @@ class MainController { public static function LoginHandler() { - echo "This is login handler"; + header("Location: http://dagojek.com/src/view/login.html"); + die(); } public static function DefaultHandler() { diff --git a/src/controller/RegisterController.php b/src/controller/RegisterController.php new file mode 100644 index 0000000000000000000000000000000000000000..ad3f27b9eaf5dd17a48c04db23df7e878a41f9d2 --- /dev/null +++ b/src/controller/RegisterController.php @@ -0,0 +1,32 @@ +<?php +/** + * Created by PhpStorm. + * User: iqbal + * Date: 05/10/17 + * Time: 16:43 + */ + + include_once "Controller.php"; + include_once "../model/User.php"; + + $hashedPassword = md5($_POST['password']); + + $newUser = array( + "id" => 0, + "name" => $_POST['name'], + "username" => $_POST['username'], + "email" => $_POST['email'], + "password" => $hashedPassword, + "phone" => $_POST['phone'], + "photo" => "http://www.simian-risk.com/wp-content/themes/custom/images/empty-profile.png", + "is_driver" => 0 + ); + + if (isset($_POST['is_driver'])) { + $newUser['is_driver'] = 1; + } + + $pdo = DB::getInstance(); + User::InsertUser($newUser, $pdo); + //header untuk redirect + echo "<script> alert('Registrasi berhasil.');</script>"; \ No newline at end of file diff --git a/src/controller/UsernameValidationController.php b/src/controller/UsernameValidationController.php new file mode 100644 index 0000000000000000000000000000000000000000..c0f29f4392cc22bbc93c5c6538ba016c06c2b250 --- /dev/null +++ b/src/controller/UsernameValidationController.php @@ -0,0 +1,21 @@ +<?php +/** + * Created by PhpStorm. + * User: iqbal + * Date: 05/10/17 + * Time: 23:12 + */ + + include_once "Controller.php"; + include_once "../model/User.php"; + + $usernameInput = $_REQUEST['q']; + + $pdo = DB::getInstance(); + if ($usernameInput !== "") { + if (User::GetUserBy("username", $usernameInput, $pdo)) { + echo "unavailable"; + } else { + echo "available"; + } + } \ No newline at end of file diff --git a/src/model/User.php b/src/model/User.php index 92c700e0b37e7c9f4b22819784a292ac2b9c167d..a62ca85ca40a653a3621e429f91ae6f3e39aab93 100644 --- a/src/model/User.php +++ b/src/model/User.php @@ -28,5 +28,99 @@ class User { } } + public static function GetAllUsers(PDO $conn) { + try { + $result = $conn->query("SELECT * FROM user")->fetchAll(); + return $result; + + } catch (PDOException $e) { + echo "Error: ".$e->getMessage(); + return false; + } + } + + public static function GetAllUsernameAndPassword(PDO $conn) { + try { + $users = $conn->query("SELECT username, password FROM user")->fetchAll(); + foreach ($users as $record) { + $result[$record['username']] = $record['password']; + } + return $result; + } catch (PDOException $e) { + echo "Error: ".$e->getMessage(); + return false; + } + } + + public static function GetUserById($id, PDO $conn) { + try { + $stmt = $conn->prepare("SELECT * FROM user WHERE id=?"); + $stmt->execute([$id]); + + $user = $stmt->fetchObject(); + return $user; + } catch (PDOException $e) { + echo "Error: ".$e->getMessage(); + return false; + } + } + + public static function GetUserBy($attribute, $value, PDO $conn) { + try { + $stmt = $conn->prepare("SELECT * FROM user WHERE $attribute='$value'"); + $stmt->execute(); + + $user = $stmt->fetchObject(); + return $user; + } catch (PDOException $e) { + echo "Error: ".$e->getMessage(); + return false; + } + } + + public static function UpdateUser($user, PDO $conn) { + try { + if ($user instanceof User) { + $newAttributes = ""; + $newAttributes .= "name = "."$user->name, "; + $newAttributes .= "email = "."$user->email, "; + $newAttributes .= "phone = "."$user->phone, "; + $newAttributes .= "is_driver = "."$user->isDriver"; + + $conn->prepare("UPDATE user SET $newAttributes WHERE id =?")->execute([$user->id]); + } + } catch (PDOException $e) { + echo "Error: ".$e->getMessage(); + return false; + } + } + + public static function InsertUser($user, PDO $conn) { + try { + $lastUser = $conn->query("SELECT * FROM user ORDER BY id DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC); + $newId = $lastUser['id'] + 1; + $values = ""; + $columns = ""; + + foreach ($user as $key => $attr) { + if ($key !== "id" && $key !== "is_driver") { + $columns .= $key.","; + $values .= "'$attr'".","; + } else if ($key === "is_driver") { + $columns .= $key; + $values .= $attr; + } + } + $columns = "id,".$columns; + $values = $newId.",".$values; + + $insertExpression = "INSERT INTO user ($columns) VALUES ($values)"; + + $conn->query($insertExpression); + } catch (PDOException $e) { + echo "Error: ".$e->getMessage(); + return false; + } + } } \ No newline at end of file diff --git a/src/view/login.html b/src/view/login.html new file mode 100644 index 0000000000000000000000000000000000000000..724d78de82b4a11ae24f7211edeab2fb44662ac2 --- /dev/null +++ b/src/view/login.html @@ -0,0 +1,43 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Login Page | Dagojek</title> + <link rel="stylesheet" type="text/css" href="style.css"> +</head> + +<body id="login-body"> + +<div class="container"> + + <header id="login-header"> + <h1>Dagojek Login</h1> + </header> + + <section id="login-main"> + <div id="login-panel"> + <form name="loginForm" method="post" action="../controller/LoginController.php"> + <fieldset> + <legend>Login:</legend> + Username: <br> + <input type="text" placeholder="Username" name="username"> + <br> + Password: <br> + <input type="password" placeholder="Password" name="password"> + <br> + <input type="submit" value="Login"> + <a id="register-link" href="register.html">Don't have an account?</a> + <br> + </fieldset> + </form> + </div> + </section> + + <footer> + + </footer> + +</div> + +</body> +</html> \ No newline at end of file diff --git a/src/view/register.html b/src/view/register.html new file mode 100644 index 0000000000000000000000000000000000000000..8438a3df9bfa4d20d1278c71ddce6a2a660e59a9 --- /dev/null +++ b/src/view/register.html @@ -0,0 +1,93 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Register Page | Dagojek</title> + <link rel="stylesheet" type="text/css" href="style.css"> +</head> + +<body id="register-body"> + +<div class="container"> + + <header id="register-header"> + <h1> + Dagojek Register<br> + <small>Create New Account</small> + </h1> + </header> + + <section id="register-main"> + <div id="register-panel"> + <form id="register-form" name="registerForm" method="post" action="../controller/RegisterController.php"> + <fieldset> + <legend>Sign Up</legend> + <table> + <tr> + <td> + Name: + </td> + <td> + <input type="text" placeholder="Your name" id="name" name="name"> + </td> + </tr> + <tr> + <td> + Username: + </td> + <td> + <input id="username" type="text" placeholder="Username" name="username"> + </td> + </tr> + <tr> + <td> + Email: + </td> + <td> + <input id="email" type="email" placeholder="Email address" name="email"> + </td> + </tr> + <tr> + <td> + Password: + </td> + <td> + <input type="password" placeholder="Password" id="password" name="password"> + </td> + </tr> + <tr> + <td> + Confirm Password: + </td> + <td> + <input type="password" placeholder="Confirm password" id="confirm-password" name="confirm-password"> + </td> + </tr> + <tr> + <td> + Phone Number: + </td> + <td> + <input type="number" placeholder="Phone number" id="phone" name="phone"> + </td> + </tr> + </table> + <input type="checkbox" value="is-driver" name="is_driver"> I want to be a driver as well! + <br> + <input id="sign-up-btn" type="submit" value="Sign up" disabled> + <br> + </fieldset> + </form> + </div> + </section> + + <footer> + + </footer> + +</div> + +<!-- Script --> +<script type="application/javascript" src="script.js"></script> +</body> +</html> \ No newline at end of file diff --git a/src/view/script.js b/src/view/script.js new file mode 100644 index 0000000000000000000000000000000000000000..ef52f8ebb5b84221116d7cf84bae413c5a59b5f1 --- /dev/null +++ b/src/view/script.js @@ -0,0 +1,103 @@ +function checkAvailability(string, elmtID, dataCollection) { + var field = document.getElementById(elmtID); + + if (string.length === 0) { + field.classList.remove("available"); + field.classList.remove("unavailable"); + return; + } else { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function () { + if (this.readyState === 4 && this.status === 200) { + if (this.responseText === "available") { + field.classList.remove("unavailable"); + field.classList.add("available"); + } else { + field.classList.remove("available"); + field.classList.add("unavailable"); + } + } + }; + xmlhttp.open("GET", dataCollection + "?q=" + string, true); + xmlhttp.send(); + } +} + +function checkRequiredField(elmtID) { + var field = document.getElementById(elmtID); + + if (field.value === "") { + field.classList.add("empty-required"); + return false; + } else { + field.classList.remove("empty-required"); + return true; + } +} + +var isNameFilled = false; +var isUsernameFilled = false; +var isPasswordFilled = false; +var isEmailFilled = false; +var isPhoneFilled = false; +var isPasswordMatch = false; +var isUsernameAvailable = false; +var isEmailAvailable = false; + + +document.getElementById("confirm-password").onkeyup = function () { + var confirmField = document.getElementById("confirm-password"); + var passwordField = document.getElementById("password"); + + if (confirmField.value !== passwordField.value) { + confirmField.classList.add("not-match"); + passwordField.classList.add("not-match"); + isPasswordMatch = false; + } else { + confirmField.classList.remove("not-match"); + passwordField.classList.remove("not-match"); + isPasswordMatch = true; + } +}; + + +document.getElementById("name").onkeyup = function () { + isNameFilled = checkRequiredField("name"); +}; + +document.getElementById("username").onkeyup = function () { + isUsernameFilled = checkRequiredField("username"); + checkAvailability(this.value, "username", '../controller/UsernameValidationController.php'); + isUsernameAvailable = this.classList.contains("available"); +}; + +document.getElementById("password").onkeyup = function () { + isPasswordFilled = checkRequiredField("password"); +}; + +document.getElementById("email").onkeyup = function () { + isEmailFilled = checkRequiredField("email"); + checkAvailability(this.value, "email", '../controller/EmailValidationController.php'); + isEmailAvailable = this.classList.contains("available"); +}; + +document.getElementById("phone").onkeyup = function () { + isPhoneFilled = checkRequiredField("phone"); +}; + +document.getElementById("register-form").onkeyup = function () { + var submitBtn = document.getElementById("sign-up-btn"); + + if (isNameFilled && + isUsernameFilled && + isPasswordFilled && + isEmailFilled && + isPhoneFilled && + isPasswordMatch && + isUsernameAvailable && + isEmailAvailable) { + submitBtn.removeAttribute("disabled"); + } else { + submitBtn.setAttribute("disabled", "true"); + } +}; \ No newline at end of file diff --git a/src/view/style.css b/src/view/style.css new file mode 100644 index 0000000000000000000000000000000000000000..26a76968eaa8ba8b31f1bae61e4e04505ab45e62 --- /dev/null +++ b/src/view/style.css @@ -0,0 +1,40 @@ +#login-body, #register-body { + background-color: #333333; + color: #ffffff; +} + +#login-header, #register-header { + text-align: center; + margin-top: 50px; +} + + +#login-main, #register-main { + horiz-align: center; + width: 25%; + height: 60%; + margin: 0 auto; +} + +#register-link { + float: right; + color: #f4df42; + margin-top: 0.3em; +} + +.available { + background-color: greenyellow; +} + +.unavailable, .empty-required, .not-match { + background-color: orangered; +} + +.not-match::-webkit-input-placeholder, .empty-required::-webkit-input-placeholder, .unavailable::-webkit-input-placeholder { + color: whitesmoke; +} + +input { + margin-top: 5px; + margin-bottom: 5px; +} \ No newline at end of file