-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.php
More file actions
199 lines (188 loc) · 6.53 KB
/
settings.php
File metadata and controls
199 lines (188 loc) · 6.53 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
require_once('./config/include.php');
require_once('./src/modify_user.php');
require_once('./src/validations.php');
class SameUsernameException extends Exception {};
class OldEmailException extends Exception {};
class SameEmailException extends Exception {};
class EmptyFieldException extends Exception {};
$info = $err_username = $err_old_email = $err_email = $err_password = $err_new_password = $err_pass = '';
$new_username = $new_email = $old_email = '';
require_login('');
if (is_post_request()) {
// Notifications
if (isset($_POST['subscribe']) || isset($_POST['unsubscribe'])) {
try {
if (isset($_POST['subscribe'])) {
update_notifications($dbc, 1);
update_session_notifications(1);
}
else {
update_notifications($dbc, 0);
update_session_notifications(0);
}
$qparam = http_build_query(array('info' => 'notifications'));
header('Location: settings.php?' . $qparam);
} catch (GeneralErrorException $e) {
$info = "Sorry, something went wrong. Please try again.";
}
}
// Change username
if (isset($_POST['submit-new-username'])) {
try {
$new_username = validate_username($_POST['new-username']);
if ($new_username == $_SESSION['username'])
throw new SameUsernameException();
update_username($dbc, $new_username);
update_session_username($new_username);
$qparam = http_build_query(array('info' => 'username_updated',
'username' => $new_username));
header('Location: settings.php?' . $qparam);
}
catch (UsernameExistsException $e) { //error thrown if username or email already in use, see unique indexes (sql)
$err_username = "Username already exists. Try another username." . PHP_EOL;
}
catch (SameUsernameException $e) {
$err_username = "Please provide new username.";
}
catch (ValidationException $e) {
$err_username = $e->getMessage();
}
if ($err_username) {
echo get_template('settings.php', array(
'title' => 'Profile settings',
'info' => $info,
'err_username' => $err_username,
'err_old_email' => $err_old_email,
'err_email' => $err_email,
'err_password' => $err_password,
'err_new_password' => $err_new_password,
'err_pass' => $err_pass,
));
}
}
// Change email address
if (isset($_POST['submit-new-email'])) {
try {
$old_email = validate_email($_POST['old-email']);
$new_email = validate_email($_POST['new-email']);
if ($old_email !== $_SESSION['email'])
throw new OldEmailException();
if ($new_email == $_SESSION['email'])
throw new SameEmailException();
update_email($dbc, $new_email);
update_session_email($new_email);
$qparam = http_build_query(array('info' => 'email_updated',
'email' => $new_email));
header('Location: settings.php?' . $qparam);
}
catch (EmailExistsException $e) {
$err_email = "This email is already in use, please try again.";
}
catch (OldEmailException $e) {
$err_old_email = "Please confirm with your current email address.";
}
catch (SameEmailException $e) {
$err_email = "Same email address provided, please insert new one.";
}
catch (ValidationException $e) {
$err_email = $e->getMessage();
}
if ($err_email || $err_old_email) {
echo get_template('settings.php', array(
'title' => 'Profile settings',
'info' => $info,
'err_username' => $err_username,
'err_old_email' => $err_old_email,
'err_email' => $err_email,
'err_password' => $err_password,
'err_new_password' => $err_new_password,
'err_pass' => $err_pass,
));
}
}
// Change password
if (isset($_POST['submit-new-password'])) {
try {
if (!$_POST['old-password'])
throw new EmptyFieldException();
verify_current_password($dbc, $_POST['old-password']);
validate_password($_POST['new-password']);
validate_confirmation($_POST['new-password'], $_POST['repeat-password']);
update_password($dbc, $_POST['new-password']);
$qparam = http_build_query(array('info' => 'password_updated'));
header('Location: settings.php?' . $qparam);
} catch (ValidationException $e) {
$err_new_password = $e->getMessage();
} catch (WrongPasswordException $e) {
$err_password = "Wrong password. Try again.";
} catch (EmptyFieldException $e) {
$err_password = "Please insert your current password.";
} catch (GeneralErrorException $e) {
$info = "Sorry, something went wrong. Please try again.";
}
if ($err_password || $err_new_password || $info) {
echo get_template('settings.php', array(
'title' => 'Profile settings',
'info' => $info,
'err_username' => $err_username,
'err_old_email' => $err_old_email,
'err_email' => $err_email,
'err_password' => $err_password,
'err_new_password' => $err_new_password,
'err_pass' => $err_pass,
));
}
}
// Delete user
if (isset($_POST['submit-removal'])) {
try {
if (!$_POST['password'])
throw new EmptyFieldException();
verify_current_password($dbc, $_POST['password']);
delete_user($dbc, $_SESSION['user_id']);
header('Location: logout.php');
} catch (EmptyFieldException $e) {
$err_pass = "Please confirm action with your current password.";
} catch (WrongPasswordException $e) {
$err_pass = "Wrong password. Try again.";
} catch (GeneralErrorException $e) {
$info = "Sorry, something went wrong. Please try again.";
}
if ($err_pass || $info) {
echo get_template('settings.php', array(
'title' => 'Profile settings',
'info' => $info,
'err_username' => $err_username,
'err_old_email' => $err_old_email,
'err_email' => $err_email,
'err_password' => $err_password,
'err_new_password' => $err_new_password,
'err_pass' => $err_pass,
));
}
}
}
if (is_get_request()) {
if (isset($_GET['info'])) {
if ($_GET['info'] === 'notifications')
$info = "Done! Notification settings are now updated.";
if ($_GET['info'] === 'username_updated')
$info = "Success! Your new username is now " . $_GET['username'];
if ($_GET['info'] === 'email_updated')
$info = "Success! Your new email address is now " . $_GET['email'];
if ($_GET['info'] === 'password_updated')
$info = "Done! Your password was updated successfully.";
}
echo get_template('settings.php', array(
'title' => 'Profile settings',
'info' => $info,
'err_username' => $err_username,
'err_old_email' => $err_old_email,
'err_email' => $err_email,
'err_password' => $err_password,
'err_new_password' => $err_new_password,
'err_pass' => $err_pass,
));
}
?>