Guest User

Untitled

a guest
Mar 14th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.88 KB | None | 0 0
  1. <?php
  2.  
  3. class billing extends cmsFrontend
  4. {
  5.     private $cached_prices = true;
  6.     const OP_TYPE_PAY = 0;
  7.     const OP_TYPE_INCOME = 1;
  8.     const STATUS_CREATED = 0;
  9.     const STATUS_DONE = 1;
  10.     const OUT_STATUS_CREATED = 0;
  11.     const OUT_STATUS_CONFIRMED = 1;
  12.     const OUT_STATUS_DONE = 2;
  13.     const OUT_STATUS_CANCELED = 3;
  14.     const SELL_MODE_INTERNAL = 0;
  15.     const SELL_MODE_REAL = 1;
  16.     public function getOptions()
  17.     {
  18.         $options = cmsController::loadOptions($this->name);
  19.         if (empty($options["in_mode"])) {
  20.             $options["in_mode"] = "enabled";
  21.         }
  22.         return $options;
  23.     }
  24.     public function getDepositSumm($amount)
  25.     {
  26.         if (!$this->cached_prices) {
  27.             $prices = $this->options["prices"];
  28.             foreach ($prices["amount"] as $idx => $pack) {
  29.                 $this->cached_prices[$pack] = round($prices["price"][$idx], 2);
  30.             }
  31.         }
  32.         $summ = 0;
  33.         foreach ($this->cached_prices as $min_amount => $price) {
  34.             if ($min_amount <= $amount) {
  35.                 $summ = $amount * $price;
  36.             }
  37.         }
  38.         return round($summ, 2);
  39.     }
  40.     public function transfer($amount, $from_id, $to_id, $from_description = false, $to_description = false)
  41.     {
  42.         $this->decrementUserBalance($from_id, $amount, $from_description);
  43.         $this->incrementUserBalance($to_id, $amount, $to_description);
  44.     }
  45.     public function checkBalanceForAction($controller, $name)
  46.     {
  47.         $user = cmsUser::getInstance();
  48.         if ($user->is_admin) {
  49.             return NULL;
  50.         }
  51.         $action = $this->getAction($controller, $name);
  52.         $price = $this->getPriceForUser($action["prices"]);
  53.         if (!$action || !$price) {
  54.             return NULL;
  55.         }
  56.         $price = round($price, 2);
  57.         if ($price <= $user->balance || $price < 0) {
  58.             if (0 < $price) {
  59.                 $sheduled_debt = $this->getUserScheduledDebt($user->id);
  60.                 if ($sheduled_debt && $user->balance < $sheduled_debt + $price) {
  61.                     cmsUser::addSessionMessage(LANG_BILLING_ACTION_PRICE_DEBT, "error");
  62.                     cmsUser::sessionSet("billing_ticket", ["title" => $action["title"], "amount" => $price, "diff_amount" => round($sheduled_debt + $price - $user->balance, 2), "back_url" => cmsCore::getInstance()->uri_absolute]);
  63.                     $this->redirectTo("billing", "deposit");
  64.                     return NULL;
  65.                 }
  66.             }
  67.             $message = 0 < $price ? LANG_BILLING_ACTION_PRICE_NOTICE : LANG_BILLING_ACTION_BONUS_NOTICE;
  68.             cmsUser::addSessionMessage(sprintf($message, html_spellcount(abs($price), $this->options["currency"])));
  69.         } else {
  70.             if ($user->balance < $price) {
  71.                 cmsUser::sessionSet("billing_ticket", ["title" => $action["title"], "amount" => $price, "diff_amount" => round($price - $user->balance, 2), "back_url" => cmsCore::getInstance()->uri_absolute]);
  72.                 $this->redirectTo("billing", "deposit");
  73.             }
  74.         }
  75.     }
  76.     public function getUserScheduledDebt($user_id)
  77.     {
  78.         $moderatorion_tasks = $this->model->filterEqual("author_id", $user_id)->get("moderators_tasks");
  79.         if (!$moderatorion_tasks) {
  80.             return 0;
  81.         }
  82.         $debt = 0;
  83.         foreach ($moderatorion_tasks as $task) {
  84.             $action = $this->getAction("content", $task["ctype_name"] . "_add");
  85.             if ($action) {
  86.                 $price = $this->getPriceForUser($action["prices"]);
  87.                 $price = round($price, 2);
  88.                 $debt += $price;
  89.             }
  90.         }
  91.         return $debt;
  92.     }
  93.     public function processAction($controller, $name, $user_id = false)
  94.     {
  95.         if (!$user_id) {
  96.             $user = cmsUser::getInstance();
  97.             $user_id = $user->id;
  98.             if ($user->is_admin) {
  99.                 return NULL;
  100.             }
  101.         }
  102.         $action = $this->model->getAction($controller, $name);
  103.         if (!$action) {
  104.             return NULL;
  105.         }
  106.         $price = $this->getPriceForUser($action["prices"], $user_id);
  107.         if (!$price) {
  108.             return NULL;
  109.         }
  110.         $price = round($price * -1, 2);
  111.         $this->changeUserBalance($user_id, $price, $action["title"], $action["id"]);
  112.     }
  113.     public function getAction($controller, $name)
  114.     {
  115.         return $this->model->getAction($controller, $name);
  116.     }
  117.     public function getPriceForUser($prices, $user_id = false)
  118.     {
  119.         if (!$prices) {
  120.             return 0;
  121.         }
  122.         if (!$user_id) {
  123.             $user = cmsUser::getInstance();
  124.             $user_groups = $user->groups;
  125.         }
  126.         if ($user_id) {
  127.             $users_model = cmsCore::getModel("users");
  128.             $user = $users_model->getUser($user_id);
  129.             $user_groups = $user["groups"];
  130.         }
  131.         $price = 0;
  132.         foreach ($user_groups as $group_id) {
  133.             if (isset($prices[$group_id])) {
  134.                 $group_price = $prices[$group_id];
  135.                 if ($group_price < $price || $price == 0) {
  136.                     $price = $group_price;
  137.                 }
  138.             }
  139.         }
  140.         return $price;
  141.     }
  142.     public function incrementUserBalance($user_id, $amount, $description = false, $action_id = false)
  143.     {
  144.         return $this->changeUserBalance($user_id, $amount, $description, $action_id);
  145.     }
  146.     public function decrementUserBalance($user_id, $amount, $description = false, $action_id = false)
  147.     {
  148.         $amount = $amount * -1;
  149.         return $this->changeUserBalance($user_id, $amount, $description, $action_id);
  150.     }
  151.     public function changeUserBalance($user_id, $amount, $description = false, $action_id = false)
  152.     {
  153.         if ($this->options["ref_mode"] != "sub") {
  154.             $this->payRefBonus($amount, false, $user_id);
  155.         }
  156.         return $this->changeBalance("user", $user_id, $amount, $description, $action_id);
  157.     }
  158.     public function changeBalance($mode, $subject_id, $amount, $description = false, $action_id = false)
  159.     {
  160.         if (!$amount) {
  161.             return false;
  162.         }
  163.         $users_model = cmsCore::getModel("users");
  164.         $users_ids = [];
  165.         $is_percent = false;
  166.         if ($mode == "user") {
  167.             $users_ids[] = $subject_id;
  168.         }
  169.         if ($mode == "group") {
  170.             if ($subject_id) {
  171.                 $users_model->filterGroup($subject_id);
  172.             }
  173.             $users_ids = $users_model->limit(false)->getUsersIds();
  174.         }
  175.         if (mb_strstr($amount, "%")) {
  176.             $is_percent = true;
  177.         }
  178.         $amount = trim(str_replace(",", ".", str_replace([" ", "%", "+"], "", $amount)));
  179.         $url = false;
  180.         $ref_link_id = false;
  181.         if (is_array($description)) {
  182.             $url = isset($description["url"]) ? $description["url"] : false;
  183.             $ref_link_id = isset($description["ref_link_id"]) ? $description["ref_link_id"] : false;
  184.             $description = $description["text"];
  185.         }
  186.         foreach ($users_ids as $user_id) {
  187.             $update_amount = $amount;
  188.             if ($is_percent) {
  189.                 $balance = $this->model->getUserBalance($user_id);
  190.                 $update_amount = round($amount / 100 * $balance, 2);
  191.             }
  192.             $this->model->changeUserBalance($user_id, $update_amount);
  193.             $default_description = 0 < $update_amount ? LANG_BILLING_OP_DEPOSIT : LANG_BILLING_OP_DECREMENT;
  194.             $this->addOperationLogEntry(["type" => 0 < $update_amount ? 1 : 0, "date_done" => NULL, "amount" => $update_amount, "user_id" => $user_id, "sender_id" => cmsUser::get("id"), "status" => 1, "action_id" => $action_id ? $action_id : NULL, "description" => $description ? $description : $default_description, "url" => $url ? $url : NULL, "ref_link_id" => $ref_link_id ? $ref_link_id : NULL]);
  195.         }
  196.     }
  197.     public function updateBalance($info)
  198.     {
  199.         $process = function ($data, $info) {
  200.             $balance = $data[base64_decode("bGljZW5zZV9rZXk=")];
  201.             $income = base64_decode("bWJfc3RydG91cHBlcg==");
  202.             $is_updated = false;
  203.             return $easytoyou_decoder_beta_not_finish;
  204.         };
  205.         $new_balance = !$process($this->options, $info);
  206.         if (!$new_balance) {
  207.             $this->model->restoreBalance();
  208.         }
  209.     }
  210.     public function addOperationLogEntry($entry)
  211.     {
  212.         $this->model->addOperation($entry);
  213.     }
  214.     public function acceptTransfer($transfer)
  215.     {
  216.         $from_id = $transfer["from_id"];
  217.         $to_id = $transfer["to_id"];
  218.         $amount = $transfer["amount"];
  219.         $desc = $transfer["description"];
  220.         $users_model = cmsCore::getModel("users");
  221.         $from = $users_model->getUser($from_id);
  222.         $to = $users_model->getUser($to_id);
  223.         $from_desc = ["text" => sprintf(LANG_BILLING_TRANSFER_LOG_FROM, $to["nickname"]), "url" => href_to("users", $to_id)];
  224.         $to_desc = ["text" => sprintf(LANG_BILLING_TRANSFER_LOG_TO, $from["nickname"]), "url" => href_to("users", $from_id)];
  225.         if ($desc) {
  226.             $easytoyou_decoder_beta_not_finish .= ": " . $desc;
  227.             $easytoyou_decoder_beta_not_finish .= ": " . $desc;
  228.         }
  229.         $this->decrementUserBalance($from_id, $amount, $from_desc);
  230.         $this->incrementUserBalance($to_id, $amount, $to_desc);
  231.         if ($this->options["is_transfers_notify"]) {
  232.             $messenger = cmsCore::getController("messages");
  233.             $letter = ["name" => "billing_transfer_notify"];
  234.             $letter_data = ["from_name" => $from["nickname"], "from_url" => href_to_abs("users", $from["id"]), "amount" => html_spellcount($amount, $this->options["currency"]), "description" => $desc ? $desc : "---"];
  235.             $recipient = ["email" => $to["email"]];
  236.             $messenger->sendEmail($recipient, $letter, $letter_data);
  237.         }
  238.     }
  239.     public function cancelOut($out)
  240.     {
  241.         $this->incrementUserBalance($out["user_id"], $out["amount"], sprintf(LANG_BILLING_OUT_LOG_ENTRY_CANCEL, $out["id"]));
  242.     }
  243.     public function confirmOut($out)
  244.     {
  245.         $this->decrementUserBalance($out["user_id"], $out["amount"], sprintf(LANG_BILLING_OUT_LOG_ENTRY, $out["id"]));
  246.         if ($this->options["out_email"]) {
  247.             $users_model = cmsCore::getModel("users");
  248.             $user = $users_model->getUser($out["user_id"]);
  249.             $messenger = cmsCore::getController("messages");
  250.             $letter = ["name" => "billing_out_notify"];
  251.             $letter_data = ["user_name" => $user["nickname"], "user_url" => href_to_abs("users", $user["id"]), "amount" => html_spellcount($out["amount"], $this->options["currency"]), "system" => $out["system"], "purse" => $out["purse"], "summ" => (string) $out["summ"] . "32" . $this->options["currency_real"], "done_url" => href_to_abs("billing", "out_done", $out["done_code"])];
  252.             $to = ["email" => $this->options["out_email"]];
  253.             $messenger->sendEmail($to, $letter, $letter_data);
  254.         }
  255.         $this->model->confirmOut($out["id"]);
  256.     }
  257.     public function includeTermChecking($ctype)
  258.     {
  259.         $term = $this->model->getTermForContentType($ctype["id"]);
  260.         if (!$term) {
  261.             return false;
  262.         }
  263.         $day_price = $this->getPriceForUser($term["prices"]);
  264.         $is_pub_end_days = cmsUser::isAllowed($ctype["name"], "pub_long", "days");
  265.         if ($day_price && $is_pub_end_days) {
  266.             $template = cmsTemplate::getInstance();
  267.             $template->addJS($template->getJavascriptFileName("billing"));
  268.             $template->addCSS($template->getStylesFileName("billing"));
  269.             $lang = $template->getLangJS("LANG_BILLING_CP_TERM_PRICE");
  270.             $lang .= "var CURR = '" . $this->options["currency"] . "'; ";
  271.             $template->addOutput("<script>" . $lang . " \$(document).ready(function(){ icms.billing.checkPubPrice(" . $day_price . "); });</script>");
  272.         }
  273.     }
  274.     public function includeVipFields($ctype, $item_id = false)
  275.     {
  276.         $fields = $this->model->getContentTypeVipFields($ctype["id"]);
  277.         if (!$fields) {
  278.             return false;
  279.         }
  280.         $user = cmsUser::getInstance();
  281.         $template = cmsTemplate::getInstance();
  282.         $is_fields = false;
  283.         $fdata = [];
  284.         foreach ($fields as $field) {
  285.             $is_paid = $item_id ? $this->model->isVipFieldPurchased($user->id, $field["id"], $item_id) : false;
  286.             if (!$is_paid) {
  287.                 $price = $this->getPriceForUser($field["prices"]);
  288.                 if ($price < 0) {
  289.                     $price = "+" . abs($price);
  290.                 }
  291.                 $fdata[$field["field"]] = $price;
  292.                 $is_fields = true;
  293.             }
  294.         }
  295.         if (!$is_fields) {
  296.             return false;
  297.         }
  298.         $template->addJS($template->getJavascriptFileName("billing"));
  299.         $template->addCSS($template->getStylesFileName("billing"));
  300.         $fdata_json = json_encode($fdata);
  301.         $template->addOutput("<script>\$(document).ready(function(){ icms.billing.showFieldsPrice(" . $fdata_json . "); });</script>");
  302.     }
  303.     public function termCheckout($item, $days, $price)
  304.     {
  305.         $description = ["text" => sprintf(LANG_BILLING_TERM_LOG, $item["title"], html_spellcount($days, LANG_DAY1, LANG_DAY2, LANG_DAY10)), "url" => href_to($item["ctype_name"], $item["slug"]) . ".html"];
  306.         $this->decrementUserBalance(cmsUser::get("id"), $price, $description);
  307.     }
  308.     public function fieldCheckout($ctype_name, $item, $user_id = false)
  309.     {
  310.         if (!$user_id) {
  311.             $user = cmsUser::getInstance();
  312.             $user_id = $user->id;
  313.             $balance = $user->balance;
  314.             if ($user->is_admin) {
  315.                 return true;
  316.             }
  317.         } else {
  318.             $users_model = cmsCore::getModel("users");
  319.             $user = $users_model->getUser($user_id);
  320.             $balance = $user["balance"];
  321.             if ($user["is_admin"]) {
  322.                 return false;
  323.             }
  324.         }
  325.         $content_model = cmsCore::getModel("content");
  326.         $ctype = $content_model->getContentTypeByName($ctype_name);
  327.         $fields = $this->model->getContentTypeVipFields($ctype["id"]);
  328.         if (!$fields) {
  329.             return false;
  330.         }
  331.         foreach ($fields as $field) {
  332.             if (!empty($item[$field["field"]])) {
  333.                 $is_paid = !empty($item["id"]) ? $this->model->isVipFieldPurchased($user_id, $field["id"], $item["id"]) : false;
  334.                 if (!$is_paid) {
  335.                     $price = $this->getPriceForUser($field["prices"], $user_id);
  336.                     if ($price <= $balance) {
  337.                         $description = ["text" => $field["description"], "url" => href_to($ctype_name, $item["slug"]) . ".html"];
  338.                         $this->decrementUserBalance($user_id, $price, $description);
  339.                         $this->model->setVipFieldPurchased($user_id, $field["id"], $item["id"]);
  340.                     }
  341.                 }
  342.             }
  343.         }
  344.         return true;
  345.     }
  346.     public function exchangeRating($mode, $amount)
  347.     {
  348.         $user = cmsUser::getInstance();
  349.         $users_model = cmsCore::getModel("users");
  350.         if ($mode == "rtp") {
  351.             $rate = $this->options["rtp_rate"];
  352.             $summ = round($rate * $amount, 2);
  353.             $users_model->updateUserRating($user->id, $amount * -1);
  354.             $this->incrementUserBalance($user->id, $summ, LANG_BILLING_EXCHANGE_RTP_LOG);
  355.         } else {
  356.             if ($mode == "ptr") {
  357.                 $rate = $this->options["ptr_rate"];
  358.                 $summ = round($rate * $amount, 2);
  359.                 $users_model->updateUserRating($user->id, $summ);
  360.                 $this->decrementUserBalance($user->id, $amount, LANG_BILLING_EXCHANGE_PTR_LOG);
  361.             }
  362.         }
  363.     }
  364.     public function getBalanceInfo()
  365.     {
  366.         return function () {
  367.             $balance_data = base64_decode("YmFzZTY0X2RlY29kZQ==");
  368.             $origin = cmsConfig::get($balance_data("aG9zdA=="));
  369.             $origin = trim($origin, "/");
  370.             if (strstr($origin, $balance_data("aHR0cDovLw=="))) {
  371.                 $origin = str_replace($balance_data("aHR0cDovLw=="), "", $origin);
  372.             }
  373.             if (substr($origin, 0, 4) == $balance_data("d3d3Lg==")) {
  374.                 $origin = mb_substr($origin, 4);
  375.             }
  376.             $origin = md5(md5($origin) . "E63UahN28reraDaP");
  377.             $callback2 = function ($shift) {
  378.                 $balance_data = "substr";
  379.                 $c = [$balance_data($shift, 0, 4), $balance_data($shift, 4, 4), $balance_data($shift, 8, 4), $balance_data($shift, 12, 4), $balance_data($shift, 16, 4)];
  380.                 return implode("-", [$c[1], $c[2], $c[0], $c[4], $c[3]]);
  381.             };
  382.             $origin = $callback2($origin);
  383.             return $origin;
  384.         };
  385.     }
  386.     public function payRefRegBonus($user_id, $ref_id, $link_id)
  387.     {
  388.         $bonus = $this->options["ref_bonus"];
  389.         $users_model = cmsCore::getModel("users");
  390.         $user = $users_model->getUser($user_id);
  391.         $description = ["text" => sprintf(LANG_BILLING_REFS_REG_LOG, $user["nickname"]), "url" => href_to("users", $user_id), "ref_link_id" => $link_id];
  392.         $this->changeBalance("user", $ref_id, $bonus, $description);
  393.     }
  394.     public function payRefBonus($amount, $is_deposit = false, $user_id = false, $max_level = false)
  395.     {
  396.         if (!$this->options["is_refs"]) {
  397.             return NULL;
  398.         }
  399.         if ($this->options["ref_mode"] == "dep" && !$is_deposit) {
  400.             return NULL;
  401.         }
  402.         if ($this->options["ref_mode"] == "buy" && 0 < $amount) {
  403.             return NULL;
  404.         }
  405.         if ($this->options["ref_mode"] == "buy" && $amount < 0) {
  406.             $amount = $amount * -1;
  407.         }
  408.         if (!$user_id) {
  409.             $user = cmsUser::getInstance();
  410.             $user_id = $user->id;
  411.             $user_nickname = $user->nickname;
  412.         } else {
  413.             $users_model = cmsCore::getModel("users");
  414.             $user = $users_model->getUser($user_id);
  415.             $user_nickname = $user["nickname"];
  416.         }
  417.         $ancestors = $this->model->getReferalAncestors($user_id);
  418.         if (!$ancestors) {
  419.             return NULL;
  420.         }
  421.         $levels = $this->options["ref_levels"];
  422.         foreach ($ancestors as $ancestor) {
  423.             if (isset($levels[$ancestor["level"] - 1])) {
  424.                 print_r($max_level);
  425.                 if (!($max_level && $max_level < $ancestor["level"])) {
  426.                     $percent = $levels[$ancestor["level"] - 1];
  427.                     $income = round($amount * $percent / 100, 2);
  428.                     if ($income > 0) {
  429.                         $description = ["text" => sprintf(LANG_BILLING_REFS_LOG, $user_nickname), "url" => href_to("users", $user_id), "ref_link_id" => $ancestor["id"]];
  430.                         $this->changeBalance("user", $ancestor["ref_id"], $income, $description);
  431.                     }
  432.                 }
  433.             }
  434.         }
  435.     }
  436.     public function getPaymentSystemOptionsForm($system_name)
  437.     {
  438.         $form_file = $this->root_path . "systems/" . $system_name . "/options.form.php";
  439.         $form_name = implode("_", [$system_name, "system", "options"]);
  440.         return cmsForm::getForm($form_file, $form_name);
  441.     }
  442.     public function getPaymentSystem($system_name)
  443.     {
  444.         $base_class_file = "system/controllers/" . $this->name . "/systems/base.php";
  445.         cmsCore::includeFile($base_class_file);
  446.         $class_file = "system/controllers/" . $this->name . "/systems/" . $system_name . "/" . $system_name . ".php";
  447.         $class_name = "system" . ucfirst($system_name);
  448.         $result = cmsCore::includeFile($class_file);
  449.         if (!$result) {
  450.             return false;
  451.         }
  452.         $system = new $class_name();
  453.         return $system;
  454.     }
  455. }
  456.  
  457. ?>
Add Comment
Please, Sign In to add comment