From 323cc191bbb8ec1e9ac6c15cbb300e7006cbba64 Mon Sep 17 00:00:00 2001 From: KIMB-technologies Date: Fri, 16 Oct 2020 14:37:12 +0200 Subject: [PATCH] Fix #28 --- core/Utilities.php | 24 ++++++++++++++++++- core/sync/ServerStatsAccess.php | 41 ++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/core/Utilities.php b/core/Utilities.php index 630eda3..befe4dc 100644 --- a/core/Utilities.php +++ b/core/Utilities.php @@ -4,7 +4,7 @@ */ class Utilities { - const VERSION = 'v1.0.10'; + const VERSION = 'v1.0.11'; const DEFAULT_LINE_LENGTH = 125; @@ -112,6 +112,28 @@ public static function getOS() : string { public static function isWindowsOS() : bool { return stripos(php_uname('s'), 'windows') !== false ; } + + /** + * Determines if a host is online. + * @param $url give a URI/ URL of the host (supports http(s)) + */ + public static function isOnline(string $url) : bool { + preg_match('/^http(s?):\/\/([^:\/]+)((?::\d+)?)(?:\/.*)?$/', $url, $matches); + $host = $matches[2]; + if(!empty($matches[1])){ // http_s_?? + $host = 'ssl://' . $host; + $port = 443; + } + else{ + $port = 80; + } + if(!empty($matches[3])){ // different port? + $port = substr($matches[3], 1); + } + + $r = @fsockopen( $host, $port, $errno, $errstr, 2); + return $r !== false; + } } ?> diff --git a/core/sync/ServerStatsAccess.php b/core/sync/ServerStatsAccess.php index 7ec74ba..85eb099 100644 --- a/core/sync/ServerStatsAccess.php +++ b/core/sync/ServerStatsAccess.php @@ -6,7 +6,10 @@ class ServerStatsAccess extends StatsAccess { private string $groupId; private string $token; private string $thisClientName; + private ServerAccessCache $cache; + private JSONReader $offlineTasks; + private bool $isOnline; private bool $requestError = false; @@ -18,10 +21,36 @@ public function __construct(){ $this->token = $c->getValue(['sync', 'server', 'token']); $this->thisClientName = $c->getValue(['sync', 'server', 'thisname']); + $this->offlineTasks = Config::getStorageReader('offlineTasks'); + $this->isOnline = Utilities::isOnline($this->uri); + $this->checkForOfflineTasks(); + $this->cache = new ServerAccessCache( $this->uri, $this->groupId, $this->token, $this->thisClientName ); } + private function checkForOfflineTasks() : void { + // online and offline tasks to upload? + if($this->isOnline && !empty($this->offlineTasks->getArray()) ){ + $ok = true; + foreach($this->offlineTasks->getArray() as $task){ + $this->postToServer('add', $task ); + $ok &= !$this->requestError; + } + if(!$ok){ + CLIOutput::error(CLIOutput::ERROR_WARN, 'Unable to upload tasks done offline'); + } + else{ + $this->offlineTasks->setArray(array()); + } + } + } + private function postToServer(string $endpoint, array $data = array() ) : array { + if(!$this->isOnline){ + CLIOutput::error(CLIOutput::ERROR_INFO, 'Client is offline, so no data from sync server will be shown.'); + return array(); + } + $context = array( 'http' => array( 'method' => 'POST', @@ -48,11 +77,11 @@ private function postToServer(string $endpoint, array $data = array() ) : array } else{ $msg = is_null($json) ? $ret : $json['error']; - echo "ERROR: Returned message from server: '". $msg ."'" . PHP_EOL; + CLIOutput::error(CLIOutput::ERROR_WARN, "Returned message from server: '". $msg ."'"); } } } - echo "ERROR: Request failed!" . PHP_EOL; + CLIOutput::error(CLIOutput::ERROR_WARN, "Request failed!"); $this->requestError = true; return array(); } @@ -103,7 +132,13 @@ public function initialSync() : bool { } public function setDayTasks(array $tasks, int $day) : void { - $this->postToServer('add', array( 'day' => $day, 'tasks' => $tasks ) ); + $data = array( 'day' => $day, 'tasks' => $tasks ); + if($this->isOnline){ + $this->postToServer('add', $data ); + } + else{ + $this->offlineTasks->setValue([null], $data); + } } }