-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.php
150 lines (140 loc) · 4.25 KB
/
user.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
140
141
142
143
144
145
146
147
148
149
150
<?php
$page = "User Profile";
include 'includes/dash.header.php';
include 'includes/library.php';
$Cname = $_POST['username']??null;
$Cemail = $_POST['email']??null;
$Cpassword = $_POST['password']??null;
$usernamevalid = '/^[0-9a-zA-Z_.-]+$/';
$passwordvalid = '/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-\/]).{8,}$/';
$emailvalid = '/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/';
$pdo = connectDB();
function update($new,$place,$id){
$pdo = connectDB();
$sql = "UPDATE users set $place = '$new' WHERE id = $id";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if($stmt->rowCount()){
alert("update successful!!");
}else {
alert("error");
}
}
function same($str,$place){
$pdo = connectDB();
$sql = "SELECT * from users WHERE $place = '$str'";
$stmt = $pdo ->prepare($sql);
$stmt->execute();
$count = $stmt->rowCount();
if($count >0){
return true;
}else {
return false;
}
}
function alert($var){
echo '<script>alert("'.$var.'")</script>';
}
if(!empty($_POST['username'])){
//update new user name
if (preg_match($usernamevalid, $Cname)) {
if(!same($Cname,'username')){
update($Cname,'username',$id);
$_SESSION['username']=$Cname;
}else {
alert("user name exits");
}
}else{
alert('new user name is not valid or repeat');
}
}
if(!empty($_POST['email'])){
//update the datebase of new email
if (preg_match($emailvalid, $Cemail)) {
if(!same($Cemail,'email')){
update($Cemail,'email',$id);
}else {
alert("email already used");
}
}else{
alert('new email name is not valid or repeat');
}
}
if(!empty($_POST['password'])){
//update the datebase of new password
if (preg_match($passwordvalid, $Cpassword)) {
update(password_hash($Cpassword,PASSWORD_DEFAULT),'password',$id);
}else{
alert('new password is not valid');
}
}
if(isset($_POST['delete'])){
try{
$sql = "DELETE FROM `lists` WHERE `ownerId` = $id";
$stmt = $pdo ->prepare($sql);
$stmt->execute();
}catch(Exception $e) {
alert('delet list failed');
}
$sql = "DELETE FROM `users` WHERE `id` = $id";
$stmt = $pdo ->prepare($sql);
$stmt->execute();
$accountdel = $stmt->rowCount();
if($accountdel >0){
alert('delete account successful');
session_destroy();
header("Location: login.php");
exit();
}else {
alert('delete account failed!');
}
}
if(isset($_GET['logout'])){
session_destroy();
header("Location: login.php");
exit();
}
try{
$stmt = $pdo->prepare("SELECT * FROM users where id = ?");
$stmt->execute([$id]);
$result = $stmt->fetch();
}catch(Exception $e) {
echo $e;
}
?>
<main>
<div class="profile">
<!-- hidden form for self-processing and honeypot -->
<form method="post">
<input type="text" name="input">
</form>
<div>
<h2><?php echo $result['username']?>'s Profile</h2>
<img src = "https://avatars.dicebear.com/api/initials/<?php echo $result['username']?>.svg" width = "64px">
<form method="post">
<ul>
<li>
<label for="username">Username</label>
<input type="text" name="username" value="<?php echo $result['username']; ?>">
<button class="btn round" onclick="edit()" type="button"><i class="ri-edit-line"></i>Edit</button>
</li>
<li>
<label for="username">Email</label>
<input type="email" name="email" value="<?php echo $result['email'] ?>">
<button class="btn round" onclick="edit()" type="button"><i class="ri-edit-line"></i>Edit</button>
</li>
<li>
<label for="username">Password</label>
<input type="password" name="password" placeholder="Please enter your new password">
<button class="btn round" onclick="edit()" type="button"><i class="ri-edit-line"></i>Edit</button>
</li>
</ul>
<div>
<button class="btn round" type="submit">Submit all changes</button>
<button class="btn round" onclick="deleteUser()" type="button">Delete Account</button>
</div>
</form>
</div>
</div>
</main>
<?php include 'includes/dash.footer.php'; ?>