<?php

// Tor list: https://www.dan.me.uk/torlist/
// if you use this cron job, be sure to edit the config.php file for the database settings
chdir("..");
include "./proxy_filter.php";
$pf = new proxy_filter();
$tor_list = @file_get_contents("https://www.dan.me.uk/torlist/");
if (!empty($tor_list)) {
    $tor_list = str_replace("\r\n", "\n", $tor_list);
    $tor_list = explode("\n", $tor_list);
    $pf->add($tor_list, "Tor exit node");
}
 public function __construct($config)
 {
     if (!defined("SF_STATUS_OPERATIONAL")) {
         define("SF_STATUS_OPERATIONAL", 100);
         define("SF_STATUS_PAYOUT_ACCEPTED", 101);
         define("SF_STATUS_PAYOUT_AND_PROMO_ACCEPTED", 102);
         //define("SF_STATUS_SUCCESS",102);
         define("SF_STATUS_RPC_CONNECTION_FAILED", 200);
         define("SF_STATUS_MYSQL_CONNECTION_FAILED", 201);
         define("SF_STATUS_PAYOUT_DENIED", 202);
         define("SF_STATUS_INVALID_DOGE_ADDRESS", 203);
         define("SF_STATUS_PAYOUT_ERROR", 204);
         define("SF_STATUS_CAPTCHA_INCORRECT", 205);
         define("SF_STATUS_DRY_FAUCET", 206);
         define("SF_STATUS_PROXY_DETECTED", 207);
         define("SF_STATUS_FAUCET_INCOMPLETE", 300);
     }
     $defaults = array("minimum_payout" => 0.01, "maximum_payout" => 10, "payout_threshold" => 250, "payout_interval" => "7h", "user_check" => "both", "wallet_passphrase" => "", "use_captcha" => true, "captcha" => "recaptcha", "captcha_config" => array(), "use_promo_codes" => true, "mysql_table_prefix" => "sf_", "donation_address" => "DTiUqjQTXwgZfvcTcdoabp7uLezK47TPkN ", "title" => "DOGE Faucet", "template" => "default", "stage_payments" => false, "stage_payment_account_name" => "account", "staged_payment_threshold" => 15, "staged_payment_cron_only" => false, "filter_proxies" => false, "proxy_filter_use_faucet_database" => false);
     $this->config = array_merge($defaults, $config);
     if ($this->config["user_check"] != "ip_address" && $this->config["user_check"] != "doge_address") {
         $this->config["user_check"] = "both";
     }
     if ($this->config["captcha"] == "recaptcha") {
         require_once './lib/recaptchalib.php';
     } elseif ($this->config["captcha"] == "recaptcha2") {
         require_once './lib/recaptchalib2.php';
     }
     if (isset($config["rpc_user"], $config["rpc_password"], $config["rpc_host"], $config["rpc_port"], $config["mysql_user"], $config["mysql_password"], $config["mysql_host"], $config["mysql_database"])) {
         if (class_exists("jsonRPCClient")) {
             $this->rpc_client = new jsonRPCClient('http://' . urlencode($config["rpc_user"]) . ':' . urlencode($config["rpc_password"]) . '@' . urlencode($config["rpc_host"]) . ':' . urlencode($config["rpc_port"]));
             $this->db = @new mysqli($config["mysql_host"], $config["mysql_user"], $config["mysql_password"], $config["mysql_database"]);
             //if (!$this->db->connect_error)
             if (!mysqli_connect_error() && !is_null($this->balance = $this->rpc("getbalance"))) {
                 if ($this->balance >= $this->config["payout_threshold"]) {
                     $this->status = SF_STATUS_OPERATIONAL;
                     if (isset($_POST["dogecoin_address"]) && ($this->config["use_captcha"] && $this->valid_captcha() || !$this->config["use_captcha"])) {
                         if ($this->config["filter_proxies"] && class_exists("proxy_filter")) {
                             $pf = new proxy_filter($this->config["proxy_filter_use_faucet_database"] ? $this->db : false);
                             if ($pf->check()) {
                                 $this->status = SF_STATUS_PROXY_DETECTED;
                                 return;
                             }
                         }
                         $dogecoin_address = $_POST["dogecoin_address"];
                         $validation = $this->rpc("validateaddress", array($dogecoin_address));
                         if ($validation["isvalid"]) {
                             $interval = "7 HOUR";
                             // hardcoded default interval if the custom interval is messed up
                             $interval_value = intval(substr($this->config["payout_interval"], 0, -1));
                             $interval_function = strtoupper(substr($this->config["payout_interval"], -1));
                             if ($interval_value >= 0 && ($interval_function == "H" || $interval_function == "M" || $interval_function == "D")) {
                                 $interval = $interval_value . " ";
                                 switch ($interval_function) {
                                     case "M":
                                         $interval .= "MINUTE";
                                         break;
                                     case "H":
                                         $interval .= "HOUR";
                                         break;
                                     case "D":
                                         $interval .= "DAY";
                                         break;
                                 }
                             }
                             $user_check = " AND (";
                             if ($this->config["user_check"] == "ip_address" || $this->config["user_check"] == "both") {
                                 $user_check .= " `ip_address` = '" . $this->db->escape_string($_SERVER["REMOTE_ADDR"]) . "'";
                             }
                             if ($this->config["user_check"] == "doge_address" || $this->config["user_check"] == "both") {
                                 $user_check .= ($this->config["user_check"] == "both" ? " OR" : "") . " `payout_address` = '" . $this->db->escape_string($dogecoin_address) . "'";
                             }
                             $user_check .= ")";
                             $result = $this->db->query("SELECT `id` FROM `" . $this->db->escape_string($this->config["mysql_table_prefix"]) . "payouts` WHERE `timestamp` > NOW() - INTERVAL " . $interval . $user_check);
                             if ($row = @$result->fetch_assoc()) {
                                 $this->status = SF_STATUS_PAYOUT_DENIED;
                             } else {
                                 $promo_code = "";
                                 if ($this->config["use_promo_codes"] && isset($_POST["promo_code"])) {
                                     $result2 = $this->db->query("SELECT `minimum_payout`,`maximum_payout`,`uses` FROM `" . $this->config["mysql_table_prefix"] . "promo_codes` WHERE `code` = '" . $this->db->escape_string($_POST["promo_code"]) . "'");
                                     if ($promo = @$result2->fetch_assoc()) {
                                         $promo["uses"] = intval($promo["uses"]);
                                         // MySQLi
                                         if ($promo["uses"] !== 0) {
                                             $promo_code = $_POST["promo_code"];
                                             $promo["minimum_payout"] = floatval($promo["minimum_payout"]);
                                             $promo["maximum_payout"] = floatval($promo["maximum_payout"]);
                                             if ($promo["minimum_payout"] >= $promo["maximum_payout"]) {
                                                 $this->promo_payout_amount = $promo["maximum_payout"];
                                             } else {
                                                 $this->promo_payout_amount = $this->float_rand($promo["minimum_payout"], $promo["maximum_payout"]);
                                             }
                                             //$this->promo_payout_amount = mt_rand($promo["minimum_payout"]*10000,$promo["maximum_payout"]*10000)/10000; // calculate a random promo DOGE amount
                                             if ($promo["uses"] > 0) {
                                                 $this->db->query("UPDATE `" . $this->config["mysql_table_prefix"] . "promo_codes` SET `uses` = `uses`-1 WHERE `code` = '" . $this->db->escape_string($promo_code) . "'");
                                             }
                                         }
                                     }
                                 }
                                 //$this->payout_amount = mt_rand($this->config["minimum_payout"]*10000,$this->config["maximum_payout"]*10000)/10000; // calculate a random DOGE amount
                                 $this->payout_amount = $this->float_rand($this->config["minimum_payout"], $this->config["maximum_payout"]);
                                 $this->db->query("INSERT INTO `" . $this->db->escape_string($this->config["mysql_table_prefix"]) . "payouts` (`payout_amount`,`ip_address`,`payout_address`,`promo_code`,`promo_payout_amount`,`timestamp`) VALUES ('" . $this->payout_amount . "','" . $this->db->escape_string($_SERVER["REMOTE_ADDR"]) . "','" . $this->db->escape_string($dogecoin_address) . "','" . $this->db->escape_string($promo_code) . "','" . $this->promo_payout_amount . "',NOW())");
                                 // insert the transaction into the payout log
                                 if ($this->config["wallet_passphrase"] != "") {
                                     $this->rpc("walletpassphrase", array($this->config["wallet_passphrase"], 5));
                                 }
                                 // unlock wallet
                                 if (isset($this->config["_debug_test_mode"])) {
                                     $this->status = true ? $this->promo_payout_amount > 0 ? SF_STATUS_PAYOUT_AND_PROMO_ACCEPTED : SF_STATUS_PAYOUT_ACCEPTED : SF_STATUS_PAYOUT_ERROR;
                                 } else {
                                     if ($this->config["stage_payments"]) {
                                         $this->status = $this->stage_payment($dogecoin_address, $this->payout_amount + $this->promo_payout_amount) ? $this->promo_payout_amount > 0 ? SF_STATUS_PAYOUT_AND_PROMO_ACCEPTED : SF_STATUS_PAYOUT_ACCEPTED : SF_STATUS_PAYOUT_ERROR;
                                     } else {
                                         $this->status = !is_null($this->rpc("sendtoaddress", array($dogecoin_address, $this->payout_amount + $this->promo_payout_amount))) ? $this->promo_payout_amount > 0 ? SF_STATUS_PAYOUT_AND_PROMO_ACCEPTED : SF_STATUS_PAYOUT_ACCEPTED : SF_STATUS_PAYOUT_ERROR;
                                     }
                                     // send the DOGE
                                 }
                                 if ($this->config["wallet_passphrase"] != "") {
                                     $this->rpc("walletlock");
                                 }
                                 // lock wallet
                             }
                         } else {
                             $this->status = SF_STATUS_INVALID_DOGE_ADDRESS;
                         }
                     } else {
                         if (isset($_POST["dogecoin_address"])) {
                             $this->status = SF_STATUS_CAPTCHA_INCORRECT;
                         }
                     }
                 } else {
                     $this->status = SF_STATUS_DRY_FAUCET;
                 }
             } else {
                 $this->status = SF_STATUS_MYSQL_CONNECTION_FAILED;
             }
         } else {
             $this->status = SF_STATUS_FAUCET_INCOMPLETE;
         }
         // missing RPC client
     } else {
         $this->status = SF_STATUS_FAUCET_INCOMPLETE;
     }
     // missing some settings
 }