From 442415f97ca0317a870714cb1d208954dac155dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Tue, 22 Mar 2022 03:05:08 +0100 Subject: [PATCH] GH-150 Simplify code by short circuiting --- reg_ajax.php | 238 ++++++++++++++++++++++++++------------------------- 1 file changed, 120 insertions(+), 118 deletions(-) diff --git a/reg_ajax.php b/reg_ajax.php index 360c7243..0d3a6283 100644 --- a/reg_ajax.php +++ b/reg_ajax.php @@ -119,147 +119,149 @@ function handleRegistration() { } } - if (empty($JSONResponse['Errors'])) { - unset($JSONResponse['Errors']); - - $newPlanetCoordinates = Registration\Utils\Galaxy\findNewPlanetPosition([ - 'preferredGalaxy' => $normalizedInput['galaxyNo'] - ]); - - if ($newPlanetCoordinates !== null) { - $passwordHash = Session\Utils\LocalIdentityV1\hashPassword([ - 'password' => $normalizedInput['password'], - ]); - - $insertNewUserResult = Registration\Utils\Queries\insertNewUser([ - 'username' => $normalizedInput['username'], - 'passwordHash' => $passwordHash, - 'langCode' => $normalizedInput['langCode'], - 'email' => $normalizedInput['email']['escaped'], - 'registrationIP' => $userSessionIP, - 'currentTimestamp' => $Now, - ]); - $UserID = $insertNewUserResult['userId']; - - // Create a Planet for User - include($_EnginePath.'includes/functions/CreateOnePlanetRecord.php'); - - $PlanetID = CreateOnePlanetRecord( - $newPlanetCoordinates['galaxy'], - $newPlanetCoordinates['system'], - $newPlanetCoordinates['planet'], - $UserID, - $_Lang['MotherPlanet'], - true - ); - - Registration\Utils\Queries\incrementUsersCounterInGameConfig(); - Registration\Utils\Queries\updateAllMailChanges([ - 'email' => $normalizedInput['email']['escaped'] - ]); + if (!empty($JSONResponse['Errors'])) { + die('regCallback('.json_encode($JSONResponse).');'); + } - $referrerUserId = Registration\Utils\General\getRegistrationReferrerId(); + unset($JSONResponse['Errors']); - if ($referrerUserId !== null) { - $registrationIPs = [ - 'r' => trim($userSessionIP), - 'p' => trim(Users\Session\getCurrentOriginatingIP()) - ]; + $newPlanetCoordinates = Registration\Utils\Galaxy\findNewPlanetPosition([ + 'preferredGalaxy' => $normalizedInput['galaxyNo'] + ]); - if (empty($registrationIPs['p'])) { - unset($registrationIPs['p']); - } + if ($newPlanetCoordinates !== null) { + $passwordHash = Session\Utils\LocalIdentityV1\hashPassword([ + 'password' => $normalizedInput['password'], + ]); - $existingMatchingEnterLogIds = Registration\Utils\Queries\findEnterLogIPsWithMatchingIPValue([ - 'ips' => $registrationIPs, - ]); + $insertNewUserResult = Registration\Utils\Queries\insertNewUser([ + 'username' => $normalizedInput['username'], + 'passwordHash' => $passwordHash, + 'langCode' => $normalizedInput['langCode'], + 'email' => $normalizedInput['email']['escaped'], + 'registrationIP' => $userSessionIP, + 'currentTimestamp' => $Now, + ]); + $UserID = $insertNewUserResult['userId']; + + // Create a Planet for User + include($_EnginePath.'includes/functions/CreateOnePlanetRecord.php'); + + $PlanetID = CreateOnePlanetRecord( + $newPlanetCoordinates['galaxy'], + $newPlanetCoordinates['system'], + $newPlanetCoordinates['planet'], + $UserID, + $_Lang['MotherPlanet'], + true + ); + + Registration\Utils\Queries\incrementUsersCounterInGameConfig(); + Registration\Utils\Queries\updateAllMailChanges([ + 'email' => $normalizedInput['email']['escaped'] + ]); - Registration\Utils\Queries\insertReferralsTableEntry([ - 'referrerUserId' => $referrerUserId, - 'referredUserId' => $UserID, - 'timestamp' => $Now, - 'registrationIPs' => $registrationIPs, - 'existingMatchingEnterLogIds' => $existingMatchingEnterLogIds, - ]); + $referrerUserId = Registration\Utils\General\getRegistrationReferrerId(); - $Message = false; - $Message['msg_id'] = '038'; - $Message['args'] = array(''); - $Message = json_encode($Message); + if ($referrerUserId !== null) { + $registrationIPs = [ + 'r' => trim($userSessionIP), + 'p' => trim(Users\Session\getCurrentOriginatingIP()) + ]; - SendSimpleMessage($referrerUserId, 0, $Now, 70, '007', '016', $Message); + if (empty($registrationIPs['p'])) { + unset($registrationIPs['p']); } - $ActivationCode = md5(mt_rand(0, 99999999999)); + $existingMatchingEnterLogIds = Registration\Utils\Queries\findEnterLogIPsWithMatchingIPValue([ + 'ips' => $registrationIPs, + ]); - // Update User with new data - Registration\Utils\Queries\updateUserFinalDetails([ - 'userId' => $UserID, - 'motherPlanetId' => $PlanetID, - 'motherPlanetGalaxy' => $newPlanetCoordinates['galaxy'], - 'motherPlanetSystem' => $newPlanetCoordinates['system'], - 'motherPlanetPlanetPos' => $newPlanetCoordinates['planet'], - 'referrerId' => $referrerUserId, - 'activationCode' => ( - REGISTER_REQUIRE_EMAILCONFIRM ? - $ActivationCode : - null - ) + Registration\Utils\Queries\insertReferralsTableEntry([ + 'referrerUserId' => $referrerUserId, + 'referredUserId' => $UserID, + 'timestamp' => $Now, + 'registrationIPs' => $registrationIPs, + 'existingMatchingEnterLogIds' => $existingMatchingEnterLogIds, ]); - // Send a invitation private msg $Message = false; - $Message['msg_id'] = '022'; + $Message['msg_id'] = '038'; $Message['args'] = array(''); $Message = json_encode($Message); - SendSimpleMessage($UserID, 0, $Now, 70, '004', '009', $Message); + SendSimpleMessage($referrerUserId, 0, $Now, 70, '007', '016', $Message); + } - if (REGISTER_REQUIRE_EMAILCONFIRM) { - include($_EnginePath.'includes/functions/SendMail.php'); + $ActivationCode = md5(mt_rand(0, 99999999999)); + + // Update User with new data + Registration\Utils\Queries\updateUserFinalDetails([ + 'userId' => $UserID, + 'motherPlanetId' => $PlanetID, + 'motherPlanetGalaxy' => $newPlanetCoordinates['galaxy'], + 'motherPlanetSystem' => $newPlanetCoordinates['system'], + 'motherPlanetPlanetPos' => $newPlanetCoordinates['planet'], + 'referrerId' => $referrerUserId, + 'activationCode' => ( + REGISTER_REQUIRE_EMAILCONFIRM ? + $ActivationCode : + null + ) + ]); - $mailContent = Registration\Components\RegistrationConfirmationMail\render([ - 'userId' => $UserID, - 'login' => $normalizedInput['username'], - 'password' => $normalizedInput['password'], - 'gameName' => $_GameConfig['game_name'], - 'universe' => $_Lang['RegMail_UniName'], - 'activationCode' => $ActivationCode, - ])['componentHTML']; + // Send a invitation private msg + $Message = false; + $Message['msg_id'] = '022'; + $Message['args'] = array(''); + $Message = json_encode($Message); - $mailTitle = parsetemplate( - $_Lang['mail_title'], - [ - 'gameName' => $_GameConfig['game_name'] - ] - ); + SendSimpleMessage($UserID, 0, $Now, 70, '004', '009', $Message); - SendMail($normalizedInput['email']['escaped'], $mailTitle, $mailContent); - } + if (REGISTER_REQUIRE_EMAILCONFIRM) { + include($_EnginePath.'includes/functions/SendMail.php'); - if (isGameStartTimeReached($Now)) { - $sessionTokenValue = Session\Utils\Cookie\packSessionCookie([ - 'userId' => $UserID, - 'username' => $normalizedInput['username'], - 'obscuredPasswordHash' => Session\Utils\Cookie\createCookiePasswordHash([ - 'passwordHash' => $passwordHash, - ]), - 'isRememberMeActive' => 0, - ]); - - $JSONResponse['Code'] = 1; - $JSONResponse['Cookie'][] = [ - 'Name' => getSessionCookieKey(), - 'Value' => $sessionTokenValue - ]; - $JSONResponse['Redirect'] = GAMEURL_UNISTRICT.'/overview.php'; - } else { - $JSONResponse['Code'] = 2; - } + $mailContent = Registration\Components\RegistrationConfirmationMail\render([ + 'userId' => $UserID, + 'login' => $normalizedInput['username'], + 'password' => $normalizedInput['password'], + 'gameName' => $_GameConfig['game_name'], + 'universe' => $_Lang['RegMail_UniName'], + 'activationCode' => $ActivationCode, + ])['componentHTML']; + + $mailTitle = parsetemplate( + $_Lang['mail_title'], + [ + 'gameName' => $_GameConfig['game_name'] + ] + ); + + SendMail($normalizedInput['email']['escaped'], $mailTitle, $mailContent); + } + + if (isGameStartTimeReached($Now)) { + $sessionTokenValue = Session\Utils\Cookie\packSessionCookie([ + 'userId' => $UserID, + 'username' => $normalizedInput['username'], + 'obscuredPasswordHash' => Session\Utils\Cookie\createCookiePasswordHash([ + 'passwordHash' => $passwordHash, + ]), + 'isRememberMeActive' => 0, + ]); + + $JSONResponse['Code'] = 1; + $JSONResponse['Cookie'][] = [ + 'Name' => getSessionCookieKey(), + 'Value' => $sessionTokenValue + ]; + $JSONResponse['Redirect'] = GAMEURL_UNISTRICT.'/overview.php'; } else { - $JSONResponse['Errors'][] = 15; - $JSONResponse['BadFields'][] = 'email'; + $JSONResponse['Code'] = 2; } + } else { + $JSONResponse['Errors'][] = 15; + $JSONResponse['BadFields'][] = 'email'; } die('regCallback('.json_encode($JSONResponse).');');