BxDolStudioStore.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. bx_import('BxTemplStudioPage');
  10. bx_import('BxDolStudioStoreQuery');
  11. bx_import('BxDolStudioInstallerUtils');
  12. define('BX_DOL_STUDIO_STR_TYPE_DEFAULT', 'downloaded');
  13. class BxDolStudioStore extends BxTemplStudioPage
  14. {
  15. protected $sPage;
  16. protected $aContent;
  17. protected $iClient;
  18. protected $aAlias;
  19. protected $bAuthAccessUpdates;
  20. function __construct($sPage = "")
  21. {
  22. parent::__construct('store');
  23. $this->oDb = new BxDolStudioStoreQuery();
  24. $this->aAlias = array(
  25. 'tag' => array(
  26. 'modules' => 'extensions',
  27. 'languages' => 'translations'
  28. ),
  29. 'category' => array()
  30. );
  31. $this->bAuthAccessUpdates = false;
  32. $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
  33. if(is_string($sPage) && !empty($sPage))
  34. $this->sPage = $sPage;
  35. bx_import('BxDolStudioOAuth');
  36. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  37. //--- Check actions ---//
  38. if(($sAction = bx_get('str_action')) !== false) {
  39. $sAction = bx_process_input($sAction);
  40. $aResult = array('code' => 1, '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' => 0, 'content' => $sContent);
  63. else
  64. $aResult = array('code' => 1, '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' => 0, 'content' => $sContent);
  71. else
  72. $aResult = array('code' => 1, 'message' => _t('_adm_act_err_failed_page_loading'));
  73. break;
  74. case 'add-to-cart':
  75. $sVendor = bx_process_input(bx_get('str_vendor'));
  76. $iItem = (int)bx_get('str_item');
  77. $iItemCount = 1;
  78. if(empty($sVendor) || empty($iItem)) {
  79. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
  80. break;
  81. }
  82. bx_import('BxDolStudioCart');
  83. BxDolStudioCart::getInstance()->add($sVendor, $iItem, $iItemCount);
  84. $aResult = array('code' => 0, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
  85. break;
  86. case 'delete-from-cart':
  87. $sVendor = bx_process_input(bx_get('str_vendor'));
  88. $iItem = (int)bx_get('str_item');
  89. if(empty($sVendor)) {
  90. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
  91. break;
  92. }
  93. bx_import('BxDolStudioCart');
  94. BxDolStudioCart::getInstance()->delete($sVendor, $iItem);
  95. $aResult = array('code' => 0, 'message' => '');
  96. break;
  97. case 'checkout-cart':
  98. $sVendor = bx_process_input(bx_get('str_vendor'));
  99. if(empty($sVendor)) {
  100. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
  101. break;
  102. }
  103. $sLocation = $this->checkoutCart($sVendor);
  104. $aResult = array('code' => 0, 'message' => '', 'redirect' => $sLocation);
  105. break;
  106. case 'install':
  107. $sValue = bx_process_input(bx_get('str_value'));
  108. if(empty($sValue))
  109. break;
  110. bx_import('BxDolStudioInstallerUtils');
  111. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true, 'html_response' => true));
  112. break;
  113. case 'update':
  114. $sValue = bx_process_input(bx_get('str_value'));
  115. if(empty($sValue))
  116. break;
  117. bx_import('BxDolStudioInstallerUtils');
  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. bx_import('BxDolStudioInstallerUtils');
  125. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete', array('html_response' => true));
  126. break;
  127. }
  128. if(!empty($aResult['message'])) {
  129. bx_import('BxDolStudioTemplate');
  130. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result.html', array('content' => $aResult['message']));
  131. bx_import('BxTemplStudioFunctions');
  132. $aResult['message'] = BxTemplStudioFunctions::getInstance()->transBox('', $aResult['message']);
  133. }
  134. echo json_encode($aResult);
  135. exit;
  136. }
  137. }
  138. protected function loadGoodies()
  139. {
  140. $iPerPage = 5;
  141. $aProducts = array();
  142. $sJsObject = $this->getPageJsObject();
  143. bx_import('BxDolStudioJson');
  144. $oJson = BxDolStudioJson::getInstance();
  145. // Load featured
  146. $aProducts[] = array(
  147. 'caption' => '_adm_block_cpt_last_featured',
  148. 'actions' => array(
  149. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  150. ),
  151. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  152. );
  153. // Load modules
  154. $aProducts[] = array(
  155. 'caption' => '_adm_block_cpt_last_modules',
  156. 'actions' => array(
  157. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_modules', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('modules', this)")
  158. ),
  159. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  160. );
  161. // Load templates
  162. $aProducts[] = array(
  163. 'caption' => '_adm_block_cpt_last_templates',
  164. 'actions' => array(
  165. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  166. ),
  167. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  168. );
  169. // Load languages
  170. $aProducts[] = array(
  171. 'caption' => '_adm_block_cpt_last_languages',
  172. 'actions' => array(
  173. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_languages', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('languages', this)")
  174. ),
  175. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  176. );
  177. return $aProducts;
  178. }
  179. protected function loadFeatured($iStart, $iPerPage)
  180. {
  181. bx_import('BxDolStudioJson');
  182. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient));
  183. }
  184. protected function loadTag($sTag, $iStart, $iPerPage)
  185. {
  186. bx_import('BxDolStudioJson');
  187. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => $this->aliasToNameTag($sTag), 'start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient));
  188. }
  189. protected function loadPurchases()
  190. {
  191. bx_import('BxDolStudioInstallerUtils');
  192. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  193. }
  194. protected function loadUpdates()
  195. {
  196. bx_import('BxDolStudioInstallerUtils');
  197. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  198. }
  199. protected function loadCheckout()
  200. {
  201. bx_import('BxDolStudioJson');
  202. $oJson = BxDolStudioJson::getInstance();
  203. bx_import('BxDolStudioCart');
  204. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  205. $aResult = array();
  206. foreach($aVendors as $sVendor => $aItems) {
  207. $aIds = $aCounts = array();
  208. foreach($aItems as $aItem) {
  209. $aIds[] = $aItem['item_id'];
  210. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  211. }
  212. $aProducts = $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  213. if(!empty($aProducts))
  214. $aResult[$sVendor] = array(
  215. 'ids' => $aIds,
  216. 'counts' => $aCounts,
  217. 'products' => $aProducts
  218. );
  219. }
  220. return $aResult;
  221. }
  222. protected function loadDownloaded()
  223. {
  224. bx_import('BxDolStudioInstallerUtils');
  225. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  226. return array(
  227. 'modules' => $oInstallerUtils->getModules(),
  228. 'updates' => $oInstallerUtils->getUpdates()
  229. );
  230. }
  231. protected function loadProduct($sModuleName)
  232. {
  233. bx_import('BxDolStudioJson');
  234. $oJson = BxDolStudioJson::getInstance();
  235. return $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient));
  236. }
  237. /*
  238. * Load package (module, update) using OAuth authorization.
  239. */
  240. protected function loadFile($iFileId)
  241. {
  242. bx_import('BxDolStudioInstallerUtils');
  243. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  244. }
  245. /*
  246. * Load update's package publicly.
  247. */
  248. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  249. {
  250. bx_import('BxDolStudioInstallerUtils');
  251. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  252. }
  253. private function checkoutCart($sVendor)
  254. {
  255. bx_import('BxDolStudioCart');
  256. $oCart = BxDolStudioCart::getInstance();
  257. $aItems = $oCart->getByVendor($sVendor);
  258. if(empty($aItems) || !is_array($aItems))
  259. return false;
  260. $aIds = array();
  261. foreach($aItems as $aItem)
  262. $aIds[] = $aItem['item_id'];
  263. $sSid = bx_site_hash();
  264. return BX_DOL_UNITY_URL_MARKET . 'purchase/' . $sVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  265. }
  266. private function aliasToNameTag($sAlias)
  267. {
  268. return $this->aliasToName('tag', $sAlias);
  269. }
  270. private function aliasToNameCategory($sAlias)
  271. {
  272. return $this->aliasToName('category', $sAlias);
  273. }
  274. private function aliasToName($sType, $sAlias)
  275. {
  276. return isset($this->aAlias[$sType][$sAlias]) ? $this->aAlias[$sType][$sAlias] : $sAlias;
  277. }
  278. }
  279. /** @} */