BxDolStudioOAuthPlugin.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth/bootstrap.php');
  17. class BxDolStudioOAuthPlugin extends BxDolStudioOAuth implements iBxDolSingleton
  18. {
  19. protected $sService;
  20. protected $oStorage;
  21. public 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 = 'Trident';
  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(TokenResponseException $e) {
  64. $this->unsetAuthorizedUser();
  65. return $this->getRequestToken();
  66. }
  67. catch(TokenNotFoundException $e) {
  68. $this->unsetAuthorizedUser();
  69. return $this->getRequestToken();
  70. }
  71. return $this->getRequestToken();
  72. }
  73. protected function getRequestToken($oService = null)
  74. {
  75. if(empty($oService))
  76. $oService = $this->getServiceObject();
  77. $oToken = $oService->requestRequestToken();
  78. if(empty($oToken))
  79. return _t('_adm_err_oauth_cannot_get_token');
  80. $this->oSession->setValue('sys_oauth_secret', $oToken->getRequestTokenSecret());
  81. $oUrl = $oService->getAuthorizationUri(array('oauth_token' => $oToken->getRequestToken()));
  82. return _t('_adm_msg_oauth_need_authorize', bx_append_url_params($oUrl, array('sid' => bx_site_hash())));
  83. }
  84. protected function getAccessToken($sToken, $sVerifier, $iUser, $oService)
  85. {
  86. $oToken = $this->oStorage->retrieveAccessToken($this->sService);
  87. $oAccessToken = $oService->requestAccessToken($sToken, $sVerifier, $oToken->getRequestTokenSecret());
  88. if(empty($oAccessToken))
  89. return _t('_adm_err_oauth_cannot_get_token');
  90. $this->oSession->setValue('sys_oauth_token', $oAccessToken->getAccessToken());
  91. $this->oSession->setValue('sys_oauth_secret', $oAccessToken->getAccessTokenSecret());
  92. $this->oSession->setValue('sys_oauth_authorized', 1);
  93. $this->oSession->setValue('sys_oauth_authorized_user', $iUser);
  94. return true;
  95. }
  96. protected function fetch($aParams = array())
  97. {
  98. if(!$this->isAuthorized())
  99. return array();
  100. try {
  101. $sResponse = $this->getServiceObject()->request(BX_DOL_OAUTH_URL_FETCH_DATA, $this->sDataRetrieveMethod, $aParams);
  102. //echo $sResponse; exit; //--- Uncomment to debug
  103. return json_decode($sResponse, true);
  104. }
  105. catch(TokenNotFoundException $e) {
  106. $this->unsetAuthorizedUser();
  107. return $this->getRequestToken();
  108. }
  109. }
  110. private function getServiceObject()
  111. {
  112. $this->oStorage = new Session();
  113. $oUrl = new Uri(BX_DOL_URL_STUDIO . 'store.php?page=purchases');
  114. $oCredentials = new Credentials($this->sKey, $this->sSecret, $oUrl->getAbsoluteUri());
  115. $oServiceFactory = new ServiceFactory();
  116. return $oServiceFactory->createService($this->sService, $oCredentials, $this->oStorage);
  117. }
  118. }
  119. /** @} */