BxDolStudioStore.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 BxTemplStudioWidget
  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 = true;
  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. }
  34. public function checkAction()
  35. {
  36. $sAction = bx_get('str_action');
  37. if($sAction === false)
  38. return false;
  39. $sAction = bx_process_input($sAction);
  40. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_mod_err_cannot_process_action'));
  41. switch($sAction) {
  42. case 'get-file':
  43. $iFileId = (int)bx_get('str_id');
  44. $aResult = $this->getFile($iFileId);
  45. break;
  46. case 'get-product':
  47. $sModuleName = bx_process_input(bx_get('str_id'));
  48. $aResult = $this->getProduct($sModuleName);
  49. break;
  50. case 'get-update':
  51. $sModuleName = bx_process_input(bx_get('str_id'));
  52. $aResult = $this->getUpdate($sModuleName);
  53. break;
  54. case 'get-update-and-install':
  55. $sModuleName = bx_process_input(bx_get('str_id'));
  56. $aResult = $this->getUpdate($sModuleName, true);
  57. break;
  58. case 'get-products-by-type':
  59. $this->sPage = bx_process_input(bx_get('str_value'));
  60. $sContent = $this->getPageCode();
  61. if(!empty($sContent))
  62. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  63. else
  64. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  65. break;
  66. case 'get-products-by-page':
  67. $this->sPage = bx_process_input(bx_get('str_type'));
  68. $sContent = $this->getPageContent();
  69. if(!empty($sContent))
  70. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  71. else
  72. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  73. break;
  74. case 'add-to-cart':
  75. $iVendor = (int)bx_get('str_vendor');
  76. $iItem = (int)bx_get('str_item');
  77. $iItemCount = 1;
  78. if(empty($iVendor) || empty($iItem)) {
  79. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
  80. break;
  81. }
  82. BxDolStudioCart::getInstance()->add($iVendor, $iItem, $iItemCount);
  83. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
  84. break;
  85. case 'delete-from-cart':
  86. $iVendor = (int)bx_get('str_vendor');
  87. $iItem = (int)bx_get('str_item');
  88. if(empty($iVendor)) {
  89. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
  90. break;
  91. }
  92. BxDolStudioCart::getInstance()->delete($iVendor, $iItem);
  93. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '');
  94. break;
  95. case 'checkout-cart':
  96. $iVendor = (int)bx_get('str_vendor');
  97. if(empty($iVendor)) {
  98. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
  99. break;
  100. }
  101. $sLocation = $this->checkoutCart($iVendor);
  102. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
  103. break;
  104. case 'subscribe':
  105. $iVendor = (int)bx_get('str_vendor');
  106. $iItem = (int)bx_get('str_item');
  107. if(empty($iVendor) || empty($iItem)) {
  108. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_subscribe'));
  109. break;
  110. }
  111. $sLocation = $this->subscribe($iVendor, $iItem);
  112. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
  113. break;
  114. case 'install':
  115. $sValue = bx_process_input(bx_get('str_value'));
  116. if(empty($sValue))
  117. break;
  118. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true, 'html_response' => true));
  119. break;
  120. case 'update':
  121. $sValue = bx_process_input(bx_get('str_value'));
  122. if(empty($sValue))
  123. break;
  124. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update', array('html_response' => true));
  125. break;
  126. case 'delete':
  127. $sValue = bx_process_input(bx_get('str_value'));
  128. if(empty($sValue))
  129. break;
  130. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete', array('html_response' => true));
  131. break;
  132. }
  133. if(!empty($aResult['message'])) {
  134. $oTemplate = BxDolStudioTemplate::getInstance();
  135. $aResult['message'] = $oTemplate->parseHtmlByName('popup_chain.html', array(
  136. 'html_id' => 'mod_action_result',
  137. 'bx_repeat:items' => array(array(
  138. 'bx_if:show_as_hidden' => array(
  139. 'condition' => false,
  140. 'content' => array(),
  141. ),
  142. 'item' => $oTemplate->parseHtmlByName('str_notification.html', array(
  143. 'content' => $aResult['message']
  144. )),
  145. 'bx_if:show_previous' => array(
  146. 'condition' => false,
  147. 'content' => array(
  148. 'onclick_previous' => ''
  149. )
  150. ),
  151. 'bx_if:show_close' => array(
  152. 'condition' => false,
  153. 'content' => array(
  154. 'onclick_close' => ''
  155. )
  156. )
  157. ))
  158. ));
  159. }
  160. return $aResult;
  161. }
  162. protected function initClient()
  163. {
  164. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  165. if(!empty($this->iClient))
  166. $this->sClientKey = $this->oDb->getParam('sys_oauth_key');
  167. }
  168. protected function authorizeClient()
  169. {
  170. $mixedResult = BxDolStudioInstallerUtils::getInstance()->getAccessObject(true)->doAuthorize();
  171. if($mixedResult === true)
  172. $this->initClient();
  173. return $mixedResult;
  174. }
  175. protected function loadGoodies()
  176. {
  177. $iPerPage = 4;
  178. $aProducts = array();
  179. $sVersion = bx_get_ver();
  180. $oJson = BxDolStudioJson::getInstance();
  181. // Load Featured
  182. $aProducts[] = array(
  183. 'caption' => '_adm_block_cpt_featured',
  184. 'actions' => array(
  185. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('featured'))
  186. ),
  187. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey, 'version' => $sVersion))
  188. );
  189. // Load Latest
  190. $aProducts[] = array(
  191. 'caption' => '_adm_block_cpt_latest',
  192. 'actions' => array(
  193. array('name' => 'latest', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('latest'))
  194. ),
  195. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_latest', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey, 'version' => $sVersion))
  196. );
  197. // Load Popular
  198. $aProducts[] = array(
  199. 'caption' => '_adm_block_cpt_popular',
  200. 'actions' => array(
  201. array('name' => 'popular', 'caption' => '_adm_action_cpt_see_all', 'url' => $this->getBaseUrl('popular'))
  202. ),
  203. 'items' => $oJson->load($this->sStoreDataUrlPublic . 'json_browse_popular', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey, 'version' => $sVersion))
  204. );
  205. return $aProducts;
  206. }
  207. protected function loadLatest($iStart, $iPerPage)
  208. {
  209. $sVersion = bx_get_ver();
  210. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_latest', array(
  211. 'start' => $iStart,
  212. 'per_page' => $iPerPage,
  213. 'client' => $this->iClient,
  214. 'key' => $this->sClientKey,
  215. 'version' => $sVersion
  216. ));
  217. }
  218. protected function loadFeatured($iStart, $iPerPage)
  219. {
  220. $sVersion = bx_get_ver();
  221. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_featured', array(
  222. 'start' => $iStart,
  223. 'per_page' => $iPerPage,
  224. 'client' => $this->iClient,
  225. 'key' => $this->sClientKey,
  226. 'version' => $sVersion
  227. ));
  228. }
  229. protected function loadPopular($iStart, $iPerPage)
  230. {
  231. $sVersion = bx_get_ver();
  232. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_popular', array(
  233. 'start' => $iStart,
  234. 'per_page' => $iPerPage,
  235. 'client' => $this->iClient,
  236. 'key' => $this->sClientKey,
  237. 'version' => $sVersion
  238. ));
  239. }
  240. protected function loadCategories()
  241. {
  242. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_categories', array(
  243. 'client' => $this->iClient,
  244. 'key' => $this->sClientKey
  245. ));
  246. }
  247. protected function loadCategory($iCategory, $iStart, $iPerPage)
  248. {
  249. $sVersion = bx_get_ver();
  250. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_category', array(
  251. 'value' => $iCategory,
  252. 'start' => $iStart,
  253. 'per_page' => $iPerPage,
  254. 'client' => $this->iClient,
  255. 'key' => $this->sClientKey,
  256. 'version' => $sVersion
  257. ));
  258. }
  259. protected function loadTags()
  260. {
  261. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_tags', array(
  262. 'client' => $this->iClient,
  263. 'key' => $this->sClientKey
  264. ));
  265. }
  266. protected function loadTag($sTag, $iStart, $iPerPage)
  267. {
  268. $sVersion = bx_get_ver();
  269. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_tag', array(
  270. 'value' => $sTag,
  271. 'start' => $iStart,
  272. 'per_page' => $iPerPage,
  273. 'client' => $this->iClient,
  274. 'key' => $this->sClientKey,
  275. 'version' => $sVersion
  276. ));
  277. }
  278. protected function loadSearch($sKeyword, $iStart, $iPerPage)
  279. {
  280. $sVersion = bx_get_ver();
  281. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_browse_by_keyword', array(
  282. 'value' => $sKeyword,
  283. 'start' => $iStart,
  284. 'per_page' => $iPerPage,
  285. 'client' => $this->iClient,
  286. 'key' => $this->sClientKey,
  287. 'version' => $sVersion
  288. ));
  289. }
  290. protected function loadPurchases()
  291. {
  292. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  293. }
  294. protected function loadUpdates()
  295. {
  296. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  297. }
  298. protected function loadCheckout()
  299. {
  300. $oJson = BxDolStudioJson::getInstance();
  301. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  302. $aResult = array();
  303. foreach($aVendors as $sVendor => $aItems) {
  304. $aIds = $aCounts = array();
  305. foreach($aItems as $aItem) {
  306. $aIds[] = $aItem['item_id'];
  307. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  308. }
  309. $aProducts = $oJson->load($this->sStoreDataUrlPublic . 'json_browse_selected', array('products' => base64_encode(json_encode($aIds)), 'client' => $this->iClient, 'key' => $this->sClientKey));
  310. if(!empty($aProducts))
  311. $aResult[$sVendor] = array(
  312. 'ids' => $aIds,
  313. 'counts' => $aCounts,
  314. 'products' => $aProducts
  315. );
  316. }
  317. return $aResult;
  318. }
  319. protected function loadDownloaded()
  320. {
  321. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  322. return array(
  323. 'modules' => $oInstallerUtils->getModules(),
  324. 'updates' => $oInstallerUtils->getUpdates()
  325. );
  326. }
  327. protected function loadProduct($sModuleName)
  328. {
  329. $sVersion = bx_get_ver();
  330. return BxDolStudioJson::getInstance()->load($this->sStoreDataUrlPublic . 'json_get_product_by_name', array(
  331. 'value' => $sModuleName,
  332. 'client' => $this->iClient,
  333. 'key' => $this->sClientKey,
  334. 'version' => $sVersion
  335. ));
  336. }
  337. protected function loadLocalProduct($sModuleName)
  338. {
  339. $aModules = BxDolStudioInstallerUtils::getInstance()->getModules(false);
  340. return isset($aModules[$sModuleName]) && is_array($aModules[$sModuleName])
  341. ? $aModules[$sModuleName]
  342. : array();
  343. }
  344. /*
  345. * Load package (module, update) using OAuth authorization.
  346. */
  347. protected function loadFile($iFileId)
  348. {
  349. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  350. }
  351. /*
  352. * Load update's package publicly.
  353. */
  354. protected function loadUpdate($sModuleName, $bApplyUpdate = false)
  355. {
  356. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bApplyUpdate);
  357. }
  358. protected function getBaseUrl($mixedParams = array())
  359. {
  360. if(is_string($mixedParams))
  361. $mixedParams = array('page' => $mixedParams);
  362. return bx_append_url_params($this->sBaseUrl, $mixedParams);
  363. }
  364. private function checkoutCart($iVendor)
  365. {
  366. $oCart = BxDolStudioCart::getInstance();
  367. $aItems = $oCart->getByVendor($iVendor);
  368. if(empty($aItems) || !is_array($aItems))
  369. return false;
  370. $aIds = array();
  371. foreach($aItems as $aItem)
  372. $aIds[] = $aItem['item_id'];
  373. $sSid = bx_site_hash();
  374. return $this->sStoreDataUrlPublic . 'purchase/' . $iVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  375. }
  376. private function subscribe($iVendor, $iItem)
  377. {
  378. $sSid = bx_site_hash();
  379. return $this->sStoreDataUrlPublic . 'subscribe/' . $iVendor . '/'. $iItem . '?sid=' . $sSid;
  380. }
  381. }
  382. /** @} */