BxDolStudioOAuth.php 2.7 KB

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