1
0

BxDolStudioOAuth.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. class BxDolStudioOAuth extends BxDol implements iBxDolSingleton
  10. {
  11. protected $sKey;
  12. protected $sSecret;
  13. protected $sErrorCode;
  14. protected $sErrorMessage;
  15. protected $oSession;
  16. public function __construct()
  17. {
  18. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  19. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  20. parent::__construct ();
  21. $this->sKey = getParam('sys_oauth_key');
  22. $this->sSecret = getParam('sys_oauth_secret');
  23. $this->sErrorCode = 'oauth_err_code';
  24. $this->sErrorMessage = 'oauth_err_message';
  25. bx_import('BxDolSession');
  26. $this->oSession = BxDolSession::getInstance();
  27. }
  28. public function __clone()
  29. {
  30. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  31. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  32. }
  33. public static function getInstance()
  34. {
  35. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  36. $sClass = __CLASS__;
  37. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  38. }
  39. return $GLOBALS['bxDolClasses'][__CLASS__];
  40. }
  41. static function isAuthorizedClient()
  42. {
  43. $sClass = __CLASS__;
  44. return $sClass::getInstance()->isAuthorized();
  45. }
  46. static function getAuthorizedClient()
  47. {
  48. $sClass = __CLASS__;
  49. return $sClass::getInstance()->getAuthorizedUser();
  50. }
  51. public function loadItems($aParams = array())
  52. {
  53. if(empty($this->sKey) || empty($this->sSecret))
  54. return _t('_adm_err_oauth_empty_key_secret');
  55. $mixedResult = $this->authorize($this->sKey, $this->sSecret);
  56. if($mixedResult !== true)
  57. return $mixedResult;
  58. $aItems = $this->fetch($this->sKey, $this->sSecret, $aParams);
  59. if(is_null($aItems))
  60. return _t('_adm_err_oauth_cannot_read_answer');
  61. else if(empty($aItems))
  62. return MsgBox(_t('_Empty'));
  63. if($this->isServerError($aItems))
  64. return $this->processServerError($aItems);
  65. return $aItems;
  66. }
  67. protected function isAuthorized()
  68. {
  69. return (int)$this->oSession->getValue('sys_oauth_authorized') == 1;
  70. }
  71. protected function getAuthorizedUser()
  72. {
  73. if(!$this->isAuthorized())
  74. return 0;
  75. return (int)$this->oSession->getValue('sys_oauth_authorized_user');
  76. }
  77. protected function authorize($sKey, $sSecret)
  78. {
  79. if($this->isAuthorized())
  80. return true;
  81. try {
  82. $oConsumer = new OAuth($sKey, $sSecret);
  83. $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
  84. $oConsumer->enableDebug();
  85. $bToken = bx_get('oauth_token') !== false;
  86. $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
  87. if(!$bToken && $mixedSecret !== false) {
  88. $this->oSession->unsetValue('sys_oauth_secret');
  89. $mixedSecret = false;
  90. }
  91. //--- Get request token and redirect to authorize.
  92. if(!$bToken && $mixedSecret === false) {
  93. $aRequestToken = $oConsumer->getRequestToken(BX_DOL_OAUTH_URL_REQUEST_TOKEN);
  94. if(empty($aRequestToken))
  95. return _t('_adm_err_oauth_cannot_get_token');
  96. if($this->isServerError($aRequestToken))
  97. return $this->processServerError($aRequestToken);
  98. $this->oSession->setValue('sys_oauth_secret', $aRequestToken['oauth_token_secret']);
  99. 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())));
  100. }
  101. //--- Get access token.
  102. if($bToken && $mixedSecret !== false) {
  103. $oConsumer->setToken(bx_get('oauth_token'), $mixedSecret);
  104. $aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => bx_get('oauth_verifier'))));
  105. if(empty($aAccessToken))
  106. return _t('_adm_err_oauth_cannot_get_token');
  107. if($this->isServerError($aAccessToken))
  108. return $this->processServerError($aAccessToken);
  109. $this->oSession->setValue('sys_oauth_token', $aAccessToken['oauth_token']);
  110. $this->oSession->setValue('sys_oauth_secret', $aAccessToken['oauth_token_secret']);
  111. $this->oSession->setValue('sys_oauth_authorized', 1);
  112. $this->oSession->setValue('sys_oauth_authorized_user', (int)bx_get('oauth_user'));
  113. return true;
  114. }
  115. } catch(OAuthException $e) {
  116. return _t('_adm_err_oauth_cannot_get_token');
  117. }
  118. }
  119. protected function fetch($sKey, $sSecret, $aParams = array())
  120. {
  121. if(!$this->isAuthorized())
  122. return array();
  123. try {
  124. $oConsumer = new OAuth($sKey, $sSecret);
  125. $oConsumer->setAuthType(OAUTH_AUTH_TYPE_URI);
  126. $oConsumer->enableDebug();
  127. $oConsumer->setToken($this->oSession->getValue('sys_oauth_token'), $this->oSession->getValue('sys_oauth_secret'));
  128. $oConsumer->fetch(BX_DOL_OAUTH_URL_FETCH_DATA, $aParams, OAUTH_HTTP_METHOD_POST);
  129. //--- Uncomment to debug
  130. //echo $oConsumer->getLastResponse(); exit;
  131. return json_decode($oConsumer->getLastResponse(), true);
  132. } catch(OAuthException $e) {
  133. return array();
  134. }
  135. }
  136. protected function isServerError($aResult)
  137. {
  138. return isset($aResult[$this->sErrorCode]) && isset($aResult[$this->sErrorMessage]);
  139. }
  140. protected function processServerError($aResult)
  141. {
  142. $iCode = $aResult[$this->sErrorCode];
  143. $sMessage = $aResult[$this->sErrorMessage];
  144. switch($iCode) {
  145. case '8':
  146. case '16':
  147. case '32':
  148. case '64':
  149. case '256':
  150. case '1024':
  151. case '2048':
  152. bx_import('BxDolSession');
  153. $this->oSession = BxDolSession::getInstance();
  154. $this->oSession->unsetValue('sys_oauth_token');
  155. $this->oSession->unsetValue('sys_oauth_secret');
  156. $this->oSession->unsetValue('sys_oauth_authorized');
  157. $this->oSession->unsetValue('sys_oauth_authorized_user');
  158. break;
  159. }
  160. return $sMessage;
  161. }
  162. }
  163. /** @} */