BxDolStudioOAuthPlugin.php 5.2 KB

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