1
0

BxDolStudioStore.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 TridentStudio Trident Studio
  7. * @{
  8. */
  9. define('BX_DOL_STUDIO_STR_TYPE_DEFAULT', 'downloaded');
  10. class BxDolStudioStore extends BxTemplStudioPage
  11. {
  12. protected $sPage;
  13. protected $aContent;
  14. protected $iClient;
  15. protected $aAlias;
  16. protected $bAuthAccessUpdates;
  17. function __construct($sPage = "")
  18. {
  19. parent::__construct('store');
  20. $this->oDb = new BxDolStudioStoreQuery();
  21. $this->aAlias = array(
  22. 'tag' => array(
  23. 'modules' => 'extensions',
  24. 'languages' => 'translations'
  25. ),
  26. 'category' => array()
  27. );
  28. $this->bAuthAccessUpdates = false;
  29. $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
  30. if(is_string($sPage) && !empty($sPage))
  31. $this->sPage = $sPage;
  32. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  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. $sVendor = bx_process_input(bx_get('str_vendor'));
  72. $iItem = (int)bx_get('str_item');
  73. $iItemCount = 1;
  74. if(empty($sVendor) || 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($sVendor, $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. $sVendor = bx_process_input(bx_get('str_vendor'));
  83. $iItem = (int)bx_get('str_item');
  84. if(empty($sVendor)) {
  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($sVendor, $iItem);
  89. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '');
  90. break;
  91. case 'checkout-cart':
  92. $sVendor = bx_process_input(bx_get('str_vendor'));
  93. if(empty($sVendor)) {
  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($sVendor);
  98. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
  99. break;
  100. case 'install':
  101. $sValue = bx_process_input(bx_get('str_value'));
  102. if(empty($sValue))
  103. break;
  104. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true, 'html_response' => true));
  105. break;
  106. case 'update':
  107. $sValue = bx_process_input(bx_get('str_value'));
  108. if(empty($sValue))
  109. break;
  110. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update', array('html_response' => true));
  111. break;
  112. case 'delete':
  113. $sValue = bx_process_input(bx_get('str_value'));
  114. if(empty($sValue))
  115. break;
  116. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete', array('html_response' => true));
  117. break;
  118. }
  119. if(!empty($aResult['message'])) {
  120. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result.html', array('content' => $aResult['message']));
  121. $aResult['message'] = BxTemplStudioFunctions::getInstance()->transBox('', $aResult['message']);
  122. }
  123. echo json_encode($aResult);
  124. exit;
  125. }
  126. }
  127. protected function loadGoodies()
  128. {
  129. $iPerPage = 5;
  130. $aProducts = array();
  131. $sJsObject = $this->getPageJsObject();
  132. $oJson = BxDolStudioJson::getInstance();
  133. // Load featured
  134. $aProducts[] = array(
  135. 'caption' => '_adm_block_cpt_last_featured',
  136. 'actions' => array(
  137. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  138. ),
  139. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  140. );
  141. // Load modules
  142. $aProducts[] = array(
  143. 'caption' => '_adm_block_cpt_last_modules',
  144. 'actions' => array(
  145. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_modules', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('modules', this)")
  146. ),
  147. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  148. );
  149. // Load templates
  150. $aProducts[] = array(
  151. 'caption' => '_adm_block_cpt_last_templates',
  152. 'actions' => array(
  153. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  154. ),
  155. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  156. );
  157. // Load languages
  158. $aProducts[] = array(
  159. 'caption' => '_adm_block_cpt_last_languages',
  160. 'actions' => array(
  161. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_languages', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('languages', this)")
  162. ),
  163. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  164. );
  165. return $aProducts;
  166. }
  167. protected function loadFeatured($iStart, $iPerPage)
  168. {
  169. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient));
  170. }
  171. protected function loadTag($sTag, $iStart, $iPerPage)
  172. {
  173. 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));
  174. }
  175. protected function loadPurchases()
  176. {
  177. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  178. }
  179. protected function loadUpdates()
  180. {
  181. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  182. }
  183. protected function loadCheckout()
  184. {
  185. $oJson = BxDolStudioJson::getInstance();
  186. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  187. $aResult = array();
  188. foreach($aVendors as $sVendor => $aItems) {
  189. $aIds = $aCounts = array();
  190. foreach($aItems as $aItem) {
  191. $aIds[] = $aItem['item_id'];
  192. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  193. }
  194. $aProducts = $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  195. if(!empty($aProducts))
  196. $aResult[$sVendor] = array(
  197. 'ids' => $aIds,
  198. 'counts' => $aCounts,
  199. 'products' => $aProducts
  200. );
  201. }
  202. return $aResult;
  203. }
  204. protected function loadDownloaded()
  205. {
  206. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  207. return array(
  208. 'modules' => $oInstallerUtils->getModules(),
  209. 'updates' => $oInstallerUtils->getUpdates()
  210. );
  211. }
  212. protected function loadProduct($sModuleName)
  213. {
  214. $oJson = BxDolStudioJson::getInstance();
  215. return $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient));
  216. }
  217. /*
  218. * Load package (module, update) using OAuth authorization.
  219. */
  220. protected function loadFile($iFileId)
  221. {
  222. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  223. }
  224. /*
  225. * Load update's package publicly.
  226. */
  227. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  228. {
  229. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  230. }
  231. private function checkoutCart($sVendor)
  232. {
  233. $oCart = BxDolStudioCart::getInstance();
  234. $aItems = $oCart->getByVendor($sVendor);
  235. if(empty($aItems) || !is_array($aItems))
  236. return false;
  237. $aIds = array();
  238. foreach($aItems as $aItem)
  239. $aIds[] = $aItem['item_id'];
  240. $sSid = bx_site_hash();
  241. return BX_DOL_UNITY_URL_MARKET . 'purchase/' . $sVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  242. }
  243. private function aliasToNameTag($sAlias)
  244. {
  245. return $this->aliasToName('tag', $sAlias);
  246. }
  247. private function aliasToNameCategory($sAlias)
  248. {
  249. return $this->aliasToName('category', $sAlias);
  250. }
  251. private function aliasToName($sType, $sAlias)
  252. {
  253. return isset($this->aAlias[$sType][$sAlias]) ? $this->aAlias[$sType][$sAlias] : $sAlias;
  254. }
  255. }
  256. /** @} */