synced-timer/storageHelper.php

31 lines
780 B
PHP
Raw Normal View History

2021-07-11 09:12:51 +02:00
<?php
class StorageHelper
{
private $_dataFile;
public function __construct($dataFile)
{
$this->_dataFile = $dataFile;
}
function loadUserdata()
{
$data = json_decode(file_get_contents($this->_dataFile), true);
return $data;
}
function writeUserdata($username, $newUserdata)
{
$allUserdata = $this->loadUserdata();
$currentUserdata = $allUserdata[$username];
$newUserdata = array_replace_recursive($currentUserdata, $newUserdata);
$dataFile = fopen($this->_dataFile, "w") or die("Unable to open file!");
$allUserdata[$username] = $newUserdata;
fwrite($dataFile, json_encode($allUserdata));
fclose($dataFile) or die("Unable to write to data file!");
}
}