Rest Api slim3 php7 Apache2 mysql mongo-replica
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

156 lines
5.2 KiB

<?php
use Slim\Http\Request;
use Slim\Http\Response;
use GuzzleHttp\Client as LClient;
use Http\Adapter\Guzzle6\Client as LAdapter;
use Psr\Http\Message\ResponseInterface as LResponseInterface;
use GuzzleHttp\Exception\RequestException as LRequestException;
use GuzzleHttp\Psr7\Request as LRequest;
use Http\Client\Exception;
use Nahid\JsonQ\Jsonq;
use Anddye\Validation\Validator;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator as v;
use Respect\Validation\Rules;
use MongoDB\Client;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
$postCallbackHelloworld = function ($request, $response, $id) use ($app, $server, $MongoClient) {
// ******************** Custom local functions helper *****************************
/**
* Creating date collection between two dates
* date_range("2014-01-01", "2014-01-20", "+1 day", "m/d/Y");
*/
function date_range($first, $last, $step = '+1 day', $output_format = 'Y/m/d' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
// ******************** Custom function *****************************
// ****************** get user info **********
$token = $server->getAccessTokenData(OAuth2\Request::createFromGlobals());
$USER=$token['client_id'];
// ****************** get user info **********
// ****************** Get data **********
//$json = $request->getBody();
$jsonpost = $request->getBody()->getContents();
$j = json_decode($jsonpost, true); // parse the JSON into an assoc. array
// ****************** Get data **********
// ****************** VALIDATION **********
$validation_error=true;
try {
// ****************** 1. validate custom **********
/*
$jname = $j['name'];
// ******** yamlname valid *****************************
$r['name'] = new Rules\AllOf(
new Rules\Regex('/^[A-Za-z0-9\-\_]+$/')
);
$r['name']->check($name);
// ******** yamlname valid end
*/
// ****************** 2. validate **********
/*
$r = new stdClass;
$r->labid = $lab;
//$r->inventoryid = $labInventoryValue;
// ****************** make obj for validate data **********
//$rv = v::attribute('labid', v::notEmpty()->alnum()->length(15,32)->noWhitespace())
// ->attribute('inventoryid',v::notEmpty()->alnum()->length(15,32)->noWhitespace());
$rv = v::attribute('labid', v::notEmpty()->alnum()->length(15,32)->noWhitespace());
$rv->check($r);
*/
} catch(ValidationException $exception) {
$validation_error=false;
$rr["error"][]=$exception->getMainMessage();
}
// ****************** VALIDATION **********
/*
// ****************** MAIN **********
if($validation_error){
// --------------- create obj with data -----------
try {
$user=$USER;
// ****************** mongo **********
$mongo = $MongoClient;
$mplaybook = $mongo->log->log;
$datetmp=(new \DateTime())->format('Y-m-d H:i:s');
$inputdate = strtotime($datetmp);
$date = new \MongoDB\BSON\UTCDateTime($inputdate * 1000);
$insertPlaybks1 = $mplaybook1->insertOne([
'user' => $USER,
'date' => $date
]);
// ****************** mongo **********
$rr["action"]='ok';
$rr["error"][]="ok";
} catch(ValidationException $exception) {
$rr["action"]='no';
$rr["error"][]=$exception->getMainMessage();
}
}
$datetmp=(new \DateTime())->format('Y-m-d H:i:s');
$inputdate = strtotime($datetmp);
$date = new \MongoDB\BSON\UTCDateTime($inputdate * 1000);
$insertPlaybks1 = $mplaybook->insertOne([
'user' => 'user',
'date' => $date
]);
*/
try {
$m=$MongoClient->log->log;
$datetmp=(new \DateTime())->format('Y-m-d H:i:s');
$inputdate = strtotime($datetmp);
$date = new \MongoDB\BSON\UTCDateTime($inputdate * 1000);
$insert = $m->insertOne([
'user' => 'user',
'date' => $date
]);
$q = $m->find([
'user' => 'user'
]);
//var_dump($q);
$c=0;
foreach ($q as $doc) {
$rr['q'][$c]=$doc; // testing comment in production
$c++;
//var_dump($doc);
}
} catch (MongoDB\Driver\Exception\AuthenticationException $e) {
echo "Exception:", $e->getMessage(), "\n";
} catch (MongoDB\Driver\Exception\ConnectionException $e) {
echo "Exception:", $e->getMessage(), "\n";
} catch (MongoDB\Driver\Exception\ConnectionTimeoutException $e) {
echo "Exception:", $e->getMessage(), "\n";
}
// ****************** MAIN **********
$rr['user']=$USER;
$rr['json']=$json; // testing comment in production
$response->getBody()->write(json_encode($rr));
return $response;
};
$app->post('/helloworld', $postCallbackHelloworld)->add($authMiddleware->withRequiredScope(['superuser', 'owner']));