BxDolStudioStore.php 14 KB

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