BxDolStudioOAuthPlugin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. use OAuth\ServiceFactory;
  10. use OAuth\OAuth1\Service\Una;
  11. use OAuth\Common\Http\Uri\Uri;
  12. use OAuth\Common\Http\Exception\TokenResponseException;
  13. use OAuth\Common\Storage\Session;
  14. use OAuth\Common\Storage\Exception\TokenNotFoundException;
  15. use OAuth\Common\Consumer\Credentials;
  16. class BxDolStudioOAuthPlugin extends BxDolStudioOAuthOAuth1 implements iBxDolSingleton
  17. {
  18. protected $sService;
  19. protected $oStorage;
  20. protected function __construct()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. parent::__construct ();
  25. $this->sKey = getParam('sys_oauth_key');
  26. $this->sSecret = getParam('sys_oauth_secret');
  27. $this->sDataRetrieveMethod = 'POST';
  28. $this->sService = 'Una';
  29. }
  30. public function __clone()
  31. {
  32. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  33. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  34. }
  35. public static function getInstance()
  36. {
  37. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  38. $sClass = __CLASS__;
  39. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  40. }
  41. return $GLOBALS['bxDolClasses'][__CLASS__];
  42. }
  43. protected function authorize()
  44. {
  45. if($this->isAuthorized())
  46. return true;
  47. $oService = $this->getServiceObject();
  48. try {
  49. $bToken = bx_get('oauth_token') !== false;
  50. $mixedSecret = $this->oSession->getValue(self::$sSessionKeySecret);
  51. if(!$bToken && $mixedSecret !== false) {
  52. $this->oSession->unsetValue(self::$sSessionKeySecret);
  53. $mixedSecret = false;
  54. }
  55. //--- Get request token and redirect to authorize.
  56. if(!$bToken && $mixedSecret === false)
  57. return $this->getRequestToken($oService);
  58. //--- Get access token.
  59. if($bToken && $mixedSecret !== false)
  60. return $this->getAccessToken(bx_get('oauth_token'), bx_get('oauth_verifier'), (int)bx_get('oauth_user'), $oService);
  61. }
  62. catch(TokenNotFoundException $e) {
  63. $this->unsetAuthorizedUser();
  64. return $this->getRequestToken();
  65. }
  66. return $this->getRequestToken();
  67. }
  68. protected function getRequestToken($oService = null)
  69. {
  70. if(empty($oService))
  71. $oService = $this->getServiceObject();
  72. $sError = _t('_adm_err_oauth_cannot_get_token');
  73. try {
  74. $oToken = $oService->requestRequestToken();
  75. }
  76. catch (TokenResponseException $e) {
  77. if(!$this->isAuthorized())
  78. return $sError;
  79. $this->unsetAuthorizedUser();
  80. return $this->getRequestToken($oService);
  81. }
  82. if(empty($oToken))
  83. return $sError;
  84. $this->oSession->setValue(self::$sSessionKeySecret, $oToken->getRequestTokenSecret());
  85. $oUrl = $oService->getAuthorizationUri(array('oauth_token' => $oToken->getRequestToken()));
  86. $sUrl = bx_append_url_params($oUrl, array('sid' => bx_site_hash()));
  87. return array(
  88. 'redirect' => $sUrl,
  89. 'message' => _t('_adm_msg_oauth_need_authorize', $sUrl)
  90. );
  91. }
  92. protected function getAccessToken($sToken, $sVerifier, $iUser, $oService)
  93. {
  94. $oToken = $this->oStorage->retrieveAccessToken($this->sService);
  95. $sError = _t('_adm_err_oauth_cannot_get_token');
  96. try {
  97. $oAccessToken = $oService->requestAccessToken($sToken, $sVerifier, $oToken->getRequestTokenSecret());
  98. }
  99. catch (TokenResponseException $e) {
  100. $sErrorException = $e->getMessage();
  101. if(!empty($sErrorException))
  102. $sError = $sErrorException;
  103. if(!$this->isAuthorized())
  104. return $sError;
  105. $this->unsetAuthorizedUser();
  106. return $this->getRequestToken($oService);
  107. }
  108. if(empty($oAccessToken))
  109. return $sError;
  110. $this->oSession->setValue(self::$sSessionKeyToken, $oAccessToken->getAccessToken());
  111. $this->oSession->setValue(self::$sSessionKeySecret, $oAccessToken->getAccessTokenSecret());
  112. $this->oSession->setValue(self::$sSessionKeyAuthorized, 1);
  113. $this->oSession->setValue(self::$sSessionKeyAuthorizedUser, $iUser);
  114. return true;
  115. }
  116. protected function fetch($aParams = array())
  117. {
  118. if(!$this->isAuthorized())
  119. return array();
  120. try {
  121. $sResponse = $this->getServiceObject()->request(BX_DOL_OAUTH_URL_FETCH_DATA, $this->sDataRetrieveMethod, $aParams);
  122. //echo $sResponse; exit; //--- Uncomment to debug
  123. return json_decode($sResponse, true);
  124. }
  125. catch(TokenNotFoundException $e) {
  126. $this->unsetAuthorizedUser();
  127. return $this->getRequestToken();
  128. }
  129. }
  130. private function getServiceObject()
  131. {
  132. $this->oStorage = new Session();
  133. $oUrl = new Uri(BX_DOL_URL_STUDIO . 'store.php?page=purchases');
  134. $oCredentials = new Credentials($this->sKey, $this->sSecret, $oUrl->getAbsoluteUri());
  135. $oServiceFactory = new ServiceFactory();
  136. return $oServiceFactory->createService($this->sService, $oCredentials, $this->oStorage);
  137. }
  138. }
  139. /** @} */