Skip to content

Commit

Permalink
Add geolocation to client sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed May 14, 2019
1 parent 1ba3ef1 commit 2fe7787
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
$session = ClientSession::create($username, $password, $save_session === "true");
// Clear previous joined server if any
$session->clearUserJoinedServer();
$session->updateUserGeolocation();
$achievements_string = $session->getAchievements();

$output->startElement('connect');
Expand Down Expand Up @@ -87,6 +88,7 @@
// Clear previous joined server if any
$session->clearUserJoinedServer();
$session->setOnline();
$session->updateUserGeolocation();
User::updateLoginTime($session->getUser()->getId());

$output->startElement('saved-session');
Expand Down
35 changes: 35 additions & 0 deletions include/ClientSession.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,4 +854,39 @@ public function updateServerConfig($ip, int $port, int $new_difficulty, int $new
throw new ServerException(_h("Failed to update server config."));
}
}
/**
* Update the geolocation of user based on his IP, called when the user login.
*/
public function updateUserGeolocation()
{
$user_geolocation = Util::getIPGeolocationFromString(Util::getClientIp());
try
{
DBConnection::get()->query(
"UPDATE `{DB_VERSION}_client_sessions`
SET `latitude` = :latitude, `longitude` = :longitude, `country_code` = :country_code
WHERE `uid`= :uid AND `cid` = :cid",
DBConnection::NOTHING,
[
':latitude' => $user_geolocation[0],
':longitude' => $user_geolocation[1],
':country_code' => $user_geolocation[2],
':uid' => $this->user->getId(),
':cid' => $this->getSessionID()
],
[
':latitude' => DBConnection::PARAM_STR,
':longitude' => DBConnection::PARAM_STR,
':country_code' => DBConnection::PARAM_STR,
':uid' => DBConnection::PARAM_STR,
':cid' => DBConnection::PARAM_STR
]
);
}
catch (DBException $e)
{
throw new ClientSessionException($e->getMessage());
}
}

}

0 comments on commit 2fe7787

Please sign in to comment.