<?php

require dirname(dirname(__FILE__)) . '/includes/bootstrap.php';
buckys_enqueue_stylesheet('shop.css');
buckys_enqueue_javascript('shop.js');
$TNB_GLOBALS['content'] = 'shop/index';
$TNB_GLOBALS['headerType'] = 'shop';
//Get Top Users
$shopProductIns = new BuckysShopProduct();
$catIns = new BuckysShopCategory();
$view = [];
$view['recent_products'] = $shopProductIns->getRecentProducts(10);
$view['categories'] = $catIns->getCategoryList(0);
$TNB_GLOBALS['title'] = 'BuckysRoomShop - Buy and Sell Items with Bitcoin';
require DIR_FS_TEMPLATE . $TNB_GLOBALS['template'] . "/" . $TNB_GLOBALS['layout'] . ".php";
 /**
  * Count Products according to the category
  *
  * @param array $prodList
  * @return stdClass
  */
 public function countProductInCategory($prodList)
 {
     $catIns = new BuckysShopCategory();
     $categoryList = $catIns->getCategoryList();
     $catProdCountList = [];
     if (count($prodList) > 0) {
         foreach ($prodList as $itemData) {
             if (isset($catProdCountList[$itemData['catID']])) {
                 $catProdCountList[$itemData['catID']]++;
             } else {
                 $catProdCountList[$itemData['catID']] = 1;
             }
         }
     }
     if (count($catProdCountList) > 0 && count($categoryList) > 0) {
         foreach ($categoryList as &$tmpCatData) {
             isset($catProdCountList[$tmpCatData['catID']]) ? $tmpCatData['count'] = $catProdCountList[$tmpCatData['catID']] : ($tmpCatData['count'] = 0);
         }
     }
     return $categoryList;
 }
/**
 * Edit Shop Product action by ajax
 */
function editProduct()
{
    $userID = buckys_is_logged_in();
    if (!$userID) {
        return;
    }
    $shopProductIns = new BuckysShopProduct();
    $inputValidFlag = true;
    $requiredFields = ['title', 'subtitle', 'description', 'category', 'return_policy', 'shipping_price', 'price'];
    foreach ($requiredFields as $requiredField) {
        if ($_REQUEST[$requiredField] == '') {
            $inputValidFlag = false;
        }
    }
    $categoryClass = new BuckysShopCategory();
    $category = $categoryClass->getCategoryByID($_REQUEST['category']);
    if (!$category['isDownloadable'] && $_REQUEST['location'] == '') {
        $inputValidFlag = false;
    } else {
        if ($category['isDownloadable'] == 1) {
            $_REQUEST['location'] = 0;
        }
    }
    if (isset($_REQUEST['price']) && (!is_numeric($_REQUEST['price']) || $_REQUEST['price'] <= 0)) {
        $inputValidFlag = false;
    }
    $shippingPriceList = [];
    if (isset($_REQUEST['shipping_price'])) {
        $shippingPriceList = json_decode($_REQUEST['shipping_price'], true);
        if (!is_array($shippingPriceList) || count($shippingPriceList) < 1) {
            $inputValidFlag = false;
        }
    }
    $actionType = get_secure_string($_REQUEST['type']);
    $paramProdID = get_secure_integer($_REQUEST['productID']);
    $listingFeeType = null;
    $data = [];
    $editableFlag = false;
    if ($actionType == 'relist') {
        $shopItemData = $shopProductIns->getProductById($paramProdID, true);
        if (!$shopItemData) {
            echo json_encode(['success' => 0, 'msg' => 'You could not relist this item.']);
            exit;
        }
        $listingFeeType = get_secure_integer($_REQUEST['listing_fee_type']);
        if ($listingFeeType === null) {
            $inputValidFlag = false;
        } else {
            //check if you can relist them
            if ($shopProductIns->hasMoneyToListProduct($userID, $listingFeeType)) {
                //Ok you can relist the product
            } else {
                echo json_encode(['success' => 0, 'msg' => 'You could not relist this item. You have no credits or bitcoin.']);
                exit;
            }
        }
        //you can relist this item
        $data['createdDate'] = date('Y-m-d H:i:s');
        if ($shopItemData['userID'] == $userID) {
            $editableFlag = true;
        } else {
            $editableFlag = false;
        }
    } else {
        $shopItemData = $shopProductIns->getProductById($paramProdID, false);
        if ($shopItemData && $shopItemData['userID'] == $userID) {
            $editableFlag = true;
        }
    }
    if ($inputValidFlag) {
        if ($editableFlag) {
            $data['title'] = get_secure_string($_REQUEST['title']);
            $data['subtitle'] = get_secure_string($_REQUEST['subtitle']);
            $data['description'] = get_secure_string($_REQUEST['description']);
            $data['catID'] = get_secure_string($_REQUEST['category']);
            $data['images'] = get_secure_string($_REQUEST['images']);
            $data['locationID'] = get_secure_string($_REQUEST['location']);
            $data['returnPolicy'] = get_secure_string($_REQUEST['return_policy']);
            $data['price'] = get_secure_string($_REQUEST['price']);
            $data['listingDuration'] = get_secure_string($_REQUEST['listing_duration']);
            $data['expiryDate'] = $data['listingDuration'] == -1 ? '0000-00-00 00:00:00' : date('Y-m-d H:i:s', time() + 3600 * 24 * $data['listingDuration']);
            $data['images'] = moveShopTmpImages($data['images']);
            if ($data['images'] === false) {
                echo json_encode(['success' => 0, 'msg' => 'Something goes wrong, please contact administrator.']);
                exit;
            }
            if ($actionType == 'relist') {
                $flag = $shopProductIns->payListingFee($userID, $paramProdID, $listingFeeType);
                if (!$flag) {
                    echo json_encode(['success' => 0, 'msg' => 'You could not relist this item. You have no credits or bitcoin.']);
                    exit;
                }
            }
            if ($category['isDownloadable'] == 1 && !empty($_REQUEST['filename'])) {
                if (!$_REQUEST['filename'] || file_exists(DIR_FS_SHOP_IMG_TMP . $_REQUEST['filename'])) {
                    echo json_encode(['success' => 0, 'msg' => 'Please select a zip file.']);
                    exit;
                }
                $data['isDownloadable'] = 1;
                $filename = moveShopTmpProduct($_REQUEST['filename']);
                //Remove Old File
                @unlink(DIR_FS_SHOP_PRODUCTS . $shopItemData['fileName']);
                $data['fileName'] = $filename;
            }
            $shopProductIns->updateProduct($paramProdID, $data);
            $shopProductIns->updateShippingPrice($paramProdID, $shippingPriceList);
            echo json_encode(['success' => 1, 'msg' => 'An item has been updated successfully.']);
        } else {
            echo json_encode(['success' => 0, 'msg' => "You don't have permission."]);
        }
    } else {
        //error
        echo json_encode(['success' => 0, 'msg' => 'Please input required field(s).']);
    }
}
<?php

require dirname(dirname(__FILE__)) . '/includes/bootstrap.php';
$userID = buckys_is_logged_in();
buckys_enqueue_stylesheet('shop.css');
buckys_enqueue_javascript('shop.js');
$TNB_GLOBALS['content'] = 'shop/view';
$TNB_GLOBALS['headerType'] = 'shop';
$paramShopID = get_secure_integer($_REQUEST['id']);
$view = [];
$shopProductIns = new BuckysShopProduct();
$catIns = new BuckysShopCategory();
$countryIns = new BuckysCountry();
$userIns = new BuckysUser();
$shippingInfoIns = new BuckysTradeUser();
$view['product'] = $shopProductIns->getProductById($paramShopID);
$view['myID'] = $userID;
if (!isset($view['product']) || $view['product']['status'] == BuckysShopProduct::STATUS_INACTIVE) {
    buckys_redirect('/shop/index.php', MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
}
//Check if the items owner is active one
$userData = $userIns->getUserData($view['product']['userID']);
if ($userData['status'] == BuckysUser::STATUS_USER_BANNED) {
    buckys_redirect('/shop/index.php', MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
}
//Read more info from DB
$catData = $catIns->getCategoryByID($view['product']['catID']);
$view['product']['categoryName'] = isset($catData) ? $catData['name'] : '';
$countryData = $countryIns->getCountryById($view['product']['locationID']);
$view['product']['locationName'] = isset($countryData) ? $countryData['country_title'] : '';
$view['product']['userInfo'] = $userIns->getUserBasicInfo($view['product']['userID']);
}
buckys_enqueue_stylesheet('uploadify.css');
buckys_enqueue_stylesheet('jquery.Jcrop.css');
buckys_enqueue_stylesheet('shop.css');
buckys_enqueue_javascript('uploadify/jquery.uploadify.js');
buckys_enqueue_javascript('jquery.Jcrop.js');
buckys_enqueue_javascript('jquery.color.js');
buckys_enqueue_javascript('shop.js');
buckys_enqueue_javascript('shop-edit.js');
buckys_enqueue_javascript('uploadify/flash_install.js');
$TNB_GLOBALS['content'] = 'shop/additem';
$TNB_GLOBALS['headerType'] = 'shop';
$view = [];
$countryIns = new BuckysCountry();
$shopProductIns = new BuckysShopProduct();
$view['category_list'] = BuckysShopCategory::getCategoryList(0);
$view['country_list'] = $countryIns->getCountryList();
$view['action_name'] = 'editProduct';
$paramProdID = get_secure_integer($_REQUEST['id']);
$paramType = get_secure_string($_REQUEST['type']);
$view['product'] = null;
switch ($paramType) {
    case 'relist':
        $userInfo = BuckysUser::getUserBasicInfo($userID);
        $view['my_bitcoin_balance'] = BuckysBitcoin::getUserWalletBalance($userID);
        $view['my_credit_balance'] = $userInfo['credits'];
        $view['product'] = $shopProductIns->getProductById($paramProdID, true);
        $view['type'] = 'relist';
        $view['page_title'] = 'Relist an Item';
        break;
    default:
if (!$bitcoinInfo) {
    $bitcoinInfo = BuckysBitcoin::createWallet($TNB_GLOBALS['user']['userID'], $TNB_GLOBALS['user']['email']);
}
buckys_enqueue_stylesheet('uploadify.css');
buckys_enqueue_stylesheet('jquery.Jcrop.css');
buckys_enqueue_stylesheet('shop.css');
buckys_enqueue_javascript('uploadify/jquery.uploadify.js');
buckys_enqueue_javascript('jquery.Jcrop.js');
buckys_enqueue_javascript('jquery.color.js');
buckys_enqueue_javascript('shop.js');
buckys_enqueue_javascript('shop-edit.js');
buckys_enqueue_javascript('uploadify/flash_install.js');
$TNB_GLOBALS['content'] = 'shop/additem';
$TNB_GLOBALS['headerType'] = 'shop';
$view = [];
$shopCatIns = new BuckysShopCategory();
$countryIns = new BuckysCountry();
$view['no_cash'] = false;
$userInfo = BuckysUser::getUserBasicInfo($userID);
$view['category_list'] = $shopCatIns->getCategoryList(0);
$view['country_list'] = $countryIns->getCountryList();
$view['action_name'] = 'addShopProduct';
$view['page_title'] = 'Sell an Item';
$view['type'] = 'additem';
$view['my_bitcoin_balance'] = BuckysBitcoin::getUserWalletBalance($userID);
$view['my_credit_balance'] = $userInfo['credits'];
$view['shipping_fee_list'] = [];
if ($view['my_bitcoin_balance'] < SHOP_PRODUCT_LISTING_FEE_IN_BTC && $view['my_credit_balance'] < SHOP_PRODUCT_LISTING_FEE_IN_CREDIT) {
    $view['no_cash'] = true;
}
$TNB_GLOBALS['title'] = 'Sell an Item - BuckysRoomShop';