BxOAuthAPI.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup OAuth2 OAuth2 server
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. use OAuth2\Response;
  12. class BxOAuthAPI extends BxDol
  13. {
  14. protected $_oModule;
  15. protected $_oDb;
  16. public $aAction2Scope = array (
  17. 'me' => 'basic,market',
  18. 'user' => 'basic',
  19. 'friends' => 'basic',
  20. 'service' => 'service',
  21. 'market' => 'market',
  22. );
  23. function __construct($oModule)
  24. {
  25. $this->_oModule = $oModule;
  26. $this->_oDb = $oModule->_oDb;
  27. }
  28. function me($aToken)
  29. {
  30. if (!($oProfile = BxDolProfile::getInstance($aToken['user_id']))) {
  31. $this->errorOutput('404', 'not_found', 'Profile was not found');
  32. return;
  33. }
  34. $this->output($this->_prepareProfileArray($oProfile, false));
  35. }
  36. function user($aToken)
  37. {
  38. $iProfileId = (int)bx_get('id');
  39. if ($iProfileId == $aToken['user_id']) {
  40. $this->me($aToken);
  41. return;
  42. }
  43. if (!($oProfile = $this->_getProfileWithAccessChecking($iProfileId)))
  44. return;
  45. $this->output($this->_prepareProfileArray($oProfile, !isAdmin($aToken['user_id'])));
  46. }
  47. function friends($aToken)
  48. {
  49. $iProfileId = (int)bx_get('id');
  50. if (!($oProfile = $this->_getProfileWithAccessChecking($iProfileId)))
  51. return;
  52. $this->output(array(
  53. 'user_id' => $iProfileId,
  54. 'friends' => getMyFriendsEx($iProfileId),
  55. ));
  56. }
  57. function market($aToken)
  58. {
  59. $_GET['key'] = $_POST['key'] = $aToken['client_id'];
  60. $_GET['module'] = $_POST['module'] = 'bx_market_api';
  61. $this->service($aToken);
  62. }
  63. /**
  64. * Service call should look like this
  65. * http://example.com/m/oauth2/api/service?module=bx_market&method=test&params[]=1&params[]=abc&class=custom_class_name_or_remove_it_if_module_class
  66. * or
  67. * http://example.com/m/oauth2/api/service?module=bx_market&method=test&params=serialized_string_of_params
  68. */
  69. function service($aToken)
  70. {
  71. bx_login($aToken['user_id'], false, false);
  72. $sModule = bx_get('module');
  73. $sMethod = bx_get('method');
  74. if (!($aParams = bx_get('params')))
  75. $aParams = array();
  76. elseif (is_string($aParams) && preg_match('/^a:[\d+]:\{/', $aParams))
  77. $aParams = @unserialize($aParams);
  78. if (!is_array($aParams))
  79. $aParams = array($aParams);
  80. if (!($sClass = bx_get('class')))
  81. $sClass = 'Module';
  82. if (!BxDolRequest::serviceExists($sModule, $sMethod, $sClass)) {
  83. $this->errorOutput(404, 'not_found', 'Service was not found');
  84. return false;
  85. }
  86. $mixedRet = BxDolService::call($sModule, $sMethod, $aParams, $sClass);
  87. $this->output(array(
  88. 'module' => $sModule,
  89. 'method' => $sMethod,
  90. 'data' => $mixedRet,
  91. ));
  92. }
  93. function errorOutput($iHttpCode, $sError, $sErrorDesc)
  94. {
  95. $oReponse = new Response();
  96. $oReponse->setError($iHttpCode, $sError, $sErrorDesc);
  97. $oReponse->send();
  98. }
  99. function output($a)
  100. {
  101. $oReponse = new Response();
  102. $oReponse->setParameters($a);
  103. $oReponse->send();
  104. }
  105. protected function _getProfileWithAccessChecking ($iProfileId)
  106. {
  107. if (!$oProfile = BxDolProfile::getInstance($iProfileId)) {
  108. $this->errorOutput('404', 'not_found', 'Profile was not found');
  109. return false;
  110. }
  111. /*
  112. // TODO: check visibility
  113. if (!bx_check_profile_visibility($iProfileId, $aToken['user_id'], true)) {
  114. $this->errorOutput(403, 'access_denied', 'You have no rights to view this user info');
  115. return false;
  116. }
  117. */
  118. return $oProfile;
  119. }
  120. protected function _prepareProfileArray ($oProfile, $bPublicFieldsOnly = true)
  121. {
  122. $oAccount = $oProfile->getAccountObject();
  123. $aProfileInfo = $oProfile->getInfo();
  124. $aAccountInfo = $oAccount->getInfo();
  125. if ($bPublicFieldsOnly) {
  126. $aProfileInfo = array(
  127. 'id' => $aProfileInfo['id'],
  128. );
  129. }
  130. else {
  131. unset($aProfileInfo['account_id']);
  132. unset($aProfileInfo['content_id']);
  133. unset($aProfileInfo['status']);
  134. $aProfileInfo['email'] = $oAccount->getEmail();
  135. // TODO: fetch extended info from profile module
  136. }
  137. $aProfileInfo['role'] = $aAccountInfo['role'];
  138. $aProfileInfo['name'] = $oAccount->getDisplayName();
  139. $aProfileInfo['profile_display_name'] = $oProfile->getDisplayName();
  140. $aProfileInfo['profile_link'] = $oProfile->getUrl();
  141. $aProfileInfo['picture'] = $oProfile->getPicture();
  142. return $aProfileInfo;
  143. }
  144. }
  145. /** @} */