-
Notifications
You must be signed in to change notification settings - Fork 47
/
clientcreateaccount.php
285 lines (253 loc) · 9.76 KB
/
clientcreateaccount.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Create account by client
*
* @package MyAAC
* @author Elson <[email protected]>
* @author OpenTibiaBR
* @copyright 2023 MyAAC
* @link https://github.com/opentibiabr/myaac
*/
global $config, $db;
require_once 'common.php';
require_once 'config.php';
require_once 'config.local.php';
require_once SYSTEM . 'functions.php';
require_once SYSTEM . 'init.php';
$request = file_get_contents('php://input');
$result = json_decode($request);
//log_append('create_account_service.log', "req: $request");
if ($result === null || (isset($result->Type) && isset($result->type))) {
log_append('create_account_service.log', 'Request error!');
}
$action = mb_strtolower($result->type ?? ($result->Type ?? ''));
$response = null;
switch ($action) {
case 'getaccountcreationstatus':
$response = json_encode([
"Worlds" => [getServerStatus()],
"RecommendedWorld" => configLua('serverName'),
"IsCaptchaDeactivated" => true
]);
break;
case 'generatecharactername':
// todo generate suggested names.
$response = json_encode([
"GeneratedName" => ""
]);
break;
case 'checkcharactername':
$name = $result->CharacterName;
$check = $db->query("SELECT `name` FROM `players` WHERE `name` = {$db->quote($name)}")->rowCount() == 0;
$response = json_encode([
"CharacterName" => $name,
"IsAvailable" => $check
]);
break;
case 'checkemail':
$email = mb_strtolower($result->Email);
$check = filter_var($email, FILTER_VALIDATE_EMAIL) && $db->query("SELECT `email` FROM `accounts` WHERE `email` = {$db->quote($email)}")->rowCount() == 0;
$response = json_encode($check ? [
"IsValid" => true,
"EMail" => $email
] : [
"IsValid" => false,
"errorCode" => 59,
"errorMessage" => "This email address has an invalid format. Please enter a correct email address!",
"EMail" => $email
]);
break;
case 'checkpassword':
$errors = validatePassword($pass = $result->Password1);
$response = json_encode([
"PasswordRequirements" => [
"PasswordLength" => !in_array(0, $errors),
"InvalidCharacters" => !in_array(1, $errors),
"HasLowerCase" => !in_array(2, $errors),
"HasUpperCase" => !in_array(3, $errors),
"HasNumber" => !in_array(4, $errors)
],
"Password1" => $pass,
"PasswordStrength" => count($errors) == 0 ? 4 : abs(count($errors) - 4),
"PasswordStrengthColor" => count($errors) == 0 ? "#20a000" : '#ec644b',
"PasswordValid" => count($errors) == 0,
]);
break;
case 'createaccountandcharacter':
$email = mb_strtolower($result->EMail);
try {
if (filter_var($email, FILTER_VALIDATE_EMAIL) && $db->query("SELECT `email` FROM `accounts` WHERE `email` = {$db->quote($email)}")->rowCount() == 0) {
$response = json_encode([
"Success" => true,
"AccountID" => createAccount([
"email" => $email,
"password" => $result->Password,
"characterName" => stripslashes(ucwords(strtolower($result->CharacterName))),
"characterSex" => $result->CharacterSex == 'male' ? 1 : 0,
//"clientType" => "Client",
//"recaptcha2token" => "",
//"recaptcha3token" => "",
//"selectedWorld" => configLua('serverName'),
]),
// "Password" => $result->Password
]);
} else {
throw new Exception('Email invalid!');
}
} catch (Exception $e) {
log_append('create_account_service.log', "Error caught: {$e->getMessage()}");
$response = json_encode([
"errorCode" => 101,
"errorMessage" => $e->getMessage(), //"An internal error has occurred. Please try again later!",
"Success" => false,
"IsRecaptcha2Requested" => false,
]);
}
break;
}
log_append('create_account_service.log', "response: $response");
die($response);
/**
* Function to return server status as array to worlds list
*
* @return array
*/
function getServerStatus(): array
{
global $db;
$playersOnline = $db->query("SELECT COUNT(*) FROM `players_online`")->fetchAll()[0][0] ?? 0;
$date = $db->query("SELECT `date` FROM `myaac_changelog`")->fetch()['date'] ?? 0;
switch (configLua('worldType')) {
default:
case 'pvp':
$pvpType = "Open PVP";
break;
case 'no-pvp':
$pvpType = 'Optional PVP';
break;
case 'pvp-enforced':
$pvpType = 'PVP';
break;
case 'retro-pvp':
$pvpType = 'Retro PvP';
break;
}
return [
"Name" => configLua('serverName'),
"PlayersOnline" => intval($playersOnline),
"CreationDate" => intval($date),
"Region" => "America",
"PvPType" => $pvpType,
"PremiumOnly" => configLua('onlyPremiumAccount'),
"TransferType" => "Blocked",
"BattlEyeActivationTimestamp" => intval($date),
"BattlEyeInitiallyActive" => 0
];
}
/**
* Function to check strength of password
*
* @param $password
* @return array|int[]
*/
function validatePassword($password): array
{
$checks = [];
$minLength = 10;
$invalids = [' ', '\'', '"', '\\', '/'];
if (strlen($password) < $minLength) $checks = [0];
foreach ($invalids as $char) {
if (strpos($password, $char) !== false) $checks = array_merge($checks, [1]);
}
if (!preg_match('/[a-z]/', $password)) $checks = array_merge($checks, [2]);
if (!preg_match('/[A-Z]/', $password)) $checks = array_merge($checks, [3]);
if (!preg_match('/[0-9]/', $password)) $checks = array_merge($checks, [4]);
return $checks;
}
/**
* Function to create account
*
* @param array $data
* @return false|string
* @throws Exception
*/
function createAccount(array $data)
{
global $db, $config, $twig;
try {
$account_name = USE_ACCOUNT_NAME ?
@(explode('@', $data['email']))[0] . date('isH')
: date('HisdmyHi');
if (USE_ACCOUNT_NAME && mb_strtolower($account_name) == mb_strtolower($data['password'])) {
throw new Exception('Password may not be the same as account name.');
}
$account_db = new OTS_Account();
$account_db->find($account_name);
if ($account_db->isLoaded()) {
throw new Exception('Account with this name already exist.');
}
$info = json_decode(@file_get_contents('https://ipinfo.io/' . $_SERVER['REMOTE_ADDR'] . '/geo'), true);
$country = strtolower($info['country'] ?? 'br');
$new_account = new OTS_Account();
$new_account->create($account_name);
$config_salt_enabled = $db->hasColumn('accounts', 'salt');
if ($config_salt_enabled) {
$salt = generateRandomString(10, false, true, true);
$data['password'] = $salt . $data['password'];
$new_account->setCustomField('salt', $salt);
}
$new_account->setPassword(encrypt($data['password']));
$new_account->setEMail($data['email']);
$new_account->setCustomField('created', time());
$new_account->setCustomField('country', $country);
$new_account->save();
$new_account->logAction('Account created.');
if (($premdays = ($config['account_premium_days'] ?? 0)) > 0) {
if ($db->hasColumn('accounts', 'premend')) { // othire
$new_account->setCustomField('premend', time() + $premdays * 86400);
} else {
$new_account->setCustomField('premdays', $premdays);
$lastDay = ($premdays < OTS_Account::GRATIS_PREMIUM_DAYS) ? time() + ($premdays * 86400) : 0;
$new_account->setCustomField('lastday', $lastDay);
}
}
if (($welcomeCoins = ($config['account_welcome_coins'] ?? 0)) > 0) {
$coinType = $config['account_coin_type_usage'] ?? 'coins_transferable';
$new_account->setCustomField($coinType, $welcomeCoins);
}
if ($config['mail_enabled'] && $config['account_mail_verify']) {
$hash = md5(generateRandomString(16, true, true) . $data['email']);
$new_account->setCustomField('email_hash', $hash);
$verify_url = getLink('account/confirm_email/' . $hash);
$body_html = $twig->render('mail.account.verify.html.twig', array(
'account' => $account_name,
'verify_url' => generateLink($verify_url, $verify_url, true)
));
if (!_mail($data['email'], 'New account on ' . configLua('serverName'), $body_html)) {
$new_account->delete();
throw new Exception('An error occurred while sending email! Account not created. Try again. For Admin: More info can be found in system/logs/mailer-error.log');
}
} else if ($config['mail_enabled'] && $config['account_welcome_mail']) {
$mailBody = $twig->render('account.welcome_mail.html.twig', ['account' => $account_name]);
if (!_mail($data['email'], 'Your account on ' . configLua('serverName'), $mailBody)) {
throw new Exception('An error occurred while sending email. For Admin: More info can be found in system/logs/mailer-error.log');
}
}
// Start an output buffer
ob_start();
// character creation only if start in dawnport
if (count(config('character_samples')) == 1 && !empty($data['characterName'])) {
require_once LIBS . 'CreateCharacter.php';
$createCharacter = new CreateCharacter();
$errors = [];
if (!$createCharacter->doCreate($data['characterName'], $data['characterSex'], 0, 0, $new_account, $errors)) {
throw new Exception('There was an error creating your character. Please create your character later in account management page.');
}
}
// Clear and closes the output buffer
ob_end_clean();
return $account_name;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}