31 lines
780 B
PHP
31 lines
780 B
PHP
|
<?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!");
|
||
|
}
|
||
|
|
||
|
}
|