BxMarketApiModule.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 MarketApi MarketApi
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. define('BX_MARKET_API_LICENSE_TYPE_NETWORK', 'network');
  12. /**
  13. * MarketApi module
  14. */
  15. class BxMarketApiModule extends BxBaseModGeneralModule
  16. {
  17. function __construct(&$aModule)
  18. {
  19. parent::__construct($aModule);
  20. }
  21. public function actionGetUsers()
  22. {
  23. $sTerm = bx_get('term');
  24. $aResult = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  25. echoJson($aResult);
  26. }
  27. public function actionJsonBrowseAll()
  28. {
  29. $aParams = $this->getBrowseParams();
  30. $aResults = $this->getBrowseProducts('latest', $aParams);
  31. echoJson($aResults);
  32. }
  33. public function actionJsonBrowseFeatured()
  34. {
  35. $aParams = $this->getBrowseParams();
  36. $aParams['value'] = $this->_oConfig->getMarketMainSellerId();
  37. $aResults = $this->getBrowseProducts('featured', $aParams);
  38. echoJson($aResults);
  39. }
  40. public function actionJsonBrowseSelected()
  41. {
  42. $aParams = $this->getBrowseParams();
  43. $aParams['selected'] = array();
  44. if(bx_get('products') !== false)
  45. $aParams['selected'] = unserialize(base64_decode(bx_get('products')));
  46. $aResults = $this->getBrowseProducts('selected', $aParams);
  47. echoJson($aResults);
  48. }
  49. public function actionJsonBrowsePurchased()
  50. {
  51. $aParams = $this->getBrowseParams();
  52. $aParams['short'] = true;
  53. if(empty($aParams['client']) && !empty($aParams['key']))
  54. $aParams['client'] = $this->_getClientByKey($aParams['key']);
  55. if(empty($aParams['client']))
  56. return echoJson(array());
  57. $aResults = $this->getBrowseProducts('purchased', $aParams);
  58. echoJson($aResults);
  59. }
  60. public function actionJsonBrowseByCategory()
  61. {
  62. $aParams = $this->getBrowseParams();
  63. $aCatName2Id = array(
  64. 'templates' => 36,
  65. 'translations' => 38
  66. );
  67. $aCategories = array_keys(BxDolFormQuery::getDataItems($this->_oConfig->CNF['MOBJECT_CATEGORY']));
  68. foreach($aCatName2Id as $sCat => $iId)
  69. unset($aCategories[$iId]);
  70. $aCatName2Id['extensions'] = $aCategories;
  71. $aParams['value'] = $aCatName2Id[$aParams['value']];
  72. $aResults = $this->getBrowseProducts('category', $aParams);
  73. echoJson($aResults);
  74. }
  75. public function actionJsonBrowseByTag()
  76. {
  77. $aParams = $this->getBrowseParams();
  78. $aResults = $this->getBrowseProducts('tag', $aParams);
  79. echoJson($aResults);
  80. }
  81. public function actionJsonBrowseByVendor()
  82. {
  83. $aParams = $this->getBrowseParams();
  84. $aResults = $this->getBrowseProducts('vendor', $aParams);
  85. echoJson($aResults);
  86. }
  87. public function actionJsonBrowseByKeyword()
  88. {
  89. $aParams = $this->getBrowseParams();
  90. $aResults = $this->getBrowseProducts('keyword', $aParams);
  91. echoJson($aResults);
  92. }
  93. public function actionJsonBrowseUpdates()
  94. {
  95. $aResults = array();
  96. $sProducts = bx_get('products') !== false ? bx_get('products') : '';
  97. if(empty($sProducts))
  98. return echoJson($aResults);
  99. $aProducts = unserialize(base64_decode($sProducts));
  100. $aParams = $this->getBrowseParams();
  101. if(empty($aParams['key']))
  102. return echoJson($aResults);
  103. if(empty($aParams['client']))
  104. $aParams['client'] = $this->_getClientByKey($aParams['key']);
  105. $aResults = $this->getBrowseUpdates($aProducts, $aParams, false);
  106. echoJson($aResults);
  107. }
  108. public function actionJsonGetProductById()
  109. {
  110. $aResults = $this->getEntryBy('id');
  111. echoJson($aResults);
  112. }
  113. public function actionJsonGetProductByUri()
  114. {
  115. //TODO: check where it's used. Need to recreated because there is no URI anymore.
  116. echoJson(array());
  117. }
  118. public function actionJsonGetProductByName()
  119. {
  120. $aResults = $this->getEntryBy('name');
  121. echoJson($aResults);
  122. }
  123. public function actionJsonDownloadUpdate()
  124. {
  125. $aResults = array();
  126. $sKey = bx_get('key') !== false ? bx_get('key') : '';
  127. $sProduct = bx_get('product') !== false ? bx_get('product') : '';
  128. if(empty($sProduct))
  129. return echoJson($aResults);
  130. $aProduct = unserialize(base64_decode($sProduct));
  131. $iClient = $this->_getClientByKey($sKey);
  132. if(empty($iClient))
  133. return echoJson($aResults);
  134. $aUpdate = $this->_getUpdate($iClient, $aProduct, $sKey, false);
  135. if(empty($aUpdate) || empty($aUpdate['file_id']))
  136. return echoJson($aResults);
  137. $aResults = $this->_getFile((int)$aUpdate['file_id']);
  138. echoJson($aResults);
  139. }
  140. public function actionPurchase($iVendor)
  141. {
  142. $CNF = &$this->_oConfig->CNF;
  143. $sSessionKeysPrefix = $this->_oConfig->getSessionKeysPrefix();
  144. if(empty($iVendor) || bx_get('products') === false)
  145. $this->_oTemplate->displayNoData('Wrong incomming paramaters. Please report.');
  146. $iModule = $this->_oConfig->getMarketModuleId();
  147. $bUser = isLogged();
  148. $sSid = bx_process_input(bx_get('sid'));
  149. $oSession = BxDolSession::getInstance();
  150. if($bUser && $oSession->isValue($sSessionKeysPrefix . 'sid') && $oSession->getValue($sSessionKeysPrefix . 'sid') != $sSid) {
  151. $bUser = false;
  152. bx_logout();
  153. }
  154. $sProducts = bx_process_input(bx_get('products'));
  155. $aProducts = explode(',', base64_decode($sProducts));
  156. $aPurchases = array();
  157. foreach($aProducts as $iProduct) {
  158. $aProduct = BxDolService::call($CNF['MARKET'], 'get_entries_by', array(array('type' => 'id', 'value' => $iProduct)));
  159. $aPurchases[] = array($iModule, $aProduct[$CNF['MFIELD_AUTHOR']], $aProduct[$CNF['MFIELD_ID']], 1);
  160. }
  161. $oSession->setValue($sSessionKeysPrefix . 'sid', $sSid);
  162. $oSession->setValue($sSessionKeysPrefix . 'purchase', serialize($aPurchases));
  163. $oSession->setValue($sSessionKeysPrefix . 'vendor', $iVendor);
  164. if(isset($_SERVER["HTTP_REFERER"]))
  165. $oSession->setValue($sSessionKeysPrefix . 'referer', bx_append_url_params($_SERVER["HTTP_REFERER"], array('vendor' => $iVendor, 'products' => $sProducts)));
  166. if($bUser)
  167. $this->addToCart();
  168. header('Location: ' . BX_DOL_URL_ROOT . 'page/login');
  169. exit;
  170. }
  171. public function actionSubscribe($iVendor, $iItem)
  172. {
  173. $CNF = &$this->_oConfig->CNF;
  174. $sSessionKeysPrefix = $this->_oConfig->getSessionKeysPrefix();
  175. if(empty($iVendor) || empty($iItem))
  176. $this->_oTemplate->displayNoData('Wrong incomming paramaters. Please report.');
  177. $iModule = $this->_oConfig->getMarketModuleId();
  178. $bUser = isLogged();
  179. $sSid = bx_process_input(bx_get('sid'));
  180. $oSession = BxDolSession::getInstance();
  181. if($bUser && $oSession->isValue($sSessionKeysPrefix . 'sid') && $oSession->getValue($sSessionKeysPrefix . 'sid') != $sSid) {
  182. $bUser = false;
  183. bx_logout();
  184. }
  185. $oSession->setValue($sSessionKeysPrefix . 'sid', $sSid);
  186. $oSession->setValue($sSessionKeysPrefix . 'subscribe', serialize(array($iModule, $iVendor, $iItem, 1)));
  187. if(isset($_SERVER["HTTP_REFERER"]))
  188. $oSession->setValue($sSessionKeysPrefix . 'referer', bx_append_url_params($_SERVER["HTTP_REFERER"], array('page' => 'checkout', 'vendor' => $iVendor, 'products' => '')));
  189. if($bUser)
  190. $this->subscribe();
  191. header('Location: ' . BX_DOL_URL_ROOT . 'page/login');
  192. exit;
  193. }
  194. public function serviceBrowsePurchased()
  195. {
  196. $CNF = &$this->_oConfig->CNF;
  197. $aParams = $this->getBrowseParams();
  198. if(empty($aParams['key']) || empty($aParams['domain']))
  199. return _t('_bx_market_api_err_wrong_data');
  200. $aClient = $this->_getOauthClientByKey($aParams['key']);
  201. if(empty($aClient) || !is_array($aClient))
  202. return _t('_bx_market_api_err_wrong_data');
  203. if(strcmp($aParams['domain'], $this->getBrowseParamsDomain($aClient['redirect_uri'])) != 0)
  204. return _t('_bx_market_api_err_ks_used');
  205. $aParams['parent'] = (int)$aClient['parent_id'];
  206. $aParams['client'] = (int)$aClient['user_id'];
  207. $aParams['products'] = bx_get('products') !== false ? unserialize(base64_decode(bx_get('products'))) : array();
  208. $aResults = $this->getBrowseProducts('purchased', $aParams);
  209. if(empty($aParams['parent']))
  210. return $aResults;
  211. $iProfile = (int)$aParams['parent'];
  212. $oProfile = BxDolProfile::getInstance($iProfile);
  213. if(!$oProfile)
  214. return $aResults;
  215. $aLicenses = $this->_oDb->getLicense(array('type' => 'profile_id_type', 'profile_id' => $iProfile, 'type' => BX_MARKET_API_LICENSE_TYPE_NETWORK));
  216. if(empty($aLicenses) || !is_array($aLicenses))
  217. return $aResults;
  218. foreach($aLicenses as $aLicense) {
  219. $aLicenseInfo = BxDolService::call($CNF['MARKET'], 'get_license', array(array('type' => 'id', 'id' => $aLicense[$CNF['FIELD_LICENSE_ID']])));
  220. $aResults = array_merge($aResults, $this->getBrowseProducts('granted', array(
  221. 'value' => $this->_oConfig->getMarketMainSellerId(),
  222. 'license' => $aLicenseInfo
  223. )));
  224. }
  225. return $aResults;
  226. }
  227. public function serviceBrowseUpdates()
  228. {
  229. $CNF = &$this->_oConfig->CNF;
  230. $aParams = $this->getBrowseParams();
  231. if(empty($aParams['key']))
  232. return _t('_bx_market_api_err_wrong_data');
  233. $aClient = $this->_getOauthClientByKey($aParams['key']);
  234. if(empty($aClient) || !is_array($aClient))
  235. return _t('_bx_market_api_err_wrong_data');
  236. $aParams['client'] = (int)$aClient['user_id'];
  237. $aProducts = bx_get('products') !== false ? unserialize(base64_decode(bx_get('products'))) : array();
  238. return $this->getBrowseUpdates($aProducts, $aParams, true);
  239. }
  240. public function serviceDownloadFile()
  241. {
  242. $CNF = &$this->_oConfig->CNF;
  243. $sKey = bx_process_input(bx_get('key'), BX_DATA_TEXT);
  244. $aClient = $this->_getOauthClientByKey($sKey);
  245. if(empty($aClient) || !is_array($aClient))
  246. return _t('_bx_market_api_err_wrong_data');
  247. $iClient = (int)$aClient['user_id'];
  248. $iFile = (int)bx_get('file_id');
  249. $aEntry = BxDolService::call($CNF['MARKET'], 'get_entries_by', array(array(
  250. 'type' => 'file_id',
  251. 'value' => $iFile
  252. )));
  253. if(empty($aEntry))
  254. return array();
  255. $aFile = $this->_getFile($iFile);
  256. $bFree = (float)$aEntry[$CNF['MFIELD_PRICE_SINGLE']] == 0 && (float)$aEntry[$CNF['MFIELD_PRICE_RECURRING']] == 0;
  257. if(!empty($aFile) && !empty($sKey) && !$bFree && !in_array($aEntry['id'], $this->_getGranted($sKey))) {
  258. $aLicense = BxDolService::call($CNF['MARKET'], 'get_license', array(array(
  259. 'type' => 'profile_id_file_id_key',
  260. 'profile_id' => $iClient,
  261. 'file_id' => $iFile,
  262. 'key' => $sKey
  263. )));
  264. if(empty($aLicense))
  265. return array();
  266. if(empty($aLicense['domain']))
  267. BxDolService::call($CNF['MARKET'], 'update_license', array(array('domain' => $sKey), array('id' => $aLicense['id'])));
  268. }
  269. return $aFile;
  270. }
  271. public function serviceGetBlockKandsManage()
  272. {
  273. $CNF = &$this->_oConfig->CNF;
  274. $sMenu = '';
  275. $sGrid = 'OBJECT_GRID_KANDS';
  276. $aLicenses = $this->_oDb->getLicense(array('type' => 'profile_id_type', 'profile_id' => bx_get_logged_profile_id(), 'type' => BX_MARKET_API_LICENSE_TYPE_NETWORK));
  277. if(!empty($aLicenses) && is_array($aLicenses)) {
  278. $sUriParam = 'clients';
  279. $bClients = (int)bx_get($sUriParam) == 1;
  280. if($bClients)
  281. $sGrid = 'OBJECT_GRID_KANDS_CLIENTS';
  282. $sLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_KANDS_MANAGE']);
  283. $oMenu = new BxTemplMenu(array(
  284. 'template' => 'menu_vertical.html',
  285. 'menu_items' => array(
  286. array('id' => 'manage-myself', 'name' => 'manage-myself', 'class' => '', 'link' => $sLink, 'target' => '_self', 'title' => _t('_bx_market_api_menu_item_title_kands_manage_myself'), 'active' => 1),
  287. array('id' => 'manage-clients', 'name' => 'manage-clients', 'class' => '', 'link' => bx_append_url_params($sLink, array($sUriParam => 1)), 'target' => '_self', 'title' => _t('_bx_market_api_menu_item_title_kands_manage_clients'), 'active' => 1)
  288. )
  289. ), $this->_oTemplate);
  290. $oMenu->setSelected($this->_aModule['name'], 'manage-' . ($bClients ? 'clients' : 'myself'));
  291. $sMenu = $oMenu->getCode();
  292. }
  293. $oGrid = BxDolGrid::getObjectInstance($CNF[$sGrid]);
  294. if($oGrid)
  295. return array(
  296. 'content' => $oGrid->getCode(),
  297. 'menu' => $sMenu
  298. );
  299. $this->_oTemplate->displayPageNotFound();
  300. }
  301. public function checkAllowedPass($aDataEntry, $isPerformAction = false)
  302. {
  303. $iProfile = bx_get_logged_profile_id();
  304. if($aDataEntry['user_id'] != $iProfile)
  305. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  306. if(isAdmin())
  307. return CHECK_ACTION_RESULT_ALLOWED;
  308. // check ACL
  309. $aCheck = checkActionModule($iProfile, 'pass', $this->getName(), $isPerformAction);
  310. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  311. return $aCheck[CHECK_ACTION_MESSAGE];
  312. return CHECK_ACTION_RESULT_ALLOWED;
  313. }
  314. public function addToCart()
  315. {
  316. $sSessionKeysPrefix = $this->_oConfig->getSessionKeysPrefix();
  317. $oSession = BxDolSession::getInstance();
  318. $sSid = $oSession->getValue($sSessionKeysPrefix . 'sid');
  319. $sPurchase = $oSession->getUnsetValue($sSessionKeysPrefix . 'purchase');
  320. $iVendor = (int)$oSession->getUnsetValue($sSessionKeysPrefix . 'vendor');
  321. if(empty($sSid) || empty($sPurchase) || empty($iVendor))
  322. return;
  323. $oPayment = BxDolPayments::getInstance();
  324. $aProducts = unserialize($sPurchase);
  325. foreach($aProducts as $aProduct) {
  326. list($iModuleId, $iVendorId, $iItemId, $iItemCount) = $aProduct;
  327. $oPayment->addToCart($iVendorId, $iModuleId, $iItemId, $iItemCount);
  328. }
  329. header('Location: ' . $oPayment->getCartUrl($iVendor));
  330. exit;
  331. }
  332. public function subscribe()
  333. {
  334. $sSessionKeysPrefix = $this->_oConfig->getSessionKeysPrefix();
  335. $oSession = BxDolSession::getInstance();
  336. $sSid = $oSession->getValue($sSessionKeysPrefix . 'sid');
  337. $sSubscribe = $oSession->getUnsetValue($sSessionKeysPrefix . 'subscribe');
  338. if(empty($sSid) || empty($sSubscribe))
  339. return;
  340. list($iModuleId, $iVendorId, $iItemId, $iItemCount) = unserialize($sSubscribe);
  341. $sVendorProvider = $this->_oConfig->getMarketMainSellerSbsProvider();
  342. header('Location: ' . BxDolPayments::getInstance()->getSubscribeUrl($iVendorId, $sVendorProvider, $iModuleId, $iItemId, $iItemCount));
  343. exit;
  344. }
  345. public function redirectAfterPayment()
  346. {
  347. $sSessionKeysPrefix = $this->_oConfig->getSessionKeysPrefix();
  348. $oSession = BxDolSession::getInstance();
  349. $sSid = $oSession->getUnsetValue($sSessionKeysPrefix . 'sid');
  350. $sReferer = $oSession->getUnsetValue($sSessionKeysPrefix . 'referer');
  351. if(empty($sSid) || empty($sReferer))
  352. return;
  353. header('Location: ' . $sReferer);
  354. exit;
  355. }
  356. protected function getEntryBy($sType)
  357. {
  358. $CNF = &$this->_oConfig->CNF;
  359. if(!in_array($sType, array('id', 'name')))
  360. return array();
  361. $aEntry = BxDolService::call($CNF['MARKET'], 'get_entry_by', array($sType, ($sType == 'id' ? (int)bx_get('value') : bx_process_input(bx_get('value'), BX_DATA_TEXT))));
  362. if(empty($aEntry))
  363. return array();
  364. $iClient = 0;
  365. $oClient = null;
  366. if(bx_get('client') !== false) {
  367. $iClient = (int)bx_get('client');
  368. $oClient = BxDolProfile::getInstance($iClient);
  369. }
  370. $sKey = '';
  371. if(bx_get('key') !== false)
  372. $sKey = bx_process_input(bx_get('key'), BX_DATA_TEXT);
  373. $oAuthor = BxDolProfile::getInstance($aEntry[$CNF['MFIELD_AUTHOR']]);
  374. $aCurrency = $this->_oConfig->getCurrency();
  375. $bFree = (float)$aEntry[$CNF['MFIELD_PRICE_SINGLE']] == 0 && (float)$aEntry[$CNF['MFIELD_PRICE_RECURRING']] == 0;
  376. $bPurchased = $oClient ? BxDolService::call($CNF['MARKET'], 'has_license', array($iClient, $aEntry[$CNF['MFIELD_ID']])) || in_array($aEntry[$CNF['MFIELD_ID']], $this->_getGranted($sKey)) : false;
  377. $bDownloadable = ($bFree || $bPurchased) && (int)$aEntry[$CNF['MFIELD_PACKAGE']] != 0;
  378. $sViewUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['MURL_VIEW_ENTRY'] . $aEntry[$CNF['MFIELD_ID']]);
  379. $sCategory = $sCategoryUrl = '';
  380. $iCategory = (int)$aEntry[$CNF['MFIELD_CATEGORY']];
  381. if($iCategory != 0) {
  382. $oCategory = BxDolCategory::getObjectInstance($CNF['MOBJECT_CATEGORY']);
  383. if($oCategory) {
  384. $aCategories = BxDolFormQuery::getDataItems($CNF['MOBJECT_CATEGORY']);
  385. $sCategory = isset($aCategories[$iCategory]) ? $aCategories[$iCategory] : '';
  386. $sCategoryUrl = $oCategory->getCategoryUrl($iCategory);
  387. }
  388. }
  389. $aFile = BxDolService::call($CNF['MARKET'], 'get_file', array($aEntry[$CNF['MFIELD_PACKAGE']]));
  390. $aScreenshots = array();
  391. $aImages = BxDolService::call($CNF['MARKET'], 'get_screenshots', array($aEntry[$CNF['MFIELD_ID']]));
  392. foreach($aImages as $aImage) {
  393. $aScreenshots[] = array(
  394. 'id' => $aImage['img_id'],
  395. 'small' => $aImage['url_sm'],
  396. 'big' => $aImage['url_bg']
  397. );
  398. }
  399. return array(
  400. 'id' => $aEntry[$CNF['MFIELD_ID']],
  401. 'name' => $aEntry[$CNF['MFIELD_NAME']],
  402. 'title' => $aEntry[$CNF['MFIELD_TITLE']],
  403. 'url' => $sViewUrl,
  404. 'description' => $aEntry[$CNF['MFIELD_TEXT']],
  405. 'description_plain' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aEntry[$CNF['MFIELD_TEXT']]), (int)getParam($CNF['PARAM_PLAIN_DESCRIPTION_CHARS'])),
  406. 'author_id' => $aEntry[$CNF['MFIELD_AUTHOR']],
  407. 'author_name' => $oAuthor->getDisplayName(),
  408. 'author_url' => $oAuthor->getUrl(),
  409. 'author_currency_code' => $aCurrency['code'],
  410. 'author_currency_sign' => $aCurrency['sign'],
  411. 'created' => $aEntry[$CNF['MFIELD_ADDED']],
  412. 'updated' => $aEntry[$CNF['MFIELD_CHANGED']],
  413. 'version' => $aFile[$CNF['MFIELD_FILE_VERSION']],
  414. //--- Single price related fields
  415. 'price_single' => $aEntry[$CNF['MFIELD_PRICE_SINGLE']],
  416. 'discount_single' => array(), //TODO: need to have Discounted Price in Market if we need this feature at all. $aEntry['entry_price_full'] != 0 && $aEntry['entry_price_full'] != $aEntry['entry_price_discounted'] ? $this->rScript->_prepareDiscount($aEntry['entry_price_full'], $aEntry['entry_price_discounted']) : array(),
  417. //--- Recurring price related fields
  418. 'price_recurring' => $aEntry[$CNF['MFIELD_PRICE_RECURRING']],
  419. 'duration_recurring' => $aEntry[$CNF['MFIELD_DURATION_RECURRING']],
  420. 'category' => $sCategory,
  421. 'category_url' => $sCategoryUrl,
  422. 'tags' => '', //TODO: integrate tags
  423. 'reviews_cnt' => $aEntry[$CNF['MFIELD_COMMENTS']],
  424. 'reviews_url' => $sViewUrl . '#cmts-anchor-bx-market-' . $aEntry[$CNF['MFIELD_ID']],
  425. 'views_cnt' => $aEntry[$CNF['MFIELD_VIEWS']],
  426. 'votes_cnt' => $aEntry[$CNF['MFIELD_VOTES']],
  427. 'rate' => $aEntry[$CNF['MFIELD_RATE']],
  428. 'screenshots' => $aScreenshots,
  429. 'is_file' => $bDownloadable ? 1 : 0,
  430. 'file_id' => $bDownloadable ? $aEntry[$CNF['MFIELD_PACKAGE']] : 0,
  431. 'is_free' => $bFree ? 1 : 0,
  432. 'is_purchased' => $bPurchased ? 1 : 0,
  433. );
  434. }
  435. protected function getBrowseParams()
  436. {
  437. $aParams = array();
  438. if(bx_get('value') !== false)
  439. $aParams['value'] = bx_process_input(bx_get('value'), BX_DATA_TEXT);
  440. if(bx_get('order_by') !== false)
  441. $aParams['order_by'] = bx_process_input(bx_get('order_by'), BX_DATA_TEXT);
  442. if(bx_get('order_trend') !== false)
  443. $aParams['order_trend'] = bx_process_input(bx_get('order_trend'), BX_DATA_TEXT);
  444. $aParams['start'] = bx_get('start') !== false ? (int)bx_get('start') : 0;
  445. $aParams['per_page'] = bx_get('per_page') !== false ? (int)bx_get('per_page') : 10;
  446. $aParams['key'] = bx_get('key') !== false ? bx_process_input(bx_get('key'), BX_DATA_TEXT) : '';
  447. $aParams['key_assigned'] = bx_get('key_assigned') !== false ? (int)bx_get('key_assigned') : 0;
  448. $aParams['parent'] = bx_get('parent') !== false ? (int)bx_get('parent') : 0;
  449. $aParams['client'] = bx_get('client') !== false ? (int)bx_get('client') : 0;
  450. $aParams['domain'] = $this->getBrowseParamsDomain();
  451. return $aParams;
  452. }
  453. protected function getBrowseParamsDomain($sDomain = '')
  454. {
  455. if(empty($sDomain) && bx_get('domain') !== false)
  456. $sDomain = bx_process_input(bx_get('domain'), BX_DATA_TEXT);
  457. if(empty($sDomain))
  458. return $sDomain;
  459. $aInfo = parse_url($sDomain);
  460. return !empty($aInfo['host']) ? $aInfo['host'] : '';
  461. }
  462. protected function getBrowseProducts($sType, $aParams = array())
  463. {
  464. $CNF = &$this->_oConfig->CNF;
  465. $aParams['type'] = $sType;
  466. $aEntries = BxDolService::call($CNF['MARKET'], 'get_entries_by', array($aParams));
  467. $this->_prepareForOutputEntries($aEntries, false, $aParams);
  468. return $aEntries;
  469. }
  470. protected function getBrowseUpdates($aProducts, $aParams = array(), $bAuthorized = true)
  471. {
  472. $aResults = array();
  473. $iClient = isset($aParams['client']) ? (int)$aParams['client'] : 0;
  474. if(empty($aProducts) || !is_array($aProducts) || empty($iClient))
  475. return $aResults;
  476. foreach($aProducts as $aProduct) {
  477. $aInfo = $this->_getUpdate($iClient, $aProduct, $aParams['key'], $bAuthorized);
  478. if(empty($aInfo) || !is_array($aInfo))
  479. continue;
  480. $aResults[] = $aInfo;
  481. }
  482. $this->_prepareForOutputEntries($aResults, true);
  483. return $aResults;
  484. }
  485. protected function _prepareForOutputEntries(&$aItems, $bForcePurchased = false, $aParams = array())
  486. {
  487. $CNF = &$this->_oConfig->CNF;
  488. $oPermalink = BxDolPermalinks::getInstance();
  489. $aCurrency = $this->_oConfig->getCurrency();
  490. $iRateMin = $iRateMax = 1;
  491. $aVoteSystems = BxDolVote::getSystems();
  492. if(!empty($aVoteSystems[$CNF['MOBJECT_VOTES']]) && is_array($aVoteSystems[$CNF['MOBJECT_VOTES']])) {
  493. $iRateMin = $aVoteSystems[$CNF['MOBJECT_VOTES']]['min_value'];
  494. $iRateMax = $aVoteSystems[$CNF['MOBJECT_VOTES']]['max_value'];
  495. }
  496. $bShort = isset($aParams['short']) && $aParams['short'] === true;
  497. $sKey = !empty($aParams['key']) ? $aParams['key'] : '';
  498. $aInstalled = !empty($aParams['products']) && is_array($aParams['products']) ? $aParams['products'] : array();
  499. $aResults = array();
  500. foreach($aItems as $aItem) {
  501. $oAuthor = BxDolProfile::getInstance($aItem[$CNF['MFIELD_AUTHOR']]);
  502. $bFree = (float)$aItem[$CNF['MFIELD_PRICE_SINGLE']] == 0 && (float)$aItem[$CNF['MFIELD_PRICE_RECURRING']] == 0;
  503. $bPurchased = (isset($aItem['purchased_on']) && (int)$aItem['purchased_on'] != 0) || $bForcePurchased;
  504. $bDownloadable = ($bFree || $bPurchased) && (int)$aItem['file_id'] != 0;
  505. /**
  506. * Associate license with Key if App is installed and user has non-associated license for this App.
  507. */
  508. $bPurchasedAndAvailable = $bPurchased && !empty($aItem['license']) && isset($aItem['purchased_for']) && $aItem['purchased_for'] == '';
  509. if(array_key_exists($aItem[$CNF['MFIELD_NAME']], $aInstalled) && $bPurchasedAndAvailable)
  510. if(BxDolService::call($CNF['MARKET'], 'update_license', array(array('domain' => $sKey), array('license' => $aItem['license']))))
  511. $aItem['purchased_for'] = $sKey;
  512. $aDiscount = array();
  513. /*
  514. //TODO: Process discount if the feature was added.
  515. if(!$bFree && $aItem[$CNF['MFIELD_PRICE_DISCOUNT']] != 0) {
  516. $aDiscount = array(
  517. 'percent' => $aItem[$CNF['MFIELD_PRICE_DISCOUNT']],
  518. 'save' => round($aItem[$CNF['MFIELD_PRICE_DISCOUNT']] * $aItem[$CNF['MFIELD_PRICE_SINGLE']] / 100, 2),
  519. 'price' => round((1 - $CNF['MFIELD_PRICE_DISCOUNT'] / 100) * $aItem[$CNF['MFIELD_PRICE_SINGLE']], 2)
  520. );
  521. }
  522. */
  523. if($bShort)
  524. $aResult = array(
  525. 'name' => $aItem[$CNF['MFIELD_NAME']],
  526. );
  527. else
  528. $aResult = array(
  529. 'id' => $aItem[$CNF['MFIELD_ID']],
  530. 'name' => $aItem[$CNF['MFIELD_NAME']],
  531. 'title' => $aItem[$CNF['MFIELD_TITLE']],
  532. 'description' => strip_tags($aItem[$CNF['MFIELD_TEXT']]),
  533. 'description_plain' => BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($aItem[$CNF['MFIELD_TEXT']]), (int)getParam($CNF['PARAM_PLAIN_DESCRIPTION_CHARS'])),
  534. 'url' => BX_DOL_URL_ROOT . $oPermalink->permalink($CNF['MURL_VIEW_ENTRY'] . $aItem[$CNF['MFIELD_ID']]),
  535. 'thumbnail' => BxDolService::call($CNF['MARKET'], 'get_thumbnail', array($aItem[$CNF['MFIELD_THUMB']])),
  536. //--- Single price related fields
  537. 'price_single' => $aItem[$CNF['MFIELD_PRICE_SINGLE']],
  538. 'discount_single' => $aDiscount,
  539. //--- Recurring price related fields
  540. 'price_recurring' => $aItem[$CNF['MFIELD_PRICE_RECURRING']],
  541. 'duration_recurring' => $aItem[$CNF['MFIELD_DURATION_RECURRING']],
  542. 'views_cnt' => $aItem[$CNF['MFIELD_VIEWS']],
  543. 'votes_cnt' => $aItem[$CNF['MFIELD_VOTES']],
  544. 'rate' => $aItem[$CNF['MFIELD_RATE']],
  545. 'rate_min' => $iRateMin,
  546. 'rate_max' => $iRateMax,
  547. 'created_date' => $aItem[$CNF['MFIELD_ADDED']],
  548. 'author_id' => $aItem[$CNF['MFIELD_AUTHOR']],
  549. 'author_name' => $oAuthor->getDisplayName(),
  550. 'author_url' => $oAuthor->getUrl(),
  551. 'author_currency_code' => $aCurrency['code'],
  552. 'author_currency_sign' => $aCurrency['sign'],
  553. 'is_file' => $bDownloadable ? 1 : 0,
  554. 'file_id' => $bDownloadable ? $aItem['file_id'] : 0,
  555. 'file_version' => $bDownloadable ? $aItem['file_version'] : '',
  556. 'file_version_to' => $bDownloadable && isset($aItem['file_version_to']) ? $aItem['file_version_to'] : '',
  557. 'is_free' => $bFree ? 1 : 0,
  558. 'is_purchased' => $bPurchased ? 1 : 0,
  559. 'purchased_for' => $bPurchased && isset($aItem['purchased_for']) ? $aItem['purchased_for'] : '',
  560. 'purchased_on' => $bPurchased && isset($aItem['purchased_on']) ? $aItem['purchased_on'] : 0,
  561. 'license' => $bPurchased && isset($aItem['license']) ? $aItem['license'] : ''
  562. );
  563. if($bPurchased && !empty($aItem['purchased_by']))
  564. $aResult['hash'] = $this->_getHash((int)$aItem[$CNF['MFIELD_ID']], $aItem['purchased_by'], $sKey);
  565. $aResults[] = $aResult;
  566. }
  567. $aItems = $aResults;
  568. }
  569. protected function _getHash($iProductId, $iClientId, $sClientKey)
  570. {
  571. if(empty($iClientId))
  572. return '';
  573. $aInfo = array(
  574. 'product' => $iProductId,
  575. 'client_ip' => $_SERVER['REMOTE_ADDR'],
  576. 'client_key' => $sClientKey,
  577. 'salt' => $iClientId
  578. );
  579. return md5(implode('-', $aInfo));
  580. }
  581. protected function _getFile($iId)
  582. {
  583. $CNF = &$this->_oConfig->CNF;
  584. $aFile = BxDolService::call($CNF['MARKET'], 'get_file', array($iId));
  585. $aEntry = BxDolService::call($CNF['MARKET'], 'get_entry_by', array('id', $aFile['content_id']));
  586. if(empty($aFile) || !is_array($aFile) || empty($aEntry) || !is_array($aEntry))
  587. return array();
  588. return array(
  589. 'module_name' => $aEntry[$CNF['MFIELD_NAME']],
  590. 'id' => $aFile['file_id'],
  591. 'name' => $aFile['file_name'],
  592. 'size' => $aFile['file_size'],
  593. 'content' => urlencode(bx_file_get_contents($aFile['file_url']))
  594. );
  595. }
  596. protected function _getUpdate($iClient, $aEntry, $sKey, $bAuthorized = true)
  597. {
  598. $CNF = &$this->_oConfig->CNF;
  599. $aEntryInfo = BxDolService::call($CNF['MARKET'], 'get_entry_by', array('name', $aEntry[$CNF['MFIELD_NAME']]));
  600. if(empty($aEntryInfo) || !is_array($aEntryInfo) || version_compare($aEntryInfo['file_version'], $aEntry['version']) == 0)
  601. return array();
  602. $bFree = (float)$aEntryInfo[$CNF['MFIELD_PRICE_SINGLE']] == 0 && (float)$aEntryInfo[$CNF['MFIELD_PRICE_RECURRING']] == 0;
  603. $bGranted = in_array($aEntryInfo[$CNF['MFIELD_ID']], $this->_getGranted($sKey));
  604. if(!$bAuthorized && !$bFree && !$bGranted && strcmp($aEntry['hash'], $this->_getHash($aEntryInfo[$CNF['MFIELD_ID']], $iClient, $sKey)) != 0)
  605. return array();
  606. if(!$bFree && !$bGranted && !BxDolService::call($CNF['MARKET'], 'has_license', array($iClient, $aEntryInfo[$CNF['MFIELD_ID']], $sKey)))
  607. return array();
  608. $aUpdate = BxDolService::call($CNF['MARKET'], 'get_updates', array($aEntryInfo[$CNF['MFIELD_ID']], $aEntry['version']));
  609. if(empty($aUpdate) || !is_array($aUpdate))
  610. return array();
  611. return array_merge($aEntryInfo, array(
  612. 'file_id' => $aUpdate['file_id'],
  613. 'file_name' => $aUpdate['file_name'],
  614. 'file_version' => $aUpdate['version'],
  615. 'file_version_to' => $aUpdate['version_to'],
  616. ));
  617. }
  618. protected function _getGranted($sKey)
  619. {
  620. $CNF = &$this->_oConfig->CNF;
  621. $aOauthClient = $this->_getOauthClientByKey($sKey);
  622. if(empty($aOauthClient) || empty($aOauthClient['parent_id']))
  623. return array();
  624. $iParent = (int)$aOauthClient['parent_id'];
  625. $oParent = BxDolProfile::getInstance($iParent);
  626. if(!$oParent)
  627. return array();
  628. $aLicense = BxDolService::call($CNF['MARKET'], 'get_license', array(array('type' => 'profile_id', 'profile_id' => $iParent)));
  629. if(empty($aLicense) || !is_array($aLicense))
  630. return array();
  631. $aLicenseInfo = $this->_oDb->getLicense(array(
  632. 'type' => 'license_id_profile_id',
  633. 'license_id' => $aLicense['id'],
  634. 'profile_id' => $iParent));
  635. if(empty($aLicenseInfo) || !is_array($aLicenseInfo) || $aLicenseInfo[$CNF['FIELD_TYPE']] != BX_MARKET_API_LICENSE_TYPE_NETWORK)
  636. return array();
  637. $aEntries = BxDolService::call($CNF['MARKET'], 'get_entries_by', array(array(
  638. 'type' => 'vendor',
  639. 'value' => $this->_oConfig->getMarketMainSellerId()
  640. )));
  641. if(empty($aEntries) || !is_array($aEntries))
  642. return array();
  643. foreach($aEntries as $aEntry)
  644. $aResult[] = $aEntry['id'];
  645. return $aResult;
  646. }
  647. protected function _getClientByKey($sKey)
  648. {
  649. $aOauthClient = $this->_getOauthClientByKey($sKey);
  650. return !empty($aOauthClient['user_id']) ? (int)$aOauthClient['user_id'] : 0;
  651. }
  652. protected function _getOauthClientByKey($sKey)
  653. {
  654. return BxDolService::call($this->_oConfig->CNF['OAUTH'], 'get_clients_by', array(array('type' => 'client_id', 'client_id' => $sKey)));
  655. }
  656. }
  657. /** @} */