BxDolStudioOAuth.php 5.9 KB

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