From 2fe7787ffd56a758d6a97f4c2f1770557a20eb8f Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 14 May 2019 16:52:01 +0800 Subject: [PATCH] Add geolocation to client sessions --- api/user.php | 2 ++ include/ClientSession.class.php | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/api/user.php b/api/user.php index d3f2f488..52b9c3c9 100644 --- a/api/user.php +++ b/api/user.php @@ -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'); @@ -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'); diff --git a/include/ClientSession.class.php b/include/ClientSession.class.php index 78542c99..f5d78ff6 100644 --- a/include/ClientSession.class.php +++ b/include/ClientSession.class.php @@ -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()); + } + } + }