BxDolStudioOAuthPlugin.php 4.8 KB

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