timesheet source code
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

170 líneas
4.8KB

  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. public function tokenExpiresHumanRedable(){
  77. return $this->oauth2['expires_human'];
  78. }
  79. private function getExpiresHuman($time_stamp) {
  80. $expire = date("Y-m-d H:i:s", $time_stamp);
  81. $utc_date = \DateTime::createFromFormat(
  82. "Y-m-d H:i:s",
  83. $expire,
  84. new \DateTimeZone('UTC')
  85. );
  86. $sydney_date = $utc_date;
  87. $sydney_date->setTimeZone(new \DateTimeZone('Australia/Sydney'));
  88. $str = $sydney_date->format("Y-m-d H:i:s");
  89. return $str;
  90. }
  91. public function getToken()
  92. {
  93. //If it doesn't exist or is expired, return null
  94. // if (empty($this->getSession())
  95. // || ($_SESSION['oauth2']['expires'] !== null
  96. // && $_SESSION['oauth2']['expires'] <= time())
  97. // ) {
  98. // return null;
  99. // }
  100. // return $this->getSession();
  101. if ( $this->oauth2['expires'] !== null && $this->oauth2['expires'] <= time() ) {
  102. return null;
  103. }
  104. return $this->oauth2;
  105. }
  106. public function getAccessToken()
  107. {
  108. //return $_SESSION['oauth2']['token'];
  109. return $this->oauth2['token'];
  110. }
  111. public function getRefreshToken()
  112. {
  113. // return $_SESSION['oauth2']['refresh_token'];
  114. return $this->oauth2['refresh_token'];
  115. }
  116. public function getExpires()
  117. {
  118. //return $_SESSION['oauth2']['expires'];
  119. return $this->oauth2['expires'];
  120. }
  121. public function getXeroTenantId()
  122. {
  123. //return $_SESSION['oauth2']['tenant_id'];
  124. return $this->oauth2['tenant_id'];
  125. }
  126. public function getIdToken()
  127. {
  128. // return $_SESSION['oauth2']['id_token'];
  129. return $this->oauth2['id_token'];
  130. }
  131. public function getHasExpired()
  132. {
  133. if (!empty($this->getSession()))
  134. {
  135. if(time() > $this->getExpires())
  136. {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. } else {
  142. return true;
  143. }
  144. }
  145. }
  146. ?>