BxDolStudioOAuthLib.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. class BxDolStudioOAuthLib extends BxDolStudioOAuthOAuth1 implements iBxDolSingleton
  10. {
  11. protected function __construct()
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  14. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  15. parent::__construct();
  16. $this->sKey = getParam('sys_oauth_key');
  17. $this->sSecret = getParam('sys_oauth_secret');
  18. $this->sDataRetrieveMethod = OAUTH_HTTP_METHOD_POST;
  19. }
  20. public function __clone()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. }
  25. public static function getInstance()
  26. {
  27. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  28. $sClass = __CLASS__;
  29. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  30. }
  31. return $GLOBALS['bxDolClasses'][__CLASS__];
  32. }
  33. protected function authorize()
  34. {
  35. if($this->isAuthorized())
  36. return true;
  37. $oConsumer = $this->getServiceObject(false);
  38. try {
  39. $bToken = bx_get('oauth_token') !== false;
  40. $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
  41. if(!$bToken && $mixedSecret !== false) {
  42. $this->oSession->unsetValue('sys_oauth_secret');
  43. $mixedSecret = false;
  44. }
  45. //--- Get request token and redirect to authorize.
  46. if(!$bToken && $mixedSecret === false)
  47. return $this->getRequestToken($oConsumer);
  48. //--- Get access token.
  49. if($bToken && $mixedSecret !== false)
  50. return $this->getAccessToken(bx_get('oauth_token'), $mixedSecret, bx_get('oauth_verifier'), (int)bx_get('oauth_user'), $oConsumer);
  51. }
  52. catch(OAuthException $e) {
  53. return $this->getRequestToken();
  54. }
  55. return $this->getRequestToken();
  56. }
  57. protected function getRequestToken($oConsumer = null)
  58. {
  59. if(empty($oConsumer))
  60. $oConsumer = $this->getServiceObject();
  61. $aRequestToken = $oConsumer->getRequestToken(BX_DOL_OAUTH_URL_REQUEST_TOKEN);
  62. if(empty($aRequestToken))
  63. return _t('_adm_err_oauth_cannot_get_token');
  64. if($this->isServerError($aRequestToken))
  65. throw new OAuthException();
  66. $this->oSession->setValue('sys_oauth_secret', $aRequestToken['oauth_token_secret']);
  67. return _t('_adm_msg_oauth_need_authorize', bx_append_url_params(BX_DOL_OAUTH_URL_AUTHORIZE, array('oauth_token' => $aRequestToken['oauth_token'], 'sid' => bx_site_hash())));
  68. }
  69. protected function getAccessToken($sToken, $mixedSecret, $sVerifier, $iUser, $oConsumer)
  70. {
  71. $oConsumer->setToken($sToken, $mixedSecret);
  72. $aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => $sVerifier)));
  73. if(empty($aAccessToken))
  74. return _t('_adm_err_oauth_cannot_get_token');
  75. if($this->isServerError($aAccessToken))
  76. throw new OAuthException();
  77. $this->oSession->setValue('sys_oauth_token', $aAccessToken['oauth_token']);
  78. $this->oSession->setValue('sys_oauth_secret', $aAccessToken['oauth_token_secret']);
  79. $this->oSession->setValue('sys_oauth_authorized', 1);
  80. $this->oSession->setValue('sys_oauth_authorized_user', $iUser);
  81. return true;
  82. }
  83. protected function fetch($aParams = array())
  84. {
  85. if(!$this->isAuthorized())
  86. return array();
  87. $oConsumer = $this->getServiceObject(false);
  88. try {
  89. $oConsumer->setToken($this->oSession->getValue('sys_oauth_token'), $this->oSession->getValue('sys_oauth_secret'));
  90. $oConsumer->fetch(BX_DOL_OAUTH_URL_FETCH_DATA, $aParams, $this->sDataRetrieveMethod);
  91. //echo $oConsumer->getLastResponse(); exit; //--- Uncomment to debug
  92. return json_decode($oConsumer->getLastResponse(), true);
  93. }
  94. catch(OAuthException $e) {
  95. return array();
  96. }
  97. }
  98. private function getServiceObject($bClearStorage = true)
  99. {
  100. if($bClearStorage)
  101. $this->unsetAuthorizedUser();
  102. $oConsumer = new OAuth($this->sKey, $this->sSecret);
  103. $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
  104. $oConsumer->enableDebug();
  105. return $oConsumer;
  106. }
  107. }
  108. /** @} */