1
0

BxDolStudioStore.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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('BxDolStudioOAuth');
  11. bx_import('BxDolStudioStoreQuery');
  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. function __construct($sPage = "")
  20. {
  21. parent::__construct('store');
  22. $this->oDb = new BxDolStudioStoreQuery();
  23. $this->aAlias = array(
  24. 'tag' => array(
  25. 'modules' => 'extensions',
  26. 'languages' => 'translations'
  27. ),
  28. 'category' => array()
  29. );
  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. //--- Check actions ---//
  35. if(($sAction = bx_get('str_action')) !== false) {
  36. $sAction = bx_process_input($sAction);
  37. $aResult = array('code' => 1, 'message' => _t('_adm_mod_err_cannot_process_action'));
  38. switch($sAction) {
  39. /*
  40. * NOTE. Is needed for download popup selector. Isn't used for now.
  41. */
  42. case 'get-files':
  43. $iId = (int)bx_get('str_id');
  44. $sType = bx_process_input(bx_get('str_type'));
  45. $aResult = $this->getFiles($iId, $sType);
  46. break;
  47. case 'get-file':
  48. $iId = (int)bx_get('str_id');
  49. $aResult = $this->getFile($iId);
  50. break;
  51. case 'get-product':
  52. $iId = (int)bx_get('str_id');
  53. $aResult = $this->getProduct($iId);
  54. break;
  55. case 'get-products-by-type':
  56. $this->sPage = bx_process_input(bx_get('str_value'));
  57. $sContent = $this->getPageCode();
  58. if(!empty($sContent))
  59. $aResult = array('code' => 0, 'content' => $sContent);
  60. else
  61. $aResult = array('code' => 1, 'message' => _t('_adm_act_err_failed_page_loading'));
  62. break;
  63. case 'get-products-by-page':
  64. $this->sPage = bx_process_input(bx_get('str_type'));
  65. $sContent = $this->getPageContent();
  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 'add-to-cart':
  72. $sVendor = bx_process_input(bx_get('str_vendor'));
  73. $iItem = (int)bx_get('str_item');
  74. $iItemCount = 1;
  75. if(empty($sVendor) || empty($iItem)) {
  76. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_add_to_cart'));
  77. break;
  78. }
  79. bx_import('BxDolStudioCart');
  80. BxDolStudioCart::getInstance()->add($sVendor, $iItem, $iItemCount);
  81. $aResult = array('code' => 0, 'message' => _t('_adm_msg_modules_success_added_to_cart'));
  82. break;
  83. case 'delete-from-cart':
  84. $sVendor = bx_process_input(bx_get('str_vendor'));
  85. $iItem = (int)bx_get('str_item');
  86. if(empty($sVendor)) {
  87. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_delete_from_cart'));
  88. break;
  89. }
  90. bx_import('BxDolStudioCart');
  91. BxDolStudioCart::getInstance()->delete($sVendor, $iItem);
  92. $aResult = array('code' => 0, 'message' => '');
  93. break;
  94. case 'checkout-cart':
  95. $sVendor = bx_process_input(bx_get('str_vendor'));
  96. if(empty($sVendor)) {
  97. $aResult = array('code' => 1, 'message' => _t('_adm_err_modules_cannot_checkout_empty_vendor'));
  98. break;
  99. }
  100. $sLocation = $this->checkoutCart($sVendor);
  101. $aResult = array('code' => 0, 'message' => '', 'redirect' => $sLocation);
  102. break;
  103. case 'install':
  104. $sValue = bx_process_input(bx_get('str_value'));
  105. if(empty($sValue))
  106. break;
  107. bx_import('BxDolStudioInstallerUtils');
  108. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'install', array('auto_enable' => true));
  109. break;
  110. case 'update':
  111. $sValue = bx_process_input(bx_get('str_value'));
  112. if(empty($sValue))
  113. break;
  114. bx_import('BxDolStudioInstallerUtils');
  115. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'update');
  116. break;
  117. case 'delete':
  118. $sValue = bx_process_input(bx_get('str_value'));
  119. if(empty($sValue))
  120. break;
  121. bx_import('BxDolStudioInstallerUtils');
  122. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($sValue, 'delete');
  123. break;
  124. }
  125. if(!empty($aResult['message'])) {
  126. bx_import('BxDolStudioTemplate');
  127. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('mod_action_result.html', array('content' => $aResult['message']));
  128. bx_import('BxTemplStudioFunctions');
  129. $aResult['message'] = BxTemplStudioFunctions::getInstance()->transBox('', $aResult['message']);
  130. }
  131. echo json_encode($aResult);
  132. exit;
  133. }
  134. }
  135. protected function loadGoodies()
  136. {
  137. $iPerPage = 5;
  138. $aProducts = array();
  139. $sJsObject = $this->getPageJsObject();
  140. bx_import('BxDolStudioJson');
  141. $oJson = BxDolStudioJson::getInstance();
  142. // Load featured
  143. $aProducts[] = array(
  144. 'caption' => '_adm_block_cpt_last_featured',
  145. 'actions' => array(
  146. array('name' => 'featured', 'caption' => '_adm_action_cpt_see_all_featured', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('featured', this)")
  147. ),
  148. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  149. );
  150. // Load modules
  151. $aProducts[] = array(
  152. 'caption' => '_adm_block_cpt_last_modules',
  153. 'actions' => array(
  154. array('name' => 'modules', 'caption' => '_adm_action_cpt_see_all_modules', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('modules', this)")
  155. ),
  156. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'extensions', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  157. );
  158. // Load templates
  159. $aProducts[] = array(
  160. 'caption' => '_adm_block_cpt_last_templates',
  161. 'actions' => array(
  162. array('name' => 'templates', 'caption' => '_adm_action_cpt_see_all_templates', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('templates', this)")
  163. ),
  164. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'templates', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  165. );
  166. // Load languages
  167. $aProducts[] = array(
  168. 'caption' => '_adm_block_cpt_last_languages',
  169. 'actions' => array(
  170. array('name' => 'languages', 'caption' => '_adm_action_cpt_see_all_languages', 'url' => 'javascript:void(0)', 'onclick' => $sJsObject . ".changePage('languages', this)")
  171. ),
  172. 'items' => $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_by_tag', array('value' => 'translations', 'start' => 0, 'per_page' => $iPerPage, 'client' => $this->iClient))
  173. );
  174. return $aProducts;
  175. }
  176. protected function loadFeatured($iStart, $iPerPage)
  177. {
  178. bx_import('BxDolStudioJson');
  179. return BxDolStudioJson::getInstance()->load(BX_DOL_UNITY_URL_MARKET . 'json_browse_featured', array('start' => $iStart, 'per_page' => $iPerPage, 'client' => $this->iClient));
  180. }
  181. protected function loadTag($sTag, $iStart, $iPerPage)
  182. {
  183. bx_import('BxDolStudioJson');
  184. 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));
  185. }
  186. protected function loadPurchases()
  187. {
  188. bx_import('BxDolStudioOAuth');
  189. return BxDolStudioOAuth::getInstance()->loadItems(array('dol_type' => 'purchased_products'));
  190. }
  191. protected function loadUpdates()
  192. {
  193. bx_import('BxDolModuleQuery');
  194. $oModules = BxDolModuleQuery::getInstance();
  195. $aModules = $oModules->getModules();
  196. $aParams = array();
  197. foreach($aModules as $aModule) {
  198. if(!isset($aModule['name']) || empty($aModule['name']))
  199. continue;
  200. $aParams[] = array(
  201. 'name' => $aModule['name'],
  202. 'version' => $aModule['version'],
  203. );
  204. }
  205. bx_import('BxDolStudioOAuth');
  206. return BxDolStudioOAuth::getInstance()->loadItems(array('dol_type' => 'available_updates', 'dol_products' => base64_encode(serialize($aParams))));
  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->loadModules(),
  237. 'updates' => $oInstallerUtils->loadUpdates()
  238. );
  239. }
  240. protected function loadProduct($iId)
  241. {
  242. bx_import('BxDolStudioJson');
  243. $oJson = BxDolStudioJson::getInstance();
  244. return $oJson->load(BX_DOL_UNITY_URL_MARKET . 'json_get_product_by_id', array('value' => $iId, '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. protected function loadFile($iId)
  255. {
  256. bx_import('BxDolStudioOAuth');
  257. $aItem = BxDolStudioOAuth::getInstance()->loadItems(array('dol_type' => 'product_file', 'dol_file_id' => $iId));
  258. if(empty($aItem) || !is_array($aItem))
  259. return $aItem;
  260. //--- write ZIP archive.
  261. $sFilePath = BX_DIRECTORY_PATH_TMP . $aItem['name'];
  262. if (!$rHandler = fopen($sFilePath, 'w'))
  263. return _t('_adm_str_err_cannot_write');
  264. if (!fwrite($rHandler, urldecode($aItem['content'])))
  265. return _t('_adm_str_err_cannot_write');
  266. fclose($rHandler);
  267. //--- Unarchive package.
  268. if(!class_exists('ZipArchive'))
  269. return _t('_adm_str_err_zip_not_available');
  270. $oZip = new ZipArchive();
  271. if($oZip->open($sFilePath) !== true)
  272. return _t('_adm_str_err_cannot_unzip_package');
  273. $sPackageRootFolder = $oZip->numFiles > 0 ? $oZip->getNameIndex(0) : false;
  274. if($sPackageRootFolder && file_exists(BX_DIRECTORY_PATH_TMP . $sPackageRootFolder)) // remove existing tmp folder with the same name
  275. bx_rrmdir(BX_DIRECTORY_PATH_TMP . $sPackageRootFolder);
  276. if($sPackageRootFolder && !$oZip->extractTo(BX_DIRECTORY_PATH_TMP))
  277. return _t('_adm_str_err_cannot_unzip_package');
  278. $oZip->close();
  279. //--- Move unarchived package.
  280. $sLogin = getParam('sys_ftp_login');
  281. $sPassword = getParam('sys_ftp_password');
  282. $sPath = getParam('sys_ftp_dir');
  283. if(empty($sLogin) || empty($sPassword) || empty($sPath))
  284. return _t('_adm_str_err_no_ftp_info');
  285. bx_import('BxDolFtp');
  286. $oFtp = new BxDolFtp($_SERVER['HTTP_HOST'], $sLogin, $sPassword, $sPath);
  287. if(!$oFtp->connect())
  288. return _t('_adm_str_err_cannot_connect_to_ftp');
  289. if(!$oFtp->isDolphin())
  290. return _t('_adm_str_err_destination_not_valid');
  291. $sConfigPath = BX_DIRECTORY_PATH_TMP . $sPackageRootFolder . '/install/config.php';
  292. if(!file_exists($sConfigPath))
  293. return _t('_adm_str_err_wrong_package_format');
  294. include($sConfigPath);
  295. if(empty($aConfig) || empty($aConfig['home_dir']) || !$oFtp->copy(BX_DIRECTORY_PATH_TMP . $sPackageRootFolder . '/', 'modules/' . $aConfig['home_dir']))
  296. return _t('_adm_str_err_ftp_copy_failed');
  297. return true;
  298. }
  299. private function checkoutCart($sVendor)
  300. {
  301. bx_import('BxDolStudioCart');
  302. $oCart = BxDolStudioCart::getInstance();
  303. $aItems = $oCart->getByVendor($sVendor);
  304. if(empty($aItems) || !is_array($aItems))
  305. return false;
  306. $aIds = array();
  307. foreach($aItems as $aItem)
  308. $aIds[] = $aItem['item_id'];
  309. $sSid = bx_site_hash();
  310. return BX_DOL_UNITY_URL_MARKET . 'purchase/' . $sVendor . '?sid=' . $sSid . '&products=' . base64_encode(implode(',', $aIds));
  311. }
  312. private function aliasToNameTag($sAlias)
  313. {
  314. return $this->aliasToName('tag', $sAlias);
  315. }
  316. private function aliasToNameCategory($sAlias)
  317. {
  318. return $this->aliasToName('category', $sAlias);
  319. }
  320. private function aliasToName($sType, $sAlias)
  321. {
  322. return isset($this->aAlias[$sType][$sAlias]) ? $this->aAlias[$sType][$sAlias] : $sAlias;
  323. }
  324. }
  325. /** @} */