Lars chat #107

Merged
11291680 merged 11 commits from lars-chat into master 2017-01-24 14:30:12 +01:00
13 changed files with 224 additions and 90 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

View File

@@ -12,23 +12,28 @@
</head> </head>
<body> <body>
<?php <?php
$notImplemented = new settingsMessage("angry", "Deze functie werkt nog niet :("); $alertClass;
$alertMessage;
if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($_SERVER["REQUEST_METHOD"] == "POST") {
switch ($_POST["form"]) { try {
case "profile": switch ($_POST["form"]) {
$result = updateSettings(); case "profile":
break; updateSettings();
case "password": break;
$result = changePassword(); case "password":
break; changePassword();
case "email": break;
$result = changeEmail(); case "email":
break; changeEmail();
case "picture": break;
updateProfilePicture(); case "picture":
$result = new settingsMessage("happy", "Deze melding doet nog niks nuttigs."); updateAvatar();
break; break;
}
} catch (AlertMessage $w) {
$alertClass = $w->getClass();
$alertMessage = $w->getMessage();
} }
} }
include("../views/main.php"); include("../views/main.php");

View File

@@ -14,4 +14,16 @@
.searchleft, .searchright { .searchleft, .searchright {
display: inline-block; display: inline-block;
vertical-align: top; vertical-align: top;
}
.user-pageselect, .searchleft h4, .group-pageselect, .searchright h4 {
display: inline-block;
}
.user-pageselect, .group-pageselect {
float: right;
}
li.search-item:hover{
background-color: #EEE;
} }

View File

@@ -8,7 +8,7 @@ function selectAllFriends($userID) {
LEFT(CONCAT(`user`.`fname`, ' ', `user`.`lname`), 20) as `name`, LEFT(CONCAT(`user`.`fname`, ' ', `user`.`lname`), 20) as `name`,
IFNULL( IFNULL(
`profilepicture`, `profilepicture`,
'../img/notbad.jpg' '../img/avatar-standard.png'
) AS profilepicture, ) AS profilepicture,
`onlinestatus`, `onlinestatus`,
`role` `role`
@@ -40,7 +40,7 @@ function selectAllFriendRequests() {
LEFT(CONCAT(`user`.`fname`, ' ', `user`.`lname`), 20) as `name`, LEFT(CONCAT(`user`.`fname`, ' ', `user`.`lname`), 20) as `name`,
IFNULL( IFNULL(
`profilepicture`, `profilepicture`,
'../img/notbad.jpg' '../img/avatar-standard.png'
) AS profilepicture, ) AS profilepicture,
`onlinestatus`, `onlinestatus`,
`role` `role`

View File

@@ -194,4 +194,22 @@ function searchSomeGroups($n, $m, $search) {
$stmt->execute(); $stmt->execute();
return $stmt; return $stmt;
} }
function countSomeGroups($search) {
$stmt = $GLOBALS["db"]->prepare("
SELECT
COUNT(*)
FROM
`group_page`
WHERE
`name` LIKE :keyword
ORDER BY
`name`
");
$search = "%$search%";
$stmt->bindParam(':keyword', $search);
$stmt->execute();
return $stmt;
}
?> ?>

View File

@@ -6,7 +6,7 @@ function getHeaderInfo() {
`lname`, `lname`,
IFNULL( IFNULL(
`profilepicture`, `profilepicture`,
'img/notbad.jpg' 'img/avatar-standard.png'
) AS profilepicture ) AS profilepicture
FROM FROM
`user` `user`

View File

@@ -1,35 +1,33 @@
<?php <?php
abstract class AlertMessage extends Exception {
public function __construct($message = "", $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
class settingsMessage { abstract public function getClass();
private $class; }
private $message;
/** class HappyAlert extends AlertMessage {
* settingsMessage constructor.
* @param string $type Happy or angry public function __construct($message = "Gelukt!", $code = 0, Exception $previous = null)
* @param string $message The message to display {
*/ parent::__construct($message, $code, $previous);
public function __construct($type, $message) {
$this->message = $message;
switch ($type) {
case "happy":
$this->class = "settings-message-happy";
break;
case "angry":
$this->class = "settings-message-angry";
break;
default:
$this->class = "settings-message";
break;
}
} }
public function getClass() { public function getClass() {
return $this->class; return "settings-message-happy";
}
}
class AngryAlert extends AlertMessage {
public function __construct($message = "Er is iets fout gegaan.", $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
} }
public function getMessage() { public function getClass() {
return $this->message; return "settings-message-angry";
} }
} }
@@ -94,24 +92,19 @@ function updateSettings() {
$stmt->bindValue(":bio", test_input($_POST["bio"])); $stmt->bindValue(":bio", test_input($_POST["bio"]));
$stmt->bindValue(":userID", $_SESSION["userID"]); $stmt->bindValue(":userID", $_SESSION["userID"]);
$stmt->execute(); $stmt->execute();
throw new HappyAlert("Instellingen zijn opgeslagen.");
return new settingsMessage("happy", "Instellingen zijn opgeslagen.");
} }
function changePassword() { function changePassword() {
$user = getPasswordHash(); $user = getPasswordHash();
if (password_verify($_POST["password-old"], $user["password"])) { if (password_verify($_POST["password-old"], $user["password"])) {
if ($_POST["password-new"] == $_POST["password-confirm"] && (strlen($_POST["password-new"]) >= 8)) { if ($_POST["password-new"] == $_POST["password-confirm"] && (strlen($_POST["password-new"]) >= 8)) {
if (doChangePassword()) { doChangePassword();
return new settingsMessage("happy", "Wachtwoord gewijzigd.");
} else {
return new settingsMessage("angry", "Er is iets mis gegaan.");
}
} else { } else {
return new settingsMessage("angry", "Wachtwoorden komen niet oveen."); throw new AngryAlert("Wachtwoorden komen niet overeen.");
} }
} else { } else {
return new settingsMessage("angry", "Oud wachtwoord niet correct."); throw new AngryAlert("Oud wachtwoord niet correct.");
} }
} }
@@ -129,7 +122,12 @@ function doChangePassword() {
$stmt->bindParam(":new_password", $hashed_password); $stmt->bindParam(":new_password", $hashed_password);
$stmt->bindParam(":userID", $_SESSION["userID"]); $stmt->bindParam(":userID", $_SESSION["userID"]);
$stmt->execute(); $stmt->execute();
return $stmt->rowCount();
if ($stmt->rowCount()) {
throw new HappyAlert("Wachtwoord gewijzigd.");
} else {
throw new AngryAlert();
}
} }
function changeEmail() { function changeEmail() {
@@ -138,20 +136,13 @@ function changeEmail() {
$email = strtolower($_POST["email"]); $email = strtolower($_POST["email"]);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//check if email exists //check if email exists
if (emailIsAvailableInDatabase($email)) { emailIsAvailableInDatabase($email);
if (doChangeEmail($email)) { doChangeEmail($email);
return new settingsMessage("happy", "Emailadres is veranderd.");
} else {
return new settingsMessage("angry", "Er is iets mis gegaan.");
}
} else {
return new settingsMessage("angry", "Emailadres bestaat al.");
}
} else { } else {
return new settingsMessage("angry", "Geef een geldig emailadres."); throw new AngryAlert("Geef een geldig emailadres");
} }
} else { } else {
return new settingsMessage("angry", "Emailadressen komen niet overeen."); throw new AngryAlert("Emailadressen komen niet overeen.");
} }
} }
@@ -167,7 +158,9 @@ function emailIsAvailableInDatabase($email) {
$stmt->bindParam(":email", $email); $stmt->bindParam(":email", $email);
$stmt->execute(); $stmt->execute();
return !$stmt->rowCount(); if ($stmt->rowCount()) {
throw new AngryAlert("Emailadres wordt al gebruikt.");
}
} }
function doChangeEmail($email) { function doChangeEmail($email) {
@@ -182,18 +175,28 @@ function doChangeEmail($email) {
$stmt->bindParam(":email", $email); $stmt->bindParam(":email", $email);
$stmt->bindParam(":userID", $_SESSION["userID"]); $stmt->bindParam(":userID", $_SESSION["userID"]);
$stmt->execute(); $stmt->execute();
return $stmt->rowCount(); // return $stmt->rowCount();
if ($stmt->rowCount()) {
throw new HappyAlert("Emailadres is veranderd.");
} else {
throw new AngryAlert();
}
} }
function updateProfilePicture() { function updateAvatar() {
$profilePictureDir = "/var/www/html/public/"; $profilePictureDir = "/var/www/html/public/";
$relativePath = "uploads/profilepictures/" . $_SESSION["userID"] . "_" . basename($_FILES["pp"]["name"]); $relativePath = "uploads/profilepictures/" . $_SESSION["userID"] . "_avatar.png";
removeOldProfilePicture();
move_uploaded_file($_FILES['pp']['tmp_name'], $profilePictureDir . $relativePath); checkAvatarSize($_FILES["pp"]["tmp_name"]);
setProfilePictureToDatabase("../" . $relativePath); $scaledImg = scaleAvatar($_FILES["pp"]["tmp_name"]);
removeOldAvatar();
imagepng($scaledImg, $profilePictureDir . $relativePath);
setAvatarToDatabase("../" . $relativePath);
throw new HappyAlert("Profielfoto veranderd.");
} }
function removeOldProfilePicture() { function removeOldAvatar() {
$stmt = $GLOBALS["db"]->prepare(" $stmt = $GLOBALS["db"]->prepare("
SELECT SELECT
`profilepicture` `profilepicture`
@@ -205,20 +208,39 @@ function removeOldProfilePicture() {
$stmt->bindParam(":userID", $_SESSION["userID"]); $stmt->bindParam(":userID", $_SESSION["userID"]);
$stmt->execute(); $stmt->execute();
$old_avatar = $stmt->fetch()["profilepicture"]; $old_avatar = $stmt->fetch()["profilepicture"];
unlink("/var/www/html/public/uploads/" . $old_avatar); if ($old_avatar != NULL) {
unlink("/var/www/html/public/uploads/" . $old_avatar);
}
} }
function setProfilePictureToDatabase($url) { function setAvatarToDatabase(string $url) {
$stmt = $GLOBALS["db"]->prepare(" $stmt = $GLOBALS["db"]->prepare("
UPDATE UPDATE
`user` `user`
SET SET
`profilepicture` = :profilePicture `profilepicture` = :avatar
WHERE WHERE
`userID` = :userID `userID` = :userID
"); ");
$stmt->bindParam(":profilePicture", $url); $stmt->bindParam(":avatar", $url);
$stmt->bindParam(":userID", $_SESSION["userID"]); $stmt->bindParam(":userID", $_SESSION["userID"]);
$stmt->execute(); $stmt->execute();
}
function checkAvatarSize(string $img) {
$minResolution = 200;
$imgSize = getimagesize($img);
if ($imgSize[0] < $minResolution or $imgSize[1] < $minResolution) {
throw new AngryAlert("Afbeelding te klein, minimaal 200x200 pixels.");
}
}
function scaleAvatar(string $imgLink, int $newWidth = 600) {
$img = imagecreatefromstring(file_get_contents($imgLink));
if ($img) {
return imagescale($img, $newWidth);
} else {
throw new AngryAlert("Afbeelding wordt niet ondersteund.");
}
} }

View File

@@ -23,7 +23,7 @@ function selectUser($userID) {
`username`, `username`,
IFNULL( IFNULL(
`profilepicture`, `profilepicture`,
'../img/notbad.jpg' '../img/avatar-standard.png'
) AS profilepicture, ) AS profilepicture,
`bio`, `bio`,
`role`, `role`,
@@ -273,7 +273,8 @@ function selectRandomNotFriendUser($userID) {
return $stmt->fetch(); return $stmt->fetch();
} }
function searchSomeUsers($n, $m, $search) { function searchSomeUsers($n, $m, $search)
{
$stmt = $GLOBALS["db"]->prepare(" $stmt = $GLOBALS["db"]->prepare("
SELECT SELECT
`username`, `username`,
@@ -301,3 +302,25 @@ function searchSomeUsers($n, $m, $search) {
$stmt->execute(); $stmt->execute();
return $stmt; return $stmt;
} }
function countSomeUsers($search) {
$q = $GLOBALS["db"]->prepare("
SELECT
COUNT(*)
FROM
`user`
WHERE
`username` LIKE :keyword OR
`fname` LIKE :keyword OR
`lname` LIKE :keyword
ORDER BY
`fname`,
`lname`,
`username`
");
$search = "%$search%";
$q->bindParam(':keyword', $search);
$q->execute();
return $q;
}

View File

@@ -17,7 +17,7 @@
// Set default values of a friend. // Set default values of a friend.
$username = $friend["username"]; $username = $friend["username"];
$userID = $friend["userID"]; $userID = $friend["userID"];
$pf = "img/notbad.jpg"; $pf = "img/avatar-standard.png";
// Change values if needed. // Change values if needed.
if (!empty($friend["profilepicture"])) if (!empty($friend["profilepicture"]))

View File

@@ -131,4 +131,4 @@
</li> </li>
</ul> </ul>
</section> </section>
</nav> </nav>

View File

@@ -1,6 +1,16 @@
<?php <?php
$search = ""; $search = "";
$filter = "all"; $filter = "all";
$user_perpage = $group_perpage = 20;
$user_currentpage = $group_currentpage = 1;
if (isset($_GET['user-pageselect'])) {
$user_currentpage = $_GET['user-pageselect'];
}
if (isset($_GET['group-pageselect'])) {
$group_currentpage = $_GET['group-pageselect'];
}
if (isset($_GET['search'])) { if (isset($_GET['search'])) {
$search = test_input($_GET['search']); $search = test_input($_GET['search']);
@@ -9,18 +19,26 @@ if (isset($_GET['search'])) {
if (isset($_GET['filter'])) { if (isset($_GET['filter'])) {
$filter = $_GET['filter']; $filter = $_GET['filter'];
} }
$user_n = ($user_currentpage - 1) * $user_perpage;
$user_count = countSomeUsers($search)->fetchColumn();
$group_n = ($group_currentpage - 1) * $group_perpage;
$group_count = countSomeGroups($search)->fetchColumn();
?> ?>
<div class="content"> <div class="content">
<div class="platform"> <div class="platform">
<form class="search-form" action="search.php" method="get"> <form class="search-form"
id="search-form"
action="search.php"
method="get">
<label> <label>
Zoek: Zoek:
</label> </label>
<input type="text" <input type="text"
name="search" name="search"
placeholder="zoek" placeholder="zoek"
required
value=<?php echo "$search";?> value=<?php echo "$search";?>
> >
<label for="filter"> <label for="filter">
@@ -40,17 +58,36 @@ if (isset($_GET['filter'])) {
<?php if ($filter == "friends") echo "selected";?>> <?php if ($filter == "friends") echo "selected";?>>
Vrienden</option> Vrienden</option>
</select> </select>
<input type="submit" <input onclick="document.getElementById('user-pageselect').value = 1;
document.getElementById('group-pageselect').value = 1"
type="submit"
value="Zoek" value="Zoek"
/> >
</form> </form>
</div> </div>
<div class="platform item-box searchleft" id="search-friends-output"> <div class="platform item-box searchleft" id="search-friends-output">
<h4>Gebruikers</h4> <h4>Gebruikers</h4>
<select class="user-pageselect"
name="user-pageselect"
id="user-pageselect"
form="search-form"
onchange="this.form.submit()">
<?php
for ($i=1; $i <= ceil($user_count / $user_perpage); $i++) {
if ($user_currentpage == $i) {
echo "<option value='$i' selected>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<ul class='nav-list'> <ul class='nav-list'>
<?php <?php
$q = searchSomeUsers(0, 20, $search); $q = searchSomeUsers($user_n, $user_perpage, $search);
while ($user = $q->fetch(PDO::FETCH_ASSOC)) { while ($user = $q->fetch(PDO::FETCH_ASSOC)) {
$username = $user['username']; $username = $user['username'];
@@ -59,7 +96,7 @@ if (isset($_GET['filter'])) {
$lname = $user['lname']; $lname = $user['lname'];
echo(" echo("
<a href='https://myhyvesbookplus.nl/profile/$username/'> <a href='https://myhyvesbookplus.nl/profile?username=$username'>
<li class='search-item'> <li class='search-item'>
<div class='friend'> <div class='friend'>
<img class='profile-picture' <img class='profile-picture'
@@ -77,17 +114,34 @@ if (isset($_GET['filter'])) {
<div class="platform item-box searchright" id="search-group-output"> <div class="platform item-box searchright" id="search-group-output">
<h4>Groepen</h4> <h4>Groepen</h4>
<select class="group-pageselect"
name="group-pageselect"
id="group-pageselect"
form="search-form"
onchange="this.form.submit()">
<?php
for ($i=1; $i <= ceil($group_count / $group_perpage); $i++) {
if ($group_currentpage == $i) {
echo "<option value='$i' selected>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<ul class="nav-list"> <ul class="nav-list">
<?php <?php
$q = searchSomeGroups(0, 20, $search); $q = searchSomeGroups($group_n, $user_perpage, $search);
while ($group = $q->fetch(PDO::FETCH_ASSOC)) { while ($group = $q->fetch(PDO::FETCH_ASSOC)) {
$groupname = $group['name']; $groupname = $group['name'];
$grouppic = $group['picture']; $grouppic = $group['picture'];
echo(" echo("
<a href='https://myhyvesbookplus.nl/group/$groupname/'> <a href='https://myhyvesbookplus.nl/group?groupName=$groupname'>
<li class='search-item'> <li class='search-item'>
<div class='group'> <div class='group'>
<img class='group-picture' <img class='group-picture'

View File

@@ -6,8 +6,8 @@ $settings = getSettings();
<div class="settings"> <div class="settings">
<?php <?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<div class='platform settings-message ". $result->getClass()."'>". echo "<div class='platform settings-message ". $alertClass ."'>".
$result->getMessage(). $alertMessage .
"</div>"; "</div>";
} }
?> ?>