1
0

BxDolStudioOAuth.php 3.5 KB

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