-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathServerAuthWebAPI.php
More file actions
395 lines (359 loc) · 10.2 KB
/
Copy pathServerAuthWebAPI.php
File metadata and controls
395 lines (359 loc) · 10.2 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/*
* ServerAuthWebAPI (v2.1) by EvolSoft
* Developer: EvolSoft (Flavius12)
* Website: http://www.evolsoft.tk
* Date: 16/01/2016 04:52 PM (UTC)
* Copyright & License: (C) 2015-2017 EvolSoft
* Licensed under MIT (https://github.com/EvolSoft/ServerAuth/blob/master/LICENSE)
*/
class ServerAuthWebAPI {
/** @var string WEBAPI_VERSION Current ServerAuthWebAPI version */
const WEBAPI_VERSION = "2.1";
/** @var string CURRENT_API Current ServerAuth plugin API version */
const CURRENT_API = "1.1.1";
/** @var int SUCCESS Success */
const SUCCESS = 1;
/** @var int ERR_OUTDATED_WEBAPI Outdated ServerAuthWebAPI error */
const ERR_OUTDATED_WEBAPI = 2;
/** @var int ERR_OUTDATED_PLUGIN Outdated ServerAuth plugin error */
const ERR_OUTDATED_PLUGIN = 3;
/** @var int ERR_MYSQL MySQL error */
const ERR_MYSQL = 4;
/** @var int $status */
private $status;
/** @var int $api_version */
private $api_version;
/** @var int $version */
private $version;
/** @var string $password_hash */
private $password_hash;
/** @var string $host */
private $host;
/** @var int $port */
private $port;
/** @var string $username */
private $username;
/** @var string $password */
private $password;
/** @var string $dbname */
private $dbname;
/** @var string $table_prefix */
private $table_prefix;
/**
* Initialize a new ServerAuthWebAPI instance
*
* @param string $host MySQL host
* @param int $port MySQL port
* @param string $username MySQL username
* @param string $password MySQL password
* @param string $database MySQL ServerAuth database
* @param string $table_prefix ServerAuth MySQL table prefix
*/
public function __construct($host, $port, $username, $password, $database, $table_prefix){
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->dbname = $database;
$this->table_prefix = $table_prefix;
$this->database = @new \mysqli($host, $username, $password, $database, $port);
if($this->database->connect_error){
$this->status = ServerAuthWebAPI::ERR_MYSQL;
}else{
$query = "SELECT api_version, version, password_hash FROM " . $table_prefix . "serverauth LIMIT 1";
if($this->database->query($query)){
$this->api_version = $this->getDatabase()->query($query)->fetch_assoc()["api_version"];
$this->version = $this->getDatabase()->query($query)->fetch_assoc()["version"];
$this->password_hash = $this->getDatabase()->query($query)->fetch_assoc()["password_hash"];
if($this->api_version > ServerAuthWebAPI::CURRENT_API){
$this->status = ServerAuthWebAPI::ERR_OUTDATED_WEBAPI;
}elseif($this->api_version < ServerAuthWebAPI::CURRENT_API){
$this->status = ServerAuthWebAPI::ERR_OUTDATED_PLUGIN;
}else{
$this->status = ServerAuthWebAPI::SUCCESS;
}
}else{
$this->status = ServerAuthWebAPI::ERR_MYSQL;
}
}
}
/**
* Get the current ServerAuthWebAPI instance status
*
* @return int (SUCCESS|ERR_OUTDATED_WEBAPI|ERR_OUTDATED_PLUGIN|ERR_MYSQL)
*/
public function getStatus(){
return $this->status;
}
/**
* Get ServerAuth plugin version
*
* @return string|int the current version string on SUCCESS, otherwise the current status
*/
public function getVersion(){
if($this->getStatus() == ServerAuthWebAPI::SUCCESS){
return $this->version;
}else{
return $this->getStatus();
}
}
/**
* Get ServerAuth plugin API version
*
* @return string|int the current API version string on SUCCESS, otherwise the current status
*/
public function getAPIVersion(){
if($this->getStatus() == ServerAuthWebAPI::SUCCESS){
return $this->api_version;
}else{
return $this->getStatus();
}
}
/**
* Get ServerAuthWebAPI version
*
* @return string
*/
public static function getWebAPIVersion(){
return ServerAuthWebAPI::WEBAPI_VERSION;
}
/**
* Get ServerAuth password hash
*
* @return string
*/
public function getPasswordHash(){
return $this->password_hash;
}
/**
* Get MySQL host
*
* @return string
*/
public function getHost(){
return $this->host;
}
/**
* Get MySQL port
*
* @return int
*/
public function getPort(){
return $this->port;
}
/**
* Get MySQL username
*
* @return string
*/
public function getUsername(){
return $this->username;
}
/**
* Get MySQL password
*
* @return string
*/
public function getPassword(){
return $this->password;
}
/**
* Get MySQL ServerAuth database name
*
* @return string
*/
public function getDatabaseName(){
return $this->dbname;
}
/**
* Get ServerAuth MySQL table prefix
*
* @return string
*/
public function getTablePrefix(){
return $this->table_prefix;
}
/**
* Get the current MySQL database instance
*
* @return mysqli|boolean
*/
public function getDatabase(){
if($this->database instanceof \mysqli){
return $this->database;
}else{
return false;
}
}
/**
* Check if a player is registered to ServerAuth
*
* @param string $player
*
* @return boolean|int true or false on SUCCESS, otherwise the current status
*/
public function isPlayerRegistered($player){
if($this->getStatus() == ServerAuthWebAPI::SUCCESS){
//Check MySQL connection
if($this->getDatabase() && $this->getDatabase()->ping()){
$stmt = $this->getDatabase()->prepare("SELECT user, password, ip, firstlogin, lastlogin FROM " . $this->getTablePrefix() . "serverauthdata WHERE user=?");
$stmt_player = strtolower($player);
$stmt->bind_param("s", $stmt_player);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows == 0){
$stmt->close();
return false;
}else{
$stmt->close();
return true;
}
}else{
return ServerAuthWebAPI::ERR_MYSQL;
}
}else{
return $this->getStatus();
}
}
/**
* Get player data
*
* @param string $player
*
* @return array|int the array of player data on SUCCESS, otherwise the current status
*/
public function getPlayerData($player){
if($this->getStatus() == ServerAuthWebAPI::SUCCESS){
if($this->isPlayerRegistered($player)){
//Check MySQL connection
if($this->getDatabase() && $this->getDatabase()->ping()){
$stmt = $this->getDatabase()->prepare("SELECT user, password, ip, firstlogin, lastlogin FROM " . $this->getTablePrefix() . "serverauthdata WHERE user=?");
$stmt_player = strtolower($player);
$stmt->bind_param("s", $stmt_player);
if($stmt->execute()){
$stmt->bind_result($user, $password, $ip, $firstlogin, $lastlogin);
$stmt->fetch();
$data = array(
"password" => $password,
"ip" => $ip,
"firstlogin" => $firstlogin,
"lastlogin" => $lastlogin
);
$stmt->close();
return $data;
}else{
$stmt->close();
return ServerAuthWebAPI::ERR_GENERIC;
}
}else{
return ServerAuthWebAPI::ERR_GENERIC;
}
}else{
return $this->isPlayerRegistered($player);
}
}else{
return $this->getStatus();
}
}
/**
* Register a player to ServerAuth
*
* @param string $player
* @param string $password
* @param string $ip
* @param int|double $firstlogin (UNIX timestamp)
* @param int|double $lastlogin (UNIX timestamp)
*
* @return int|boolean true on SUCCESS or false if the player is already registered, otherwise the current status
*/
public function registerPlayer($player, $password, $ip, $firstlogin, $lastlogin){
if($this->getStatus() == ServerAuthWebAPI::SUCCESS){
if(!$this->isPlayerRegistered($player)){
//Check MySQL connection
if($this->getDatabase() && $this->getDatabase()->ping()){
$stmt = $this->getDatabase()->prepare("INSERT INTO " . $this->getTablePrefix() . "serverauthdata (user, password, ip, firstlogin, lastlogin) VALUES (?, ?, ?, ?, ?)");
$password = hash($this->getPasswordHash(), $password);
$stmt->bind_param("sssss", $player, $password, $ip, $firstlogin, $lastlogin);
if($stmt->execute()){
$stmt->close();
return ServerAuthWebAPI::SUCCESS;
}else{
$stmt->close();
return ServerAuthWebAPI::ERR_MYSQL;
}
}else{
return ServerAuthWebAPI::ERR_MYSQL;
}
}else{
return $this->isPlayerRegistered($player);
}
}else{
return $this->getStatus();
}
}
/**
* Unregister a player
*
* @param string $player
*
* @return int|boolean true on SUCCESS or false if the player is not registered, otherwise the current status
*/
public function unregisterPlayer($player){
if($this->getStatus() == ServerAuthWebAPI::SUCCESS){
if($this->isPlayerRegistered($player)){
//Check MySQL connection
if($this->getDatabase() && $this->getDatabase()->ping()){
$stmt = $this->getDatabase()->prepare("DELETE FROM " . $this->getTablePrefix() . "serverauthdata WHERE user=?");
$stmt_player = strtolower($player);
$stmt->bind_param("s", $stmt_player);
if($stmt->execute()){
$stmt->close();
return ServerAuthWebAPI::SUCCESS;
}else{
$stmt->close();
return ServerAuthWebAPI::ERR_MYSQL;
}
}else{
return ServerAuthWebAPI::ERR_MYSQL;
}
}else{
return $this->isPlayerRegistered($player);
}
}else{
return $this->getStatus();
}
}
/**
* Change player password
*
* @param string $player
* @param string $new_password
*
* @return int|boolean true on SUCCESS or false if the player is not registered, otherwise the current status
*/
public function changePlayerPassword($player, $new_password){
if($this->getStatus() == ServerAuthWebAPI::SUCCESS){
if($this->isPlayerRegistered($player)){
//Check MySQL connection
if($this->getDatabase() && $this->getDatabase()->ping()){
$stmt = $this->getDatabase()->prepare("UPDATE " . $this->getTablePrefix() . "serverauthdata SET password=? WHERE user=?");
$stmt_password = hash($this->getPasswordHash(), $new_password);
$stmt_player = strtolower($player);
$stmt->bind_param("ss", $stmt_password, $stmt_player);
if($stmt->execute()){
$stmt->close();
return ServerAuthWebAPI::SUCCESS;
}else{
$stmt->close();
return ServerAuthWebAPI::ERR_MYSQL;
}
}
}else{
return $this->isPlayerRegistered($player);
}
}else{
return $this->getStatus();
}
}
}