BxDolStudioOAuth.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 $this->onAuthorizeFailed($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. return $this->onAuthorizeFailed($mixedResult);
  54. BxDolStudioInstallerUtils::getInstance()->checkModules(true);
  55. return $mixedResult;
  56. }
  57. protected function onAuthorizeFailed($mixedResult)
  58. {
  59. if(is_string($mixedResult))
  60. return MsgBox($mixedResult);
  61. $bArray = is_array($mixedResult);
  62. if($bArray && !empty($mixedResult['redirect'])) {
  63. header('Location: ' . $mixedResult['redirect']);
  64. exit;
  65. }
  66. if($bArray && !empty($mixedResult['message']))
  67. return MsgBox($mixedResult['message']);
  68. }
  69. protected function isAuthorized()
  70. {
  71. return self::isAuthorizedClient();
  72. }
  73. protected function getAuthorizedUser()
  74. {
  75. return self::getAuthorizedClient();
  76. }
  77. protected function unsetAuthorizedUser()
  78. {
  79. $this->oSession->unsetValue('sys_oauth_token');
  80. $this->oSession->unsetValue('sys_oauth_authorized');
  81. $this->oSession->unsetValue('sys_oauth_authorized_user');
  82. }
  83. protected function isServerError($aResult)
  84. {
  85. return isset($aResult[$this->sErrorCode]) && isset($aResult[$this->sErrorMessage]);
  86. }
  87. }
  88. /** @} */