BxDolStudioStore.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. protected $sSessionKeyNonOwnerNotified;
  21. function __construct($sPage = "")
  22. {
  23. parent::__construct('store');
  24. $this->oDb = new BxDolStudioStoreQuery();
  25. $this->sBaseUrl = BX_DOL_URL_STUDIO . 'store.php';
  26. $this->sStoreDataUrlPublic = BxDolStudioInstallerUtils::getInstance()->getStoreDataUrl();
  27. $this->bAuthAccessUpdates = false;
  28. $this->sSessionKeyNonOwnerNotified = 'str_non_owner_notified';
  29. $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
  30. if(is_string($sPage) && !empty($sPage))
  31. $this->sPage = $sPage;
  32. $this->initClient();
  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 initClient()
  161. {
  162. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  163. if(!empty($this->iClient))
  164. $this->sClientKey = $this->oDb->getParam('sys_oauth_key');
  165. }
  166. protected function authorizeClient()
  167. {
  168. $mixedResult = BxDolStudioInstallerUtils::getInstance()->getAccessObject(true)->doAuthorize();
  169. if($mixedResult === true)
  170. $this->initClient();
  171. return $mixedResult;
  172. }
  173. protected function loadGoodies()
  174. {
  175. $iPerPage = 3;
  176. $aProducts = array();
  177. $oJson = BxDolStudioJson::getInstance();
  178. // Load Featured
  179. $aProducts[] = array(
  180. 'caption' => '_adm_block_cpt_featured',
  181. 'actions' => array(
  182. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('featured'))
  183. ),
  184. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  185. );
  186. // Load Latest
  187. $aProducts[] = array(
  188. 'caption' => '_adm_block_cpt_latest',
  189. 'actions' => array(
  190. array('name' => 'latest', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('latest'))
  191. ),
  192. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  193. );
  194. // Load Popular
  195. $aProducts[] = array(
  196. 'caption' => '_adm_block_cpt_popular',
  197. 'actions' => array(
  198. array('name' => 'popular', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('popular'))
  199. ),
  200. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  201. );
  202. return $aProducts;
  203. }
  204. protected function loadLatest($iStart, $iPerPage)
  205. {
  206. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  207. }
  208. protected function loadFeatured($iStart, $iPerPage)
  209. {
  210. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  211. }
  212. protected function loadPopular($iStart, $iPerPage)
  213. {
  214. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  215. }
  216. protected function loadCategories()
  217. {
  218. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_categories', array('client' => $this->iClient, 'key' => $this->sClientKey));
  219. }
  220. protected function loadCategory($iCategory, $iStart, $iPerPage)
  221. {
  222. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array('value' => $iCategory, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  223. }
  224. protected function loadTags()
  225. {
  226. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_tags', array('client' => $this->iClient, 'key' => $this->sClientKey));
  227. }
  228. protected function loadTag($sTag, $iStart, $iPerPage)
  229. {
  230. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_tag', array('value' => $sTag, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  231. }
  232. protected function loadSearch($sKeyword, $iStart, $iPerPage)
  233. {
  234. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_keyword', array('value' => $sKeyword, 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  235. }
  236. protected function loadPurchases()
  237. {
  238. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  239. }
  240. protected function loadUpdates()
  241. {
  242. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  243. }
  244. protected function loadCheckout()
  245. {
  246. $oJson = BxDolStudioJson::getInstance();
  247. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  248. $aResult = array();
  249. foreach($aVendors as $sVendor => $aItems) {
  250. $aIds = $aCounts = array();
  251. foreach($aItems as $aItem) {
  252. $aIds[] = $aItem['item_id'];
  253. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  254. }
  255. $aProducts = $oJson->load($this->sStoreDataUrlPublic . 'json_browse_selected', array('products' => base64_encode(serialize($aIds)), 'client' => $this->iClient, 'key' => $this->sClientKey));
  256. if(!empty($aProducts))
  257. $aResult[$sVendor] = array(
  258. 'ids' => $aIds,
  259. 'counts' => $aCounts,
  260. 'products' => $aProducts
  261. );
  262. }
  263. return $aResult;
  264. }
  265. protected function loadDownloaded()
  266. {
  267. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  268. return array(
  269. 'modules' => $oInstallerUtils->getModules(),
  270. 'updates' => $oInstallerUtils->getUpdates()
  271. );
  272. }
  273. protected function loadProduct($sModuleName)
  274. {
  275. $oJson = BxDolStudioJson::getInstance();
  276. return $oJson->load($this->sStoreDataUrlPublic . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient, 'key' => $this->sClientKey));
  277. }
  278. /*
  279. * Load package (module, update) using OAuth authorization.
  280. */
  281. protected function loadFile($iFileId)
  282. {
  283. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  284. }
  285. /*
  286. * Load update's package publicly.
  287. */
  288. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  289. {
  290. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  291. }
  292. protected function getBaseUrl($mixedParams = array())
  293. {
  294. if(is_string($mixedParams))
  295. $mixedParams = array('page' => $mixedParams);
  296. return bx_append_url_params($this->sBaseUrl, $mixedParams);
  297. }
  298. private function checkoutCart($iVendor)
  299. {
  300. $oCart = BxDolStudioCart::getInstance();
  301. $aItems = $oCart->getByVendor($iVendor);
  302. if(empty($aItems) || !is_array($aItems))
  303. return false;
  304. $aIds = array();
  305. foreach($aItems as $aItem)
  306. $aIds[] = $aItem['item_id'];
  307. $sSid = bx_site_hash();
  308. return $this->sStoreDataUrlPublic . 'purchase/' . $iVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  309. }
  310. private function subscribe($iVendor, $iItem)
  311. {
  312. $sSid = bx_site_hash();
  313. return $this->sStoreDataUrlPublic . 'subscribe/' . $iVendor . '/'. $iItem . '?sid=' . $sSid;
  314. }
  315. }
  316. /** @} */