BxDolStudioOAuthPlugin.php 4.6 KB

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