-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.php
139 lines (124 loc) · 6.43 KB
/
settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
session_start();
require_once 'inc/db_connection.php';
$userid = $_SESSION["id"];
$sql = "SELECT name, password, image FROM user_info WHERE userid = $userid";
$result = $conn -> query($sql);
$row = $result -> fetch_assoc();
$userName = $row["name"];
$userImage = $row["image"];
$oldPassword = $newPassword = $confirmNewPassword = "";
$oldPasswordError = $newPasswordError = $confirmNewPasswordError = $successful_msg = "" ;
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["saveChanges"])) {
$name = $conn -> real_escape_string($_POST["name"]);
$oldPassword = $conn -> real_escape_string($_POST["oldPassword"]);
$newPassword = $conn -> real_escape_string($_POST["newPassword"]);
$confirmNewPassword = $conn -> real_escape_string($_POST["confirmNewPassword"]);
if(!empty($_POST["newPassword"]) || !empty($_POST["confirmNewPassword"])) {
if(password_verify($oldPassword, $row["password"])) {
if(!preg_match("/^[A-Za-z0-9]+$/", $newPassword))
$newPasswordError = "Password must contain at least one alphabet or number.";
elseif($newPassword != $confirmNewPassword)
$confirmNewPasswordError = "Passwords do not match";
else {
$password_hash = password_hash($newPassword, PASSWORD_DEFAULT);
$sql = "UPDATE user_info SET name = '$name', password = '$password_hash' WHERE userid = $userid";
if($conn -> query($sql) == TRUE) {
$successful_msg = '<div id="successful_msg">Your settings have been successfully saved.</div>';
$oldPassword = $newPassword = $confirmNewPassword = "";
}
else
echo "Error: " . $conn -> error;
}
}
else
$oldPasswordError = "The password you specified is not correct.";
}
else if($userName != $name) {
$sql = "UPDATE user_info SET name = '$name' WHERE userid = $userid";
if($conn -> query($sql) == TRUE) {
$successful_msg = '<div id="successful_msg">Your settings have been successfully saved.</div>';
$userName = $name;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Settings - UniChat</title>
<link rel="shortcut icon" type="image/x-icon" href="assets/img/favicon 1.png">
<link rel="stylesheet" type="text/css" href="assets/css/all.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/fontawesome.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/settings.css">
</head>
<body>
<?php echo $successful_msg; ?>
<div id="container">
<div id="form_container">
<span id="logo">UniChat</span>
<span id="title">Settings</span>
<div id="form_wrapper">
<div>
<img src="<?php echo $profile_images_folder.$userImage; ?>" alt="" id="profile_pic">
<label for="input_image" id="change_image">Change Image</label> <input type="file" id="input_image" onchange="uploadProfileImage(this.files)">
</div>
<form action="settings" method="post">
<input type="text" name="name" placeholder="Name" value="<?php echo $userName;?>" required> <br>
<input type="password" name="oldPassword" id="oldPassword" value="<?php echo $oldPassword;?>" placeholder="Old password">
<span class="error"><?php echo $oldPasswordError ?></span><br>
<input type="password" name="newPassword" id="newPassword" value="<?php echo $newPassword;?>" placeholder="New password" onkeyup="addRequired()">
<span class="error"><?php echo $newPasswordError ?></span><br>
<input type="password" name="confirmNewPassword" id="confirmNewPassword" value="<?php echo $confirmNewPassword;?>" placeholder="Confirm new password" onkeyup="addRequired()">
<span class="error"><?php echo $confirmNewPasswordError ?></span><br>
<input type="Submit" name="saveChanges" value="Save changes"> <br>
</form>
</div>
<div id="go_back">
<a href="http://localhost/real-time-chat-application/">Go to Homepage</a>
</div>
</div>
</div>
<script>
function addRequired() {
var oldPasswordEle = document.getElementById("oldPassword");
var newPasswordEle = document.getElementById("newPassword");
var confirmNewPasswordEle = document.getElementById("confirmNewPassword");
var newPassword = newPasswordEle.value;
var confirmNewPassword = confirmNewPasswordEle.value;
if(newPassword.length > 0 || confirmNewPassword.length > 0) {
oldPasswordEle.required = true;
newPasswordEle.required = true;
confirmNewPasswordEle.required = true;
}
else {
oldPasswordEle.required = false;
newPasswordEle.required = false;
confirmNewPasswordEle.required = false;
}
}
function uploadProfileImage(files) {
var change_image = document.getElementById("change_image");
var input_image = document.getElementById("input_image")
input_image.disabled = true;
change_image.innerHTML = "Uploading Image...";
var formData = new FormData();
formData.append('file', files[0]);
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
imageName = xhr.responseText;
input_image.disabled = false;
change_image.innerHTML = "Change Image";
document.getElementById("profile_pic").src = "assets/img/users/" + xhr.responseText;
}
}
xhr.open("POST", "inc/upload_profile_image.php", true);
xhr.send(formData);
}
</script>
</body>
</html>