BxDolStudioStore.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. bx_import('BxDolStudioInstallerUtils');
  10. define('BX_DOL_STUDIO_STR_TYPE_DEFAULT', 'downloaded');
  11. class BxDolStudioStore extends BxTemplStudioPage
  12. {
  13. protected $sPage;
  14. protected $aContent;
  15. protected $sBaseUrl;
  16. protected $iClient;
  17. protected $sClientKey;
  18. protected $sStoreDataUrlPublic;
  19. protected $bAuthAccessUpdates;
  20. function __construct($sPage = "")
  21. {
  22. parent::__construct('store');
  23. $this->oDb = new BxDolStudioStoreQuery();
  24. $this->sBaseUrl = BX_DOL_URL_STUDIO . 'store.php';
  25. $this->sStoreDataUrlPublic = BxDolStudioInstallerUtils::getInstance()->getStoreDataUrl();
  26. $this->bAuthAccessUpdates = false;
  27. $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
  28. if(is_string($sPage) && !empty($sPage))
  29. $this->sPage = $sPage;
  30. $this->initClient();
  31. //--- Check actions ---//
  32. if(($sAction = bx_get('str_action')) !== false) {
  33. $sAction = bx_process_input($sAction);
  34. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_mod_err_cannot_process_action'));
  35. switch($sAction) {
  36. case 'get-file':
  37. $iFileId = (int)bx_get('str_id');
  38. $aResult = $this->getFile($iFileId);
  39. break;
  40. case 'get-product':
  41. $sModuleName = bx_process_input(bx_get('str_id'));
  42. $aResult = $this->getProduct($sModuleName);
  43. break;
  44. case 'get-update':
  45. $sModuleName = bx_process_input(bx_get('str_id'));
  46. $aResult = $this->getUpdate($sModuleName);
  47. break;
  48. case 'get-update-and-install':
  49. $sModuleName = bx_process_input(bx_get('str_id'));
  50. $aResult = $this->getUpdate($sModuleName, true);
  51. break;
  52. case 'get-products-by-type':
  53. $this->sPage = bx_process_input(bx_get('str_value'));
  54. $sContent = $this->getPageCode();
  55. if(!empty($sContent))
  56. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  57. else
  58. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  59. break;
  60. case 'get-products-by-page':
  61. $this->sPage = bx_process_input(bx_get('str_type'));
  62. $sContent = $this->getPageContent();
  63. if(!empty($sContent))
  64. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  65. else
  66. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  67. break;
  68. case 'add-to-cart':
  69. $iVendor = (int)bx_get('str_vendor');
  70. $iItem = (int)bx_get('str_item');
  71. $iItemCount = 1;
  72. if(empty($iVendor) || empty($iItem)) {
  73. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
  74. break;
  75. }
  76. BxDolStudioCart::getInstance()->add($iVendor, $iItem, $iItemCount);
  77. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
  78. break;
  79. case 'delete-from-cart':
  80. $iVendor = (int)bx_get('str_vendor');
  81. $iItem = (int)bx_get('str_item');
  82. if(empty($iVendor)) {
  83. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
  84. break;
  85. }
  86. BxDolStudioCart::getInstance()->delete($iVendor, $iItem);
  87. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '');
  88. break;
  89. case 'checkout-cart':
  90. $iVendor = (int)bx_get('str_vendor');
  91. if(empty($iVendor)) {
  92. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
  93. break;
  94. }
  95. $sLocation = $this->checkoutCart($iVendor);
  96. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
  97. break;
  98. case 'subscribe':
  99. $iVendor = (int)bx_get('str_vendor');
  100. $iItem = (int)bx_get('str_item');
  101. if(empty($iVendor) || empty($iItem)) {
  102. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_subscribe'));
  103. break;
  104. }
  105. $sLocation = $this->subscribe($iVendor, $iItem);
  106. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
  107. break;
  108. case 'install':
  109. $sValue = bx_process_input(bx_get('str_value'));
  110. if(empty($sValue))
  111. break;
  112. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true, 'html_response' => true));
  113. break;
  114. case 'update':
  115. $sValue = bx_process_input(bx_get('str_value'));
  116. if(empty($sValue))
  117. break;
  118. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update', array('html_response' => true));
  119. break;
  120. case 'delete':
  121. $sValue = bx_process_input(bx_get('str_value'));
  122. if(empty($sValue))
  123. break;
  124. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete', array('html_response' => true));
  125. break;
  126. }
  127. if(!empty($aResult['message'])) {
  128. $oTemplate = BxDolStudioTemplate::getInstance();
  129. $aResult['message'] = $oTemplate->parseHtmlByName('popup_chain.html', array(
  130. 'html_id' => 'mod_action_result',
  131. 'bx_repeat:items' => array(array(
  132. 'bx_if:show_as_hidden' => array(
  133. 'condition' => false,
  134. 'content' => array(),
  135. ),
  136. 'item' => $oTemplate->parseHtmlByName('str_notification.html', array(
  137. 'content' => $aResult['message']
  138. )),
  139. 'bx_if:show_previous' => array(
  140. 'condition' => false,
  141. 'content' => array(
  142. 'onclick_previous' => ''
  143. )
  144. ),
  145. 'bx_if:show_close' => array(
  146. 'condition' => false,
  147. 'content' => array(
  148. 'onclick_close' => ''
  149. )
  150. )
  151. ))
  152. ));
  153. }
  154. echo json_encode($aResult);
  155. exit;
  156. }
  157. }
  158. protected function initClient()
  159. {
  160. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  161. if(!empty($this->iClient))
  162. $this->sClientKey = $this->oDb->getParam('sys_oauth_key');
  163. }
  164. protected function authorizeClient()
  165. {
  166. $mixedResult = BxDolStudioInstallerUtils::getInstance()->getAccessObject(true)->doAuthorize();
  167. if($mixedResult === true)
  168. $this->initClient();
  169. return $mixedResult;
  170. }
  171. protected function loadGoodies()
  172. {
  173. $iPerPage = 3;
  174. $aProducts = array();
  175. $oJson = BxDolStudioJson::getInstance();
  176. // Load Featured
  177. $aProducts[] = array(
  178. 'caption' => '_adm_block_cpt_featured',
  179. 'actions' => array(
  180. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('featured'))
  181. ),
  182. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  183. );
  184. // Load Latest
  185. $aProducts[] = array(
  186. 'caption' => '_adm_block_cpt_latest',
  187. 'actions' => array(
  188. array('name' => 'latest', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('latest'))
  189. ),
  190. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  191. );
  192. // Load Popular
  193. $aProducts[] = array(
  194. 'caption' => '_adm_block_cpt_popular',
  195. 'actions' => array(
  196. array('name' => 'popular', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('popular'))
  197. ),
  198. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  199. );
  200. return $aProducts;
  201. }
  202. protected function loadLatest($iStart, $iPerPage)
  203. {
  204. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  205. }
  206. protected function loadFeatured($iStart, $iPerPage)
  207. {
  208. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  209. }
  210. protected function loadPopular($iStart, $iPerPage)
  211. {
  212. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  213. }
  214. protected function loadCategories()
  215. {
  216. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_categories', array('client' => $this->iClient, 'key' => $this->sClientKey));
  217. }
  218. protected function loadCategory($iCategory, $iStart, $iPerPage)
  219. {
  220. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => $iCategory, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  221. }
  222. protected function loadTags()
  223. {
  224. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_tags', array('client' => $this->iClient, 'key' => $this->sClientKey));
  225. }
  226. protected function loadTag($sTag, $iStart, $iPerPage)
  227. {
  228. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_tag', array('value' => $sTag, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  229. }
  230. protected function loadSearch($sKeyword, $iStart, $iPerPage)
  231. {
  232. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_keyword', array('value' => $sKeyword, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  233. }
  234. protected function loadPurchases()
  235. {
  236. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  237. }
  238. protected function loadUpdates()
  239. {
  240. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  241. }
  242. protected function loadCheckout()
  243. {
  244. $oJson = BxDolStudioJson::getInstance();
  245. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  246. $aResult = array();
  247. foreach($aVendors as $sVendor => $aItems) {
  248. $aIds = $aCounts = array();
  249. foreach($aItems as $aItem) {
  250. $aIds[] = $aItem['item_id'];
  251. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  252. }
  253. $aProducts = $oJson->load($this->sStoreDataUrlPublic . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  254. if(!empty($aProducts))
  255. $aResult[$sVendor] = array(
  256. 'ids' => $aIds,
  257. 'counts' => $aCounts,
  258. 'products' => $aProducts
  259. );
  260. }
  261. return $aResult;
  262. }
  263. protected function loadDownloaded()
  264. {
  265. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  266. return array(
  267. 'modules' => $oInstallerUtils->getModules(),
  268. 'updates' => $oInstallerUtils->getUpdates()
  269. );
  270. }
  271. protected function loadProduct($sModuleName)
  272. {
  273. $oJson = BxDolStudioJson::getInstance();
  274. return $oJson->load($this->sStoreDataUrlPublic . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient, 'key' => $this->sClientKey));
  275. }
  276. /*
  277. * Load package (module, update) using OAuth authorization.
  278. */
  279. protected function loadFile($iFileId)
  280. {
  281. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  282. }
  283. /*
  284. * Load update's package publicly.
  285. */
  286. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  287. {
  288. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  289. }
  290. protected function getBaseUrl($mixedParams = array())
  291. {
  292. if(is_string($mixedParams))
  293. $mixedParams = array('page' => $mixedParams);
  294. return bx_append_url_params($this->sBaseUrl, $mixedParams);
  295. }
  296. private function checkoutCart($iVendor)
  297. {
  298. $oCart = BxDolStudioCart::getInstance();
  299. $aItems = $oCart->getByVendor($iVendor);
  300. if(empty($aItems) || !is_array($aItems))
  301. return false;
  302. $aIds = array();
  303. foreach($aItems as $aItem)
  304. $aIds[] = $aItem['item_id'];
  305. $sSid = bx_site_hash();
  306. return $this->sStoreDataUrlPublic . 'purchase/' . $iVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  307. }
  308. private function subscribe($iVendor, $iItem)
  309. {
  310. $sSid = bx_site_hash();
  311. return $this->sStoreDataUrlPublic . 'subscribe/' . $iVendor . '/'. $iItem . '?sid=' . $sSid;
  312. }
  313. }
  314. /** @} */