1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Smr\Pages\Account; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Smr\Database; |
7
|
|
|
use Smr\DisplayNameValidator; |
8
|
|
|
use Smr\Exceptions\AccountNotFound; |
9
|
|
|
use Smr\Page\AccountPageProcessor; |
10
|
|
|
use Smr\Pages\Player\CurrentSector; |
11
|
|
|
use Smr\Request; |
12
|
|
|
use Smr\Session; |
13
|
|
|
use SmrAccount; |
14
|
|
|
|
15
|
|
|
class PreferencesProcessor extends AccountPageProcessor { |
16
|
|
|
|
17
|
|
|
public function build(SmrAccount $account): never { |
18
|
|
|
if (Session::getInstance()->hasGame()) { |
19
|
|
|
$nextPage = CurrentSector::class; |
20
|
|
|
} else { |
21
|
|
|
$nextPage = GamePlay::class; |
22
|
|
|
} |
23
|
|
|
$action = Request::get('action'); |
24
|
|
|
|
25
|
|
|
if ($action == 'Save and resend validation code') { |
26
|
|
|
$email = Request::get('email'); |
27
|
|
|
|
28
|
|
|
$account->changeEmail($email); |
29
|
|
|
|
30
|
|
|
// overwrite container |
31
|
|
|
$nextPage = Validate::class; |
32
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your email address, you will now need to revalidate with the code sent to the new email address.'; |
33
|
|
|
|
34
|
|
|
} elseif ($action == 'Change Password') { |
35
|
|
|
$new_password = Request::get('new_password'); |
36
|
|
|
$old_password = Request::get('old_password'); |
37
|
|
|
$retype_password = Request::get('retype_password'); |
38
|
|
|
|
39
|
|
|
if (empty($new_password)) { |
40
|
|
|
create_error('You must enter a non empty password!'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!$account->checkPassword($old_password)) { |
44
|
|
|
create_error('Your current password is wrong!'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($new_password != $retype_password) { |
48
|
|
|
create_error('The passwords you entered don\'t match!'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($new_password == $account->getLogin()) { |
52
|
|
|
create_error('Your chosen password is invalid!'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$account->setPassword($new_password); |
56
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your password.'; |
57
|
|
|
|
58
|
|
|
} elseif ($action == 'Change Name') { |
59
|
|
|
$HoF_name = Request::get('HoF_name'); |
60
|
|
|
|
61
|
|
|
DisplayNameValidator::validate($HoF_name); |
62
|
|
|
|
63
|
|
|
//no duplicates |
64
|
|
|
try { |
65
|
|
|
$other = SmrAccount::getAccountByHofName($HoF_name); |
66
|
|
|
if (!$account->equals($other)) { |
67
|
|
|
create_error('Someone is already using that Hall of Fame name!'); |
68
|
|
|
} |
69
|
|
|
} catch (AccountNotFound) { |
70
|
|
|
// Proceed, this Hall of Fame name is not in use |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// set the HoF name in account stat |
74
|
|
|
$account->setHofName($HoF_name); |
75
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your Hall of Fame name.'; |
76
|
|
|
|
77
|
|
|
} elseif ($action == 'Change Discord ID') { |
78
|
|
|
$discordId = Request::get('discord_id'); |
79
|
|
|
|
80
|
|
|
if (empty($discordId)) { |
81
|
|
|
$account->setDiscordId(null); |
82
|
|
|
$message = '<span class="green">SUCCESS: </span>You have deleted your Discord User ID.'; |
83
|
|
|
} else { |
84
|
|
|
// no duplicates |
85
|
|
|
try { |
86
|
|
|
$other = SmrAccount::getAccountByDiscordId($discordId); |
87
|
|
|
if (!$account->equals($other)) { |
88
|
|
|
create_error('Someone is already using that Discord User ID!'); |
89
|
|
|
} |
90
|
|
|
} catch (AccountNotFound) { |
91
|
|
|
// Proceed, this Discord ID is not in use |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$account->setDiscordId($discordId); |
95
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your Discord User ID.'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} elseif ($action == 'Change IRC Nick') { |
99
|
|
|
$ircNick = Request::get('irc_nick'); |
100
|
|
|
|
101
|
|
|
// here you can delete your registered irc nick |
102
|
|
|
if (empty($ircNick)) { |
103
|
|
|
$account->setIrcNick(null); |
104
|
|
|
$message = '<span class="green">SUCCESS: </span>You have deleted your irc nick.'; |
105
|
|
|
} else { |
106
|
|
|
// Disallow control characters and spaces |
107
|
|
|
if (!ctype_graph($ircNick)) { |
108
|
|
|
create_error('Your IRC Nick may only contain visible printed characters!'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// no duplicates |
112
|
|
|
try { |
113
|
|
|
$other = SmrAccount::getAccountByIrcNick($ircNick); |
114
|
|
|
if (!$account->equals($other)) { |
115
|
|
|
create_error('Someone is already using that IRC nick!'); |
116
|
|
|
} |
117
|
|
|
} catch (AccountNotFound) { |
118
|
|
|
// Proceed, this IRC nick is not in use |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// save irc nick in db and set message |
122
|
|
|
$account->setIrcNick($ircNick); |
123
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your irc nick.'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} elseif ($action == 'Change Timezone') { |
127
|
|
|
$timez = Request::getInt('timez'); |
128
|
|
|
|
129
|
|
|
$db = Database::getInstance(); |
130
|
|
|
$db->write('UPDATE account SET offset = ' . $db->escapeNumber($timez) . ' WHERE account_id = ' . $db->escapeNumber($account->getAccountID())); |
131
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your time offset.'; |
132
|
|
|
|
133
|
|
|
} elseif ($action == 'Change Date Formats') { |
134
|
|
|
$account->setDateFormat(Request::get('dateformat')); |
135
|
|
|
$account->setTimeFormat(Request::get('timeformat')); |
136
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your date formats.'; |
137
|
|
|
|
138
|
|
|
} elseif ($action == 'Change Images') { |
139
|
|
|
$account->setDisplayShipImages(Request::get('images') == 'Yes'); |
140
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your ship images preferences.'; |
141
|
|
|
|
142
|
|
|
} elseif ($action == 'Change Centering') { |
143
|
|
|
$account->setCenterGalaxyMapOnPlayer(Request::get('centergalmap') == 'Yes'); |
144
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your centering galaxy map preferences.'; |
145
|
|
|
|
146
|
|
|
} elseif ($action == 'Change Size') { |
147
|
|
|
$fontsize = Request::getInt('fontsize'); |
148
|
|
|
if ($fontsize < 50) { |
149
|
|
|
create_error('Minimum font size is 50%'); |
150
|
|
|
} |
151
|
|
|
$account->setFontSize($fontsize); |
152
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your font size.'; |
153
|
|
|
|
154
|
|
|
} elseif ($action == 'Change CSS Options') { |
155
|
|
|
$account->setCssLink(Request::get('csslink')); |
156
|
|
|
$cssTemplateAndColor = Request::get('template'); |
157
|
|
|
if ($cssTemplateAndColor == 'None') { |
158
|
|
|
$account->setDefaultCSSEnabled(false); |
159
|
|
|
} else { |
160
|
|
|
$account->setDefaultCSSEnabled(true); |
161
|
|
|
[$cssTemplate, $cssColourScheme] = explode(' - ', $cssTemplateAndColor); |
162
|
|
|
$account->setTemplate($cssTemplate); |
163
|
|
|
$account->setColourScheme($cssColourScheme); |
164
|
|
|
} |
165
|
|
|
$message = '<span class="green">SUCCESS: </span>You have changed your CSS options.'; |
166
|
|
|
|
167
|
|
|
} elseif ($action == 'Save Hotkeys') { |
168
|
|
|
foreach (SmrAccount::getDefaultHotkeys() as $hotkey => $binding) { |
169
|
|
|
$account->setHotkey($hotkey, explode(' ', Request::get($hotkey))); |
170
|
|
|
} |
171
|
|
|
$message = '<span class="green">SUCCESS: </span>You have saved your hotkeys.'; |
172
|
|
|
|
173
|
|
|
} elseif ($action == 'Update Colours') { |
174
|
|
|
$friendlyColour = Request::get('friendly_color'); |
175
|
|
|
$neutralColour = Request::get('neutral_color'); |
176
|
|
|
$enemyColour = Request::get('enemy_color'); |
177
|
|
|
|
178
|
|
|
if (strlen($friendlyColour) == 6) { |
179
|
|
|
$account->setFriendlyColour($friendlyColour); |
180
|
|
|
} |
181
|
|
|
if (strlen($neutralColour) == 6) { |
182
|
|
|
$account->setNeutralColour($neutralColour); |
183
|
|
|
} |
184
|
|
|
if (strlen($enemyColour) == 6) { |
185
|
|
|
$account->setEnemyColour($enemyColour); |
186
|
|
|
} |
187
|
|
|
$message = '<span class="green">SUCCESS: </span> You have updated your colors.'; |
188
|
|
|
|
189
|
|
|
} elseif ($action == 'Toggle Ajax') { |
190
|
|
|
$account->setUseAJAX(!$account->isUseAJAX()); |
191
|
|
|
$status = $account->isUseAJAX() ? 'enabled' : 'disabled'; |
192
|
|
|
$message = '<span class="green">SUCCESS: </span> You have ' . $status . ' AJAX auto-refresh.'; |
193
|
|
|
|
194
|
|
|
} else { |
195
|
|
|
throw new Exception('Unknown action: ' . $action); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Update the account in case it has changed |
199
|
|
|
$account->update(); |
200
|
|
|
|
201
|
|
|
$container = new $nextPage(message: $message); |
202
|
|
|
$container->go(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
|