BxDolStudioStore.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. /*
  43. * NOTE. Is needed for download popup selector. Isn't used for now.
  44. */
  45. case 'get-files':
  46. $iId = (int)bx_get('str_id');
  47. $sType = bx_process_input(bx_get('str_type'));
  48. $aResult = $this->getFiles($iId, $sType);
  49. break;
  50. case 'get-file':
  51. $iFileId = (int)bx_get('str_id');
  52. $aResult = $this->getFile($iFileId);
  53. break;
  54. case 'get-product':
  55. $sModuleName = bx_process_input(bx_get('str_id'));
  56. $bDownloaded = (int)bx_get('str_downloaded');
  57. $aResult = $this->getProduct($sModuleName, $bDownloaded);
  58. break;
  59. case 'get-update':
  60. $sModuleName = bx_process_input(bx_get('str_id'));
  61. $aResult = $this->getUpdate($sModuleName);
  62. break;
  63. case 'get-products-by-type':
  64. $this->sPage = bx_process_input(bx_get('str_value'));
  65. $sContent = $this->getPageCode();
  66. if(!empty($sContent))
  67. $aResult = array('code' => 0, 'content' => $sContent);
  68. else
  69. $aResult = array('code' => 1, 'message' => _t('_adm_act_err_failed_page_loading'));
  70. break;
  71. case 'get-products-by-page':
  72. $this->sPage = bx_process_input(bx_get('str_type'));
  73. $sContent = $this->getPageContent();
  74. if(!empty($sContent))
  75. $aResult = array('code' => 0, 'content' => $sContent);
  76. else
  77. $aResult = array('code' => 1, 'message' => _t('_adm_act_err_failed_page_loading'));
  78. break;
  79. case 'add-to-cart':
  80. $sVendor = bx_process_input(bx_get('str_vendor'));
  81. $iItem = (int)bx_get('str_item');
  82. $iItemCount = 1;
  83. if(empty($sVendor) || empty($iItem)) {
  84. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
  85. break;
  86. }
  87. bx_import('BxDolStudioCart');
  88. BxDolStudioCart::getInstance()->add($sVendor, $iItem, $iItemCount);
  89. $aResult = array('code' => 0, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
  90. break;
  91. case 'delete-from-cart':
  92. $sVendor = bx_process_input(bx_get('str_vendor'));
  93. $iItem = (int)bx_get('str_item');
  94. if(empty($sVendor)) {
  95. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
  96. break;
  97. }
  98. bx_import('BxDolStudioCart');
  99. BxDolStudioCart::getInstance()->delete($sVendor, $iItem);
  100. $aResult = array('code' => 0, 'message' => '');
  101. break;
  102. case 'checkout-cart':
  103. $sVendor = bx_process_input(bx_get('str_vendor'));
  104. if(empty($sVendor)) {
  105. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
  106. break;
  107. }
  108. $sLocation = $this->checkoutCart($sVendor);
  109. $aResult = array('code' => 0, 'message' => '', 'redirect' => $sLocation);
  110. break;
  111. case 'install':
  112. $sValue = bx_process_input(bx_get('str_value'));
  113. if(empty($sValue))
  114. break;
  115. bx_import('BxDolStudioInstallerUtils');
  116. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true));
  117. break;
  118. case 'update':
  119. $sValue = bx_process_input(bx_get('str_value'));
  120. if(empty($sValue))
  121. break;
  122. bx_import('BxDolStudioInstallerUtils');
  123. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update');
  124. break;
  125. case 'delete':
  126. $sValue = bx_process_input(bx_get('str_value'));
  127. if(empty($sValue))
  128. break;
  129. bx_import('BxDolStudioInstallerUtils');
  130. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete');
  131. break;
  132. }
  133. if(!empty($aResult['message'])) {
  134. bx_import('BxDolStudioTemplate');
  135. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result.html', array('content' => $aResult['message']));
  136. bx_import('BxTemplStudioFunctions');
  137. $aResult['message'] = BxTemplStudioFunctions::getInstance()->transBox('', $aResult['message']);
  138. }
  139. echo json_encode($aResult);
  140. exit;
  141. }
  142. }
  143. protected function loadGoodies()
  144. {
  145. $iPerPage = 5;
  146. $aProducts = array();
  147. $sJsObject = $this->getPageJsObject();
  148. bx_import('BxDolStudioJson');
  149. $oJson = BxDolStudioJson::getInstance();
  150. // Load featured
  151. $aProducts[] = array(
  152. 'caption' => '_adm_block_cpt_last_featured',
  153. 'actions' => array(
  154. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  155. ),
  156. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  157. );
  158. // Load modules
  159. $aProducts[] = array(
  160. 'caption' => '_adm_block_cpt_last_modules',
  161. 'actions' => array(
  162. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_modules', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('modules', this)")
  163. ),
  164. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  165. );
  166. // Load templates
  167. $aProducts[] = array(
  168. 'caption' => '_adm_block_cpt_last_templates',
  169. 'actions' => array(
  170. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  171. ),
  172. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  173. );
  174. // Load languages
  175. $aProducts[] = array(
  176. 'caption' => '_adm_block_cpt_last_languages',
  177. 'actions' => array(
  178. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_languages', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('languages', this)")
  179. ),
  180. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  181. );
  182. return $aProducts;
  183. }
  184. protected function loadFeatured($iStart, $iPerPage)
  185. {
  186. bx_import('BxDolStudioJson');
  187. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient));
  188. }
  189. protected function loadTag($sTag, $iStart, $iPerPage)
  190. {
  191. bx_import('BxDolStudioJson');
  192. 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));
  193. }
  194. protected function loadPurchases()
  195. {
  196. bx_import('BxDolStudioOAuth');
  197. $aProducts = BxDolStudioOAuth::getInstance()->loadItems(array('dol_type' => 'purchased_products', 'dol_domain' => BX_DOL_URL_ROOT));
  198. if(!empty($aProducts) && is_array($aProducts))
  199. foreach ($aProducts as $aProduct)
  200. $this->oDb->updateModule(array('hash' => $aProduct['hash']), array('name' => $aProduct['name']));
  201. return $aProducts;
  202. }
  203. protected function loadUpdates()
  204. {
  205. bx_import('BxDolStudioInstallerUtils');
  206. return BxDolStudioInstallerUtils::getInstance()->checkUpdates($this->bAuthAccessUpdates);
  207. }
  208. protected function loadCheckout()
  209. {
  210. bx_import('BxDolStudioJson');
  211. $oJson = BxDolStudioJson::getInstance();
  212. bx_import('BxDolStudioCart');
  213. $aVendors = BxDolStudioCart::getInstance()->parseByVendor();
  214. $aResult = array();
  215. foreach($aVendors as $sVendor => $aItems) {
  216. $aIds = $aCounts = array();
  217. foreach($aItems as $aItem) {
  218. $aIds[] = $aItem['item_id'];
  219. $aCounts[$aItem['item_id']] = $aItem['item_count'];
  220. }
  221. $aProducts = $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_selected', array('products' => base64_encode(serialize($aIds))));
  222. if(!empty($aProducts))
  223. $aResult[$sVendor] = array(
  224. 'ids' => $aIds,
  225. 'counts' => $aCounts,
  226. 'products' => $aProducts
  227. );
  228. }
  229. return $aResult;
  230. }
  231. protected function loadDownloaded()
  232. {
  233. bx_import('BxDolStudioInstallerUtils');
  234. $oInstallerUtils = BxDolStudioInstallerUtils::getInstance();
  235. return array(
  236. 'modules' => $oInstallerUtils->getModules(),
  237. 'updates' => $oInstallerUtils->getUpdates()
  238. );
  239. }
  240. protected function loadProduct($sModuleName)
  241. {
  242. bx_import('BxDolStudioJson');
  243. $oJson = BxDolStudioJson::getInstance();
  244. return $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_get_product_by_name', array('value' => $sModuleName, 'client' => $this->iClient));
  245. }
  246. /*
  247. * NOTE. Is needed for download popup selector. Isn't used for now.
  248. */
  249. protected function loadFiles($iId, $sType)
  250. {
  251. bx_import('BxDolStudioOAuth');
  252. return BxDolStudioOAuth::getInstance()->loadItems(array('dol_type' => 'product_files', 'dol_product_id' => $iId, 'dol_file_type' => $sType));
  253. }
  254. /*
  255. * Load package (module, update) using OAuth authorization.
  256. */
  257. protected function loadFile($iFileId)
  258. {
  259. bx_import('BxDolStudioInstallerUtils');
  260. return BxDolStudioInstallerUtils::getInstance()->downloadFileAuthorized($iFileId);
  261. }
  262. /*
  263. * Load update's package publicly.
  264. */
  265. protected function loadUpdate($sModuleName)
  266. {
  267. bx_import('BxDolStudioInstallerUtils');
  268. return BxDolStudioInstallerUtils::getInstance()->downloadUpdatePublic($sModuleName);
  269. }
  270. private function checkoutCart($sVendor)
  271. {
  272. bx_import('BxDolStudioCart');
  273. $oCart = BxDolStudioCart::getInstance();
  274. $aItems = $oCart->getByVendor($sVendor);
  275. if(empty($aItems) || !is_array($aItems))
  276. return false;
  277. $aIds = array();
  278. foreach($aItems as $aItem)
  279. $aIds[] = $aItem['item_id'];
  280. $sSid = bx_site_hash();
  281. return BX_DOL_UNITY_URL_MARKET . 'purchase/' . $sVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  282. }
  283. private function aliasToNameTag($sAlias)
  284. {
  285. return $this->aliasToName('tag', $sAlias);
  286. }
  287. private function aliasToNameCategory($sAlias)
  288. {
  289. return $this->aliasToName('category', $sAlias);
  290. }
  291. private function aliasToName($sType, $sAlias)
  292. {
  293. return isset($this->aAlias[$sType][$sAlias]) ? $this->aAlias[$sType][$sAlias] : $sAlias;
  294. }
  295. }
  296. /** @} */