BxDolStudioStore.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 $iClient;
  16. protected $sClientKey;
  17. protected $sStoreDataUrlPublic;
  18. protected $bAuthAccessUpdates;
  19. function __construct($sPage = "")
  20. {
  21. parent::__construct('store');
  22. $this->oDb = new BxDolStudioStoreQuery();
  23. $this->sStoreDataUrlPublic = BxDolStudioInstallerUtils::getInstance()->getStoreDataUrl();
  24. $this->bAuthAccessUpdates = false;
  25. $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
  26. if(is_string($sPage) && !empty($sPage))
  27. $this->sPage = $sPage;
  28. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  29. if(!empty($this->iClient))
  30. $this->sClientKey = $this->oDb->getParam('sys_oauth_key');
  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. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('popup_chain.html', array(
  129. 'html_id' => 'mod_action_result',
  130. 'bx_repeat:items' => array(array(
  131. 'bx_if:show_as_hidden' => array(
  132. 'condition' => false,
  133. 'content' => array(),
  134. ),
  135. 'item' => $aResult['message'],
  136. 'bx_if:show_previous' => array(
  137. 'condition' => false,
  138. 'content' => array(
  139. 'onclick_previous' => ''
  140. )
  141. ),
  142. 'onclick_close' => '$(this).parents(\'.bx-popup-applied:visible:first\').dolPopupHide();'
  143. ))
  144. ));
  145. echo json_encode($aResult);
  146. exit;
  147. }
  148. }
  149. protected function loadGoodies()
  150. {
  151. $iPerPage = 6;
  152. $aProducts = array();
  153. $sJsObject = $this->getPageJsObject();
  154. $oJson = BxDolStudioJson::getInstance();
  155. // Load featured
  156. $aProducts[] = array(
  157. 'caption' => '_adm_block_cpt_last_featured',
  158. 'actions' => array(
  159. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  160. ),
  161. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  162. );
  163. // Load modules
  164. $aProducts[] = array(
  165. 'caption' => '_adm_block_cpt_last_extensions',
  166. 'actions' => array(
  167. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_extensions', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('extensions', this)")
  168. ),
  169. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  170. );
  171. // Load templates
  172. $aProducts[] = array(
  173. 'caption' => '_adm_block_cpt_last_templates',
  174. 'actions' => array(
  175. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  176. ),
  177. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  178. );
  179. // Load languages
  180. $aProducts[] = array(
  181. 'caption' => '_adm_block_cpt_last_translations',
  182. 'actions' => array(
  183. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_translations', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('translations', this)")
  184. ),
  185. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  186. );
  187. return $aProducts;
  188. }
  189. protected function loadFeatured($iStart, $iPerPage)
  190. {
  191. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  192. }
  193. protected function loadCategory($sCategory, $iStart, $iPerPage)
  194. {
  195. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => $sCategory, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  196. }
  197. protected function loadTag($sTag, $iStart, $iPerPage)
  198. {
  199. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_tag', array('value' => $sTag, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  200. }
  201. protected function loadSearch($sKeyword, $iStart, $iPerPage)
  202. {
  203. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_keyword', array('value' => $sKeyword, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  204. }
  205. protected function loadPurchases()
  206. {
  207. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  208. }
  209. protected function loadUpdates()
  210. {
  211. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  212. }
  213. protected function loadCheckout()
  214. {
  215. $oJson = BxDolStudioJson::getInstance();
  216. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  217. $aResult = array();
  218. foreach($aVendors as $sVendor => $aItems) {
  219. $aIds = $aCounts = array();
  220. foreach($aItems as $aItem) {
  221. $aIds[] = $aItem['item_id'];
  222. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  223. }
  224. $aProducts = $oJson->load($this->sStoreDataUrlPublic . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  225. if(!empty($aProducts))
  226. $aResult[$sVendor] = array(
  227. 'ids' => $aIds,
  228. 'counts' => $aCounts,
  229. 'products' => $aProducts
  230. );
  231. }
  232. return $aResult;
  233. }
  234. protected function loadDownloaded()
  235. {
  236. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  237. return array(
  238. 'modules' => $oInstallerUtils->getModules(),
  239. 'updates' => $oInstallerUtils->getUpdates()
  240. );
  241. }
  242. protected function loadProduct($sModuleName)
  243. {
  244. $oJson = BxDolStudioJson::getInstance();
  245. return $oJson->load($this->sStoreDataUrlPublic . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient, 'key' => $this->sClientKey));
  246. }
  247. /*
  248. * Load package (module, update) using OAuth authorization.
  249. */
  250. protected function loadFile($iFileId)
  251. {
  252. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  253. }
  254. /*
  255. * Load update's package publicly.
  256. */
  257. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  258. {
  259. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  260. }
  261. private function checkoutCart($iVendor)
  262. {
  263. $oCart = BxDolStudioCart::getInstance();
  264. $aItems = $oCart->getByVendor($iVendor);
  265. if(empty($aItems) || !is_array($aItems))
  266. return false;
  267. $aIds = array();
  268. foreach($aItems as $aItem)
  269. $aIds[] = $aItem['item_id'];
  270. $sSid = bx_site_hash();
  271. return $this->sStoreDataUrlPublic . 'purchase/' . $iVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  272. }
  273. private function subscribe($iVendor, $iItem)
  274. {
  275. $sSid = bx_site_hash();
  276. return $this->sStoreDataUrlPublic . 'subscribe/' . $iVendor . '/'. $iItem . '?sid=' . $sSid;
  277. }
  278. }
  279. /** @} */