<?php
require_once('db.php');
require_once('response.php');
//require_once('connectDB');
//header('Access-Control-Allow-Methods: GET');
// function that redirects to another url
function redirect($url, $statusCode = 404)
{
header('Location: ' . $url, true, $statusCode);
die();
}
//giving permission to access only for the specific IP
if($_SERVER[REMOTE_ADDR]==='87.202.220.188')
{
//connection with the database
include('connectDB.php');
// if it's not a post request, redirection to non-existent page
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
redirect('http://paragon.gr/content/', false);
}
// check request's content type header is JSON
if($_SERVER['CONTENT_TYPE'] !== 'application/json') {
// set up response for unsuccessful request
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("Content Type header not set to JSON");
$response->send();
exit;
}
// get POST request body as the POSTed data will be JSON format
//$rawPostData = file_get_contents('php://input');
/*
if(!$jsonData = json_decode($rawPostData)) {
// set up response for unsuccessful request
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("Request body is not valid JSON");
$response->send();
exit;
}
*/
//triming name in case there are spaces in between
$name = trim($jsonData->name);
$test = $jsonData->test;
// inserting the data in the database table
// $query = $writeDB->prepare('UPDATE test_db (name, test) values (:name, :test) WHERE `test_db`.`id` = 12');
//UPDATE `test_db` SET `name` = 'ha!' WHERE `users`.`id` = 44
$query = $writeDB->prepare('UPDATE `test_db` SET `name` = `ha!` WHERE `users`.`id` = 44');
$query->bindParam(':name', $name, PDO::PARAM_STR);
$query->bindParam(':test', $test, PDO::PARAM_STR);
$query->execute();
$returnData = array();
// $returnData['user_id'] = $lastID;
$returnData['name'] = $name;
$returnData['test'] = $test;
// .json format to show success when content is added to the database
$response = new Response();
$response->setHttpStatusCode(201);
$response->setSuccess(true);
$response->addMessage("= created");
$response->setData($returnData);
$response->send();
exit;
// will put redirection link to send command to specific ip
// to give and update on the location they are going to mine the data.
} // redirection to non-existent page for the IPs without permission to access
else{
redirect('http://paragon.gr/content/', false);
}
?>