BxDolStudioOAuth.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. bx_import('BxDolStudioInstallerUtils');
  10. class BxDolStudioOAuth extends BxDolFactory
  11. {
  12. protected $oSession;
  13. protected $sErrorCode;
  14. protected $sErrorMessage;
  15. protected $sKey;
  16. protected $sSecret;
  17. protected $sDataRetrieveMethod;
  18. protected function __construct()
  19. {
  20. parent::__construct ();
  21. $this->oSession = BxDolSession::getInstance();
  22. $this->sErrorCode = 'oauth_err_code';
  23. $this->sErrorMessage = 'oauth_err_message';
  24. }
  25. static function isAuthorizedClient()
  26. {
  27. return (int)BxDolSession::getInstance()->getValue('sys_oauth_authorized') == 1;
  28. }
  29. static function getAuthorizedClient()
  30. {
  31. return (int)BxDolSession::getInstance()->getValue('sys_oauth_authorized_user');
  32. }
  33. public function loadItems($aParams = array())
  34. {
  35. if(empty($this->sKey) || empty($this->sSecret))
  36. return _t('_adm_err_oauth_empty_key_secret');
  37. $mixedResult = $this->authorize();
  38. if($mixedResult !== true)
  39. return $mixedResult;
  40. $aItems = $this->fetch($aParams);
  41. if(is_null($aItems))
  42. return _t('_adm_err_oauth_cannot_read_answer');
  43. else if(empty($aItems))
  44. return _t('_Empty');
  45. return $aItems;
  46. }
  47. public function doAuthorize()
  48. {
  49. if(empty($this->sKey) || empty($this->sSecret))
  50. return _t('_adm_err_oauth_empty_key_secret');
  51. $mixedResult = $this->authorize();
  52. if($mixedResult === true)
  53. BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  54. return $mixedResult;
  55. }
  56. protected function isAuthorized()
  57. {
  58. return self::isAuthorizedClient();
  59. }
  60. protected function getAuthorizedUser()
  61. {
  62. return self::getAuthorizedClient();
  63. }
  64. protected function unsetAuthorizedUser()
  65. {
  66. $this->oSession->unsetValue('sys_oauth_token');
  67. $this->oSession->unsetValue('sys_oauth_authorized');
  68. $this->oSession->unsetValue('sys_oauth_authorized_user');
  69. }
  70. protected function isServerError($aResult)
  71. {
  72. return isset($aResult[$this->sErrorCode]) && isset($aResult[$this->sErrorMessage]);
  73. }
  74. }
  75. /** @} */