BxDolStudioOAuth.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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('BxDolSession');
  10. bx_import('BxDolStudioInstallerUtils');
  11. define('BX_DOL_OAUTH_URL_BASE', BX_DOL_UNITY_URL_ROOT . 'scripts_public/');
  12. define('BX_DOL_OAUTH_URL_REQUEST_TOKEN', BX_DOL_OAUTH_URL_BASE . 'oauth_request_token.php5');
  13. define('BX_DOL_OAUTH_URL_AUTHORIZE', BX_DOL_OAUTH_URL_BASE . 'oauth_authorize.php5');
  14. define('BX_DOL_OAUTH_URL_ACCESS_TOKEN', BX_DOL_OAUTH_URL_BASE . 'oauth_access_token.php5');
  15. define('BX_DOL_OAUTH_URL_FETCH_DATA', BX_DOL_OAUTH_URL_BASE . 'oauth_fetch_data.php5');
  16. class BxDolStudioOAuth extends BxDol
  17. {
  18. protected $oSession;
  19. protected $sErrorCode;
  20. protected $sErrorMessage;
  21. protected $sKey;
  22. protected $sSecret;
  23. protected $sDataRetrieveMethod;
  24. public function __construct()
  25. {
  26. parent::__construct ();
  27. $this->oSession = BxDolSession::getInstance();
  28. $this->sErrorCode = 'oauth_err_code';
  29. $this->sErrorMessage = 'oauth_err_message';
  30. }
  31. static function isAuthorizedClient()
  32. {
  33. return (int)BxDolSession::getInstance()->getValue('sys_oauth_authorized') == 1;
  34. }
  35. static function getAuthorizedClient()
  36. {
  37. return (int)BxDolSession::getInstance()->getValue('sys_oauth_authorized_user');
  38. }
  39. public function loadItems($aParams = array())
  40. {
  41. if(empty($this->sKey) || empty($this->sSecret))
  42. return _t('_adm_err_oauth_empty_key_secret');
  43. $mixedResult = $this->authorize();
  44. if($mixedResult !== true)
  45. return $mixedResult;
  46. $aItems = $this->fetch($aParams);
  47. if(is_null($aItems))
  48. return _t('_adm_err_oauth_cannot_read_answer');
  49. else if(empty($aItems))
  50. return MsgBox(_t('_Empty'));
  51. return $aItems;
  52. }
  53. public function doAuthorize()
  54. {
  55. if(empty($this->sKey) || empty($this->sSecret))
  56. return _t('_adm_err_oauth_empty_key_secret');
  57. return $this->authorize();
  58. }
  59. protected function isAuthorized()
  60. {
  61. return self::isAuthorizedClient();
  62. }
  63. protected function getAuthorizedUser()
  64. {
  65. return self::getAuthorizedClient();
  66. }
  67. protected function unsetAuthorizedUser()
  68. {
  69. $this->oSession->unsetValue('sys_oauth_token');
  70. $this->oSession->unsetValue('sys_oauth_secret');
  71. $this->oSession->unsetValue('sys_oauth_authorized');
  72. $this->oSession->unsetValue('sys_oauth_authorized_user');
  73. }
  74. protected function isServerError($aResult)
  75. {
  76. return isset($aResult[$this->sErrorCode]) && isset($aResult[$this->sErrorMessage]);
  77. }
  78. }
  79. /** @} */