BxDolStudioOAuthLib.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. 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($sKey, $sSecret)
  35. {
  36. if($this->isAuthorized())
  37. return true;
  38. try {
  39. $oConsumer = new OAuth($sKey, $sSecret);
  40. $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
  41. $oConsumer->enableDebug();
  42. $bToken = bx_get('oauth_token') !== false;
  43. $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
  44. if(!$bToken && $mixedSecret !== false) {
  45. $this->oSession->unsetValue('sys_oauth_secret');
  46. $mixedSecret = false;
  47. }
  48. //--- Get request token and redirect to authorize.
  49. if(!$bToken && $mixedSecret === false) {
  50. $aRequestToken = $oConsumer->getRequestToken(BX_DOL_OAUTH_URL_REQUEST_TOKEN);
  51. if(empty($aRequestToken))
  52. return _t('_adm_err_oauth_cannot_get_token');
  53. if($this->isServerError($aRequestToken))
  54. return $this->processServerError($aRequestToken);
  55. $this->oSession->setValue('sys_oauth_secret', $aRequestToken['oauth_token_secret']);
  56. 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())));
  57. }
  58. //--- Get access token.
  59. if($bToken && $mixedSecret !== false) {
  60. $oConsumer->setToken(bx_get('oauth_token'), $mixedSecret);
  61. $aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => bx_get('oauth_verifier'))));
  62. if(empty($aAccessToken))
  63. return _t('_adm_err_oauth_cannot_get_token');
  64. if($this->isServerError($aAccessToken))
  65. return $this->processServerError($aAccessToken);
  66. $this->oSession->setValue('sys_oauth_token', $aAccessToken['oauth_token']);
  67. $this->oSession->setValue('sys_oauth_secret', $aAccessToken['oauth_token_secret']);
  68. $this->oSession->setValue('sys_oauth_authorized', 1);
  69. $this->oSession->setValue('sys_oauth_authorized_user', (int)bx_get('oauth_user'));
  70. return true;
  71. }
  72. } catch(OAuthException $e) {
  73. return _t('_adm_err_oauth_cannot_get_token');
  74. }
  75. }
  76. protected function fetch($sKey, $sSecret, $aParams = array())
  77. {
  78. if(!$this->isAuthorized())
  79. return array();
  80. try {
  81. $oConsumer = new OAuth($sKey, $sSecret);
  82. $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
  83. $oConsumer->enableDebug();
  84. $oConsumer->setToken($this->oSession->getValue('sys_oauth_token'), $this->oSession->getValue('sys_oauth_secret'));
  85. $oConsumer->fetch(BX_DOL_OAUTH_URL_FETCH_DATA, $aParams, $this->sDataRetrieveMethod);
  86. //echo $oConsumer->getLastResponse(); exit; //--- Uncomment to debug
  87. return json_decode($oConsumer->getLastResponse(), true);
  88. } catch(OAuthException $e) {
  89. return array();
  90. }
  91. }
  92. }
  93. /** @} */