BxDolStudioStore.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. $oTemplate = BxDolStudioTemplate::getInstance();
  131. $aResult['message'] = $oTemplate->parseHtmlByName('popup_chain.html', array(
  132. 'html_id' => 'mod_action_result',
  133. 'bx_repeat:items' => array(array(
  134. 'bx_if:show_as_hidden' => array(
  135. 'condition' => false,
  136. 'content' => array(),
  137. ),
  138. 'item' => $oTemplate->parseHtmlByName('str_notification.html', array(
  139. 'content' => $aResult['message']
  140. )),
  141. 'bx_if:show_previous' => array(
  142. 'condition' => false,
  143. 'content' => array(
  144. 'onclick_previous' => ''
  145. )
  146. ),
  147. 'bx_if:show_close' => array(
  148. 'condition' => false,
  149. 'content' => array(
  150. 'onclick_close' => ''
  151. )
  152. )
  153. ))
  154. ));
  155. }
  156. echo json_encode($aResult);
  157. exit;
  158. }
  159. }
  160. protected function loadGoodies()
  161. {
  162. $iPerPage = 3;
  163. $aProducts = array();
  164. $oJson = BxDolStudioJson::getInstance();
  165. // Load Featured
  166. $aProducts[] = array(
  167. 'caption' => '_adm_block_cpt_featured',
  168. 'actions' => array(
  169. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('featured'))
  170. ),
  171. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  172. );
  173. // Load Latest
  174. $aProducts[] = array(
  175. 'caption' => '_adm_block_cpt_latest',
  176. 'actions' => array(
  177. array('name' => 'latest', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('latest'))
  178. ),
  179. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  180. );
  181. // Load Popular
  182. $aProducts[] = array(
  183. 'caption' => '_adm_block_cpt_popular',
  184. 'actions' => array(
  185. array('name' => 'popular', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('popular'))
  186. ),
  187. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  188. );
  189. return $aProducts;
  190. }
  191. protected function loadLatest($iStart, $iPerPage)
  192. {
  193. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  194. }
  195. protected function loadFeatured($iStart, $iPerPage)
  196. {
  197. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  198. }
  199. protected function loadPopular($iStart, $iPerPage)
  200. {
  201. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  202. }
  203. protected function loadCategories()
  204. {
  205. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_categories', array('client' => $this->iClient, 'key' => $this->sClientKey));
  206. }
  207. protected function loadCategory($iCategory, $iStart, $iPerPage)
  208. {
  209. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => $iCategory, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  210. }
  211. protected function loadTags()
  212. {
  213. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_tags', array('client' => $this->iClient, 'key' => $this->sClientKey));
  214. }
  215. protected function loadTag($sTag, $iStart, $iPerPage)
  216. {
  217. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_tag', array('value' => $sTag, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  218. }
  219. protected function loadSearch($sKeyword, $iStart, $iPerPage)
  220. {
  221. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_keyword', array('value' => $sKeyword, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  222. }
  223. protected function loadPurchases()
  224. {
  225. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  226. }
  227. protected function loadUpdates()
  228. {
  229. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  230. }
  231. protected function loadCheckout()
  232. {
  233. $oJson = BxDolStudioJson::getInstance();
  234. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  235. $aResult = array();
  236. foreach($aVendors as $sVendor => $aItems) {
  237. $aIds = $aCounts = array();
  238. foreach($aItems as $aItem) {
  239. $aIds[] = $aItem['item_id'];
  240. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  241. }
  242. $aProducts = $oJson->load($this->sStoreDataUrlPublic . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  243. if(!empty($aProducts))
  244. $aResult[$sVendor] = array(
  245. 'ids' => $aIds,
  246. 'counts' => $aCounts,
  247. 'products' => $aProducts
  248. );
  249. }
  250. return $aResult;
  251. }
  252. protected function loadDownloaded()
  253. {
  254. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  255. return array(
  256. 'modules' => $oInstallerUtils->getModules(),
  257. 'updates' => $oInstallerUtils->getUpdates()
  258. );
  259. }
  260. protected function loadProduct($sModuleName)
  261. {
  262. $oJson = BxDolStudioJson::getInstance();
  263. return $oJson->load($this->sStoreDataUrlPublic . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient, 'key' => $this->sClientKey));
  264. }
  265. /*
  266. * Load package (module, update) using OAuth authorization.
  267. */
  268. protected function loadFile($iFileId)
  269. {
  270. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  271. }
  272. /*
  273. * Load update's package publicly.
  274. */
  275. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  276. {
  277. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  278. }
  279. protected function getBaseUrl($mixedParams = array())
  280. {
  281. if(is_string($mixedParams))
  282. $mixedParams = array('page' => $mixedParams);
  283. return bx_append_url_params($this->sBaseUrl, $mixedParams);
  284. }
  285. private function checkoutCart($iVendor)
  286. {
  287. $oCart = BxDolStudioCart::getInstance();
  288. $aItems = $oCart->getByVendor($iVendor);
  289. if(empty($aItems) || !is_array($aItems))
  290. return false;
  291. $aIds = array();
  292. foreach($aItems as $aItem)
  293. $aIds[] = $aItem['item_id'];
  294. $sSid = bx_site_hash();
  295. return $this->sStoreDataUrlPublic . 'purchase/' . $iVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  296. }
  297. private function subscribe($iVendor, $iItem)
  298. {
  299. $sSid = bx_site_hash();
  300. return $this->sStoreDataUrlPublic . 'subscribe/' . $iVendor . '/'. $iItem . '?sid=' . $sSid;
  301. }
  302. }
  303. /** @} */