timesheet source code
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

168 lines
4.7KB

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