From cc63bb53e654f7db7c761fd3efe660324506739b Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Mon, 25 Jul 2022 03:31:19 +0800 Subject: [PATCH] record mybouns page bonus consume log --- app/Models/BonusLogs.php | 14 ++++++++ app/Models/User.php | 2 +- app/Repositories/BonusRepository.php | 21 ++++++++---- include/constants.php | 2 +- public/mybonus.php | 48 ++++++++++++++++------------ 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/app/Models/BonusLogs.php b/app/Models/BonusLogs.php index bd6df7f1..96d5bb71 100644 --- a/app/Models/BonusLogs.php +++ b/app/Models/BonusLogs.php @@ -17,6 +17,13 @@ class BonusLogs extends NexusModel const BUSINESS_TYPE_BUY_ATTENDANCE_CARD = 3; const BUSINESS_TYPE_STICKY_PROMOTION = 4; const BUSINESS_TYPE_POST_REWARD = 5; + const BUSINESS_TYPE_EXCHANGE_UPLOAD = 6; + const BUSINESS_TYPE_EXCHANGE_INVITE = 7; + const BUSINESS_TYPE_CUSTOM_TITLE = 8; + const BUSINESS_TYPE_BUY_VIP = 9; + const BUSINESS_TYPE_GIFT_TO_SOMEONE = 10; + const BUSINESS_TYPE_NO_AD = 11; + const BUSINESS_TYPE_GIFT_TO_LOW_SHARE_RATIO = 12; public static array $businessTypes = [ self::BUSINESS_TYPE_CANCEL_HIT_AND_RUN => ['text' => 'Cancel H&R'], @@ -24,6 +31,13 @@ class BonusLogs extends NexusModel self::BUSINESS_TYPE_BUY_ATTENDANCE_CARD => ['text' => 'Buy attendance card'], self::BUSINESS_TYPE_STICKY_PROMOTION => ['text' => 'Buy torrent sticky promotion'], self::BUSINESS_TYPE_POST_REWARD => ['text' => 'Reward post'], + self::BUSINESS_TYPE_EXCHANGE_UPLOAD => ['text' => 'Exchange upload'], + self::BUSINESS_TYPE_EXCHANGE_INVITE => ['text' => 'Exchange invite'], + self::BUSINESS_TYPE_CUSTOM_TITLE => ['text' => 'Custom title'], + self::BUSINESS_TYPE_BUY_VIP => ['text' => 'Buy VIP'], + self::BUSINESS_TYPE_GIFT_TO_SOMEONE => ['text' => 'Gift to someone'], + self::BUSINESS_TYPE_NO_AD => ['text' => 'No ad'], + self::BUSINESS_TYPE_GIFT_TO_LOW_SHARE_RATIO => ['text' => 'Gift to low share ratio'], ]; public static function getBonusForCancelHitAndRun() diff --git a/app/Models/User.php b/app/Models/User.php index 609ba094..da80cdfa 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -146,7 +146,7 @@ class User extends Authenticatable implements FilamentUser, HasName protected $fillable = [ 'username', 'email', 'passhash', 'secret', 'stylesheet', 'editsecret', 'added', 'modcomment', 'enabled', 'status', 'leechwarn', 'leechwarnuntil', 'page', 'class', 'uploaded', 'downloaded', 'clientselect', 'showclienterror', 'last_home', - 'seedbonus', 'bonuscomment', 'downloadpos' + 'seedbonus', 'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', ]; /** diff --git a/app/Repositories/BonusRepository.php b/app/Repositories/BonusRepository.php index eb18b510..c00a6b7d 100644 --- a/app/Repositories/BonusRepository.php +++ b/app/Repositories/BonusRepository.php @@ -95,28 +95,35 @@ class BonusRepository extends BaseRepository } - public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logComment = '') + public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logComment = '', array $userUpdates = []) { + if (!isset(BonusLogs::$businessTypes[$logBusinessType])) { + throw new \InvalidArgumentException("Invalid logBusinessType: $logBusinessType"); + } + if (isset($userUpdates['seedbonus']) || isset($userUpdates['bonuscomment'])) { + throw new \InvalidArgumentException("Not support update seedbonus or bonuscomment"); + } if ($requireBonus <= 0) { return; } + $user = $this->getUser($user); if ($user->seedbonus < $requireBonus) { do_log("user: {$user->id}, bonus: {$user->seedbonus} < requireBonus: $requireBonus", 'error'); throw new \LogicException("User bonus point not enough."); } - NexusDB::transaction(function () use ($user, $requireBonus, $logBusinessType, $logComment) { + NexusDB::transaction(function () use ($user, $requireBonus, $logBusinessType, $logComment, $userUpdates) { + $logComment = addslashes($logComment); $bonusComment = date('Y-m-d') . " - $logComment"; $oldUserBonus = $user->seedbonus; $newUserBonus = bcsub($oldUserBonus, $requireBonus); $log = "user: {$user->id}, requireBonus: $requireBonus, oldUserBonus: $oldUserBonus, newUserBonus: $newUserBonus, logBusinessType: $logBusinessType, logComment: $logComment"; do_log($log); + $userUpdates['seedbonus'] = $newUserBonus; + $userUpdates['bonuscomment'] = NexusDB::raw("if(bonuscomment = '', '$bonusComment', concat_ws('\n', '$bonusComment', bonuscomment))"); $affectedRows = NexusDB::table($user->getTable()) ->where('id', $user->id) ->where('seedbonus', $oldUserBonus) - ->update([ - 'seedbonus' => $newUserBonus, - 'bonuscomment' => NexusDB::raw("if(bonuscomment = '', '$bonusComment', concat_ws('\n', '$bonusComment', bonuscomment))") - ]); + ->update($userUpdates); if ($affectedRows != 1) { do_log("update user seedbonus affected rows != 1, query: " . last_query(), 'error'); throw new \RuntimeException("Update user seedbonus fail."); @@ -127,7 +134,7 @@ class BonusRepository extends BaseRepository 'old_total_value' => $oldUserBonus, 'value' => $requireBonus, 'new_total_value' => $newUserBonus, - 'comment' => $logComment, + 'comment' => sprintf('[%s] %s', BonusLogs::$businessTypes[$logBusinessType]['text'], $logComment), ]; BonusLogs::query()->insert($bonusLog); do_log("bonusLog: " . nexus_json_encode($bonusLog)); diff --git a/include/constants.php b/include/constants.php index 450e74cd..f75f578d 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ = $points) { + $bonusRep = new \App\Repositories\BonusRepository(); //=== trade for upload if($art == "traffic") { if ($CURUSER['uploaded'] > $dlamountlimit_bonus * 1073741824)//uploaded amount reach limit @@ -494,8 +495,9 @@ if ($action == "exchange") { else { $upload = $CURUSER['uploaded']; $up = $upload + $bonusarray['menge']; - $bonuscomment = date("Y-m-d") . " - " .$points. " Points for upload bonus.\n " .$bonuscomment; - sql_query("UPDATE users SET uploaded = ".sqlesc($up).", seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); +// $bonuscomment = date("Y-m-d") . " - " .$points. " Points for upload bonus.\n " .$bonuscomment; +// sql_query("UPDATE users SET uploaded = ".sqlesc($up).", seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); + $bonusRep->consumeUserBonus($CURUSER['id'], $points, \App\Models\BonusLogs::BUSINESS_TYPE_EXCHANGE_UPLOAD, $points. " Points for upload bonus.", ['uploaded' => $up]); nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=upload"); } } @@ -507,9 +509,10 @@ if ($action == "exchange") { die; } $vip_until = date("Y-m-d H:i:s",(strtotime(date("Y-m-d H:i:s")) + 28*86400)); - $bonuscomment = date("Y-m-d") . " - " .$points. " Points for 1 month VIP Status.\n " .htmlspecialchars($bonuscomment); - sql_query("UPDATE users SET class = '".UC_VIP."', vip_added = 'yes', vip_until = ".sqlesc($vip_until).", seedbonus = seedbonus - $points, bonuscomment=".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); - nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=vip"); +// $bonuscomment = date("Y-m-d") . " - " .$points. " Points for 1 month VIP Status.\n " .htmlspecialchars($bonuscomment); +// sql_query("UPDATE users SET class = '".UC_VIP."', vip_added = 'yes', vip_until = ".sqlesc($vip_until).", seedbonus = seedbonus - $points, bonuscomment=".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); + $bonusRep->consumeUserBonus($CURUSER['id'], $points, \App\Models\BonusLogs::BUSINESS_TYPE_BUY_VIP, $points. " Points for 1 month VIP Status.", ['class' => UC_VIP, 'vip_added' => 'yes', 'vip_until' => $vip_until]); + nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=vip"); } //=== trade for invites elseif($art == "invite") { @@ -517,9 +520,10 @@ if ($action == "exchange") { die(get_user_class_name($buyinvite_class,false,false,true).$lang_mybonus['text_plus_only']); $invites = $CURUSER['invites']; $inv = $invites+$bonusarray['menge']; - $bonuscomment = date("Y-m-d") . " - " .$points. " Points for invites.\n " .htmlspecialchars($bonuscomment); - sql_query("UPDATE users SET invites = ".sqlesc($inv).", seedbonus = seedbonus - $points, bonuscomment=".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); - nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=invite"); +// $bonuscomment = date("Y-m-d") . " - " .$points. " Points for invites.\n " .htmlspecialchars($bonuscomment); +// sql_query("UPDATE users SET invites = ".sqlesc($inv).", seedbonus = seedbonus - $points, bonuscomment=".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); + $bonusRep->consumeUserBonus($CURUSER['id'], $points, \App\Models\BonusLogs::BUSINESS_TYPE_EXCHANGE_INVITE, $points. " Points for invites.", ['invites' => $inv, ]); + nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=invite"); } //=== trade for special title /**** the $words array are words that you DO NOT want the user to have... use to filter "bad words" & user class... @@ -528,21 +532,22 @@ if ($action == "exchange") { elseif($art == "title") { //===custom title $title = $_POST["title"]; - $title = sqlesc($title); $words = array("fuck", "shit", "pussy", "cunt", "nigger", "Staff Leader","SysOp", "Administrator","Moderator","Uploader","Retiree","VIP","Nexus Master","Ultimate User","Extreme User","Veteran User","Insane User","Crazy User","Elite User","Power User","User","Peasant","Champion"); $title = str_replace($words, $lang_mybonus['text_wasted_karma'], $title); - $bonuscomment = date("Y-m-d") . " - " .$points. " Points for custom title. Old title is ".htmlspecialchars(trim($CURUSER["title"]))." and new title is $title\n " .htmlspecialchars($bonuscomment); - sql_query("UPDATE users SET title = $title, seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); - nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=title"); +// $bonuscomment = date("Y-m-d") . " - " .$points. " Points for custom title. Old title is ".htmlspecialchars(trim($CURUSER["title"]))." and new title is $title\n " .htmlspecialchars($bonuscomment); +// sql_query("UPDATE users SET title = ".sqlesc($title).", seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); + $bonusRep->consumeUserBonus($CURUSER['id'], $points, \App\Models\BonusLogs::BUSINESS_TYPE_CUSTOM_TITLE, $points. " Points for custom title. Old title is ".htmlspecialchars(trim($CURUSER["title"]))." and new title is $title.", ['title' => $title, ]); + nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=title"); } elseif($art == "noad" && $enablead_advertisement == 'yes' && $enablebonusnoad_advertisement == 'yes') { if (($enablenoad_advertisement == 'yes' && get_user_class() >= $noad_advertisement) || strtotime($CURUSER['noaduntil']) >= TIMENOW || get_user_class() < $bonusnoad_advertisement) die($lang_mybonus['text_cheat_alert']); else{ $noaduntil = date("Y-m-d H:i:s",(TIMENOW + $bonusarray['menge'])); - $bonuscomment = date("Y-m-d") . " - " .$points. " Points for ".$bonusnoadtime_advertisement." days without ads.\n " .htmlspecialchars($bonuscomment); - sql_query("UPDATE users SET noad='yes', noaduntil='".$noaduntil."', seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id=".sqlesc($userid)); - nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=noad"); +// $bonuscomment = date("Y-m-d") . " - " .$points. " Points for ".$bonusnoadtime_advertisement." days without ads.\n " .htmlspecialchars($bonuscomment); +// sql_query("UPDATE users SET noad='yes', noaduntil='".$noaduntil."', seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id=".sqlesc($userid)); + $bonusRep->consumeUserBonus($CURUSER['id'], $points, \App\Models\BonusLogs::BUSINESS_TYPE_NO_AD, $points. " Points for ".$bonusnoadtime_advertisement." days without ads.", ['noad' => 'yes', 'noaduntil' => $noaduntil]); + nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=noad"); } } elseif($art == 'gift_2') // charity giving @@ -561,13 +566,14 @@ if ($action == "exchange") { } if($CURUSER['seedbonus'] >= $points) { $points2= number_format($points,1); - $bonuscomment = date("Y-m-d") . " - " .$points2. " Points as charity to users with ratio below ".htmlspecialchars(trim($ratiocharity)).".\n " .htmlspecialchars($bonuscomment); +// $bonuscomment = date("Y-m-d") . " - " .$points2. " Points as charity to users with ratio below ".htmlspecialchars(trim($ratiocharity)).".\n " .htmlspecialchars($bonuscomment); $charityReceiverCount = get_row_count("users", "WHERE enabled='yes' AND 10737418240 < downloaded AND $ratiocharity > uploaded/downloaded"); if ($charityReceiverCount) { - sql_query("UPDATE users SET seedbonus = seedbonus - $points, charity = charity + $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); +// sql_query("UPDATE users SET seedbonus = seedbonus - $points, charity = charity + $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); + $bonusRep->consumeUserBonus($CURUSER['id'], $points, \App\Models\BonusLogs::BUSINESS_TYPE_GIFT_TO_LOW_SHARE_RATIO, $points. " Points as charity to users with ratio below ".htmlspecialchars(trim($ratiocharity)).".", ['charity' => \Nexus\Database\NexusDB::raw("charity + $points"), ]); $charityPerUser = $points/$charityReceiverCount; sql_query("UPDATE users SET seedbonus = seedbonus + $charityPerUser WHERE enabled='yes' AND 10737418240 < downloaded AND $ratiocharity > uploaded/downloaded") or sqlerr(__FILE__, __LINE__); - nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=charity"); + nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=charity"); } else { @@ -596,7 +602,7 @@ if ($action == "exchange") { } if($CURUSER['seedbonus'] >= $points) { $points2= number_format($points,1); - $bonuscomment = date("Y-m-d") . " - " .$points2. " Points as gift to ".htmlspecialchars(trim($_POST["username"])).".\n " .htmlspecialchars($bonuscomment); +// $bonuscomment = date("Y-m-d") . " - " .$points2. " Points as gift to ".htmlspecialchars(trim($_POST["username"])).".\n " .htmlspecialchars($bonuscomment); $aftertaxpoint = $points; if ($taxpercentage_bonus) @@ -617,7 +623,8 @@ if ($action == "exchange") { die; } - sql_query("UPDATE users SET seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); +// sql_query("UPDATE users SET seedbonus = seedbonus - $points, bonuscomment = ".sqlesc($bonuscomment)." WHERE id = ".sqlesc($userid)) or sqlerr(__FILE__, __LINE__); + $bonusRep->consumeUserBonus($CURUSER['id'], $points, \App\Models\BonusLogs::BUSINESS_TYPE_GIFT_TO_SOMEONE, $points2 . " Points as gift to ".htmlspecialchars(trim($_POST["username"]))); sql_query("UPDATE users SET seedbonus = seedbonus + $aftertaxpoint, bonuscomment = ".sqlesc($newreceiverbonuscomment)." WHERE id = ".sqlesc($useridgift)); //===send message @@ -640,7 +647,6 @@ if ($action == "exchange") { stderr("Error","Invalid H&R ID: " . ($_POST['hr_id'] ?? ''), false, false); } try { - $bonusRep = new \App\Repositories\BonusRepository(); $bonusRep->consumeToCancelHitAndRun($userid, $_POST['hr_id']); nexus_redirect("" . get_protocol_prefix() . "$BASEURL/mybonus.php?do=cancel_hr"); } catch (\Exception $exception) {