From d44ddf2793168c126329e31f9e1c30bb9804343a Mon Sep 17 00:00:00 2001 From: "K. Nobel" Date: Tue, 24 Jan 2017 14:36:27 +0100 Subject: [PATCH 1/4] Added functionality for add friend buttons. --- website/public/edit_friendship.php | 30 ++++++++++++ website/public/profile.php | 5 +- website/public/styles/profile.css | 3 +- website/queries/friendship.php | 78 +++++++++++++++++++++++++++++- website/queries/user.php | 52 +++++++++++++++----- website/views/profile.php | 25 +++++++--- 6 files changed, 170 insertions(+), 23 deletions(-) create mode 100644 website/public/edit_friendship.php diff --git a/website/public/edit_friendship.php b/website/public/edit_friendship.php new file mode 100644 index 0000000..d88e264 --- /dev/null +++ b/website/public/edit_friendship.php @@ -0,0 +1,30 @@ +prepare(" SELECT @@ -21,8 +23,8 @@ function selectAllFriends($userID) { `friendship`.`user2ID` = `user`.`userID` OR `friendship`.`user2ID` = :userID AND `friendship`.`user1ID` = `user`.`userID`) AND - `role` != 5 AND - `status` = 1 + `role` != 'banned' AND + `status` = 'confirmed' "); $stmt->bindParam(':userID', $userID, PDO::PARAM_INT); @@ -60,4 +62,76 @@ function selectAllFriendRequests() { $stmt->execute(); return json_encode($stmt->fetchAll()); +} + +function getFriendshipStatus($userID) { + $stmt = $GLOBALS["db"]->prepare(" + SELECT + CASE `status` IS NULL + WHEN TRUE THEN 0 + WHEN FALSE THEN + CASE `status` = 'confirmed' + WHEN TRUE THEN + 1 + WHEN FALSE THEN + CASE `user1ID` = :me AND `user2ID` = :other + WHEN TRUE THEN + 2 + WHEN FALSE THEN + 3 + END + END + END AS `friend_state` + FROM + `friendship` + WHERE + `user1ID` = :other AND `user2ID` = :me OR + `user1ID` = :me AND `user2ID` = :other + "); + + $stmt->bindParam(':me', $_SESSION["userID"], PDO::PARAM_INT); + $stmt->bindParam(':other', $userID, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetch()["friend_state"]; +} + +function requestFriendship($userID) { + $stmt = $GLOBALS["db"]->prepare(" + INSERT INTO `friendship` (user1ID, user2ID) + VALUES (:user1, :user2) + "); + + $stmt->bindParam(':user1', $_SESSION["userID"], PDO::PARAM_INT); + $stmt->bindParam(':user2', $userID, PDO::PARAM_INT); + $stmt->execute(); +} + +function removeFriendship($userID) { + $stmt = $GLOBALS["db"]->prepare(" + DELETE FROM `friendship` + WHERE + `user1ID` = :user1 AND + `user2ID` = :user2 OR + `user1ID` = :user2 AND + `user2ID` = :user1 + "); + + $stmt->bindParam(':user1', $_SESSION["userID"], PDO::PARAM_INT); + $stmt->bindParam(':user2', $userID, PDO::PARAM_INT); + $stmt->execute(); +} + +function acceptFriendship($userID) { + $stmt = $GLOBALS["db"]->prepare(" + UPDATE `friendship` + SET `status`='confirmed' + WHERE + `user1ID` = :user1 AND + `user2ID` = :user2 + LIMIT 1 + "); + + $stmt->bindParam(':user1', $userID, PDO::PARAM_INT); + $stmt->bindParam(':user2', $_SESSION["userID"], PDO::PARAM_INT); + $stmt->execute(); } \ No newline at end of file diff --git a/website/queries/user.php b/website/queries/user.php index 114d673..04f379e 100644 --- a/website/queries/user.php +++ b/website/queries/user.php @@ -17,27 +17,53 @@ function getUserID($username) { return $stmt->fetch()["userID"]; } -function selectUser($userID) { +function getUsername($userID) { $stmt = $GLOBALS["db"]->prepare(" SELECT - `username`, - IFNULL( - `profilepicture`, - '../img/notbad.jpg' - ) AS profilepicture, - `bio`, - `role`, - `onlinestatus`, - `loggedin`, - `fname`, - `lname` + `username` FROM `user` WHERE `userID` = :userID "); - $stmt->bindParam(':userID', $userID, PDO::PARAM_INT); + $stmt->bindParam(':userID', $userID, PDO::PARAM_STR); + $stmt->execute(); + return $stmt->fetch()["username"]; +} + +function selectUser($me, $other) { + $stmt = $GLOBALS["db"]->prepare(" + SELECT + `username`, `birthdate`, `location`, `profilepicture`, `bio`, `user`.`creationdate`, `onlinestatus`, `fname`, `lname`, + CASE `status` IS NULL + WHEN TRUE THEN 0 + WHEN FALSE THEN + CASE `status` = 'confirmed' + WHEN TRUE THEN + 1 + WHEN FALSE THEN + CASE `user1ID` = `userID` AND `user2ID` = :me + WHEN TRUE THEN + 2 + WHEN FALSE THEN + 3 + END + END + END AS `friend_status` + FROM + `user` + LEFT JOIN + `friendship` + ON + `user1ID` = `userID` AND `user2ID` = :me OR + `user1ID` = :me AND `user2ID` = `userID` + WHERE + `user`.`userID` = :other + "); + + $stmt->bindParam(':me', $me, PDO::PARAM_INT); + $stmt->bindParam(':other', $other, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetch(); } diff --git a/website/views/profile.php b/website/views/profile.php index 19bd908..83dbca7 100644 --- a/website/views/profile.php +++ b/website/views/profile.php @@ -1,11 +1,24 @@
"> -
-

Als vriend toevoegen

-
-

-
+ +
+ + "; + } else if($user["friend_status"] == 1) { + echo ""; + } else if($user["friend_status"] == 2) { + echo ""; + echo ""; + } else if($user["friend_status"] == 3) { + echo ""; + } + ?> +
+

+

@@ -14,7 +27,7 @@

fetch()) { - echo "${friend["username"]}"; + echo "${friend["username"]}"; } From 4967ab6ea07ed2d20d4bd4909f946ae90ccb9936 Mon Sep 17 00:00:00 2001 From: "K. Nobel" Date: Tue, 24 Jan 2017 15:01:55 +0100 Subject: [PATCH 2/4] Made some small changes for Lars. --- website/queries/user.php | 13 ++++++++++++- website/views/profile.php | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/website/queries/user.php b/website/queries/user.php index 15b7fb1..ed431f6 100644 --- a/website/queries/user.php +++ b/website/queries/user.php @@ -35,7 +35,18 @@ function getUsername($userID) { function selectUser($me, $other) { $stmt = $GLOBALS["db"]->prepare(" SELECT - `username`, `birthdate`, `location`, `profilepicture`, `bio`, `user`.`creationdate`, `onlinestatus`, `fname`, `lname`, + `username`, + `birthdate`, + `location`, + IFNULL( + `profilepicture`, + '../img/avatar-standard.png' + ) AS profilepicture, + `bio`, + `user`.`creationdate`, + `onlinestatus`, + `fname`, + `lname`, CASE `status` IS NULL WHEN TRUE THEN 0 WHEN FALSE THEN diff --git a/website/views/profile.php b/website/views/profile.php index 83dbca7..2bc0341 100644 --- a/website/views/profile.php +++ b/website/views/profile.php @@ -27,7 +27,7 @@

fetch()) { - echo "${friend["username"]}"; + echo "${friend["username"]}"; } From 7a19fea5f9d8ce126d4fa6bb3f995eab036f8625 Mon Sep 17 00:00:00 2001 From: Joey Lai Date: Tue, 24 Jan 2017 15:08:24 +0100 Subject: [PATCH 3/4] Redesigned register functions --- website/public/register.php | 93 +++++++++++--- website/public/styles/index.css | 64 ++++------ website/queries/checkInput.php | 206 ++++++++++++++++++++++++-------- website/views/login_head.php | 1 + website/views/register-view.php | 22 ++-- 5 files changed, 267 insertions(+), 119 deletions(-) diff --git a/website/public/register.php b/website/public/register.php index 8c0f8e4..99af6c0 100644 --- a/website/public/register.php +++ b/website/public/register.php @@ -10,33 +10,90 @@ getMessage(); } - checkInputChoice("username", "username"); - checkInputChoice("password", "longerEight"); - checkInputChoice("confirmpassword", ""); - matchPassword(); - checkInputChoice("location", "lettersAndSpace"); - checkInputChoice("email", "email"); - registerCheck(); + try { + $surname = test_input(($_POST["surname"])); + checkInputChoice($surname, "lettersAndSpaces"); + } + catch(lettersAndSpacesException $e){ + $correct = false; + $surnameErr = $e->getMessage(); + } + + try{ + $bday = test_input(($_POST["bday"])); + checkInputChoice($bday, "bday"); + } catch(bdayException $e){ + $correct = false; + $bdayErr = $e->getMessage(); + } + + try{ + $username = test_input(($_POST["username"])); + checkInputChoice($username, "username"); + } catch(usernameException $e){ + $correct = false; + $usernameErr = $e->getMessage(); + } + + try{ + $password = test_input(($_POST["password"])); + checkInputChoice($password, "longerEight"); + matchPassword(); + } catch(passwordException $e){ + $correct = false; + $passwordErr = $e->getMessage(); + } catch(confirmPasswordException $e){ + $correct = false; + $confirmPasswordErr = $e->getMessage(); + } + + try{ + $location = test_input(($_POST["location"])); + checkInputChoice($location, "lettersAndSpaces"); + } catch(lettersAndSpacesException $e){ + $correct = false; + $locationErr = $e->getMessage(); + } + + try{ + $email = test_input(($_POST["email"])); + checkInputChoice($email, "email"); + } catch(emailException $e){ + $correct = false; + $emailErr = $e->getMessage(); + } + + try{ + $captcha = $_POST['g-recaptcha-response']; + checkCaptcha($captcha); + } catch(captchaException $e){ + $correct = false; + $captchaErr = $e->getMessage(); + } + + try { + getIp(); + registerCheck($correct); + } catch(registerException $e){ + $genericErr = $e->getMessage(); + } } /* This view adds register view */ include("../views/register-view.php"); diff --git a/website/public/styles/index.css b/website/public/styles/index.css index a24bdd0..8482cea 100644 --- a/website/public/styles/index.css +++ b/website/public/styles/index.css @@ -4,17 +4,16 @@ a.button { color: black; cursor: pointer; height: 50%; - margin: 8px 0; - padding: 14px 20px; - width: 25%; + padding: 8px 20px; + width: 50%; font-family: Arial; - font-size: 16px; + font-size: 20px; } /* Body */ body { height: 100%; - background-color: #C8CABD; + background-color: #FBC02D; /*background-image: url(http://play.pokemonshowdown.com/fx/client-bg-shaymin.jpg); background-size: cover; background-attachment: fixed;*/ @@ -24,31 +23,14 @@ body { font-family: Arial, sans-serif; } -/* The Close Button */ -.close { - /* Position it in the top right corner outside of the modal */ - color: white; - font-size: 100px; - font-weight: bold; - position: absolute; - right: 25px; - top: 0; -} - -/* Close button on hover */ -.close:hover, -.close:focus { - color: red; - cursor: pointer; -} /* inlogform */ form { /*background-color: #a87a87;*/ border-radius: 12px; - height: 70%; + height: 75%; margin: auto; - width: 70%; + width: 80%; overflow-y:auto; } @@ -72,24 +54,20 @@ input[type=text], input[type=password], input[type=email], input[type="date"] { border-color: #C8CABD; display: inline-block; height: 60%; + font-size: 16px; padding: 8px 20px; margin: 4px 0; - width: 70%; + width: 55%; } -/* -input[type=text], input[type=password], input[type=email], input[type="date"] { - border: 0px; - border-bottom: 4px solid lightgray; - border-radius: 0px; -}*/ button[type=submit] { background-color: #C8CABD; - color: black ; + color: black; cursor: pointer; font-family: Arial; - font-size: 16px; - width: 50%; + font-size: 22px; + height: 30px; + width: 120px; } .error { @@ -106,12 +84,12 @@ label { display: inline-block; position: relative; background-color: #C8CABD; - height: 30px; - width: 90px; - padding: 3px 3px 3px 0px; + height: 25px; + width: 120px; + padding: 3px 3px 3px 3px; text-align: center; - border-radius: 0px 10px 10px 0px; - font-size: 24px; + border-radius: 0px 5px 5px 0px; + font-size: 22px; } .left-arrow:after { @@ -121,9 +99,9 @@ label { right: 100%; top: 0; bottom: 0; - border-top: 15px solid transparent; + border-top: 12px solid transparent; border-right: 20px solid #C8CABD; - border-bottom: 15px solid transparent; + border-bottom: 12px solid transparent; border-left: 0px solid transparent; } @@ -135,7 +113,7 @@ label { /* padding voor login_containers */ .login_containerlogin { - padding:25px; + padding:16px; text-align: center; } @@ -163,7 +141,7 @@ label { margin: 34px auto; overflow-y: auto; padding: 20px; - width: 50%; + width: 45%; } /*.platform { diff --git a/website/queries/checkInput.php b/website/queries/checkInput.php index f711676..cc32626 100644 --- a/website/queries/checkInput.php +++ b/website/queries/checkInput.php @@ -1,97 +1,138 @@ format($format) == $date; +} + /* checks if username exist and if its longer than 6 characters. */ function username($variable){ - if (strlen($GLOBALS[$variable]) < 6) { - $GLOBALS[$variable . "Err"] = "Gebruikersnaam moet minstens 6 karakters bevatten"; - $correct = false; + if (empty($variable)) { + throw new usernameException("Verplicht!"); + } else if (strlen($variable) < 6) { + throw new usernameException("Moet minstens 6 karakters bevatten"); } else if (getExistingUsername() == 1) { - $GLOBALS[$variable . "Err"] = "Gebruikersnaam bestaat al"; - $correct = false; + throw new usernameException("Gebruikersnaam bestaal al"); } } /* checks if an input is longer that 8 characters. */ function longerEight($variable){ - if (strlen($GLOBALS[$variable]) < 8) { - $GLOBALS[$variable . "Err"] = "Moet minstens 8 karakters bevatten"; - $correct = false; + if (empty($variable)) { + throw new passwordException("Verplicht!"); + } else if (strlen($variable) < 8) { + throw new passwordException("Moet minstens 8 karakters bevatten"); } } /* checks if an input is a valid email. */ function validateEmail($variable){ - if (!filter_var($GLOBALS[$variable], FILTER_VALIDATE_EMAIL)) { - $GLOBALS[$variable . "Err"] = "Geldige email invullen!"; - $correct = false; - + if (empty($variable)) { + throw new emailException("Verplicht!"); + } else if (!filter_var($variable, FILTER_VALIDATE_EMAIL)) { + throw new emailException("Geldige email invullen"); } else if (getExistingEmail() == 1){ - $GLOBALS[$variable . "Err"] = "Email bestaat al"; - $correct = false; - + throw new emailException("Email bestaal al!"); } } /* checks if two passwords matches. */ function matchPassword(){ if ($_POST["password"] != $_POST["confirmpassword"]) { - $GLOBALS["confirmpasswordErr"] = "Wachtwoorden matchen niet"; - $GLOBALS["correct"] = false; - + throw new confirmPasswordException("Wachtwoorden matchen niet!"); } } -// Checks if everything is filled in correctly -function registerCheck(){ - if ($GLOBALS["correct"] == false){ - $GLOBALS["genericErr"] = "Bepaalde velden zijn verkeerd of niet ingevuld!"; +/* Checks if captcha is correctly filled in */ +function checkCaptcha($captcha){ + if(!$captcha){ + throw new captchaException("Captcha needs to be filled in!"); + } else { + $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Lc72xIUAAAAAPizuF3nUbklCPljVCVzgYespz8o&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR'])); + if($response->success==false) { + throw new captchaException("You are a spammer!"); + } + } +} +/* Get ip adres */ +function getIp(){ + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { + $GLOBALS["ip"] = $_SERVER['HTTP_CLIENT_IP']; + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + $GLOBALS["ip"] = $_SERVER['HTTP_X_FORWARDED_FOR']; + } else { + $GLOBALS["ip"] = $_SERVER['REMOTE_ADDR']; + } +} + +/* Checks if everything is filled in correctly */ +function registerCheck($status){ + if ($status == false){ + throw new registerException("Bepaalde velden zijn verkeerd of niet ingevuld"); } else { registerAccount(); header("location: login.php"); - } } @@ -102,4 +143,69 @@ function test_input($data) { $data = htmlspecialchars($data); return $data; } + +class lettersAndSpacesException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} + + +class bdayException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} + +class usernameException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} + +class passwordException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} + +class confirmPasswordException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} + +class emailException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} + +class captchaException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} + +class registerException extends Exception +{ + public function __construct($message = "", $code = 0, Exception $previous = null) + { + parent::__construct($message, $code, $previous); + } +} ?> diff --git a/website/views/login_head.php b/website/views/login_head.php index 26d439e..e983fab 100644 --- a/website/views/login_head.php +++ b/website/views/login_head.php @@ -9,4 +9,5 @@ href="styles/index.css"> + diff --git a/website/views/register-view.php b/website/views/register-view.php index 7d1843b..4c660c9 100644 --- a/website/views/register-view.php +++ b/website/views/register-view.php @@ -41,11 +41,12 @@

@@ -117,18 +118,23 @@ *
-
+
+ +
+ + +
+ + Login + -
+ + -
- - Login -
From fd055e8355e0ad46e43e120dfb6c49f24b35d49b Mon Sep 17 00:00:00 2001 From: Lars van Hijfte Date: Tue, 24 Jan 2017 15:16:15 +0100 Subject: [PATCH 4/4] Fixed bugs --- website/public/API/loadMessages.php | 1 + website/views/chat-view.php | 1 + 2 files changed, 2 insertions(+) diff --git a/website/public/API/loadMessages.php b/website/public/API/loadMessages.php index a02de26..0fdc740 100644 --- a/website/public/API/loadMessages.php +++ b/website/public/API/loadMessages.php @@ -9,6 +9,7 @@ require_once("../../queries/friendship.php"); if (isset($_POST["lastID"]) && $_POST["lastID"] != "") { echo getNewChatMessages(test_input($_POST["lastID"]), test_input($_POST["destination"])); + setLastVisited(test_input($_POST["destination"])); } else { echo getOldChatMessages(test_input($_POST["destination"])); setLastVisited(test_input($_POST["destination"])); diff --git a/website/views/chat-view.php b/website/views/chat-view.php index 9b40f71..598b3ee 100644 --- a/website/views/chat-view.php +++ b/website/views/chat-view.php @@ -16,6 +16,7 @@ // Set default values of a friend. $username = $friend["username"]; + $name = $friend["name"]; $userID = $friend["userID"]; $pf = "img/avatar-standard.png";