BxDolStudioOAuthLib.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(self::$sSessionKeySecret);
  41. if(!$bToken && $mixedSecret !== false) {
  42. $this->oSession->unsetValue(self::$sSessionKeySecret);
  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(self::$sSessionKeySecret, $aRequestToken['oauth_token_secret']);
  67. $sUrl = bx_append_url_params(BX_DOL_OAUTH_URL_AUTHORIZE, array('oauth_token' => $aRequestToken['oauth_token'], 'sid' => bx_site_hash()));
  68. return array(
  69. 'redirect' => $sUrl,
  70. 'message' => _t('_adm_msg_oauth_need_authorize', $sRedirect)
  71. );
  72. }
  73. protected function getAccessToken($sToken, $mixedSecret, $sVerifier, $iUser, $oConsumer)
  74. {
  75. $oConsumer->setToken($sToken, $mixedSecret);
  76. $aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => $sVerifier)));
  77. if(empty($aAccessToken))
  78. return _t('_adm_err_oauth_cannot_get_token');
  79. if($this->isServerError($aAccessToken))
  80. throw new OAuthException();
  81. $this->oSession->setValue(self::$sSessionKeyToken, $aAccessToken['oauth_token']);
  82. $this->oSession->setValue(self::$sSessionKeySecret, $aAccessToken['oauth_token_secret']);
  83. $this->oSession->setValue(self::$sSessionKeyAuthorized, 1);
  84. $this->oSession->setValue(self::$sSessionKeyAuthorizedUser, $iUser);
  85. return true;
  86. }
  87. protected function fetch($aParams = array())
  88. {
  89. if(!$this->isAuthorized())
  90. return array();
  91. $oConsumer = $this->getServiceObject(false);
  92. try {
  93. $oConsumer->setToken($this->oSession->getValue(self::$sSessionKeyToken), $this->oSession->getValue(self::$sSessionKeySecret));
  94. $oConsumer->fetch(BX_DOL_OAUTH_URL_FETCH_DATA, $aParams, $this->sDataRetrieveMethod);
  95. //echo $oConsumer->getLastResponse(); exit; //--- Uncomment to debug
  96. return json_decode($oConsumer->getLastResponse(), true);
  97. }
  98. catch(OAuthException $e) {
  99. return array();
  100. }
  101. }
  102. private function getServiceObject($bClearStorage = true)
  103. {
  104. if($bClearStorage)
  105. $this->unsetAuthorizedUser();
  106. $oConsumer = new OAuth($this->sKey, $this->sSecret);
  107. $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
  108. $oConsumer->enableDebug();
  109. return $oConsumer;
  110. }
  111. }
  112. /** @} */