BxDolStudioOAuthLib.php 4.5 KB

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