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

Commit

Permalink
Fix #28
Browse files Browse the repository at this point in the history
  • Loading branch information
kimbtech committed Oct 16, 2020
1 parent 1522d65 commit 323cc19
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
24 changes: 23 additions & 1 deletion core/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
class Utilities {

const VERSION = 'v1.0.10';
const VERSION = 'v1.0.11';

const DEFAULT_LINE_LENGTH = 125;

Expand Down Expand Up @@ -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;
}
}

?>
41 changes: 38 additions & 3 deletions core/sync/ServerStatsAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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',
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
}
}

}
Expand Down

0 comments on commit 323cc19

Please sign in to comment.