referralprogram dependency
This commit is contained in:
parent
50ba988609
commit
191a05c55a
|
|
@ -0,0 +1,115 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
Alphaland 2021
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Alphaland\Users {
|
||||||
|
|
||||||
|
use Alphaland\Moderation\UserModerationManager;
|
||||||
|
use Alphaland\Common\HashingUtiltity;
|
||||||
|
use PDO;
|
||||||
|
|
||||||
|
class ReferralProgram
|
||||||
|
{
|
||||||
|
public static function IsMember(int $userid)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
if (isInGroup($userid, 22)) //id 22 is the official referral program group
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
*/
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function IsUserGeneratedKey(string $key)
|
||||||
|
{
|
||||||
|
$check = $GLOBALS['pdo']->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -99,6 +99,7 @@ try
|
||||||
//alphaland specfic dependencies (listing manually for now due to active rewrite of stuff)
|
//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/Activation.php";
|
||||||
include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Users/TwoFactor.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/Moderation/UserModerationManager.php";
|
||||||
include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Common/HashingUtiltity.php";
|
include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Common/HashingUtiltity.php";
|
||||||
include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Web/IpRange.php";
|
include "C:/Webserver/nginx/Alphaland/globals/Dependencies/Web/IpRange.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
|
//filter shit
|
||||||
|
|
||||||
function getWordList()
|
function getWordList()
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Alphaland\Users\Activation;
|
use Alphaland\Users\Activation;
|
||||||
|
use Alphaland\Users\ReferralProgram;
|
||||||
use Alphaland\Web\WebContextManager;
|
use Alphaland\Web\WebContextManager;
|
||||||
|
|
||||||
$body = '';
|
$body = '';
|
||||||
|
|
@ -90,7 +91,7 @@ else
|
||||||
{
|
{
|
||||||
$isUserGen = false;
|
$isUserGen = false;
|
||||||
$isAdminGen = false;
|
$isAdminGen = false;
|
||||||
if (isSignupKeyUserGenerated($signupkey)) //referral system
|
if (ReferralProgram::IsUserGeneratedKey($signupkey)) //referral system
|
||||||
{
|
{
|
||||||
$isUserGen = true;
|
$isUserGen = true;
|
||||||
}
|
}
|
||||||
|
|
@ -119,7 +120,7 @@ else
|
||||||
//referral system
|
//referral system
|
||||||
if ($isUserGen)
|
if ($isUserGen)
|
||||||
{
|
{
|
||||||
handleSignupWithUserKey($userID, $signupkey);
|
ReferralProgram::ConfirmSignup($userID, $signupkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
//first place
|
//first place
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@ Alphaland 2021
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//headers
|
//headers
|
||||||
|
|
||||||
|
use Alphaland\Users\ReferralProgram;
|
||||||
|
|
||||||
header("Access-Control-Allow-Origin: https://www.alphaland.cc");
|
header("Access-Control-Allow-Origin: https://www.alphaland.cc");
|
||||||
header("access-control-allow-credentials: true");
|
header("access-control-allow-credentials: true");
|
||||||
header("Cache-Control: no-cache");
|
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("Last-Modified: " . gmdate("D, d M Y H:i:s T") . " GMT");
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
$key = generateUserSignupKey();
|
$key = ReferralProgram::GenerateUserKey($user->id);
|
||||||
$alert = "";
|
$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;
|
$alert = $key;
|
||||||
$key = "";
|
$key = "";
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ Alphaland 2021
|
||||||
|
|
||||||
//headers
|
//headers
|
||||||
|
|
||||||
|
use Alphaland\Users\ReferralProgram;
|
||||||
use Alphaland\Users\TwoFactor;
|
use Alphaland\Users\TwoFactor;
|
||||||
|
|
||||||
header("Access-Control-Allow-Origin: https://www.alphaland.cc");
|
header("Access-Control-Allow-Origin: https://www.alphaland.cc");
|
||||||
|
|
@ -39,7 +40,7 @@ $userInfo = array (
|
||||||
"verified" => $verified,
|
"verified" => $verified,
|
||||||
"blurb" => $blurb,
|
"blurb" => $blurb,
|
||||||
"twofactorenabled" => TwoFactor::Is2FAInitialized($userid),
|
"twofactorenabled" => TwoFactor::Is2FAInitialized($userid),
|
||||||
"referralprogram" => inReferralProgram($userid),
|
"referralprogram" => ReferralProgram::IsMember($userid),
|
||||||
"joinpref" => $joinpref,
|
"joinpref" => $joinpref,
|
||||||
"tradepref" => $tradepref,
|
"tradepref" => $tradepref,
|
||||||
"theme" => $theme
|
"theme" => $theme
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue