From 191a05c55aa994273a0838c5c49a2c1608e666b2 Mon Sep 17 00:00:00 2001 From: Astrologies Date: Thu, 2 Dec 2021 20:13:31 -0500 Subject: [PATCH] referralprogram dependency --- .../Dependencies/Users/ReferralProgram.php | 115 ++++++++++++++++++ globals/config.php | 1 + globals/functions.php | 102 ---------------- html/register.php | 5 +- html_api/referrals/generateSignupKey.php | 7 +- html_api/settings/index.php | 3 +- 6 files changed, 126 insertions(+), 107 deletions(-) create mode 100644 globals/Dependencies/Users/ReferralProgram.php diff --git a/globals/Dependencies/Users/ReferralProgram.php b/globals/Dependencies/Users/ReferralProgram.php new file mode 100644 index 0000000..2d8e0ba --- /dev/null +++ b/globals/Dependencies/Users/ReferralProgram.php @@ -0,0 +1,115 @@ +prepare("SELECT * FROM user_signup_keys WHERE signupkey = :ke"); + $check->bindParam(":ke", $key, PDO::PARAM_STR); + $check->execute(); + if ($check->rowCount() > 0) { + if (!UserModerationManager::IsBanned($check->fetch(PDO::FETCH_OBJ)->userGen)) { + return true; + } + } + return false; + } + + private static function GenerateKey($len) + { + $hash = ""; + do { + $hash = HashingUtiltity::GenerateByteHash($len); + } while (ReferralProgram::IsUserGeneratedKey($hash)); + return $hash; + } + + public static function DeleteUserKey(string $key) + { + $userkey = $GLOBALS['pdo']->prepare("DELETE FROM user_signup_keys WHERE signupkey = :ke"); + $userkey->bindParam(":ke", $key, PDO::PARAM_STR); + $userkey->execute(); + return $userkey->rowCount() > 0; + } + + public static function UserKeysCount(int $user) + { + $keys = $GLOBALS['pdo']->prepare("SELECT COUNT(*) FROM user_signup_keys WHERE userGen = :u"); + $keys->bindParam(":u", $user, PDO::PARAM_INT); + $keys->execute(); + return $keys->fetchColumn(); + } + + public static function UserKeysLimit(int $user) + { + return userInfo($user)->referralCooldown + 604800 >= time(); + } + + public static function UpdateCooldown(int $user) + { + $updateuser = $GLOBALS['pdo']->prepare('UPDATE users SET referralCooldown = (UNIX_TIMESTAMP() + 604800) WHERE id = :userid'); + $updateuser->bindParam(":userid", $user, PDO::PARAM_INT); + $updateuser->execute(); + } + + public static function GenerateUserKey(int $user) + { + if (ReferralProgram::IsMember($user)) { + if (!ReferralProgram::UserKeysLimit($user)) { + if (ReferralProgram::UserKeysCount($user) >= 1) { + ReferralProgram::UpdateCooldown($user); + } + $newkey = ReferralProgram::GenerateKey(32); + $n = $GLOBALS['pdo']->prepare("INSERT INTO user_signup_keys(userGen,signupkey,whenGenerated) VALUES(:user,:key,UNIX_TIMESTAMP())"); + $n->bindParam(":user", $user, PDO::PARAM_INT); + $n->bindParam(":key", $newkey, PDO::PARAM_STR); + $n->execute(); + return $newkey; + } + return "Maximum keys generated, check back in a week."; + } + return "Error occurred"; + } + + public static function ConfirmSignup(int $newuser, string $key) + { + $userkey = $GLOBALS['pdo']->prepare("SELECT * FROM user_signup_keys WHERE signupkey = :ke"); + $userkey->bindParam(":ke", $key, PDO::PARAM_STR); + $userkey->execute(); + if ($userkey->rowCount() > 0) { + $whoinvited = $userkey->fetch(PDO::FETCH_OBJ)->userGen; + $n = $GLOBALS['pdo']->prepare("INSERT INTO users_invited(invitedUser,whoInvited,whenAccepted) VALUES(:inviteduser,:whoinvited,UNIX_TIMESTAMP())"); + $n->bindParam(":inviteduser", $newuser, PDO::PARAM_INT); + $n->bindParam(":whoinvited", $whoinvited, PDO::PARAM_INT); + $n->execute(); + + if (ReferralProgram::DeleteUserKey($key)){ + return true; + } + } + return false; + } + } +} \ No newline at end of file diff --git a/globals/config.php b/globals/config.php index 53dface..fb4d6c0 100644 --- a/globals/config.php +++ b/globals/config.php @@ -99,6 +99,7 @@ try //alphaland specfic dependencies (listing manually for now due to active rewrite of stuff) include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Users/Activation.php"; include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Users/TwoFactor.php"; + include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Users/ReferralProgram.php"; include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Moderation/UserModerationManager.php"; include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Common/HashingUtiltity.php"; include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Web/IpRange.php"; diff --git a/globals/functions.php b/globals/functions.php index 786ed88..fcf0d78 100644 --- a/globals/functions.php +++ b/globals/functions.php @@ -740,108 +740,6 @@ function inFeatureTesterGroup($user) // ... -//referral program - -function inReferralProgram($user) -{ - /* - if (isInGroup($user, 22)) //id 22 is the official referral program group - { - return true; - } - return false; - */ - return true; -} - -function isSignupKeyUserGenerated($signupkey) -{ - $key = $GLOBALS['pdo']->prepare("SELECT * FROM user_signup_keys WHERE signupkey = :ke"); - $key->bindParam(":ke", $signupkey, PDO::PARAM_STR); - $key->execute(); - if ($key->rowCount() > 0) - { - $banned = UserModerationManager::IsBanned($key->fetch(PDO::FETCH_OBJ)->userGen); - if (!$banned) - { - return true; - } - } - return false; -} - -function deleteUserSignupKey($key) -{ - $userkey = $GLOBALS['pdo']->prepare("DELETE FROM user_signup_keys WHERE signupkey = :ke"); - $userkey->bindParam(":ke", $key, PDO::PARAM_STR); - $userkey->execute(); -} - -function handleSignupWithUserKey($newuser, $key) -{ - $userkey = $GLOBALS['pdo']->prepare("SELECT * FROM user_signup_keys WHERE signupkey = :ke"); - $userkey->bindParam(":ke", $key, PDO::PARAM_STR); - $userkey->execute(); - if ($userkey->rowCount() > 0) - { - $whoinvited = $userkey->fetch(PDO::FETCH_OBJ)->userGen; - - $n = $GLOBALS['pdo']->prepare("INSERT INTO users_invited(invitedUser,whoInvited,whenAccepted) VALUES(:inviteduser,:whoinvited,UNIX_TIMESTAMP())"); - $n->bindParam(":inviteduser", $newuser, PDO::PARAM_INT); - $n->bindParam(":whoinvited", $whoinvited, PDO::PARAM_INT); - $n->execute(); - - deleteUserSignupKey($key); - } -} - -function countUserSignupKeys($user) -{ - $keys = $GLOBALS['pdo']->prepare("SELECT * FROM user_signup_keys WHERE userGen = :u"); - $keys->bindParam(":u", $user, PDO::PARAM_INT); - $keys->execute(); - return $keys->rowCount(); -} - -function userMonthlySignupKeyLimit($user) -{ - $lastGen = 0; - $lastGen = $GLOBALS['pdo']->prepare("SELECT * FROM user_signup_keys WHERE userGen = :u AND (whenGenerated + 1209600) > UNIX_TIMESTAMP() ORDER BY whenGenerated DESC LIMIT 2"); - $lastGen->bindParam(":u", $user, PDO::PARAM_INT); - $lastGen->execute(); - if ($lastGen->rowCount() == 2) - { - return true; - } - return false; -} - -function generateUserSignupKey() -{ - $localuser = $GLOBALS['user']->id; - - if (inReferralProgram($localuser)) - { - //if (countUserSignupKeys($localuser) < 2) //under 2 referrals - //{ - if (!userMonthlySignupKeyLimit($localuser)) //not at the limit this month - { - $newkey = safeSignupKey(32); - $n = $GLOBALS['pdo']->prepare("INSERT INTO user_signup_keys(userGen,signupkey,whenGenerated) VALUES(:user,:key,UNIX_TIMESTAMP())"); - $n->bindParam(":user", $localuser, PDO::PARAM_INT); - $n->bindParam(":key", $newkey, PDO::PARAM_STR); - $n->execute(); - return $newkey; - } - return "Maximum keys generated, check back in two weeks."; - //} - //return "Maximum of two active keys."; - } - return "Error occurred"; -} - -// ... - //filter shit function getWordList() diff --git a/html/register.php b/html/register.php index 06fd6b3..85e82c3 100644 --- a/html/register.php +++ b/html/register.php @@ -6,6 +6,7 @@ */ use Alphaland\Users\Activation; +use Alphaland\Users\ReferralProgram; use Alphaland\Web\WebContextManager; $body = ''; @@ -90,7 +91,7 @@ else { $isUserGen = false; $isAdminGen = false; - if (isSignupKeyUserGenerated($signupkey)) //referral system + if (ReferralProgram::IsUserGeneratedKey($signupkey)) //referral system { $isUserGen = true; } @@ -119,7 +120,7 @@ else //referral system if ($isUserGen) { - handleSignupWithUserKey($userID, $signupkey); + ReferralProgram::ConfirmSignup($userID, $signupkey); } //first place diff --git a/html_api/referrals/generateSignupKey.php b/html_api/referrals/generateSignupKey.php index 04d4929..f312638 100644 --- a/html_api/referrals/generateSignupKey.php +++ b/html_api/referrals/generateSignupKey.php @@ -6,6 +6,9 @@ Alphaland 2021 */ //headers + +use Alphaland\Users\ReferralProgram; + header("Access-Control-Allow-Origin: https://www.alphaland.cc"); header("access-control-allow-credentials: true"); header("Cache-Control: no-cache"); @@ -14,9 +17,9 @@ header("Expires: -1"); header("Last-Modified: " . gmdate("D, d M Y H:i:s T") . " GMT"); header('Content-Type: application/json'); -$key = generateUserSignupKey(); +$key = ReferralProgram::GenerateUserKey($user->id); $alert = ""; -if ($key == "Error occurred" || $key == "Maximum keys generated, check back in two weeks." || $key == "Maximum of two active keys.") //ghetto as well +if ($key == "Error occurred" || $key == "Maximum keys generated, check back in a week." || $key == "Maximum of two active keys.") //ghetto as well { $alert = $key; $key = ""; diff --git a/html_api/settings/index.php b/html_api/settings/index.php index e2a39a6..fddd1ff 100644 --- a/html_api/settings/index.php +++ b/html_api/settings/index.php @@ -7,6 +7,7 @@ Alphaland 2021 //headers +use Alphaland\Users\ReferralProgram; use Alphaland\Users\TwoFactor; header("Access-Control-Allow-Origin: https://www.alphaland.cc"); @@ -39,7 +40,7 @@ $userInfo = array ( "verified" => $verified, "blurb" => $blurb, "twofactorenabled" => TwoFactor::Is2FAInitialized($userid), - "referralprogram" => inReferralProgram($userid), + "referralprogram" => ReferralProgram::IsMember($userid), "joinpref" => $joinpref, "tradepref" => $tradepref, "theme" => $theme