Example PHP Server Side Script




Download Source File

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$action = $_GET['action'];
$name = $_GET['name'];
$type = $_GET['type'];
$index = $_GET['index'];
$ID = $_GET['ID'];
$category = $_GET['category'];
$issued = $_GET['issued'];
$expire = $_GET['expire'];
$point = $_GET['point'];
$forecaster = $_GET['forecaster'];
$password = $_GET['password'];
$text = $_GET['text'];
$variable = $_GET['variable'];

$ACCESS_CODE = "test";

if ($password != $ACCESS_CODE)
{
    die("Access Denied");
}

$oldFile = fopen("products.json", "r");
$oldData = fread($oldFile, filesize("products.json"));
fclose($oldFile);

$oldLine = explode("\n", $oldData);
$oldJSON = json_decode($oldData, true);
$N = count($oldJSON);

$mode = 0;

if (strlen($oldData) == 0)
{
    $mode = 1;
}

if ($action == "NEW")
{
    $ID = (int) $ID;

    for ($i = 0; $i < $N; $i++)
    {
        if ($oldJSON[$i]['product']['ID'] == $ID && $oldJSON[$i]['product']['type'] == $type)
        {
            $ID = $oldJSON[$i]['product']['ID'] + 1;
        }
    }
}

$newString = "";
$newString = $newString . '{"product": {"name": "' . $name . '", ';
$newString = $newString . '"type": "' . $type . '", ';
$newString = $newString . '"index": ' . $index . ', ';
$newString = $newString . '"ID": ' . $ID . ', ';
$newString = $newString . '"category": "' . $category . '", ';
$newString = $newString . '"issued": "' . $issued . '", ';
$newString = $newString . '"expire": "' . $expire . '", ';
$newString = $newString . '"point": "' . $point . '", ';
$newString = $newString . '"forecaster": "' . $forecaster . '", ';
$newString = $newString . '"text": "' . $text . '", ';

if ($action == "CAN")
{
    $newString = $newString . '"show": false, ';
}
else
{
    $newString = $newString . '"show": true, ';
}

$newString = $newString . '"variable": ' . $variable . '}}';

if ($mode == 0)
{
    if ($action == "CON" || $action == "CAN")
    {
        $ID = (int) $ID;

        for ($i = 0; $i < $N; $i++)
        {
            if ($oldJSON[$i]['product']['ID'] == $ID && $oldJSON[$i]['product']['type'] == $type)
            {
                if ($i != $N - 1)
                {
                    $newString = $newString . ',';
                }

                $newData = str_replace($oldLine[$i+1], $newString, $oldData);
            }
        }
    }
    else if ($action == "DEL")
    {
        $ID = (int) $ID;

        if ($N == 1)
        {
            $newData = "";
        }
        else
        {
            for ($i = 0; $i < $N; $i++)
            {
                if ($oldJSON[$i]['product']['ID'] == $ID && $oldJSON[$i]['product']['type'] == $type)
                {
                    $textKey = $oldLine[$i+1] . "\n";

                    if ($i == $N - 1)
                    {
                        $textKey = ",\n" . $oldLine[$i+1];
                    }

                    $newData = str_replace($textKey, "", $oldData);
                }
            }
        }
    }
    else
    {
        $newData = substr($oldData, 0, strlen($oldData) - 2) . ",\n" . $newString . "\n]";
    }
}
else
{
    $newData = "[\n" . $newString . "\n]";
}

$outFile = fopen("products.json", 'w');
fwrite($outFile, $newData);
fclose($outFile);

?>


Download Source File