BxDolStudioStore.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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'] = BxTemplStudioFunctions::getInstance()->inlineBox('', BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result_inline.html', array('content' => $aResult['message'])), true);
  129. echo json_encode($aResult);
  130. exit;
  131. }
  132. }
  133. protected function loadGoodies()
  134. {
  135. $iPerPage = 6;
  136. $aProducts = array();
  137. $sJsObject = $this->getPageJsObject();
  138. $oJson = BxDolStudioJson::getInstance();
  139. /*
  140. //TODO: Uncomment 'featured loading' when "Mark As Featured" functionality will be ready.
  141. // Load featured
  142. $aProducts[] = array(
  143. 'caption' => '_adm_block_cpt_last_featured',
  144. 'actions' => array(
  145. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  146. ),
  147. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  148. );
  149. */
  150. // Load modules
  151. $aProducts[] = array(
  152. 'caption' => '_adm_block_cpt_last_extensions',
  153. 'actions' => array(
  154. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_extensions', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('extensions', this)")
  155. ),
  156. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  157. );
  158. // Load templates
  159. $aProducts[] = array(
  160. 'caption' => '_adm_block_cpt_last_templates',
  161. 'actions' => array(
  162. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  163. ),
  164. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  165. );
  166. // Load languages
  167. $aProducts[] = array(
  168. 'caption' => '_adm_block_cpt_last_translations',
  169. 'actions' => array(
  170. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_translations', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('translations', this)")
  171. ),
  172. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  173. );
  174. return $aProducts;
  175. }
  176. protected function loadFeatured($iStart, $iPerPage)
  177. {
  178. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  179. }
  180. protected function loadCategory($sCategory, $iStart, $iPerPage)
  181. {
  182. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => $sCategory, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  183. }
  184. protected function loadTag($sTag, $iStart, $iPerPage)
  185. {
  186. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_tag', array('value' => $sTag, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  187. }
  188. protected function loadPurchases()
  189. {
  190. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  191. }
  192. protected function loadUpdates()
  193. {
  194. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  195. }
  196. protected function loadCheckout()
  197. {
  198. $oJson = BxDolStudioJson::getInstance();
  199. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  200. $aResult = array();
  201. foreach($aVendors as $sVendor => $aItems) {
  202. $aIds = $aCounts = array();
  203. foreach($aItems as $aItem) {
  204. $aIds[] = $aItem['item_id'];
  205. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  206. }
  207. $aProducts = $oJson->load($this->sStoreDataUrlPublic . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  208. if(!empty($aProducts))
  209. $aResult[$sVendor] = array(
  210. 'ids' => $aIds,
  211. 'counts' => $aCounts,
  212. 'products' => $aProducts
  213. );
  214. }
  215. return $aResult;
  216. }
  217. protected function loadDownloaded()
  218. {
  219. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  220. return array(
  221. 'modules' => $oInstallerUtils->getModules(),
  222. 'updates' => $oInstallerUtils->getUpdates()
  223. );
  224. }
  225. protected function loadProduct($sModuleName)
  226. {
  227. $oJson = BxDolStudioJson::getInstance();
  228. return $oJson->load($this->sStoreDataUrlPublic . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient, 'key' => $this->sClientKey));
  229. }
  230. /*
  231. * Load package (module, update) using OAuth authorization.
  232. */
  233. protected function loadFile($iFileId)
  234. {
  235. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  236. }
  237. /*
  238. * Load update's package publicly.
  239. */
  240. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  241. {
  242. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  243. }
  244. private function checkoutCart($iVendor)
  245. {
  246. $oCart = BxDolStudioCart::getInstance();
  247. $aItems = $oCart->getByVendor($iVendor);
  248. if(empty($aItems) || !is_array($aItems))
  249. return false;
  250. $aIds = array();
  251. foreach($aItems as $aItem)
  252. $aIds[] = $aItem['item_id'];
  253. $sSid = bx_site_hash();
  254. return $this->sStoreDataUrlPublic . 'purchase/' . $iVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  255. }
  256. private function subscribe($iVendor, $iItem)
  257. {
  258. $sSid = bx_site_hash();
  259. return $this->sStoreDataUrlPublic . 'subscribe/' . $iVendor . '/'. $iItem . '?sid=' . $sSid;
  260. }
  261. }
  262. /** @} */