path = dirname(__FILE__) . "/xero.json"; // \Carbon_Fields\Carbon_Fields::boot(); // if( !isset($_SESSION) ){ // $this->init_session(); // } } public function read_value() { $this->oauth2state = carbon_get_theme_option("xero_oauth2state"); $this->oauth2["token"] = carbon_get_theme_option("xero_token"); $this->oauth2["refresh_token"] = carbon_get_theme_option("xero_refresh_token"); $this->oauth2["expires"] = carbon_get_theme_option("xero_expires"); $this->oauth2["tenant_id"] = carbon_get_theme_option("xero_tenant_id"); $this->oauth2["id_token"] = carbon_get_theme_option("xero_id_token"); $this->oauth2['expires_human'] = $this->getExpiresHuman($this->oauth2["expires"]); } public function init_session(){ // session_start(); } public function getSession() { return $this->oauth2; // return $_SESSION['oauth2']; } private function write_json() { $serialize = [ "oauth2state" => $this->getOauth2State(), "oauth2" => $this->oauth2, ]; file_put_contents($this->path , json_encode($serialize)); } public function startSession($token, $secret, $expires = null) { // session_start(); } public function setToken($token, $expires = null, $tenantId, $refreshToken, $idToken) { // $_SESSION['oauth2'] = [ // 'token' => $token, // 'expires' => $expires, // 'tenant_id' => $tenantId, // 'refresh_token' => $refreshToken, // 'id_token' => $idToken // ]; $this->oauth2 = [ 'token' => $token, 'expires' => $expires, 'tenant_id' => $tenantId, 'refresh_token' => $refreshToken, 'id_token' => $idToken, 'expires_human' => $this->getExpiresHuman($expires) ]; carbon_set_theme_option("xero_token", $token); carbon_set_theme_option("xero_refresh_token", $refreshToken); carbon_set_theme_option("xero_expires", $expires); carbon_set_theme_option("xero_tenant_id", $tenantId); carbon_set_theme_option("xero_id_token", $idToken); carbon_set_theme_option("xero_expires_human", $this->getExpiresHuman($expires)); $this->write_json(); } public function getOauth2State() { $this->oauth2state = carbon_get_theme_option("xero_oauth2state"); return $this->oauth2state; } public function setOauth2State($state) { $this->oauth2state = $state; carbon_set_theme_option("xero_oauth2state", $state); $this->write_json(); } public function tokenExpiresHumanRedable(){ return $this->oauth2['expires_human']; } private function getExpiresHuman($time_stamp) { $expire = date("Y-m-d H:i:s", $time_stamp); $utc_date = \DateTime::createFromFormat( "Y-m-d H:i:s", $expire, new \DateTimeZone('UTC') ); $sydney_date = $utc_date; $sydney_date->setTimeZone(new \DateTimeZone('Australia/Sydney')); $str = $sydney_date->format("Y-m-d H:i:s"); return $str; } public function getToken() { //If it doesn't exist or is expired, return null // if (empty($this->getSession()) // || ($_SESSION['oauth2']['expires'] !== null // && $_SESSION['oauth2']['expires'] <= time()) // ) { // return null; // } // return $this->getSession(); if ( $this->oauth2['expires'] !== null && $this->oauth2['expires'] <= time() ) { return null; } return $this->oauth2; } public function getAccessToken() { //return $_SESSION['oauth2']['token']; return $this->oauth2['token']; } public function getRefreshToken() { // return $_SESSION['oauth2']['refresh_token']; return $this->oauth2['refresh_token']; } public function getExpires() { //return $_SESSION['oauth2']['expires']; return $this->oauth2['expires']; } public function getXeroTenantId() { //return $_SESSION['oauth2']['tenant_id']; return $this->oauth2['tenant_id']; } public function getIdToken() { // return $_SESSION['oauth2']['id_token']; return $this->oauth2['id_token']; } public function getHasExpired() { if (!empty($this->getSession())) { if(time() > $this->getExpires()) { return true; } else { return false; } } else { return true; } } } ?>