/**
  * Get balance
  *
  * @param mixed $userID
  * @return bool|float|int
  */
 public static function getUserWalletBalance($userID)
 {
     $bitcoinInfo = BuckysUser::getUserBitcoinInfo($userID);
     if (!$bitcoinInfo) {
         return 0;
     }
     $balance = BuckysBitcoin::getWalletBalance($bitcoinInfo['bitcoin_guid'], buckys_decrypt($bitcoinInfo['bitcoin_password']));
     return $balance;
 }
 /**
  * make payment
  *
  * @param mixed $buyerID
  * @param mixed $sellerID
  * @param mixed $amount
  */
 public function makePayment($buyerID, $sellerID, $amount)
 {
     $sellerBitcoinInfo = BuckysUser::getUserBitcoinInfo($sellerID);
     if ($amount <= 0 || !$sellerBitcoinInfo) {
         return false;
         //no payment
     }
     $flag = BuckysBitcoin::sendBitcoin($buyerID, $sellerBitcoinInfo['bitcoin_address'], $amount);
     buckys_get_messages();
     // this will flash the messages
     return $flag;
 }
 /**
  * Pay to list products
  *
  * @param mixed $userID
  * @param mixed $paymentType
  * @return bool|int|null|string|void
  */
 public function payListingFee($userID, $prodID, $paymentType = BuckysShopProduct::LIST_FEE_PAYMENT_TYPE_BTC)
 {
     $flag = false;
     if ($paymentType == BuckysShopProduct::LIST_FEE_PAYMENT_TYPE_CREDIT) {
         $transactionIns = new BuckysTransaction();
         $flag = $transactionIns->useCreditsInShop($userID, SHOP_PRODUCT_LISTING_FEE_IN_CREDIT);
     } else {
         if ($paymentType == BuckysShopProduct::LIST_FEE_PAYMENT_TYPE_BTC) {
             $flag = BuckysBitcoin::sendBitcoin($userID, SHOP_TNB_LISTING_FEE_RECEIVER_BITCOIN_ADDRESS, SHOP_PRODUCT_LISTING_FEE_IN_BTC);
             buckys_get_messages();
             // this will flash the messages
             if ($flag) {
                 //Create bitcoin transaction
                 BuckysBitcoinTransaction::addTransaction(BuckysBitcoinTransaction::TNB_BITCOIN_RECEIVER_ID, $userID, BuckysBitcoinTransaction::ACTIVITY_TYPE_LISTING_PRODUCT, $prodID, SHOP_PRODUCT_LISTING_FEE_IN_BTC);
             }
         }
     }
     return $flag;
 }
<?php

require dirname(__FILE__) . '/includes/bootstrap.php';
//Getting Current User ID
if (!buckys_check_user_acl(USER_ACL_REGISTERED)) {
    buckys_redirect('/register.php');
}
$bitcoinClass = new BuckysBitcoin();
//Create Wallet if it is not created
$bitcoinInfo = BuckysUser::getUserBitcoinInfo($userID);
if (!$bitcoinInfo) {
    $bitcoinInfo = $bitcoinClass->createWallet($TNB_GLOBALS['user']['userID'], $TNB_GLOBALS['user']['email']);
}
if (isset($_POST['action']) && $_POST['action'] == 'send-bitcoins') {
    //Check Token
    if (!buckys_check_form_token()) {
        buckys_redirect("/wallet.php", MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
    }
    $toAddress = $_POST['receiver'];
    $amount = doubleval($_POST['amount']);
    $password = $_POST['password'];
    $user = BuckysUser::getUserData($TNB_GLOBALS['user']['userID']);
    $is_error = false;
    if (!$password || !buckys_validate_password($password, $user['password'])) {
        buckys_redirect("/wallet.php", MSG_CURRENT_PASSWORD_NOT_CORRECT, MSG_TYPE_ERROR);
    }
    if (!$toAddress) {
        buckys_redirect("/wallet.php", MSG_ENTER_BITCOINS_ADDRESS_OF_RECIPIENT, MSG_TYPE_ERROR);
    }
    if (!$amount || $amount <= 0) {
        buckys_redirect("/wallet.php", MSG_INVALID_BITCOIN_AMOUNT, MSG_TYPE_ERROR);
 /**
  * @param $userID
  * @param $adID
  * @param $amount
  * @return bool
  */
 public function addFunds($userID, $adID, $amount)
 {
     global $db;
     $amount = doubleval($amount);
     //Check User Balance
     $bitcoinClass = new BuckysBitcoin();
     $userBalance = $bitcoinClass->getUserWalletBalance($userID);
     if ($userBalance < $amount) {
         $this->last_message = sprintf(MSG_AD_BITCOIN_BALANCE_NOT_ENOUGH_ERROR, $userBalance . ' BTC');
         return false;
     }
     $sendPayment = $bitcoinClass->sendBitcoin($userID, TNB_BITCOIN_ADDRESS, $amount);
     //they tried to send all the BTC in their wallet and didn't have enough for the fee
     if ($sendPayment === false) {
         $_SESSION['message'] = [];
         $tryPaymentAgain = $bitcoinClass->sendBitcoin($userID, TNB_BITCOIN_ADDRESS, $amount - BLOCKCHAIN_FEE);
         if ($tryPaymentAgain === false) {
             $this->last_message = MSG_INVALID_REQUEST;
             return false;
         }
     }
     $impressions = round($amount / ADS_PRICE_FOR_THOUSAND_IMPRESSIONS * 1000);
     //Update AD
     $query = $db->prepare("UPDATE " . TABLE_ADS . " SET `status`=1, `budget`=`budget` + " . $amount . ", `impressions`=`impressions` + " . $impressions . " WHERE id=%d", $adID);
     $db->query($query);
     $this->last_message = MSG_AD_UPDATED;
     return true;
 }
buckys_enqueue_stylesheet('trade.css');
buckys_enqueue_javascript('uploadify/jquery.uploadify.js');
buckys_enqueue_javascript('jquery.Jcrop.js');
buckys_enqueue_javascript('jquery.color.js');
buckys_enqueue_javascript('trade.js');
buckys_enqueue_javascript('trade-edit.js');
buckys_enqueue_javascript('uploadify/flash_install.js');
$TNB_GLOBALS['content'] = 'trade/additem';
$TNB_GLOBALS['headerType'] = 'trade';
$view = [];
$tradeCatIns = new BuckysTradeCategory();
$countryIns = new BuckysCountry();
$tradeUserIns = new BuckysTradeUser();
$view['no_cash'] = false;
$view['no_credits'] = false;
if (!$tradeUserIns->hasCredits($userID)) {
    $view['no_credits'] = true;
}
$userInfo = BuckysUser::getUserBasicInfo($userID);
$view['category_list'] = $tradeCatIns->getCategoryList(0);
$view['country_list'] = $countryIns->getCountryList();
$view['action_name'] = 'addTradeItem';
$view['page_title'] = 'Add an Item';
$view['type'] = 'additem';
$view['my_bitcoin_balance'] = BuckysBitcoin::getUserWalletBalance($userID);
$view['my_credit_balance'] = $userInfo['credits'];
if ($view['my_bitcoin_balance'] < TRADE_ITEM_LISTING_FEE_IN_BTC && $view['my_credit_balance'] < TRADE_ITEM_LISTING_FEE_IN_CREDIT) {
    $view['no_cash'] = true;
}
$TNB_GLOBALS['title'] = 'Add an Item - BuckysRoomTrade';
require DIR_FS_TEMPLATE . $TNB_GLOBALS['template'] . "/" . $TNB_GLOBALS['layout'] . ".php";
/**
 * Purchase product function
 * this function is POST
 */
function purchaseProduct()
{
    $productIns = new BuckysShopProduct();
    $orderIns = new BuckysShopOrder();
    $buyerID = get_secure_integer($_REQUEST['buyerID']);
    $productID = get_secure_integer($_REQUEST['productID']);
    $userID = buckys_is_logged_in();
    //Can  you purchase this item?
    if ($buyerID != $userID) {
        buckys_redirect('/shop/view.php?id=' . $productID, MSG_PERMISSION_DENIED, MSG_TYPE_ERROR);
    }
    //Product is active?
    $prodData = $productIns->getProductById($productID, false);
    if (!$prodData || $prodData['status'] == BuckysShopProduct::STATUS_INACTIVE) {
        echo "here";
        exit;
        buckys_redirect('/shop/index.php' . $productID, MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
    }
    //Is this your product?
    if ($prodData['userID'] == $buyerID) {
        buckys_redirect('/shop/view.php?id=' . $productID, MSG_PERMISSION_DENIED, MSG_TYPE_ERROR);
    }
    //Shipping price is set?
    if (!$prodData['isDownloadable']) {
        $shippingPrice = fn_buckys_get_available_shipping_price($buyerID, $productID);
        if ($shippingPrice === null) {
            buckys_redirect('/shop/view.php?id=' . $productID, 'This item can not be shipped to your address. Check your shipping address or contact the owner.', MSG_TYPE_ERROR);
        }
    } else {
        $shippingPrice = 0;
    }
    //Do you have money?
    $balance = BuckysBitcoin::getUserWalletBalance($buyerID);
    $balance = 100;
    $total = $prodData['price'] + $shippingPrice;
    if ($total > $balance) {
        buckys_redirect('/shop/view.php?id=' . $productID, 'You do not have bitcoin enough to purchase this item.', MSG_TYPE_ERROR);
    }
    //Purchase product
    $flag = $orderIns->makePayment($buyerID, $prodData['userID'], $total);
    if ($flag) {
        if (!$prodData['isDownloadable']) {
            $buyerShippingInfoID = $orderIns->createShippingInfo($buyerID);
        } else {
            $buyerShippingInfoID = 0;
        }
        $param = ['sellerID' => $prodData['userID'], 'buyerID' => $buyerID, 'productID' => $productID, 'unitPrice' => $prodData['price'], 'shippingPrice' => $shippingPrice, 'totalPrice' => $total, 'buyerShippingID' => $buyerShippingInfoID, 'trackingNo' => '', 'createdDate' => date('Y-m-d H:i:s'), 'status' => BuckysShopOrder::STATUS_SOLD];
        if ($orderIns->createOrder($param)) {
            buckys_redirect('/shop/purchase.php', 'You have purchased an item successfully!', MSG_TYPE_SUCCESS);
        } else {
            buckys_redirect('/shop/view.php?id=' . $productID, 'Something goes wrong with your purchase. Please contact customer support!', MSG_TYPE_ERROR);
        }
    } else {
        buckys_redirect('/shop/view.php?id=' . $productID, 'Payment problem. Please contact customer support!', MSG_TYPE_ERROR);
    }
}
 /**
  * @param $email
  * @param $token
  * @return bool
  */
 public static function verifyAccount($email, $token)
 {
     global $db;
     $query = $db->prepare("SELECT userID FROM " . TABLE_USERS . " WHERE token=%s AND email=%s AND STATUS=0", $token, $email);
     $userID = $db->getVar($query);
     if (!$userID) {
         buckys_add_message(MSG_INVALID_TOKEN, MSG_TYPE_ERROR);
         return false;
     }
     //Verify links
     $query = $db->prepare("UPDATE " . TABLE_USERS . " SET status=1, token='' WHERE userID=%d", $userID);
     $db->query($query);
     buckys_add_message(MSG_ACCOUNT_VERIFIED, MSG_TYPE_SUCCESS);
     //Make this user to friend with bucky
     $query = $db->prepare("SELECT userID FROM " . TABLE_USERS . " WHERE email=%s", TNB_ADMIN_EMAIL);
     $buckysID = $db->getVar($query);
     //$buckysID = $db->getVar("Select userID FROM " . TABLE_USERS . " WHERE email='*****@*****.**'");
     $db->insertFromArray(TABLE_FRIENDS, ['userID' => $buckysID, 'userFriendID' => $userID, 'status' => '1']);
     $db->insertFromArray(TABLE_FRIENDS, ['userID' => $userID, 'userFriendID' => $buckysID, 'status' => '1']);
     //Create Bitcoin account
     BuckysBitcoin::createWallet($userID, $email);
     //Create Default Ads for the users
     $classPublisherAds = new BuckysPublisherAds();
     $classPublisherAds->createDefaultPublisherAds($userID);
     return true;
 }
<?php

require dirname(dirname(__FILE__)) . '/includes/bootstrap.php';
//Getting Publisher Ads
$query = "SELECT pa.*, b.bitcoin_address FROM " . TABLE_PUBLISHER_ADS . " AS pa LEFT JOIN " . TABLE_USERS_BITCOIN . " AS b ON b.userID=pa.publisherID WHERE pa.impressions > pa.paidImpressions";
$results = $db->getResultsArray($query);
$bitcoinClass = new BuckysBitcoin();
$classPublisherAds = new BuckysPublisherAds();
//$price_per_impression = ADS_PRICE_FOR_THOUSAND_IMPRESSIONS * ADS_PUBLISHER_PERCENTAGE / 1000;
foreach ($results as $row) {
    $userBalance = $classPublisherAds->getUserBalance($row['publisherID']);
    if ($userBalance >= ADS_MINIMUM_PAYOUT_BALANCE) {
        $amountToSend = $userBalance - BLOCKCHAIN_FEE;
        $paymentSend = $bitcoinClass->sendBitcoinFromBuckysroom($row['bitcoin_address'], $amountToSend);
        if ($paymentSend) {
            $db->update("UPDATE " . TABLE_PUBLISHER_ADS . " SET `paidImpressions`=`impressions` WHERE publisherID=" . $row['publisherID']);
        }
    }
    /*
    $unpaidImpressions = $row['impressions'] - $row['paidImpressions'];
    
    //0.00036 for every 1000 impressions
    $amount = $unpaidImpressions * $price_per_impression;
    if($amount < ADS_MINIMUM_PAYOUT_BALANCE)
        continue;
    
    $amount = $amount - BLOCKCHAIN_FEE;
    
    $bitcoinClass->sendBitcoinFromBuckysroom($row['bitcoin_address'], $amount);
    //Update PaidImpressions
    $db->update("UPDATE " . TABLE_PUBLISHER_ADS . " SET `paidImpressions`=`impressions`, `earnings` = `earnings` + " . $amount . " WHERE id=" . $row['id']);