mirror of
https://github.com/slawkens/myaac.git
synced 2025-10-13 17:24:54 +02:00
Update login.php for latest TFS 1.x and otservbr
Works in both. Thanks for Znote for rfc6238 lib.
This commit is contained in:
277
login.php
277
login.php
@@ -1,45 +1,47 @@
|
||||
<?php
|
||||
require_once('common.php');
|
||||
require_once('config.php');
|
||||
require_once('config.local.php');
|
||||
require_once(SYSTEM . 'functions.php');
|
||||
require_once(SYSTEM . 'init.php');
|
||||
require_once(SYSTEM . 'status.php');
|
||||
require_once 'common.php';
|
||||
require_once 'config.php';
|
||||
require_once 'config.local.php';
|
||||
require_once SYSTEM . 'functions.php';
|
||||
require_once SYSTEM . 'init.php';
|
||||
require_once SYSTEM . 'status.php';
|
||||
|
||||
# error function
|
||||
function sendError($msg){
|
||||
function sendError($message, $code = 3){
|
||||
$ret = [];
|
||||
$ret["errorCode"] = 3;
|
||||
$ret["errorMessage"] = $msg;
|
||||
$ret['errorCode'] = $code;
|
||||
$ret['errorMessage'] = $message;
|
||||
die(json_encode($ret));
|
||||
}
|
||||
|
||||
# event schedule function
|
||||
function parseEvent($table1, $date, $table2)
|
||||
{
|
||||
if ($table1) {
|
||||
if ($date) {
|
||||
if ($table2) {
|
||||
$date = $table1->getAttribute('startdate');
|
||||
return date_create("{$date}")->format('U');
|
||||
if ($table1) {
|
||||
if ($date) {
|
||||
if ($table2) {
|
||||
$date = $table1->getAttribute('startdate');
|
||||
return date_create("{$date}")->format('U');
|
||||
} else {
|
||||
$date = $table1->getAttribute('enddate');
|
||||
return date_create("{$date}")->format('U');
|
||||
}
|
||||
} else {
|
||||
$date = $table1->getAttribute('enddate');
|
||||
return date_create("{$date}")->format('U');
|
||||
}
|
||||
} else {
|
||||
foreach($table1 as $attr) {
|
||||
if ($attr) {
|
||||
return $attr->getAttribute($table2);
|
||||
foreach($table1 as $attr) {
|
||||
if ($attr) {
|
||||
return $attr->getAttribute($table2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
return 'error';
|
||||
}
|
||||
|
||||
$request = file_get_contents('php://input');
|
||||
$result = json_decode($request);
|
||||
$action = isset($result->type) ? $result->type : '';
|
||||
$request = json_decode(file_get_contents('php://input'));
|
||||
$action = $request->type ?? '';
|
||||
|
||||
/** @var OTS_Base_DB $db */
|
||||
/** @var array $config */
|
||||
|
||||
switch ($action) {
|
||||
case 'cacheinfo':
|
||||
@@ -51,35 +53,32 @@ switch ($action) {
|
||||
'gamingyoutubestreams' => 0,
|
||||
'gamingyoutubeviewer' => 0
|
||||
]));
|
||||
break;
|
||||
|
||||
case 'eventschedule':
|
||||
$eventlist = [];
|
||||
$file_path = config('server_path') . 'data/XML/events.xml';
|
||||
if (!file_exists($file_path)) {
|
||||
die(json_encode([]));
|
||||
break;
|
||||
}
|
||||
$xml = new DOMDocument;
|
||||
$xml->load($file_path);
|
||||
$tmplist = [];
|
||||
$tableevent = $xml->getElementsByTagName('event');
|
||||
|
||||
foreach ($tableevent as $event) {
|
||||
if ($event) { $tmplist = [
|
||||
'colorlight' => parseEvent($event->getElementsByTagName('colors'), false, 'colorlight'),
|
||||
'colordark' => parseEvent($event->getElementsByTagName('colors'), false, 'colordark'),
|
||||
'description' => parseEvent($event->getElementsByTagName('description'), false, 'description'),
|
||||
'displaypriority' => intval(parseEvent($event->getElementsByTagName('details'), false, 'displaypriority')),
|
||||
'enddate' => intval(parseEvent($event, true, false)),
|
||||
'isseasonal' => getBoolean(intval(parseEvent($event->getElementsByTagName('details'), false, 'isseasonal'))),
|
||||
'name' => $event->getAttribute('name'),
|
||||
'startdate' => intval(parseEvent($event, true, true)),
|
||||
'specialevent' => intval(parseEvent($event->getElementsByTagName('details'), false, 'specialevent'))
|
||||
];
|
||||
$eventlist[] = $tmplist; } }
|
||||
die(json_encode(['eventlist' => $eventlist, 'lastupdatetimestamp' => time()]));
|
||||
break;
|
||||
case 'eventschedule':
|
||||
$eventlist = [];
|
||||
$file_path = config('server_path') . 'data/XML/events.xml';
|
||||
if (!file_exists($file_path)) {
|
||||
die(json_encode([]));
|
||||
}
|
||||
$xml = new DOMDocument;
|
||||
$xml->load($file_path);
|
||||
$tmplist = [];
|
||||
$tableevent = $xml->getElementsByTagName('event');
|
||||
|
||||
foreach ($tableevent as $event) {
|
||||
if ($event) { $tmplist = [
|
||||
'colorlight' => parseEvent($event->getElementsByTagName('colors'), false, 'colorlight'),
|
||||
'colordark' => parseEvent($event->getElementsByTagName('colors'), false, 'colordark'),
|
||||
'description' => parseEvent($event->getElementsByTagName('description'), false, 'description'),
|
||||
'displaypriority' => intval(parseEvent($event->getElementsByTagName('details'), false, 'displaypriority')),
|
||||
'enddate' => intval(parseEvent($event, true, false)),
|
||||
'isseasonal' => getBoolean(intval(parseEvent($event->getElementsByTagName('details'), false, 'isseasonal'))),
|
||||
'name' => $event->getAttribute('name'),
|
||||
'startdate' => intval(parseEvent($event, true, true)),
|
||||
'specialevent' => intval(parseEvent($event->getElementsByTagName('details'), false, 'specialevent'))
|
||||
];
|
||||
$eventlist[] = $tmplist; } }
|
||||
die(json_encode(['eventlist' => $eventlist, 'lastupdatetimestamp' => time()]));
|
||||
|
||||
case 'boostedcreature':
|
||||
$boostDB = $db->query("select * from " . $db->tableName('boosted_creature'))->fetchAll();
|
||||
@@ -92,9 +91,9 @@ switch ($action) {
|
||||
break;
|
||||
|
||||
case 'login':
|
||||
|
||||
|
||||
$port = $config['lua']['gameProtocolPort'];
|
||||
|
||||
|
||||
// default world info
|
||||
$world = [
|
||||
'id' => 0,
|
||||
@@ -115,75 +114,136 @@ switch ($action) {
|
||||
];
|
||||
|
||||
$characters = [];
|
||||
$account = null;
|
||||
|
||||
// common columns
|
||||
$columns = 'name, level, sex, vocation, looktype, lookhead, lookbody, looklegs, lookfeet, lookaddons, lastlogin, isreward, istutorial';
|
||||
|
||||
$account = new OTS_Account();
|
||||
$account->findByEmail($result->email);
|
||||
|
||||
$inputEmail = $request->email ?? false;
|
||||
$inputAccountName = $request->accountname ?? false;
|
||||
$inputToken = $request->token ?? false;
|
||||
|
||||
if ($inputEmail != false) { // login by email
|
||||
$account->findByEmail($request->email);
|
||||
}
|
||||
else if($inputAccountName != false) { // login by account name
|
||||
$account->find($inputAccountName);
|
||||
}
|
||||
|
||||
$config_salt_enabled = fieldExist('salt', 'accounts');
|
||||
$current_password = encrypt(($config_salt_enabled ? $account->getCustomField('salt') : '') . $result->password);
|
||||
$current_password = encrypt(($config_salt_enabled ? $account->getCustomField('salt') : '') . $request->password);
|
||||
|
||||
if (!$account->isLoaded() || $account->getPassword() != $current_password) {
|
||||
sendError('Email or password is not correct.');
|
||||
sendError(($inputEmail != false ? 'Email' : 'Account name') . ' or password is not correct.');
|
||||
}
|
||||
|
||||
//log_append('test.log', var_export($account->getCustomField('secret'), true));
|
||||
$accountHasSecret = false;
|
||||
if (fieldExist('secret', 'accounts')) {
|
||||
$accountSecret = $account->getCustomField('secret');
|
||||
if ($accountSecret != null && $accountSecret != '') {
|
||||
$accountHasSecret = true;
|
||||
if ($inputToken === false) {
|
||||
sendError('Submit a valid two-factor authentication token.', 6);
|
||||
} else {
|
||||
require_once LIBS . 'rfc6238.php';
|
||||
if (TokenAuth6238::verify($accountSecret, $inputToken) !== true) {
|
||||
sendError('Two-factor authentication failed, token is wrong.', 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// common columns
|
||||
$columns = 'id, name, level, sex, vocation, looktype, lookhead, lookbody, looklegs, lookfeet, lookaddons';
|
||||
|
||||
if (fieldExist('isreward', 'accounts')) {
|
||||
$columns .= ', isreward';
|
||||
}
|
||||
|
||||
if (fieldExist('istutorial', 'accounts')) {
|
||||
$columns .= ', istutorial';
|
||||
}
|
||||
|
||||
$players = $db->query("select {$columns} from players where account_id = " . $account->getId() . " AND deletion = 0");
|
||||
if($players && $players->rowCount() > 0) {
|
||||
$players = $players->fetchAll();
|
||||
|
||||
$highestLevelId = 0;
|
||||
$highestLevel = 0;
|
||||
foreach ($players as $player) {
|
||||
$characters[] = create_char($player);
|
||||
if ($player['level'] >= $highestLevel) {
|
||||
$highestLevel = $player['level'];
|
||||
$highestLevelId = $player['id'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($players as $player) {
|
||||
$characters[] = create_char($player, $highestLevelId);
|
||||
}
|
||||
}
|
||||
|
||||
$save = false;
|
||||
$timeNow = time();
|
||||
$query = $db->query("select `premdays`, `lastday` from `accounts` where `id` = " . $account->getId());
|
||||
if($query->rowCount() > 0) {
|
||||
|
||||
if (fieldExist('premdays', 'accounts') && fieldExist('lastday', 'accounts')) {
|
||||
$save = false;
|
||||
$timeNow = time();
|
||||
$query = $db->query("select `premdays`, `lastday` from `accounts` where `id` = " . $account->getId());
|
||||
if ($query->rowCount() > 0) {
|
||||
$query = $query->fetch();
|
||||
$premDays = (int)$query['premdays'];
|
||||
$lastDay = (int)$query['lastday'];
|
||||
$lastLogin = $lastDay;
|
||||
}
|
||||
else {
|
||||
sendError("Error while fetching your account data. Please contact admin.");
|
||||
}
|
||||
if($premDays != 0 && $premDays != PHP_INT_MAX ) {
|
||||
if($lastDay == 0) {
|
||||
$lastDay = $timeNow;
|
||||
$save = true;
|
||||
} else {
|
||||
$days = (int)(($timeNow - $lastDay) / 86400);
|
||||
if($days > 0) {
|
||||
if($days >= $premDays) {
|
||||
$premDays = 0;
|
||||
$lastDay = 0;
|
||||
} else {
|
||||
$premDays -= $days;
|
||||
$reminder = (int)(($timeNow - $lastDay) % 86400);
|
||||
$lastDay = $timeNow - $reminder;
|
||||
}
|
||||
|
||||
$save = true;
|
||||
}
|
||||
sendError("Error while fetching your account data. Please contact admin.");
|
||||
}
|
||||
if ($premDays != 0 && $premDays != PHP_INT_MAX) {
|
||||
if ($lastDay == 0) {
|
||||
$lastDay = $timeNow;
|
||||
$save = true;
|
||||
} else {
|
||||
$days = (int)(($timeNow - $lastDay) / 86400);
|
||||
if ($days > 0) {
|
||||
if ($days >= $premDays) {
|
||||
$premDays = 0;
|
||||
$lastDay = 0;
|
||||
} else {
|
||||
$premDays -= $days;
|
||||
$reminder = ($timeNow - $lastDay) % 86400;
|
||||
$lastDay = $timeNow - $reminder;
|
||||
}
|
||||
|
||||
$save = true;
|
||||
}
|
||||
}
|
||||
} else if ($lastDay != 0) {
|
||||
$lastDay = 0;
|
||||
$save = true;
|
||||
}
|
||||
if ($save) {
|
||||
$db->query("update `accounts` set `premdays` = " . $premDays . ", `lastday` = " . $lastDay . " where `id` = " . $account->getId());
|
||||
}
|
||||
} else if ($lastDay != 0) {
|
||||
$lastDay = 0;
|
||||
$save = true;
|
||||
}
|
||||
if($save) {
|
||||
$db->query("update `accounts` set `premdays` = " . $premDays . ", `lastday` = " . $lastDay . " where `id` = " . $account->getId());
|
||||
}
|
||||
$premiumAccount = $premDays > 0;
|
||||
$timePremium = time() + ($premDays * 86400);
|
||||
|
||||
$worlds = [$world];
|
||||
$playdata = compact('worlds', 'characters');
|
||||
|
||||
$sessionKey = ($inputEmail !== false) ? $inputEmail : $inputAccountName; // email or account name
|
||||
$sessionKey .= "\n" . $request->password; // password
|
||||
if (!fieldExist('istutorial', 'players')) {
|
||||
$sessionKey .= "\n";
|
||||
}
|
||||
$sessionKey .= ($accountHasSecret && strlen($accountSecret) > 5) ? $inputToken : '';
|
||||
|
||||
// this is workaround to distinguish between TFS 1.x and otservbr
|
||||
// TFS 1.x requires the number in session key
|
||||
// otservbr requires just login and password
|
||||
// so we check for istutorial field which is present in otservbr, and not in TFS
|
||||
if (!fieldExist('istutorial', 'players')) {
|
||||
$sessionKey .= "\n".floor(time() / 30);
|
||||
}
|
||||
|
||||
log_append('slaw.log', $sessionKey);
|
||||
|
||||
$session = [
|
||||
'sessionkey' => "$result->email\n$result->password",
|
||||
'lastlogintime' => (!$account) ? 0 : $account->getLastLogin(),
|
||||
'ispremium' => ($config['lua']['freePremium']) ? true : $account->isPremium(),
|
||||
'sessionkey' => $sessionKey,
|
||||
'lastlogintime' => 0,
|
||||
'ispremium' => $config['lua']['freePremium'] || $account->isPremium(),
|
||||
'premiumuntil' => ($account->getPremDays()) > 0 ? (time() + ($account->getPremDays() * 86400)) : 0,
|
||||
'status' => 'active', // active, frozen or suspended
|
||||
'returnernotification' => false,
|
||||
@@ -195,20 +255,19 @@ switch ($action) {
|
||||
'emailcoderequest' => false
|
||||
];
|
||||
die(json_encode(compact('session', 'playdata')));
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
sendError("Unrecognized event {$action}.");
|
||||
break;
|
||||
}
|
||||
|
||||
function create_char($player) {
|
||||
function create_char($player, $highestLevelId) {
|
||||
global $config;
|
||||
return [
|
||||
'worldid' => 0,
|
||||
'name' => $player['name'],
|
||||
'ismale' => intval($player['sex']) === 1,
|
||||
'tutorial' => (bool)$player['istutorial'],
|
||||
'tutorial' => isset($player['istutorial']) && $player['istutorial'],
|
||||
'level' => intval($player['level']),
|
||||
'vocation' => $config['vocations'][$player['vocation']],
|
||||
'outfitid' => intval($player['looktype']),
|
||||
@@ -217,10 +276,10 @@ function create_char($player) {
|
||||
'legscolor' => intval($player['looklegs']),
|
||||
'detailcolor' => intval($player['lookfeet']),
|
||||
'addonsflags' => intval($player['lookaddons']),
|
||||
'ishidden' => 0,
|
||||
'ishidden' => isset($player['deletion']) && (int)$player['deletion'] === 1,
|
||||
'istournamentparticipant' => false,
|
||||
'ismaincharacter' => true,
|
||||
'dailyrewardstate' => intval($player['isreward']),
|
||||
'ismaincharacter' => $highestLevelId == $player['id'],
|
||||
'dailyrewardstate' => isset($player['isreward']) ? intval($player['isreward']) : 0,
|
||||
'remainingdailytournamentplaytime' => 0
|
||||
];
|
||||
}
|
||||
|
Reference in New Issue
Block a user