referral program update
This commit is contained in:
parent
7194aeb391
commit
fd100eec75
|
|
@ -54,6 +54,13 @@ namespace Alphaland\Users {
|
||||||
return $userkey->rowCount() > 0;
|
return $userkey->rowCount() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function DeleteAllKeys(int $user)
|
||||||
|
{
|
||||||
|
$keys = $GLOBALS['pdo']->prepare("DELETE FROM user_signup_keys WHERE userGen = :u");
|
||||||
|
$keys->bindParam(":u", $user, PDO::PARAM_INT);
|
||||||
|
$keys->execute();
|
||||||
|
}
|
||||||
|
|
||||||
public static function UserKeysCount(int $user)
|
public static function UserKeysCount(int $user)
|
||||||
{
|
{
|
||||||
$keys = $GLOBALS['pdo']->prepare("SELECT COUNT(*) FROM user_signup_keys WHERE userGen = :u");
|
$keys = $GLOBALS['pdo']->prepare("SELECT COUNT(*) FROM user_signup_keys WHERE userGen = :u");
|
||||||
|
|
@ -62,35 +69,52 @@ namespace Alphaland\Users {
|
||||||
return $keys->fetchColumn();
|
return $keys->fetchColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function UserKeysLimit(int $user)
|
public static function NextRenewal(int $user)
|
||||||
{
|
{
|
||||||
return userInfo($user)->referralCooldown + 604800 >= time();
|
return userInfo($user)->referralNextRenewal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function UpdateCooldown(int $user)
|
public static function IsRenewable(int $user)
|
||||||
{
|
{
|
||||||
$updateuser = $GLOBALS['pdo']->prepare('UPDATE users SET referralCooldown = (UNIX_TIMESTAMP() + 604800) WHERE id = :userid');
|
return time() >= ReferralProgram::NextRenewal($user); //returns true if the current timestamp is greater or equal than the scheduled renewal
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function UpdateNextRenewal(int $user)
|
||||||
|
{
|
||||||
|
$updateuser = $GLOBALS['pdo']->prepare('UPDATE users SET referralNextRenewal = (UNIX_TIMESTAMP() + 604800) WHERE id = :userid');
|
||||||
$updateuser->bindParam(":userid", $user, PDO::PARAM_INT);
|
$updateuser->bindParam(":userid", $user, PDO::PARAM_INT);
|
||||||
$updateuser->execute();
|
$updateuser->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function GenerateUserKey(int $user)
|
public static function CreateKey(int $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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function CheckUserKeys(int $user)
|
||||||
{
|
{
|
||||||
if (ReferralProgram::IsMember($user)) {
|
if (ReferralProgram::IsMember($user)) {
|
||||||
if (!ReferralProgram::UserKeysLimit($user)) {
|
if (ReferralProgram::IsRenewable($user))
|
||||||
if (ReferralProgram::UserKeysCount($user) >= 1) {
|
{
|
||||||
ReferralProgram::UpdateCooldown($user);
|
//step 1, update the next renewal time
|
||||||
}
|
ReferralProgram::UpdateNextRenewal($user);
|
||||||
$newkey = ReferralProgram::GenerateKey(32);
|
|
||||||
$n = $GLOBALS['pdo']->prepare("INSERT INTO user_signup_keys(userGen,signupkey,whenGenerated) VALUES(:user,:key,UNIX_TIMESTAMP())");
|
//step 2, delete all the current keys
|
||||||
$n->bindParam(":user", $user, PDO::PARAM_INT);
|
ReferralProgram::DeleteAllKeys($user);
|
||||||
$n->bindParam(":key", $newkey, PDO::PARAM_STR);
|
|
||||||
$n->execute();
|
//step 3, generate two keys
|
||||||
return $newkey;
|
ReferralProgram::CreateKey($user);
|
||||||
|
ReferralProgram::CreateKey($user);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return "Maximum keys generated, check back in a week.";
|
|
||||||
}
|
}
|
||||||
return "Error occurred";
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function ConfirmSignup(int $newuser, string $key)
|
public static function ConfirmSignup(int $newuser, string $key)
|
||||||
|
|
|
||||||
|
|
@ -171,28 +171,16 @@ $body = <<<EOT
|
||||||
</div>
|
</div>
|
||||||
<div class="tab-pane fade" id="v-pills-referral" role="tabpanel" aria-labelledby="v-pills-referral-tab">
|
<div class="tab-pane fade" id="v-pills-referral" role="tabpanel" aria-labelledby="v-pills-referral-tab">
|
||||||
<h5>Referral Program</h5>
|
<h5>Referral Program</h5>
|
||||||
<h6>Maximum of two referral codes every 2 weeks.</h6>
|
<h6 id="next_ref_regen"></h6>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<div class="row">
|
|
||||||
<div class="col-sm">
|
|
||||||
<div class="input-group">
|
|
||||||
<input type="text" id="generatedkey" class="form-control" autocomplete="off" readonly>
|
|
||||||
<div class="input-group-append">
|
|
||||||
<button type="button" onclick="generateKey()" class="btn btn-success" type="button">Generate</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<hr>
|
|
||||||
<p>
|
<p>
|
||||||
<button class="btn btn-danger w-50" type="button" data-toggle="collapse" data-target="#signupkeyslist" aria-expanded="false" aria-controls="signupkeyslist" onclick="activeKeys()">Active Keys</button>
|
<button class="btn btn-danger w-50" type="button" data-toggle="collapse" data-target="#signupkeyslist" aria-expanded="false" aria-controls="signupkeyslist" onclick="activeKeys()">Show Keys</button>
|
||||||
</p>
|
</p>
|
||||||
<div class="collapse" id="signupkeyslist">
|
<div class="collapse" id="signupkeyslist">
|
||||||
<table class="table atable-dark">
|
<table class="table atable-dark">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Date Generated</th>
|
|
||||||
<th>Signup Key</th>
|
<th>Signup Key</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
@ -316,6 +304,7 @@ function initializeSettings()
|
||||||
$("#settings_username").html(object.username);
|
$("#settings_username").html(object.username);
|
||||||
$("#settings_email").html(object.email);
|
$("#settings_email").html(object.email);
|
||||||
$("#settings_blurb").html(object.blurb);
|
$("#settings_blurb").html(object.blurb);
|
||||||
|
$("#next_ref_regen").html("New keys available starting: <b>" + object.referralkeyrefresh + "</b>");
|
||||||
$('#settings_theme').val(object.theme);
|
$('#settings_theme').val(object.theme);
|
||||||
$('#settings_joinpref').val(object.joinpref);
|
$('#settings_joinpref').val(object.joinpref);
|
||||||
|
|
||||||
|
|
@ -366,8 +355,8 @@ function generateKey()
|
||||||
}
|
}
|
||||||
function activeKeys()
|
function activeKeys()
|
||||||
{
|
{
|
||||||
|
initializeSettings();
|
||||||
var html = '<tr>';
|
var html = '<tr>';
|
||||||
html += '<td>{whenGenerated}</td>';
|
|
||||||
html += '<td>{key}</td>';
|
html += '<td>{key}</td>';
|
||||||
html += '</tr>';
|
html += '</tr>';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,6 +17,8 @@ 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');
|
||||||
|
|
||||||
|
ReferralProgram::CheckUserKeys($GLOBALS['user']->id);
|
||||||
|
|
||||||
$b = $pdo->prepare("SELECT * FROM user_signup_keys WHERE userGen = :userid");
|
$b = $pdo->prepare("SELECT * FROM user_signup_keys WHERE userGen = :userid");
|
||||||
$b->bindParam(":userid", $GLOBALS['user']->id, PDO::PARAM_INT);
|
$b->bindParam(":userid", $GLOBALS['user']->id, PDO::PARAM_INT);
|
||||||
$b->execute();
|
$b->execute();
|
||||||
|
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
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");
|
|
||||||
header("Pragma: no-cache");
|
|
||||||
header("Expires: -1");
|
|
||||||
header("Last-Modified: " . gmdate("D, d M Y H:i:s T") . " GMT");
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
|
|
||||||
$key = ReferralProgram::GenerateUserKey($user->id);
|
|
||||||
$alert = "";
|
|
||||||
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 = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
$generatedKey = array(
|
|
||||||
"alert" => $alert,
|
|
||||||
"key" => $key
|
|
||||||
);
|
|
||||||
|
|
||||||
die(json_encode($generatedKey));
|
|
||||||
|
|
@ -14,7 +14,7 @@ header("Access-Control-Allow-Origin: https://www.alphaland.cc");
|
||||||
header("access-control-allow-credentials: true");
|
header("access-control-allow-credentials: true");
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
$userid = $user->id;
|
$userid = $GLOBALS['user']->id;
|
||||||
|
|
||||||
//user info
|
//user info
|
||||||
$userquery = $pdo->prepare('SELECT * FROM `users` WHERE id = :uid');
|
$userquery = $pdo->prepare('SELECT * FROM `users` WHERE id = :uid');
|
||||||
|
|
@ -32,6 +32,7 @@ $theme = $userquery->theme;
|
||||||
|
|
||||||
//initialize 2FA in the database if it hasnt been already
|
//initialize 2FA in the database if it hasnt been already
|
||||||
TwoFactor::Initialize2FA($userid);
|
TwoFactor::Initialize2FA($userid);
|
||||||
|
ReferralProgram::CheckUserKeys($userid);
|
||||||
|
|
||||||
$userInfo = array (
|
$userInfo = array (
|
||||||
"userid" => $userid,
|
"userid" => $userid,
|
||||||
|
|
@ -41,6 +42,7 @@ $userInfo = array (
|
||||||
"blurb" => $blurb,
|
"blurb" => $blurb,
|
||||||
"twofactorenabled" => TwoFactor::Is2FAInitialized($userid),
|
"twofactorenabled" => TwoFactor::Is2FAInitialized($userid),
|
||||||
"referralprogram" => ReferralProgram::IsMember($userid),
|
"referralprogram" => ReferralProgram::IsMember($userid),
|
||||||
|
"referralkeyrefresh" => date("m/d/Y", ReferralProgram::NextRenewal($userid)),
|
||||||
"joinpref" => $joinpref,
|
"joinpref" => $joinpref,
|
||||||
"tradepref" => $tradepref,
|
"tradepref" => $tradepref,
|
||||||
"theme" => $theme
|
"theme" => $theme
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue