| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaStudio UNA Studio
- * @{
- */
- bx_import('BxDolStudioInstallerUtils');
- define('BX_DOL_STUDIO_STR_TYPE_DEFAULT', 'downloaded');
- class BxDolStudioStore extends BxTemplStudioWidget
- {
- protected $sPage;
- protected $aContent;
- protected $sBaseUrl;
- protected $iClient;
- protected $sClientKey;
- protected $sStoreDataUrlPublic;
- protected $bAuthAccessUpdates;
- protected $sSessionKeyNonOwnerNotified;
- function __construct($sPage = "")
- {
- parent::__construct('store');
- $this->oDb = new BxDolStudioStoreQuery();
- $this->sBaseUrl = BX_DOL_URL_STUDIO . 'store.php';
- $this->sStoreDataUrlPublic = BxDolStudioInstallerUtils::getInstance()->getStoreDataUrl();
- $this->bAuthAccessUpdates = true;
- $this->sSessionKeyNonOwnerNotified = 'str_non_owner_notified';
- $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
- if(is_string($sPage) && !empty($sPage))
- $this->sPage = $sPage;
- $this->initClient();
- }
- public function checkAction()
- {
- $sAction = bx_get('str_action');
- if($sAction === false)
- return false;
- $sAction = bx_process_input($sAction);
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_mod_err_cannot_process_action'));
- switch($sAction) {
- case 'get-file':
- $iFileId = (int)bx_get('str_id');
- $aResult = $this->getFile($iFileId);
- break;
- case 'get-product':
- $sModuleName = bx_process_input(bx_get('str_id'));
- $aResult = $this->getProduct($sModuleName);
- break;
- case 'get-update':
- $sModuleName = bx_process_input(bx_get('str_id'));
- $aResult = $this->getUpdate($sModuleName);
- break;
- case 'get-update-and-install':
- $sModuleName = bx_process_input(bx_get('str_id'));
- $aResult = $this->getUpdate($sModuleName, true);
- break;
- case 'get-products-by-type':
- $this->sPage = bx_process_input(bx_get('str_value'));
- $sContent = $this->getPageCode();
- if(!empty($sContent))
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
- else
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
- break;
- case 'get-products-by-page':
- $this->sPage = bx_process_input(bx_get('str_type'));
- $sContent = $this->getPageContent();
- if(!empty($sContent))
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
- else
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
- break;
- case 'add-to-cart':
- $iVendor = (int)bx_get('str_vendor');
- $iItem = (int)bx_get('str_item');
- $iItemCount = 1;
- if(empty($iVendor) || empty($iItem)) {
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
- break;
- }
- BxDolStudioCart::getInstance()->add($iVendor, $iItem, $iItemCount);
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
- break;
- case 'delete-from-cart':
- $iVendor = (int)bx_get('str_vendor');
- $iItem = (int)bx_get('str_item');
- if(empty($iVendor)) {
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
- break;
- }
- BxDolStudioCart::getInstance()->delete($iVendor, $iItem);
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '');
- break;
- case 'checkout-cart':
- $iVendor = (int)bx_get('str_vendor');
- if(empty($iVendor)) {
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
- break;
- }
- $sLocation = $this->checkoutCart($iVendor);
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
- break;
- case 'subscribe':
- $iVendor = (int)bx_get('str_vendor');
- $iItem = (int)bx_get('str_item');
- if(empty($iVendor) || empty($iItem)) {
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_subscribe'));
- break;
- }
- $sLocation = $this->subscribe($iVendor, $iItem);
- $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
- break;
- case 'install':
- $sValue = bx_process_input(bx_get('str_value'));
- if(empty($sValue))
- break;
- $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true, 'html_response' => true));
- break;
- case 'update':
- $sValue = bx_process_input(bx_get('str_value'));
- if(empty($sValue))
- break;
- $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update', array('html_response' => true));
- break;
- case 'delete':
- $sValue = bx_process_input(bx_get('str_value'));
- if(empty($sValue))
- break;
- $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete', array('html_response' => true));
- break;
- }
- if(!empty($aResult['message'])) {
- $oTemplate = BxDolStudioTemplate::getInstance();
- $aResult['message'] = $oTemplate->parseHtmlByName('popup_chain.html', array(
- 'html_id' => 'mod_action_result',
- 'bx_repeat:items' => array(array(
- 'bx_if:show_as_hidden' => array(
- 'condition' => false,
- 'content' => array(),
- ),
- 'item' => $oTemplate->parseHtmlByName('str_notification.html', array(
- 'content' => $aResult['message']
- )),
- 'bx_if:show_previous' => array(
- 'condition' => false,
- 'content' => array(
- 'onclick_previous' => ''
- )
- ),
- 'bx_if:show_close' => array(
- 'condition' => false,
- 'content' => array(
- 'onclick_close' => ''
- )
- )
- ))
- ));
- }
- return $aResult;
- }
-
-
- protected function initClient()
- {
- $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
- if(!empty($this->iClient))
- $this->sClientKey = $this->oDb->getParam('sys_oauth_key');
- }
- protected function authorizeClient()
- {
- $mixedResult = BxDolStudioInstallerUtils::getInstance()->getAccessObject(true)->doAuthorize();
- if($mixedResult === true)
- $this->initClient();
- return $mixedResult;
- }
- protected function loadGoodies()
- {
- $iPerPage = 4;
- $aProducts = array();
- $sVersion = bx_get_ver();
- $oJson = BxDolStudioJson::getInstance();
- // Load Featured
- $aProducts[] = array(
- 'caption' => '_adm_block_cpt_featured',
- 'actions' => array(
- array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('featured'))
- ),
- 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey, 'version' => $sVersion))
- );
- // Load Latest
- $aProducts[] = array(
- 'caption' => '_adm_block_cpt_latest',
- 'actions' => array(
- array('name' => 'latest', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('latest'))
- ),
- 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey, 'version' => $sVersion))
- );
- // Load Popular
- $aProducts[] = array(
- 'caption' => '_adm_block_cpt_popular',
- 'actions' => array(
- array('name' => 'popular', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('popular'))
- ),
- 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey, 'version' => $sVersion))
- );
- return $aProducts;
- }
- protected function loadLatest($iStart, $iPerPage)
- {
- $sVersion = bx_get_ver();
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_latest', array(
- 'start' => $iStart,
- 'per_page' => $iPerPage,
- 'client' => $this->iClient,
- 'key' => $this->sClientKey,
- 'version' => $sVersion
- ));
- }
- protected function loadFeatured($iStart, $iPerPage)
- {
- $sVersion = bx_get_ver();
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_featured', array(
- 'start' => $iStart,
- 'per_page' => $iPerPage,
- 'client' => $this->iClient,
- 'key' => $this->sClientKey,
- 'version' => $sVersion
- ));
- }
- protected function loadPopular($iStart, $iPerPage)
- {
- $sVersion = bx_get_ver();
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_popular', array(
- 'start' => $iStart,
- 'per_page' => $iPerPage,
- 'client' => $this->iClient,
- 'key' => $this->sClientKey,
- 'version' => $sVersion
- ));
- }
- protected function loadCategories()
- {
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_categories', array(
- 'client' => $this->iClient,
- 'key' => $this->sClientKey
- ));
- }
- protected function loadCategory($iCategory, $iStart, $iPerPage)
- {
- $sVersion = bx_get_ver();
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array(
- 'value' => $iCategory,
- 'start' => $iStart,
- 'per_page' => $iPerPage,
- 'client' => $this->iClient,
- 'key' => $this->sClientKey,
- 'version' => $sVersion
- ));
- }
- protected function loadTags()
- {
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_tags', array(
- 'client' => $this->iClient,
- 'key' => $this->sClientKey
- ));
- }
- protected function loadTag($sTag, $iStart, $iPerPage)
- {
- $sVersion = bx_get_ver();
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_tag', array(
- 'value' => $sTag,
- 'start' => $iStart,
- 'per_page' => $iPerPage,
- 'client' => $this->iClient,
- 'key' => $this->sClientKey,
- 'version' => $sVersion
- ));
- }
- protected function loadSearch($sKeyword, $iStart, $iPerPage)
- {
- $sVersion = bx_get_ver();
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_keyword', array(
- 'value' => $sKeyword,
- 'start' => $iStart,
- 'per_page' => $iPerPage,
- 'client' => $this->iClient,
- 'key' => $this->sClientKey,
- 'version' => $sVersion
- ));
- }
- protected function loadPurchases()
- {
- return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
- }
- protected function loadUpdates()
- {
- return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
- }
- protected function loadCheckout()
- {
- $oJson = BxDolStudioJson::getInstance();
- $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
- $aResult = array();
- foreach($aVendors as $sVendor => $aItems) {
- $aIds = $aCounts = array();
- foreach($aItems as $aItem) {
- $aIds[] = $aItem['item_id'];
- $aCounts[$aItem['item_id']] = $aItem['item_count'];
- }
- $aProducts = $oJson->load($this->sStoreDataUrlPublic . 'json_browse_selected', array('products' => base64_encode(json_encode($aIds)), 'client' => $this->iClient, 'key' => $this->sClientKey));
- if(!empty($aProducts))
- $aResult[$sVendor] = array(
- 'ids' => $aIds,
- 'counts' => $aCounts,
- 'products' => $aProducts
- );
- }
- return $aResult;
- }
- protected function loadDownloaded()
- {
- $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
- return array(
- 'modules' => $oInstallerUtils->getModules(),
- 'updates' => $oInstallerUtils->getUpdates()
- );
- }
- protected function loadProduct($sModuleName)
- {
- $sVersion = bx_get_ver();
- return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_get_product_by_name', array(
- 'value' => $sModuleName,
- 'client' => $this->iClient,
- 'key' => $this->sClientKey,
- 'version' => $sVersion
- ));
- }
- protected function loadLocalProduct($sModuleName)
- {
- $aModules = BxDolStudioInstallerUtils::getInstance()->getModules(false);
- return isset($aModules[$sModuleName]) && is_array($aModules[$sModuleName])
- ? $aModules[$sModuleName]
- : array();
- }
- /*
- * Load package (module, update) using OAuth authorization.
- */
- protected function loadFile($iFileId)
- {
- return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
- }
- /*
- * Load update's package publicly.
- */
- protected function loadUpdate($sModuleName, $bApplyUpdate = false)
- {
- return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bApplyUpdate);
- }
- protected function getBaseUrl($mixedParams = array())
- {
- if(is_string($mixedParams))
- $mixedParams = array('page' => $mixedParams);
- return bx_append_url_params($this->sBaseUrl, $mixedParams);
- }
- private function checkoutCart($iVendor)
- {
- $oCart = BxDolStudioCart::getInstance();
- $aItems = $oCart->getByVendor($iVendor);
- if(empty($aItems) || !is_array($aItems))
- return false;
- $aIds = array();
- foreach($aItems as $aItem)
- $aIds[] = $aItem['item_id'];
- $sSid = bx_site_hash();
- return $this->sStoreDataUrlPublic . 'purchase/' . $iVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
- }
- private function subscribe($iVendor, $iItem)
- {
- $sSid = bx_site_hash();
- return $this->sStoreDataUrlPublic . 'subscribe/' . $iVendor . '/'. $iItem . '?sid=' . $sSid;
- }
- }
- /** @} */
|