Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 323cc19

Browse files
committed
Fix #28
1 parent 1522d65 commit 323cc19

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

core/Utilities.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
class Utilities {
66

7-
const VERSION = 'v1.0.10';
7+
const VERSION = 'v1.0.11';
88

99
const DEFAULT_LINE_LENGTH = 125;
1010

@@ -112,6 +112,28 @@ public static function getOS() : string {
112112
public static function isWindowsOS() : bool {
113113
return stripos(php_uname('s'), 'windows') !== false ;
114114
}
115+
116+
/**
117+
* Determines if a host is online.
118+
* @param $url give a URI/ URL of the host (supports http(s))
119+
*/
120+
public static function isOnline(string $url) : bool {
121+
preg_match('/^http(s?):\/\/([^:\/]+)((?::\d+)?)(?:\/.*)?$/', $url, $matches);
122+
$host = $matches[2];
123+
if(!empty($matches[1])){ // http_s_??
124+
$host = 'ssl://' . $host;
125+
$port = 443;
126+
}
127+
else{
128+
$port = 80;
129+
}
130+
if(!empty($matches[3])){ // different port?
131+
$port = substr($matches[3], 1);
132+
}
133+
134+
$r = @fsockopen( $host, $port, $errno, $errstr, 2);
135+
return $r !== false;
136+
}
115137
}
116138

117139
?>

core/sync/ServerStatsAccess.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ class ServerStatsAccess extends StatsAccess {
66
private string $groupId;
77
private string $token;
88
private string $thisClientName;
9+
910
private ServerAccessCache $cache;
11+
private JSONReader $offlineTasks;
12+
private bool $isOnline;
1013

1114
private bool $requestError = false;
1215

@@ -18,10 +21,36 @@ public function __construct(){
1821
$this->token = $c->getValue(['sync', 'server', 'token']);
1922
$this->thisClientName = $c->getValue(['sync', 'server', 'thisname']);
2023

24+
$this->offlineTasks = Config::getStorageReader('offlineTasks');
25+
$this->isOnline = Utilities::isOnline($this->uri);
26+
$this->checkForOfflineTasks();
27+
2128
$this->cache = new ServerAccessCache( $this->uri, $this->groupId, $this->token, $this->thisClientName );
2229
}
2330

31+
private function checkForOfflineTasks() : void {
32+
// online and offline tasks to upload?
33+
if($this->isOnline && !empty($this->offlineTasks->getArray()) ){
34+
$ok = true;
35+
foreach($this->offlineTasks->getArray() as $task){
36+
$this->postToServer('add', $task );
37+
$ok &= !$this->requestError;
38+
}
39+
if(!$ok){
40+
CLIOutput::error(CLIOutput::ERROR_WARN, 'Unable to upload tasks done offline');
41+
}
42+
else{
43+
$this->offlineTasks->setArray(array());
44+
}
45+
}
46+
}
47+
2448
private function postToServer(string $endpoint, array $data = array() ) : array {
49+
if(!$this->isOnline){
50+
CLIOutput::error(CLIOutput::ERROR_INFO, 'Client is offline, so no data from sync server will be shown.');
51+
return array();
52+
}
53+
2554
$context = array(
2655
'http' => array(
2756
'method' => 'POST',
@@ -48,11 +77,11 @@ private function postToServer(string $endpoint, array $data = array() ) : array
4877
}
4978
else{
5079
$msg = is_null($json) ? $ret : $json['error'];
51-
echo "ERROR: Returned message from server: '". $msg ."'" . PHP_EOL;
80+
CLIOutput::error(CLIOutput::ERROR_WARN, "Returned message from server: '". $msg ."'");
5281
}
5382
}
5483
}
55-
echo "ERROR: Request failed!" . PHP_EOL;
84+
CLIOutput::error(CLIOutput::ERROR_WARN, "Request failed!");
5685
$this->requestError = true;
5786
return array();
5887
}
@@ -103,7 +132,13 @@ public function initialSync() : bool {
103132
}
104133

105134
public function setDayTasks(array $tasks, int $day) : void {
106-
$this->postToServer('add', array( 'day' => $day, 'tasks' => $tasks ) );
135+
$data = array( 'day' => $day, 'tasks' => $tasks );
136+
if($this->isOnline){
137+
$this->postToServer('add', $data );
138+
}
139+
else{
140+
$this->offlineTasks->setValue([null], $data);
141+
}
107142
}
108143

109144
}

0 commit comments

Comments
 (0)