Some parts restructured

This commit is contained in:
Michael Schmidt 2013-10-25 17:18:23 +02:00
parent 836e066ded
commit 69c033f36a
3 changed files with 41 additions and 30 deletions

19
api.php
View file

@ -1,4 +1,5 @@
<?php <?php
require_once "classes/class.request.php"
require_once "classes/class.datahandler.php"; require_once "classes/class.datahandler.php";
if (isset($_REQUEST['action'])) { if (isset($_REQUEST['action'])) {
@ -8,22 +9,24 @@ if (isset($_REQUEST['action'])) {
// get data // get data
case 'get': case 'get':
if (!isset($_GET['id'])) break; if (!isset($_GET['id'])) break;
$id = (string) $_GET['id']; $request = new Request();
$request->id = (string) $_GET['id'];
$datahandler = new DataHandler($id); $datahandler = new DataHandler($request);
echo json_encode($datahandler->_get()); echo json_encode($datahandler->_get());
break; break;
// write new data or update existing data // write new data or update existing data
case 'set': case 'set':
$id = (isset($_POST['id'])) ? (string) $_POST['id'] : 0;
$version = (isset($_POST['version'])) ? (string) $_POST['version'] : '';
if (!isset($_POST['data'])) break; if (!isset($_POST['data'])) break;
$data = (string) $_POST['data'];
$datahandler = new DataHandler($id); $request = new Request();
echo json_encode($datahandler->_set($data, $version)); $request->id = (isset($_POST['id'])) ? (string) $_POST['id'] : '';
$request->version = (isset($_POST['version'])) ? (string) $_POST['version'] : '';
$request->data = (string) $_POST['data'];
$datahandler = new DataHandler($request);
echo json_encode($datahandler->_set());
break; break;
} }
} }

View file

@ -1,4 +1,7 @@
<?php <?php
require_once "classes/class.request.php"
class DataHandler { class DataHandler {
// (string) folder to store data in // (string) folder to store data in
// webserver has to have write access to this folder // webserver has to have write access to this folder
@ -12,19 +15,19 @@ class DataHandler {
// (int) limit request per IP in seconds; no limit if zero // (int) limit request per IP in seconds; no limit if zero
const TRAFIC_LIMITER = 30; const TRAFIC_LIMITER = 30;
protected $id; protected $request;
protected $return; protected $return;
protected $version;
public function __construct($id = '') { public function __construct(Request &$request) {
if ($id === '') $id = $this->getNewId(); $this->request =& $request;
$this->id = preg_replace('/[^0-9a-zA-Z]/', '', $id); // remove every char, except allowed if ($request->id === '') $request->id = $this->getNewId();
$request->id = preg_replace('/[^0-9a-zA-Z]/', '', $request->id); // remove every char, except allowed
$this->return = new stdClass(); $this->return = new stdClass();
} }
public function _get() { public function _get() {
$this->return->id = $this->id; $this->return->id = $this->request->id;
$data = $this->readData(); $data = $this->readData();
if ($data === false) { if ($data === false) {
@ -39,12 +42,8 @@ class DataHandler {
return $this->return; return $this->return;
} }
public function _set($data, $version = '') { public function _set() {
$data = (string) $data; $this->return->id = $this->request->id;
$version = (string) $version;
$this->return = new stdClass();
$this->return->id = $this->id;
// try to read existing data // try to read existing data
$data_org = $this->readData(); $data_org = $this->readData();
@ -135,20 +134,20 @@ class DataHandler {
protected function readData() { protected function readData() {
// check if must have files exist // check if must have files exist
if (!file_exists(self::DATA_FOLDER.$this->id."/head") OR if (!file_exists(self::DATA_FOLDER.$this->request->id."/head") OR
!file_exists(self::DATA_FOLDER.$this->id."/data")) { !file_exists(self::DATA_FOLDER.$this->request->id."/data")) {
return false; return false;
} }
$data = new stdClass(); $data = new stdClass();
$data->head = file_get_contents(self::DATA_FOLDER.$this->id."/head"); $data->head = file_get_contents(self::DATA_FOLDER.$this->request->id."/head");
$data->data = file_get_contents(self::DATA_FOLDER.$this->id."/data"); $data->data = file_get_contents(self::DATA_FOLDER.$this->request->id."/data");
$data->user = array (); $data->user = array ();
$i = 0; $i = 0;
while (true) { while (true) {
if (file_exists(self::DATA_FOLDER.$this->id."/user_".$i)) { if (file_exists(self::DATA_FOLDER.$this->request->id."/user_".$i)) {
$data->user[] = file_get_contents(self::DATA_FOLDER.$this->id."/user_".$i); $data->user[] = file_get_contents(self::DATA_FOLDER.$this->request->id."/user_".$i);
$i++; $i++;
} }
else break; else break;
@ -158,8 +157,8 @@ class DataHandler {
} }
protected function writeData($data) { protected function writeData($data) {
if (!file_exists(self::DATA_FOLDER.$this->id."/")) { if (!file_exists(self::DATA_FOLDER.$this->request->id."/")) {
if (!mkdir(self::DATA_FOLDER.$this->id)) { if (!mkdir(self::DATA_FOLDER.$this->request->id)) {
$this->return->result = false; $this->return->result = false;
$this->return->errorMsg = 'data could not be written 1'; $this->return->errorMsg = 'data could not be written 1';
return false; return false;
@ -181,9 +180,9 @@ class DataHandler {
} }
protected function writeDatum($typ, $data) { protected function writeDatum($typ, $data) {
if(file_put_contents(self::DATA_FOLDER.$this->id.'/'.$typ, $data, LOCK_EX) === false) { if(file_put_contents(self::DATA_FOLDER.$this->request->id.'/'.$typ, $data, LOCK_EX) === false) {
$this->return->result = false; $this->return->result = false;
$this->return->errorMsg = 'data could not be written 2'.$typ; $this->return->errorMsg = 'data could not be written to '.$typ;
return false; return false;
} }

View file

@ -0,0 +1,9 @@
<?php
class Request {
public $id;
public $version;
public $data;
}
?>