1
0

BxDolStudioStore.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 $sClientKey;
  16. protected $aAlias;
  17. protected $bAuthAccessUpdates;
  18. function __construct($sPage = "")
  19. {
  20. parent::__construct('store');
  21. $this->oDb = new BxDolStudioStoreQuery();
  22. $this->aAlias = array(
  23. 'tag' => array(
  24. 'modules' => 'extensions',
  25. 'languages' => 'translations'
  26. ),
  27. 'category' => array()
  28. );
  29. $this->bAuthAccessUpdates = false;
  30. $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
  31. if(is_string($sPage) && !empty($sPage))
  32. $this->sPage = $sPage;
  33. $this->iClient = BxDolStudioOAuth::getAuthorizedClient();
  34. if(!empty($this->iClient))
  35. $this->sClientKey = $this->oDb->getParam('sys_oauth_key');
  36. //--- Check actions ---//
  37. if(($sAction = bx_get('str_action')) !== false) {
  38. $sAction = bx_process_input($sAction);
  39. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_mod_err_cannot_process_action'));
  40. switch($sAction) {
  41. case 'get-file':
  42. $iFileId = (int)bx_get('str_id');
  43. $aResult = $this->getFile($iFileId);
  44. break;
  45. case 'get-product':
  46. $sModuleName = bx_process_input(bx_get('str_id'));
  47. $aResult = $this->getProduct($sModuleName);
  48. break;
  49. case 'get-update':
  50. $sModuleName = bx_process_input(bx_get('str_id'));
  51. $aResult = $this->getUpdate($sModuleName);
  52. break;
  53. case 'get-update-and-install':
  54. $sModuleName = bx_process_input(bx_get('str_id'));
  55. $aResult = $this->getUpdate($sModuleName, true);
  56. break;
  57. case 'get-products-by-type':
  58. $this->sPage = bx_process_input(bx_get('str_value'));
  59. $sContent = $this->getPageCode();
  60. if(!empty($sContent))
  61. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  62. else
  63. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  64. break;
  65. case 'get-products-by-page':
  66. $this->sPage = bx_process_input(bx_get('str_type'));
  67. $sContent = $this->getPageContent();
  68. if(!empty($sContent))
  69. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'content' => $sContent);
  70. else
  71. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_act_err_failed_page_loading'));
  72. break;
  73. case 'add-to-cart':
  74. $sVendor = bx_process_input(bx_get('str_vendor'));
  75. $iItem = (int)bx_get('str_item');
  76. $iItemCount = 1;
  77. if(empty($sVendor) || empty($iItem)) {
  78. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
  79. break;
  80. }
  81. BxDolStudioCart::getInstance()->add($sVendor, $iItem, $iItemCount);
  82. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
  83. break;
  84. case 'delete-from-cart':
  85. $sVendor = bx_process_input(bx_get('str_vendor'));
  86. $iItem = (int)bx_get('str_item');
  87. if(empty($sVendor)) {
  88. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
  89. break;
  90. }
  91. BxDolStudioCart::getInstance()->delete($sVendor, $iItem);
  92. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '');
  93. break;
  94. case 'checkout-cart':
  95. $sVendor = bx_process_input(bx_get('str_vendor'));
  96. if(empty($sVendor)) {
  97. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_FAILED, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
  98. break;
  99. }
  100. $sLocation = $this->checkoutCart($sVendor);
  101. $aResult = array('code' => BX_DOL_STUDIO_IU_RC_SUCCESS, 'message' => '', 'redirect' => $sLocation);
  102. break;
  103. case 'install':
  104. $sValue = bx_process_input(bx_get('str_value'));
  105. if(empty($sValue))
  106. break;
  107. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true, 'html_response' => true));
  108. break;
  109. case 'update':
  110. $sValue = bx_process_input(bx_get('str_value'));
  111. if(empty($sValue))
  112. break;
  113. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update', array('html_response' => true));
  114. break;
  115. case 'delete':
  116. $sValue = bx_process_input(bx_get('str_value'));
  117. if(empty($sValue))
  118. break;
  119. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete', array('html_response' => true));
  120. break;
  121. }
  122. if(!empty($aResult['message']))
  123. $aResult['message'] = BxTemplStudioFunctions::getInstance()->inlineBox('', BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result_inline.html', array('content' => $aResult['message'])), true);
  124. echo json_encode($aResult);
  125. exit;
  126. }
  127. }
  128. protected function loadGoodies()
  129. {
  130. $iPerPage = 6;
  131. $aProducts = array();
  132. $sJsObject = $this->getPageJsObject();
  133. $oJson = BxDolStudioJson::getInstance();
  134. // Load featured
  135. $aProducts[] = array(
  136. 'caption' => '_adm_block_cpt_last_featured',
  137. 'actions' => array(
  138. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  139. ),
  140. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  141. );
  142. // Load modules
  143. $aProducts[] = array(
  144. 'caption' => '_adm_block_cpt_last_modules',
  145. 'actions' => array(
  146. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_modules', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('modules', this)")
  147. ),
  148. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  149. );
  150. // Load templates
  151. $aProducts[] = array(
  152. 'caption' => '_adm_block_cpt_last_templates',
  153. 'actions' => array(
  154. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  155. ),
  156. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  157. );
  158. // Load languages
  159. $aProducts[] = array(
  160. 'caption' => '_adm_block_cpt_last_languages',
  161. 'actions' => array(
  162. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_languages', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('languages', this)")
  163. ),
  164. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey))
  165. );
  166. return $aProducts;
  167. }
  168. protected function loadFeatured($iStart, $iPerPage)
  169. {
  170. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient, 'key' => $this->sClientKey));
  171. }
  172. protected function loadTag($sTag, $iStart, $iPerPage)
  173. {
  174. 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, 'key' => $this->sClientKey));
  175. }
  176. protected function loadPurchases()
  177. {
  178. return BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  179. }
  180. protected function loadUpdates()
  181. {
  182. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  183. }
  184. protected function loadCheckout()
  185. {
  186. $oJson = BxDolStudioJson::getInstance();
  187. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  188. $aResult = array();
  189. foreach($aVendors as $sVendor => $aItems) {
  190. $aIds = $aCounts = array();
  191. foreach($aItems as $aItem) {
  192. $aIds[] = $aItem['item_id'];
  193. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  194. }
  195. $aProducts = $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  196. if(!empty($aProducts))
  197. $aResult[$sVendor] = array(
  198. 'ids' => $aIds,
  199. 'counts' => $aCounts,
  200. 'products' => $aProducts
  201. );
  202. }
  203. return $aResult;
  204. }
  205. protected function loadDownloaded()
  206. {
  207. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  208. return array(
  209. 'modules' => $oInstallerUtils->getModules(),
  210. 'updates' => $oInstallerUtils->getUpdates()
  211. );
  212. }
  213. protected function loadProduct($sModuleName)
  214. {
  215. $oJson = BxDolStudioJson::getInstance();
  216. return $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient, 'key' => $this->sClientKey));
  217. }
  218. /*
  219. * Load package (module, update) using OAuth authorization.
  220. */
  221. protected function loadFile($iFileId)
  222. {
  223. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  224. }
  225. /*
  226. * Load update's package publicly.
  227. */
  228. protected function loadUpdate($sModuleName, $bAutoUpdate = false)
  229. {
  230. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName, $bAutoUpdate);
  231. }
  232. private function checkoutCart($sVendor)
  233. {
  234. $oCart = BxDolStudioCart::getInstance();
  235. $aItems = $oCart->getByVendor($sVendor);
  236. if(empty($aItems) || !is_array($aItems))
  237. return false;
  238. $aIds = array();
  239. foreach($aItems as $aItem)
  240. $aIds[] = $aItem['item_id'];
  241. $sSid = bx_site_hash();
  242. return BX_DOL_UNITY_URL_MARKET . 'purchase/' . $sVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  243. }
  244. private function aliasToNameTag($sAlias)
  245. {
  246. return $this->aliasToName('tag', $sAlias);
  247. }
  248. private function aliasToNameCategory($sAlias)
  249. {
  250. return $this->aliasToName('category', $sAlias);
  251. }
  252. private function aliasToName($sType, $sAlias)
  253. {
  254. return isset($this->aAlias[$sType][$sAlias]) ? $this->aAlias[$sType][$sAlias] : $sAlias;
  255. }
  256. }
  257. /** @} */