Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

155 lines
4.1KB

  1. <?php
  2. namespace Biukop;
  3. class StorageClass
  4. {
  5. private $oauth2 = [];
  6. private $oauth2state = "";
  7. function __construct() {
  8. \Carbon_Fields\Carbon_Fields::boot();
  9. // if( !isset($_SESSION) ){
  10. // $this->init_session();
  11. // }
  12. }
  13. public function read_value() {
  14. $this->oauth2state = carbon_get_theme_option("xero_oauth2state");
  15. $this->oauth2["token"] = carbon_get_theme_option("xero_token");
  16. $this->oauth2["refresh_token"] = carbon_get_theme_option("xero_refresh_token");
  17. $this->oauth2["expires"] = carbon_get_theme_option("xero_expires");
  18. $this->oauth2["tenant_id"] = carbon_get_theme_option("xero_tenant_id");
  19. $this->oauth2["id_token"] = carbon_get_theme_option("xero_id_token");
  20. $this->oauth2['expires_human'] = $this->getExpiresHuman($this->oauth2["expires"]);
  21. }
  22. public function init_session(){
  23. // session_start();
  24. }
  25. public function getSession() {
  26. return $this->oauth2;
  27. // return $_SESSION['oauth2'];
  28. }
  29. public function startSession($token, $secret, $expires = null)
  30. {
  31. // session_start();
  32. }
  33. public function setToken($token, $expires = null, $tenantId, $refreshToken, $idToken)
  34. {
  35. // $_SESSION['oauth2'] = [
  36. // 'token' => $token,
  37. // 'expires' => $expires,
  38. // 'tenant_id' => $tenantId,
  39. // 'refresh_token' => $refreshToken,
  40. // 'id_token' => $idToken
  41. // ];
  42. $this->oauth2 = [
  43. 'token' => $token,
  44. 'expires' => $expires,
  45. 'tenant_id' => $tenantId,
  46. 'refresh_token' => $refreshToken,
  47. 'id_token' => $idToken,
  48. 'expires_human' => $this->getExpiresHuman($expires)
  49. ];
  50. carbon_set_theme_option("xero_token", $token);
  51. carbon_set_theme_option("xero_refresh_token", $refreshToken);
  52. carbon_set_theme_option("xero_expires", $expires);
  53. carbon_set_theme_option("xero_tenant_id", $tenantId);
  54. carbon_set_theme_option("xero_id_token", $idToken);
  55. carbon_set_theme_option("xero_expires_human", $this->getExpiresHuman($expires));
  56. }
  57. public function getOauth2State() {
  58. $this->oauth2state = carbon_get_theme_option("xero_oauth2state");
  59. return $this->oauth2state;
  60. }
  61. public function setOauth2State($state) {
  62. $this->oauth2state = $state;
  63. carbon_set_theme_option("xero_oauth2state", $state);
  64. }
  65. private function getExpiresHuman($time_stamp) {
  66. $expire = date("Y-m-d H:i:s", $time_stamp);
  67. $utc_date = \DateTime::createFromFormat(
  68. "Y-m-d H:i:s",
  69. $expire,
  70. new \DateTimeZone('UTC')
  71. );
  72. $sydney_date = $utc_date;
  73. $sydney_date->setTimeZone(new \DateTimeZone('Australia/Sydney'));
  74. $str = $sydney_date->format("Y-m-d H:i:s");
  75. return $str;
  76. }
  77. public function getToken()
  78. {
  79. //If it doesn't exist or is expired, return null
  80. // if (empty($this->getSession())
  81. // || ($_SESSION['oauth2']['expires'] !== null
  82. // && $_SESSION['oauth2']['expires'] <= time())
  83. // ) {
  84. // return null;
  85. // }
  86. // return $this->getSession();
  87. if ( $this->oauth2['expires'] !== null && $this->oauth2['expires'] <= time() ) {
  88. return null;
  89. }
  90. return $this->oauth2;
  91. }
  92. public function getAccessToken()
  93. {
  94. //return $_SESSION['oauth2']['token'];
  95. return $this->oauth2['token'];
  96. }
  97. public function getRefreshToken()
  98. {
  99. // return $_SESSION['oauth2']['refresh_token'];
  100. return $this->oauth2['refresh_token'];
  101. }
  102. public function getExpires()
  103. {
  104. //return $_SESSION['oauth2']['expires'];
  105. return $this->oauth2['expires'];
  106. }
  107. public function getXeroTenantId()
  108. {
  109. //return $_SESSION['oauth2']['tenant_id'];
  110. return $this->oauth2['tenant_id'];
  111. }
  112. public function getIdToken()
  113. {
  114. // return $_SESSION['oauth2']['id_token'];
  115. return $this->oauth2['id_token'];
  116. }
  117. public function getHasExpired()
  118. {
  119. if (!empty($this->getSession()))
  120. {
  121. if(time() > $this->getExpires())
  122. {
  123. return true;
  124. } else {
  125. return false;
  126. }
  127. } else {
  128. return true;
  129. }
  130. }
  131. }
  132. ?>