File: simpleserver.php

Recommend this page to a friend!
  Classes of Chris Jeffries  >  Web JSON Editor  >  simpleserver.php  >  Download  
File: simpleserver.php
Role: Auxiliary script
Content type: text/plain
Description: server for example
Class: Web JSON Editor
Create data values in JSON format interactively
Author: By
Last change:
Date: 4 years ago
Size: 4,803 bytes
 

Contents

Class file image Download
<?php session_start(); //require_once('SimpleServer.class.php'); header('Content-type: application/json'); /*** * REQUEST FORMAT * JSON encoded string supplied as the POST parameter 'data' * action getData, putData or listData * objectname (getData & putData) name of a file in the designated directory * objectdata (putdata)the data to be stored in that file * * REPLY FORMAT * JSON encoded string containing the following properties * errnum numeric code (Server else SQL92) * error textual desxription of error * severity errorlevel * objectdata as retrieved from file ***/ if(!isset($_REQUEST['data']) ) { echo '{ "errorMessage":"SSRV: No request received", "errorNum":"SSRV:1", "severity":2 }'; die(); } $server = new SimpleServer(); echo json_encode($server->evaluate($_REQUEST['data']), JSON_PRETTY_PRINT); /*** * Class simpleserver.php - serves JSON files * * the file must start <?php /* and end appropriately too, must already * exist and must be valid JSON. The call may be POST or GET. * * @param string fn the name of the file to get * @returns object PHP type ***/ class SimpleServer { private $fn; private $json; private $FPATH = "/home/chris/json/"; private $response; function __construct() { $this->response = new stdClass(); $this->response->errorMessage ='OK'; $this->response->errorNo = 0; $this->response->severity = 0; $this->response->resultData = new stdClass(); } function evaluate($data) { $data = $this->testjson($data); if(!isset($data->action)) { $this->send('{"errorMessage":"SSRV:Invalid action type","errorNo":25,"severity":2}'); } switch($data->action) { case 'getData': if(!isset($data->objectName) && !isset($data->queryName)) { $this->send('{"errorMessage":"SSRV:Invalid name","errorNo":26,"severity":2}'); } if(!isset($data->objectName)) { $data->objectName = $data->queryName; } $this->response->resultData = $this->get($data->objectName); break; case 'putData': $this->response->resultData = $this->put($data->objectData, $data->objectName); break; case 'list': $this->response->resultData = $this->getList(); break; default: $this->response->errorMessage = "SSRV:Invalid action type: {$data->action}"; $this->response->errorNo = 24; $this->response->severity = 2; } return $this->response; } function get($fn) { $fn = $this->FPATH . $fn; //does the file even exist? if (!file_exists($fn)) { $this->response->errorMessage = "SSRV:File does not exist ".$fn; $this->response->errorNo = 20; $this->response->severity = 2; return (object) []; } $json = file_get_contents($fn); $_SESSION['simpleserver'] = []; $_SESSION['simpleserver']['lastfile'] = $fn; $result = new stdClass(); return $this->testJSON($json); } function put($json, $fn) { $fn = $this->FPATH . $fn; if(!isset($_SESSION['simpleserver']) || !isset($_SESSION['simpleserver']['lastfile'])) { $this->response->errorMessage = "SSRV:Invalid invocation - get file first"; $this->response->errorNo = 21; $this->response->severity = 2; return (object) []; } if ($fn != $_SESSION['simpleserver']['lastfile']) { $this->response->errorMessage = "SSRV:Invalid invocation - wrong file name"; $this->response->errorNo = 22; $this->response->severity = 2; return (object) []; } try { file_put_contents($fn, json_encode($json, JSON_PRETTY_PRINT)); $this->response->errorMessage = 'Your data has been saved'; return (object)[]; } catch (Exception $e) { $this->response->errorMessage ="SSRV:Error saving file:".$e; $this->response->errorNo = 23; $this->response->severity = 2; return (object) []; } } function getList() { $searchpath = $this->FPATH. '*'; $files = glob($searchpath); $resultData = []; $crop = strlen($this->FPATH); foreach($files as $file) { $name = substr($file, $crop); $resultData[] = (object) ["queryName"=>$name]; } return $resultData; } /*** * testjson - receive a JSON string - convert to PHP object * * @note set repsonse error codes if there is an error - check * $this->response->severity if error status needs to be known * @para string JSON * @return object PHP version of JSON string or empty object on error * ***/ function testJSON($json,$candie=false) { $temp = json_decode($json); if ($this->response->errorNo == JSON_ERROR_NONE) { return $temp; } else { $this->response->errorMessage = "JSON: " . json_last_error_msg(); $this->response->errorNo = json_last_error(); $this->response->severity = 2; return (object) []; } } }