BxDolStudioStore.php 14 KB

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