This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
/
edit_profile.php
266 lines (244 loc) · 11.4 KB
/
edit_profile.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* File defines the EditProfileController PageController class
* @package PageController
*/
/**
*/
$current_dir = dirname (__FILE__);
require_once ($current_dir . DIRECTORY_SEPARATOR . "shared" . DIRECTORY_SEPARATOR . "bootstrap.php");
require_once (joinPath (INCLUDES_DIR, "models", "User.php"));
require_once (joinPath (INCLUDES_DIR, "Session.php"));
/**
* ADMIN AND USER PAGE (ADMIN WILL HAVE ACCESS TO EDIT MORE FIELDS). Interface for editing a user profile entry
*
* Display form for editing an profile entry. For POST requests,
* check user credentials, check if profile exists and then update entry in database.
* Available to members only
* @package PageController
*/
class EditProfileController implements Controller {
/**
* PageTemplate object used to render page
* @access protected
* @var PageTemplate
*/
protected $template;
/**
* Constructor. Create instance of PageTemplate using default index_tpl.php file
* @access public
*/
public function __construct () {
$this->template = new PageTemplate ();
}
/**
* Run method with main page logic
*
* Populate template and display form for editing an profile entry. For POST requests,
* check user credentials, check if profile exists and then update entry in database.
* Available to members only
* @access public
*/
public function run () {
$session = Session::getInstance ();
$user = $session->getUser ();
if ($user == null || !$user->validUser ()) {
$session->setMessage ("Do not have permission to access", Session::MESSAGE_ERROR);
header ("Location: " . BASE_URL);
return;
}
$userDAO = UserDAO::getInstance ();
$alter_user = null;
$form_errors = array ();
$form_values = array ("id" => "", "password" => "", "password2" => "", "status" => "", "usertype" => "", "steamId" => "", "xboxId" => "", "psnId" => "", "wiiId" => "");
// Check form
if (!empty ($_POST)) {
$form_values["id"] = isset ($_POST["id"]) ? trim ($_POST["id"]) : "";
$form_values["password"] = isset ($_POST["password"]) ? trim ($_POST["password"]) : "";
$form_values["password2"] = isset ($_POST["password2"]) ? trim ($_POST["password2"]) : "";
$form_values["status"] = isset ($_POST["status"]) ? trim ($_POST["status"]) : "";
$form_values["usertype"] = isset ($_POST["usertype"]) ? trim ($_POST["usertype"]) : "";
$form_values["steamId"] = isset ($_POST["steamId"]) ? trim ($_POST["steamId"]) : "";
$form_values["xboxId"] = isset ($_POST["xboxId"]) ? trim ($_POST["xboxId"]) : "";
$form_values["psnId"] = isset ($_POST["psnId"]) ? trim ($_POST["psnId"]) : "";
$form_values["wiiId"] = isset ($_POST["wiiId"]) ? trim ($_POST["wiiId"]) : "";
if (empty ($form_values["id"])) {
$form_errors["id"] = "User id not set";
}
if (empty ($form_values["password"]) && empty ($form_values["password2"])) {
}
else if (empty ($form_values["password"])) {
$form_errors["password"] = "Passwords not set";
}
else if (empty ($form_values["password2"])) {
$form_errors["password"] = "Passwords not set";
}
else if (strcmp ($form_values["password"], $form_values["password2"]) != 0) {
$form_errors["password"] = "Passwords do not match";
$form_values["password2"] = "";
}
if ($user->isAdmin () && !empty ($form_values["status"])) {
if (!is_numeric ($form_values["status"])) {
$form_errors["status"] = "Status must be a number";
}
// Check for valid status flag
else {
$status = intval ($form_values["status"]);
$tmp = new User ();
try {
$tmp->setUserType ($status);
} catch (InvalidUserTypeException $e) {
$form_errors["status"] = "Invalid value for status";
}
}
}
else if ($user->isAdmin () && empty ($form_values["status"])) {
$form_errors["status"] = "Status not defined";
}
if ($user->isAdmin () && !empty ($form_values["usertype"])) {
if (!is_numeric ($form_values["usertype"])) {
$form_errors["usertype"] = "Status must be a number";
}
$tmp = new User ();
try {
$tmp->setUserType ($status);
} catch (InvalidStatusException $e) {
$form_errors["usertype"] = "Invalid value for status";
}
}
else if ($user->isAdmin () && !empty ($form_values["usertype"])) {
$form_errors["usertype"] = "Type not defined";
}
// Regular expression check for identities
if (!empty ($form_values["steamId"])) {
if (strlen ($form_values["steamId"]) > 20) {
$form_errors["steamId"] = "Steam ID too long";
}
else if (!preg_match ("/^([A-Za-z0-9_]{3,20})$/", $form_values["steamId"])) {
$form_errors["steamId"] = "Steam ID is not valid";
}
}
if (!empty ($form_values["xboxId"])) {
if (strlen ($form_values["xboxId"]) > 15) {
$form_errors["xboxId"] = "Xbox gamertag too long";
}
else if (!preg_match ("/^[A-Za-z0-9 ]{3,15}$/", $form_values["xboxId"])) {
$form_errors["xboxId"] = "Xbox gamertag is not valid";
}
}
if (!empty ($form_values["psnId"])) {
if (strlen ($form_values["psnId"]) > 16) {
$form_errors["psnId"] = "PSN ID too long";
}
else if (!preg_match ("/^([A-Za-z0-9-_]+){3,16}$/", $form_values["psnId"])) {
$form_errors["psnId"] = "PSN ID is not valid";
}
}
if (!empty ($form_values["wiiId"])) {
if (strlen ($form_values["wiiId"]) > 20) {
$form_errors["wiiId"] = "Steam Id too long";
}
else if (!preg_match ("/^([0-9]{4}[- ][0-9]{4}[- ][0-9]{4}[- ][0-9]{4})$/", $form_values["wiiId"])) {
$form_errors["wiiId"] = "Wii Friend Code is not valid";
}
}
// No errors found
if (empty ($form_errors)) {
// Status call not done
$alter_user = $userDAO->load ($form_values["id"]);
if ($alter_user != null) {
if ($session->getUser ()->isAdmin () || ($alter_user->getId () == $session->getUser ()->id)) {
if (!empty ($form_values["password"])) {
$alter_user->setPassHash (sha1 ($form_values["password"]));
}
if (!empty ($form_values["status"])) {
$alter_user->setStatus (intval ($form_values["status"]));
}
if (!empty ($form_values["usertype"])) {
$alter_user->setUserType (intval ($form_values["usertype"]));
}
if (!empty ($form_values["steamId"])) {
$alter_user->setSteamId ($form_values["steamId"]);
}
if (!empty ($form_values["xboxId"])) {
$alter_user->setXboxId ($form_values["xboxId"]);
}
if (!empty ($form_values["psnId"])) {
$alter_user->setPsnId ($form_values["psnId"]);
}
if (!empty ($form_values["wiiId"])) {
$alter_user->setWiiId ($form_values["wiiId"]);
}
// Save profile
if ($userDAO->save ($alter_user)) {
$session->setMessage ("User profile altered");
header ("Location: {$_SERVER["PHP_SELF"]}?id={$alter_user->id}");
return;
}
// Save failed
else {
$session->setMessage ("User profile not altered", Session::MESSAGE_ERROR);
}
}
// User does not exist. Redirect
else {
header ("Location: " . BASE_URL);
return;
}
}
}
// Reload object
else if (empty ($form_errors["id"])) {
$alter_user = $userDAO->load ($form_values["id"]);
}
}
else if (!empty ($_GET)) {
$form_values["id"] = isset ($_GET["id"]) ? trim ($_GET["id"]) : "";
if (empty ($form_values["id"])) {
$form_errors["id"] = "User id not set";
}
if (empty ($form_errors)) {
$alter_user = $userDAO->load ($form_values["id"]);
// Value is null so user does not exist. Allow null to be passed to template
if (!$alter_user) {
}
// Admin access approval
else if ($session->getUser ()->isAdmin ()) {
$form_values["steamId"] = $alter_user->getSteamId ();
$form_values["xboxId"] = $alter_user->getXboxId ();
$form_values["psnId"] = $alter_user->getPsnId ();
$form_values["wiiId"] = $alter_user->getWiiId ();
}
// Disallow one regular member from altering another member's profile
else if (!$session->getUser ()->isAdmin () && $alter_user->getId () != $session->getUser ()->getId ()) {
$session->setMessage ("Do not have permission", Session::MESSAGE_ERROR);
header ("Location: " . BASE_URL);
return;
}
// Regular user access to own profile
else {
$form_values["steamId"] = $alter_user->getSteamId ();
$form_values["xboxId"] = $alter_user->getXboxId ();
$form_values["psnId"] = $alter_user->getPsnId ();
$form_values["wiiId"] = $alter_user->getWiiId ();
}
}
}
// Direct file access. Redirect
else {
header ("Location: " . BASE_URL);
return;
}
$this->template->render (array (
"title" => "Edit Profile",
"main_page" => "edit_profile_tpl.php",
"session" => $session,
"alter_user" => $alter_user,
"form_errors" => $form_errors,
"form_values" => $form_values,
));
}
}
$controller = new EditProfileController ();
$controller->run ();
?>