BxDolStudioStore.php 14 KB

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