BxDolStudioOAuth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 static $sSessionKeyToken = 'sys_oauth_token';
  13. protected static $sSessionKeySecret = 'sys_oauth_secret';
  14. protected static $sSessionKeyAuthorized = 'sys_oauth_authorized';
  15. protected static $sSessionKeyAuthorizedUser = 'sys_oauth_authorized_user';
  16. protected static $sSessionKeyAuthorizedOwner = 'sys_oauth_authorized_owner';
  17. protected $oSession;
  18. protected $sErrorCode;
  19. protected $sErrorMessage;
  20. protected $sKey;
  21. protected $sSecret;
  22. protected $sDataRetrieveMethod;
  23. protected 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 unsetAuthorized()
  31. {
  32. $oSession = BxDolSession::getInstance();
  33. $oSession->unsetValue(self::$sSessionKeyToken);
  34. $oSession->unsetValue(self::$sSessionKeyAuthorized);
  35. $oSession->unsetValue(self::$sSessionKeyAuthorizedUser);
  36. $oSession->unsetValue(self::$sSessionKeyAuthorizedOwner);
  37. }
  38. static function isAuthorizedClient()
  39. {
  40. return (int)BxDolSession::getInstance()->getValue(self::$sSessionKeyAuthorized) == 1;
  41. }
  42. static function getAuthorizedClient()
  43. {
  44. return (int)BxDolSession::getInstance()->getValue(self::$sSessionKeyAuthorizedUser);
  45. }
  46. static function isAuthorizedOwner()
  47. {
  48. return (int)BxDolSession::getInstance()->getValue(self::$sSessionKeyAuthorizedOwner) == 1;
  49. }
  50. public function loadItems($aParams = array())
  51. {
  52. if(empty($this->sKey) || empty($this->sSecret))
  53. return MsgBox(_t('_adm_err_oauth_empty_key_secret'));
  54. $mixedResult = $this->authorize();
  55. if($mixedResult !== true)
  56. return $this->onAuthorizeFailed($mixedResult);
  57. $aItems = $this->fetch($aParams);
  58. if(is_null($aItems))
  59. return MsgBox(_t('_adm_err_oauth_cannot_read_answer'));
  60. else if(empty($aItems))
  61. return MsgBox(_t('_Empty'));
  62. return $aItems;
  63. }
  64. public function doAuthorize()
  65. {
  66. if(empty($this->sKey) || empty($this->sSecret))
  67. return MsgBox(_t('_adm_err_oauth_empty_key_secret'));
  68. $mixedResult = $this->authorize();
  69. if($mixedResult !== true)
  70. return $this->onAuthorizeFailed($mixedResult);
  71. BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  72. return $mixedResult;
  73. }
  74. protected function onAuthorizeFailed($mixedResult)
  75. {
  76. if(is_string($mixedResult))
  77. return MsgBox($mixedResult);
  78. $bArray = is_array($mixedResult);
  79. if($bArray && !empty($mixedResult['redirect'])) {
  80. header('Location: ' . $mixedResult['redirect']);
  81. exit;
  82. }
  83. if($bArray && !empty($mixedResult['message']))
  84. return MsgBox($mixedResult['message']);
  85. }
  86. protected function isAuthorized()
  87. {
  88. return self::isAuthorizedClient();
  89. }
  90. protected function getAuthorizedUser()
  91. {
  92. return self::getAuthorizedClient();
  93. }
  94. protected function unsetAuthorizedUser()
  95. {
  96. self::unsetAuthorized();
  97. }
  98. protected function isServerError($aResult)
  99. {
  100. return isset($aResult[$this->sErrorCode]) && isset($aResult[$this->sErrorMessage]);
  101. }
  102. }
  103. /** @} */