Merge branch 'marijn-settings' into 'master'
Marijn settings See merge request !102
This commit was merged in pull request #106.
This commit is contained in:
BIN
website/public/img/avatar-standard.png
Normal file
BIN
website/public/img/avatar-standard.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 98 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 24 KiB |
@@ -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");
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ function selectAllFriends($userID) {
|
|||||||
`username`,
|
`username`,
|
||||||
IFNULL(
|
IFNULL(
|
||||||
`profilepicture`,
|
`profilepicture`,
|
||||||
'../img/notbad.jpg'
|
'../img/avatar-standard.png'
|
||||||
) AS profilepicture,
|
) AS profilepicture,
|
||||||
`onlinestatus`,
|
`onlinestatus`,
|
||||||
`role`
|
`role`
|
||||||
@@ -38,7 +38,7 @@ function selectAllFriendRequests() {
|
|||||||
`username`,
|
`username`,
|
||||||
IFNULL(
|
IFNULL(
|
||||||
`profilepicture`,
|
`profilepicture`,
|
||||||
'../img/notbad.jpg'
|
'../img/avatar-standard.png'
|
||||||
) AS profilepicture,
|
) AS profilepicture,
|
||||||
`onlinestatus`,
|
`onlinestatus`,
|
||||||
`role`
|
`role`
|
||||||
|
|||||||
@@ -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`
|
||||||
|
|||||||
@@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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`,
|
||||||
|
|||||||
@@ -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"]))
|
||||||
|
|||||||
@@ -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>";
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user