BxDolStudioOAuth.php 7.2 KB

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