Skip to content

Instantly share code, notes, and snippets.

@LeoDJ
Last active March 16, 2017 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeoDJ/1b72ead728b7b2b455041d33e29d4a15 to your computer and use it in GitHub Desktop.
Save LeoDJ/1b72ead728b7b2b455041d33e29d4a15 to your computer and use it in GitHub Desktop.
A simple PHP persistance key/val store // Simply set the $secret to your password of choice and host the .php file on a server, then call the .php file with ?help for more information
<?php
header('Access-Control-Allow-Origin: https://yourDomain.tld');
$secret = "yourSecret"; //set you secret for write access here
error_reporting(E_ALL);
ini_set('display_errors', true);
global $filePath, $secret;
$filePath = "doorStatusData.json";
function readDataFile() { //read data from persistance file
global $filePath;
if(file_exists($filePath)) {
$result = file_get_contents($filePath);
if(!is_null($result))
return json_decode($result, true);
else {
return NULL;
}
} else
return NULL;
}
function writeToFile($key, $val) { //write a key/val pair into persistance file
global $filePath;
$data = readDataFile();
if(is_null($data))
$data = array();
$data[$key] = $val;
file_put_contents($filePath, json_encode($data));
}
if(sizeof($_GET) > 0) { //if any parameters were given
if(array_key_exists("getData", $_GET)){ //handle getData function
$data = readDataFile();
if(is_null($data))
echo '<div class="error"><b>Error</b>: could not read file<div>';
else {
if(array_key_exists("key", $_GET)) {
if(array_key_exists($_GET["key"], $data))
echo $data[$_GET["key"]];
else
echo '<div class="error"><b>Error</b>: Key ' . $_GET["key"] . ' not found.<div>';
}
else
echo '<div class="error"><b>Error</b>: No key field provided, please consult the <a href="?help">help page</a><div>';
}
}
elseif(array_key_exists("setData", $_GET)) { //handle setData function
if(array_key_exists("key", $_GET) && array_key_exists("val", $_GET) && array_key_exists("secret", $_GET)) {
global $secret;
if($_GET["secret"] == $secret) {
writeToFile($_GET["key"], $_GET["val"]);
echo "Write successful";
}
else
echo '<div class="error"><b>Error</b>: Wrong secret<div>';
}
else
echo '<div class="error"><b>Error</b>: Insufficient parameters. Please consult the <a href="?help">help page</a><div>';
}
elseif(array_key_exists("help", $_GET)) {
echo '
<style>
#doorStatusHelpPage table, #doorStatusHelpPage th, #doorStatusHelpPage td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<div id="doorStatusHelpPage">
The following methods are currently supported:<br>
<table>
<tr>
<th><b>Parameters</b></th>
<th><b>Description</b></th>
</tr>
<tr>
<td>?help</td>
<td>shows this help page</td>
</tr>
<tr>
<td>?getData&key=[key]</td>
<td>gets the value of the supplied key</td>
</tr>
<tr>
<td>?setData&key=[key]&val=[val]&secret=[secret]</td>
<td>sets the value of the supplied key to the supplied val if the correct secret is supplied</td>
</tr>
</table>
<p>The source code is available <a href="https://gist.github.com/LeoDJ/1b72ead728b7b2b455041d33e29d4a15">here</a></p>
</div>';
}
else //if none of the above methods were given
echo '<div class="error"><b>Error</b>: Unknown method. Please consult the <a href="?help">help page</a><div><div>';
}
else //if no parameters were given at all
echo '<div class="error"><b>Error</b>: No method provided. Please consult the <a href="?help">help page</a><div><div>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment