2013-10-22 11:16:08 +02:00
|
|
|
<?php
|
|
|
|
class DataHandler {
|
|
|
|
protected $data_folder = 'data/';
|
|
|
|
protected $id;
|
|
|
|
protected $version;
|
|
|
|
|
|
|
|
public function __construct($id = '') {
|
|
|
|
if ($id === '') $id = $this->getNewId();
|
|
|
|
$this->id = preg_replace('/[^0-9a-zA-Z]/', '', $id); // remove every char, except allowed
|
|
|
|
}
|
|
|
|
|
|
|
|
public function _get() {
|
|
|
|
$return = new stdClass();
|
|
|
|
$return->id = $this->id;
|
|
|
|
|
|
|
|
$data = $this->readData();
|
|
|
|
if ($data === false) {
|
|
|
|
$return->result = false;
|
2013-10-22 15:08:44 +02:00
|
|
|
$return->errorMsg = 'there is no data with this identifier or data could not be read';
|
|
|
|
return $return;
|
2013-10-22 11:16:08 +02:00
|
|
|
}
|
2013-10-22 15:08:44 +02:00
|
|
|
|
|
|
|
$return->result = true;
|
|
|
|
$return->version = md5($data);
|
|
|
|
$return->data = $data;
|
2013-10-22 11:16:08 +02:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function _set($data, $version = '') {
|
|
|
|
$data = (string) $data;
|
|
|
|
$version = (string) $version;
|
|
|
|
|
|
|
|
$return = new stdClass();
|
|
|
|
$return->id = $this->id;
|
|
|
|
|
2013-10-22 15:08:44 +02:00
|
|
|
// check if version is out of date
|
2013-10-22 11:16:08 +02:00
|
|
|
$data_org = $this->readData();
|
|
|
|
if ($data_org !== false && md5($data_org) !== $version) {
|
|
|
|
$return->result = false;
|
2013-10-22 15:08:44 +02:00
|
|
|
$return->errorMsg = 'used version is out of date';
|
|
|
|
return $return;
|
2013-10-22 11:16:08 +02:00
|
|
|
}
|
2013-10-22 15:08:44 +02:00
|
|
|
|
|
|
|
// write data
|
|
|
|
if(file_put_contents($this->data_folder.$this->id, $data, LOCK_EX ) === false) {
|
|
|
|
$return->result = false;
|
|
|
|
$return->errorMsg = 'data could not be written';
|
|
|
|
return $return;
|
2013-10-22 11:16:08 +02:00
|
|
|
}
|
2013-10-22 15:08:44 +02:00
|
|
|
|
|
|
|
$return->version = md5($data);
|
|
|
|
$return->result = true;
|
2013-10-22 11:16:08 +02:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getNewId() {
|
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
$length = 10;
|
2013-10-22 15:34:53 +02:00
|
|
|
$randomString = '';
|
|
|
|
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$randomString .= $characters[rand(0, strlen($characters) - 1)];
|
|
|
|
}
|
2013-10-22 11:16:08 +02:00
|
|
|
|
|
|
|
// check if id is already used, generate new one if necessary
|
|
|
|
if (file_exists($this->data_folder.$randomString)) $randomString = getNewId();
|
|
|
|
|
2013-10-22 15:34:53 +02:00
|
|
|
return $randomString;
|
2013-10-22 11:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function readData() {
|
|
|
|
return file_exists($this->data_folder.$this->id) ? file_get_contents($this->data_folder.$this->id) : false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|