mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 06:06:45 +02:00
feat(entries): Storage API implementation - next round #552
This commit is contained in:
@@ -1,672 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Http\Response;
|
||||
use Atomastic\Arrays\Arrays;
|
||||
|
||||
use function array_replace_recursive;
|
||||
use function count;
|
||||
use function filesystem;
|
||||
use function flextype;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Validate content content token
|
||||
*/
|
||||
function validateContentToken(string $token): bool
|
||||
{
|
||||
return filesystem()->file(PATH['project'] . '/tokens/content/' . $token . '/token.yaml')->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch content(content)
|
||||
*
|
||||
* endpoint: GET /api/content
|
||||
*
|
||||
* Query:
|
||||
* id - [REQUIRED] - Unique identifier of the content(content).
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* filter - [OPTIONAL] - Select items in collection by given conditions.
|
||||
*
|
||||
* Returns:
|
||||
* An array of content item objects.
|
||||
*/
|
||||
flextype()->get('/api/content', function (Request $request, Response $response) use ($apiErrors) {
|
||||
// Get Query Params
|
||||
$query = $request->getQueryParams();
|
||||
|
||||
if (! isset($query['id']) || ! isset($query['token'])) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0100']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0100']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$id = $query['id'];
|
||||
$token = $query['token'];
|
||||
$options = $query['options'] ?? [];
|
||||
$method = $query['options']['method'] ?? '';
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.content.enabled')) {
|
||||
// Validate content token
|
||||
if (validateContentToken($token)) {
|
||||
$contentTokenFilePath = PATH['project'] . '/tokens/content/' . $token . '/token.yaml';
|
||||
|
||||
// Set content token file
|
||||
if ($contentTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($contentTokenFilePath)->get())) {
|
||||
if (
|
||||
$contentTokenFileData['state'] === 'disabled' ||
|
||||
($contentTokenFileData['limit_calls'] !== 0 && $contentTokenFileData['calls'] >= $contentTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
// override content.fetch.result
|
||||
flextype('registry')->set('flextype.settings.content.content.fields.content.fetch.result', 'toArray');
|
||||
|
||||
if (isset($method) &&
|
||||
strpos($method, 'fetch') !== false &&
|
||||
is_callable([flextype('content'), $method])) {
|
||||
$fetchFromCallbackMethod = $method;
|
||||
} else {
|
||||
$fetchFromCallbackMethod = 'fetch';
|
||||
}
|
||||
|
||||
// Get fetch result
|
||||
$responseData['data'] = flextype('content')->{$fetchFromCallbackMethod}($id, $options);
|
||||
$responseData['data'] = ($responseData['data'] instanceof Arrays) ? $responseData['data']->toArray() : $responseData['data'];
|
||||
|
||||
// Set response code
|
||||
$responseCode = count($responseData['data']) > 0 ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($contentTokenFilePath)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($contentTokenFileData, ['calls' => $contentTokenFileData['calls'] + 1])));
|
||||
|
||||
if ($responseCode === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($apiErrors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0102']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($responseCode)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($responseData));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Create content
|
||||
*
|
||||
* endpoint: POST /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* accessToken - [REQUIRED] - Valid Access token.
|
||||
* data - [REQUIRED] - Data to store for the content.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content item object for the content item that was just created.
|
||||
*/
|
||||
flextype()->post('/api/content', function (Request $request, Response $response) use ($apiErrors) {
|
||||
// Get Post Data
|
||||
$postData = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($postData['token']) || ! isset($postData['access_token']) || ! isset($postData['id']) || ! isset($postData['data'])) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $postData['token'];
|
||||
$accessToken = $postData['access_token'];
|
||||
$id = $postData['id'];
|
||||
$data = $postData['data'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.content.enabled')) {
|
||||
// Validate content and access token
|
||||
if (validateContentToken($token) && validate_accessToken($accessToken)) {
|
||||
$contentTokenFilePath = PATH['project'] . '/tokens/content/' . $token . '/token.yaml';
|
||||
$accessTokenFilePath = PATH['project'] . '/tokens/access/' . $accessToken . '/token.yaml';
|
||||
|
||||
// Set content and access token file
|
||||
if (
|
||||
($contentTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($contentTokenFilePath)->get())) &&
|
||||
($accessTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($accessTokenFilePath)->get()))
|
||||
) {
|
||||
if (
|
||||
$contentTokenFileData['state'] === 'disabled' ||
|
||||
($contentTokenFileData['limit_calls'] !== 0 && $contentTokenFileData['calls'] >= $contentTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$accessTokenFileData['state'] === 'disabled' ||
|
||||
($accessTokenFileData['limit_calls'] !== 0 && $accessTokenFileData['calls'] >= $accessTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
// Create content
|
||||
$createContent = flextype('content')->create($id, $data);
|
||||
|
||||
if ($createContent) {
|
||||
$responseData['data'] = flextype('content')->fetch($id);
|
||||
} else {
|
||||
$responseData['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$responseCode = $createContent ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($contentTokenFilePath)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($contentTokenFileData, ['calls' => $contentTokenFileData['calls'] + 1])));
|
||||
|
||||
if ($responseCode === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($apiErrors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($responseCode)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($responseData));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Update content
|
||||
*
|
||||
* endpoint: PATCH /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* accessToken - [REQUIRED] - Valid Authentication token.
|
||||
* data - [REQUIRED] - Data to update for the content.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content item object for the content item that was just updated.
|
||||
*/
|
||||
flextype()->patch('/api/content', function (Request $request, Response $response) use ($apiErrors) {
|
||||
// Get Post Data
|
||||
$postData = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($postData['token']) || ! isset($postData['access_token']) || ! isset($postData['id']) || ! isset($postData['data'])) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $postData['token'];
|
||||
$accessToken = $postData['access_token'];
|
||||
$id = $postData['id'];
|
||||
$data = $postData['data'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.content.enabled')) {
|
||||
// Validate content and access token
|
||||
if (validateContentToken($token) && validate_accessToken($accessToken)) {
|
||||
$contentTokenFilePath = PATH['project'] . '/tokens/content/' . $token . '/token.yaml';
|
||||
$accessTokenFilePath = PATH['project'] . '/tokens/access/' . $accessToken . '/token.yaml';
|
||||
|
||||
// Set content and access token file
|
||||
if (
|
||||
($contentTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($contentTokenFilePath)->get())) &&
|
||||
($accessTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($accessTokenFilePath)->get()))
|
||||
) {
|
||||
if (
|
||||
$contentTokenFileData['state'] === 'disabled' ||
|
||||
($contentTokenFileData['limit_calls'] !== 0 && $contentTokenFileData['calls'] >= $contentTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$accessTokenFileData['state'] === 'disabled' ||
|
||||
($accessTokenFileData['limit_calls'] !== 0 && $accessTokenFileData['calls'] >= $accessTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
// Update content
|
||||
$update_content = flextype('content')->update($id, $data);
|
||||
|
||||
if ($update_content) {
|
||||
$responseData['data'] = flextype('content')->fetch($id);
|
||||
} else {
|
||||
$responseData['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$responseCode = $update_content ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($contentTokenFilePath)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($contentTokenFileData, ['calls' => $contentTokenFileData['calls'] + 1])));
|
||||
|
||||
if ($responseCode === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($apiErrors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($responseCode)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($responseData));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Move content
|
||||
*
|
||||
* endpoint: PUT /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* newId - [REQUIRED] - New Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* accessToken - [REQUIRED] - Valid Authentication token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content item object for the content item that was just moved.
|
||||
*/
|
||||
flextype()->put('/api/content', function (Request $request, Response $response) use ($apiErrors) {
|
||||
// Get Post Data
|
||||
$postData = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($postData['token']) || ! isset($postData['access_token']) || ! isset($postData['id']) || ! isset($postData['new_id'])) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $postData['token'];
|
||||
$accessToken = $postData['access_token'];
|
||||
$id = $postData['id'];
|
||||
$newId = $postData['new_id'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.content.enabled')) {
|
||||
// Validate content and access token
|
||||
if (validateContentToken($token) && validate_accessToken($accessToken)) {
|
||||
$contentTokenFilePath = PATH['project'] . '/tokens/content/' . $token . '/token.yaml';
|
||||
$accessTokenFilePath = PATH['project'] . '/tokens/access/' . $accessToken . '/token.yaml';
|
||||
|
||||
// Set content and access token file
|
||||
if (
|
||||
($contentTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($contentTokenFilePath)->get())) &&
|
||||
($accessTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($accessTokenFilePath)->get()))
|
||||
) {
|
||||
if (
|
||||
$contentTokenFileData['state'] === 'disabled' ||
|
||||
($contentTokenFileData['limit_calls'] !== 0 && $contentTokenFileData['calls'] >= $contentTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$accessTokenFileData['state'] === 'disabled' ||
|
||||
($accessTokenFileData['limit_calls'] !== 0 && $accessTokenFileData['calls'] >= $accessTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
// Move content
|
||||
$move_content = flextype('content')->move($id, $newId);
|
||||
|
||||
// Get content data
|
||||
if ($move_content) {
|
||||
$responseData['data'] = flextype('content')->fetch($newId);
|
||||
} else {
|
||||
$responseData['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$responseCode = $move_content ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($contentTokenFilePath)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($contentTokenFileData, ['calls' => $contentTokenFileData['calls'] + 1])));
|
||||
|
||||
if ($responseCode === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($apiErrors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($responseCode)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($responseData));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Copy content(content)
|
||||
*
|
||||
* endpoint: PUT /api/content/copy
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* newId - [REQUIRED] - New Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* accessToken - [REQUIRED] - Valid Authentication token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content item object for the content item that was just copied.
|
||||
*/
|
||||
flextype()->put('/api/content/copy', function (Request $request, Response $response) use ($apiErrors) {
|
||||
// Get Post Data
|
||||
$postData = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($postData['token']) || ! isset($postData['access_token']) || ! isset($postData['id']) || ! isset($postData['new_id'])) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $postData['token'];
|
||||
$accessToken = $postData['access_token'];
|
||||
$id = $postData['id'];
|
||||
$newId = $postData['new_id'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.content.enabled')) {
|
||||
// Validate content and access token
|
||||
if (validateContentToken($token) && validate_accessToken($accessToken)) {
|
||||
$contentTokenFilePath = PATH['project'] . '/tokens/content/' . $token . '/token.yaml';
|
||||
$accessTokenFilePath = PATH['project'] . '/tokens/access/' . $accessToken . '/token.yaml';
|
||||
|
||||
// Set content and access token file
|
||||
if (
|
||||
($contentTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($contentTokenFilePath)->get())) &&
|
||||
($accessTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($accessTokenFilePath)->get()))
|
||||
) {
|
||||
if (
|
||||
$contentTokenFileData['state'] === 'disabled' ||
|
||||
($contentTokenFileData['limit_calls'] !== 0 && $contentTokenFileData['calls'] >= $contentTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$accessTokenFileData['state'] === 'disabled' ||
|
||||
($accessTokenFileData['limit_calls'] !== 0 && $accessTokenFileData['calls'] >= $accessTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
// Copy content
|
||||
$copy_content = flextype('content')->copy($id, $newId, true);
|
||||
|
||||
// Get content data
|
||||
if ($copy_content === null) {
|
||||
$responseData['data'] = flextype('content')->fetch($newId);
|
||||
} else {
|
||||
$responseData['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$responseCode = $copy_content === null ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($contentTokenFilePath)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($contentTokenFileData, ['calls' => $contentTokenFileData['calls'] + 1])));
|
||||
|
||||
if ($responseCode === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($apiErrors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($responseCode)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($responseData));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Delete content
|
||||
*
|
||||
* endpoint: DELETE /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* accessToken - [REQUIRED] - Valid Authentication token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns an empty body with HTTP status 204
|
||||
*/
|
||||
flextype()->delete('/api/content', function (Request $request, Response $response) use ($apiErrors) {
|
||||
// Get Post Data
|
||||
$postData = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($postData['token']) || ! isset($postData['access_token']) || ! isset($postData['id'])) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $postData['token'];
|
||||
$accessToken = $postData['access_token'];
|
||||
$id = $postData['id'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.content.enabled')) {
|
||||
// Validate content and access token
|
||||
if (validateContentToken($token) && validate_accessToken($accessToken)) {
|
||||
$contentTokenFilePath = PATH['project'] . '/tokens/content/' . $token . '/token.yaml';
|
||||
$accessTokenFilePath = PATH['project'] . '/tokens/access/' . $accessToken . '/token.yaml';
|
||||
|
||||
// Set content and access token file
|
||||
if (
|
||||
($contentTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($contentTokenFilePath)->get())) &&
|
||||
($accessTokenFileData = flextype('serializers')->yaml()->decode(filesystem()->file($accessTokenFilePath)->get()))
|
||||
) {
|
||||
if (
|
||||
$contentTokenFileData['state'] === 'disabled' ||
|
||||
($contentTokenFileData['limit_calls'] !== 0 && $contentTokenFileData['calls'] >= $contentTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$accessTokenFileData['state'] === 'disabled' ||
|
||||
($accessTokenFileData['limit_calls'] !== 0 && $accessTokenFileData['calls'] >= $accessTokenFileData['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
// Delete content
|
||||
$delete_content = flextype('content')->delete($id);
|
||||
|
||||
// Set response code
|
||||
$responseCode = $delete_content ? 204 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($contentTokenFilePath)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($contentTokenFileData, ['calls' => $contentTokenFileData['calls'] + 1])));
|
||||
|
||||
if ($responseCode === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($apiErrors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($responseCode)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($delete_content));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($apiErrors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($apiErrors['0003']));
|
||||
});
|
672
src/flextype/Endpoints/entries.php
Normal file
672
src/flextype/Endpoints/entries.php
Normal file
@@ -0,0 +1,672 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Http\Response;
|
||||
use Atomastic\Arrays\Arrays;
|
||||
|
||||
use function array_replace_recursive;
|
||||
use function count;
|
||||
use function filesystem;
|
||||
use function flextype;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Validate entries entries token
|
||||
*/
|
||||
function validateEntriesToken(string $token): bool
|
||||
{
|
||||
return filesystem()->file(PATH['project'] . '/tokens/entries/' . $token . '/token.yaml')->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch entry(entries)
|
||||
*
|
||||
* endpoint: GET /api/entries
|
||||
*
|
||||
* Query:
|
||||
* id - [REQUIRED] - Unique identifier of the entry(entries).
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* filter - [OPTIONAL] - Select items in collection by given conditions.
|
||||
*
|
||||
* Returns:
|
||||
* An array of entry item objects.
|
||||
*/
|
||||
flextype()->get('/api/entries', function (Request $request, Response $response) use ($api_errors) {
|
||||
// Get Query Params
|
||||
$query = $request->getQueryParams();
|
||||
|
||||
if (! isset($query['id']) || ! isset($query['token'])) {
|
||||
return $response
|
||||
->withStatus($api_errors['0100']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0100']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$id = $query['id'];
|
||||
$token = $query['token'];
|
||||
$options = $query['options'] ?? [];
|
||||
$method = $query['options']['method'] ?? '';
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.entries.enabled')) {
|
||||
// Validate entries token
|
||||
if (validateEntriesToken($token)) {
|
||||
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
|
||||
|
||||
// Set entries token file
|
||||
if ($entries_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($entries_token_file_path)->get())) {
|
||||
if (
|
||||
$entries_token_file_data['state'] === 'disabled' ||
|
||||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
// override entries.fetch.result
|
||||
flextype('registry')->set('flextype.settings.entries.fields.entries.fetch.result', 'toArray');
|
||||
|
||||
if (isset($method) &&
|
||||
strpos($method, 'fetch') !== false &&
|
||||
is_callable([flextype('entries'), $method])) {
|
||||
$fetchFromCallbackMethod = $method;
|
||||
} else {
|
||||
$fetchFromCallbackMethod = 'fetch';
|
||||
}
|
||||
|
||||
// Get fetch result
|
||||
$response_data['data'] = flextype('entries')->{$fetchFromCallbackMethod}($id, $options);
|
||||
$response_data['data'] = ($response_data['data'] instanceof Arrays) ? $response_data['data']->toArray() : $response_data['data'];
|
||||
|
||||
// Set response code
|
||||
$response_code = count($response_data['data']) > 0 ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($entries_token_file_path)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1])));
|
||||
|
||||
if ($response_code === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($api_errors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0102']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($response_code)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($response_data));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Create entry
|
||||
*
|
||||
* endpoint: POST /api/entries
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the entry.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* access_token - [REQUIRED] - Valid Access token.
|
||||
* data - [REQUIRED] - Data to store for the entry.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the entry item object for the entry item that was just created.
|
||||
*/
|
||||
flextype()->post('/api/entries', function (Request $request, Response $response) use ($api_errors) {
|
||||
// Get Post Data
|
||||
$post_data = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($post_data['token']) || ! isset($post_data['access_token']) || ! isset($post_data['id']) || ! isset($post_data['data'])) {
|
||||
return $response
|
||||
->withStatus($api_errors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $post_data['token'];
|
||||
$access_token = $post_data['access_token'];
|
||||
$id = $post_data['id'];
|
||||
$data = $post_data['data'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.entries.enabled')) {
|
||||
// Validate entries and access token
|
||||
if (validateEntriesToken($token) && validate_access_token($access_token)) {
|
||||
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
|
||||
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
|
||||
|
||||
// Set entries and access token file
|
||||
if (
|
||||
($entries_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($entries_token_file_path)->get())) &&
|
||||
($access_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($access_token_file_path)->get()))
|
||||
) {
|
||||
if (
|
||||
$entries_token_file_data['state'] === 'disabled' ||
|
||||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$access_token_file_data['state'] === 'disabled' ||
|
||||
($access_token_file_data['limit_calls'] !== 0 && $access_token_file_data['calls'] >= $access_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
// Create entry
|
||||
$create_entry = flextype('entries')->create($id, $data);
|
||||
|
||||
if ($create_entry) {
|
||||
$response_data['data'] = flextype('entries')->fetch($id);
|
||||
} else {
|
||||
$response_data['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$response_code = $create_entry ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($entries_token_file_path)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1])));
|
||||
|
||||
if ($response_code === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($api_errors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($response_code)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($response_data));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Update entry
|
||||
*
|
||||
* endpoint: PATCH /api/entries
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the entry.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* access_token - [REQUIRED] - Valid Authentication token.
|
||||
* data - [REQUIRED] - Data to update for the entry.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the entry item object for the entry item that was just updated.
|
||||
*/
|
||||
flextype()->patch('/api/entries', function (Request $request, Response $response) use ($api_errors) {
|
||||
// Get Post Data
|
||||
$post_data = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($post_data['token']) || ! isset($post_data['access_token']) || ! isset($post_data['id']) || ! isset($post_data['data'])) {
|
||||
return $response
|
||||
->withStatus($api_errors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $post_data['token'];
|
||||
$access_token = $post_data['access_token'];
|
||||
$id = $post_data['id'];
|
||||
$data = $post_data['data'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.entries.enabled')) {
|
||||
// Validate entries and access token
|
||||
if (validateEntriesToken($token) && validate_access_token($access_token)) {
|
||||
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
|
||||
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
|
||||
|
||||
// Set entries and access token file
|
||||
if (
|
||||
($entries_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($entries_token_file_path)->get())) &&
|
||||
($access_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($access_token_file_path)->get()))
|
||||
) {
|
||||
if (
|
||||
$entries_token_file_data['state'] === 'disabled' ||
|
||||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$access_token_file_data['state'] === 'disabled' ||
|
||||
($access_token_file_data['limit_calls'] !== 0 && $access_token_file_data['calls'] >= $access_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
// Update entry
|
||||
$update_entry = flextype('entries')->update($id, $data);
|
||||
|
||||
if ($update_entry) {
|
||||
$response_data['data'] = flextype('entries')->fetch($id);
|
||||
} else {
|
||||
$response_data['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$response_code = $update_entry ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($entries_token_file_path)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1])));
|
||||
|
||||
if ($response_code === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($api_errors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($response_code)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($response_data));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Move entry
|
||||
*
|
||||
* endpoint: PUT /api/entries
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the entry.
|
||||
* new_id - [REQUIRED] - New Unique identifier of the entry.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* access_token - [REQUIRED] - Valid Authentication token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the entry item object for the entry item that was just moved.
|
||||
*/
|
||||
flextype()->put('/api/entries', function (Request $request, Response $response) use ($api_errors) {
|
||||
// Get Post Data
|
||||
$post_data = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($post_data['token']) || ! isset($post_data['access_token']) || ! isset($post_data['id']) || ! isset($post_data['new_id'])) {
|
||||
return $response
|
||||
->withStatus($api_errors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $post_data['token'];
|
||||
$access_token = $post_data['access_token'];
|
||||
$id = $post_data['id'];
|
||||
$new_id = $post_data['new_id'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.entries.enabled')) {
|
||||
// Validate entries and access token
|
||||
if (validateEntriesToken($token) && validate_access_token($access_token)) {
|
||||
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
|
||||
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
|
||||
|
||||
// Set entries and access token file
|
||||
if (
|
||||
($entries_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($entries_token_file_path)->get())) &&
|
||||
($access_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($access_token_file_path)->get()))
|
||||
) {
|
||||
if (
|
||||
$entries_token_file_data['state'] === 'disabled' ||
|
||||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$access_token_file_data['state'] === 'disabled' ||
|
||||
($access_token_file_data['limit_calls'] !== 0 && $access_token_file_data['calls'] >= $access_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
// Move entry
|
||||
$move_entry = flextype('entries')->move($id, $new_id);
|
||||
|
||||
// Get entry data
|
||||
if ($move_entry) {
|
||||
$response_data['data'] = flextype('entries')->fetch($new_id);
|
||||
} else {
|
||||
$response_data['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$response_code = $move_entry ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($entries_token_file_path)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1])));
|
||||
|
||||
if ($response_code === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($api_errors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($response_code)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($response_data));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Copy entry(entries)
|
||||
*
|
||||
* endpoint: PUT /api/entries/copy
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the entry.
|
||||
* new_id - [REQUIRED] - New Unique identifier of the entry.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* access_token - [REQUIRED] - Valid Authentication token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the entry item object for the entry item that was just copied.
|
||||
*/
|
||||
flextype()->put('/api/entries/copy', function (Request $request, Response $response) use ($api_errors) {
|
||||
// Get Post Data
|
||||
$post_data = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($post_data['token']) || ! isset($post_data['access_token']) || ! isset($post_data['id']) || ! isset($post_data['new_id'])) {
|
||||
return $response
|
||||
->withStatus($api_errors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $post_data['token'];
|
||||
$access_token = $post_data['access_token'];
|
||||
$id = $post_data['id'];
|
||||
$new_id = $post_data['new_id'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.entries.enabled')) {
|
||||
// Validate entries and access token
|
||||
if (validateEntriesToken($token) && validate_access_token($access_token)) {
|
||||
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
|
||||
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
|
||||
|
||||
// Set entries and access token file
|
||||
if (
|
||||
($entries_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($entries_token_file_path)->get())) &&
|
||||
($access_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($access_token_file_path)->get()))
|
||||
) {
|
||||
if (
|
||||
$entries_token_file_data['state'] === 'disabled' ||
|
||||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$access_token_file_data['state'] === 'disabled' ||
|
||||
($access_token_file_data['limit_calls'] !== 0 && $access_token_file_data['calls'] >= $access_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
// Copy entry
|
||||
$copy_entry = flextype('entries')->copy($id, $new_id, true);
|
||||
|
||||
// Get entry data
|
||||
if ($copy_entry === null) {
|
||||
$response_data['data'] = flextype('entries')->fetch($new_id);
|
||||
} else {
|
||||
$response_data['data'] = [];
|
||||
}
|
||||
|
||||
// Set response code
|
||||
$response_code = $copy_entry === null ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($entries_token_file_path)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1])));
|
||||
|
||||
if ($response_code === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($api_errors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($response_code)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($response_data));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
});
|
||||
|
||||
/**
|
||||
* Delete entry
|
||||
*
|
||||
* endpoint: DELETE /api/entries
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the entry.
|
||||
* token - [REQUIRED] - Valid Entries token.
|
||||
* access_token - [REQUIRED] - Valid Authentication token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns an empty body with HTTP status 204
|
||||
*/
|
||||
flextype()->delete('/api/entries', function (Request $request, Response $response) use ($api_errors) {
|
||||
// Get Post Data
|
||||
$post_data = (array) $request->getParsedBody();
|
||||
|
||||
if (! isset($post_data['token']) || ! isset($post_data['access_token']) || ! isset($post_data['id'])) {
|
||||
return $response
|
||||
->withStatus($api_errors['0101'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0101']['http_status_code']));
|
||||
}
|
||||
|
||||
// Set variables
|
||||
$token = $post_data['token'];
|
||||
$access_token = $post_data['access_token'];
|
||||
$id = $post_data['id'];
|
||||
|
||||
if (flextype('registry')->get('flextype.settings.api.entries.enabled')) {
|
||||
// Validate entries and access token
|
||||
if (validateEntriesToken($token) && validate_access_token($access_token)) {
|
||||
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
|
||||
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
|
||||
|
||||
// Set entries and access token file
|
||||
if (
|
||||
($entries_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($entries_token_file_path)->get())) &&
|
||||
($access_token_file_data = flextype('serializers')->yaml()->decode(filesystem()->file($access_token_file_path)->get()))
|
||||
) {
|
||||
if (
|
||||
$entries_token_file_data['state'] === 'disabled' ||
|
||||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
if (
|
||||
$access_token_file_data['state'] === 'disabled' ||
|
||||
($access_token_file_data['limit_calls'] !== 0 && $access_token_file_data['calls'] >= $access_token_file_data['limit_calls'])
|
||||
) {
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
// Delete entry
|
||||
$delete_entry = flextype('entries')->delete($id);
|
||||
|
||||
// Set response code
|
||||
$response_code = $delete_entry ? 204 : 404;
|
||||
|
||||
// Update calls counter
|
||||
filesystem()->file($entries_token_file_path)->put(flextype('serializers')->yaml()->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1])));
|
||||
|
||||
if ($response_code === 404) {
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($api_errors['0102']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0102']));
|
||||
}
|
||||
|
||||
// Return response
|
||||
return $response
|
||||
->withStatus($response_code)
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($delete_entry));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
}
|
||||
|
||||
return $response
|
||||
->withStatus($api_errors['0003']['http_status_code'])
|
||||
->withHeader('Content-Type', 'application/json;charset=' . flextype('registry')->get('flextype.settings.charset'))
|
||||
->write(flextype('serializers')->json()->encode($api_errors['0003']));
|
||||
});
|
@@ -55,60 +55,62 @@ errors:
|
||||
|
||||
# Entries
|
||||
entries:
|
||||
content:
|
||||
collection: content
|
||||
fields:
|
||||
media:
|
||||
files:
|
||||
fetch:
|
||||
enabled: true
|
||||
result: toObject
|
||||
folders:
|
||||
fetch:
|
||||
enabled: true
|
||||
result: toObject
|
||||
registry:
|
||||
path: "/src/flextype/Foundation/Content/Fields/IdField.php"
|
||||
get:
|
||||
enabled: true
|
||||
content:
|
||||
path: "/src/flextype/Foundation/Content/Fields/ContentField.php"
|
||||
directory: entries
|
||||
filename: entry
|
||||
extension: yaml
|
||||
serializer: yaml
|
||||
fields:
|
||||
media:
|
||||
files:
|
||||
fetch:
|
||||
enabled: true
|
||||
result: toObject
|
||||
slug:
|
||||
path: "/src/flextype/Foundation/Content/Fields/SlugField.php"
|
||||
folders:
|
||||
fetch:
|
||||
enabled: true
|
||||
result: toObject
|
||||
registry:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/IdField.php"
|
||||
get:
|
||||
enabled: true
|
||||
published_at:
|
||||
path: "/src/flextype/Foundation/Content/Fields/PublsishedAtField.php"
|
||||
enabled: true
|
||||
published_by:
|
||||
path: "/src/flextype/Foundation/Content/Fields/PublishedByField.php"
|
||||
enabled: true
|
||||
modified_at:
|
||||
path: "/src/flextype/Foundation/Content/Fields/ModifiedAtField.php"
|
||||
enabled: true
|
||||
created_at:
|
||||
path: "/src/flextype/Foundation/Content/Fields/CreatedAtField.php"
|
||||
enabled: true
|
||||
created_by:
|
||||
path: "/src/flextype/Foundation/Content/Fields/CreatedByField.php"
|
||||
enabled: true
|
||||
routable:
|
||||
path: "/src/flextype/Foundation/Content/Fields/RoutableField.php"
|
||||
enabled: true
|
||||
parsers:
|
||||
path: "/src/flextype/Foundation/Content/Fields/ParsesField.php"
|
||||
enabled: true
|
||||
visibility:
|
||||
path: "/src/flextype/Foundation/Content/Fields/VisibilityField.php"
|
||||
enabled: true
|
||||
uuid:
|
||||
path: "/src/flextype/Foundation/Content/Fields/UuidField.php"
|
||||
enabled: true
|
||||
id:
|
||||
path: "/src/flextype/Foundation/Content/Fields/IdField.php"
|
||||
entries:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/EntriesField.php"
|
||||
fetch:
|
||||
enabled: true
|
||||
result: toObject
|
||||
slug:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/SlugField.php"
|
||||
enabled: true
|
||||
published_at:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/PublsishedAtField.php"
|
||||
enabled: true
|
||||
published_by:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/PublishedByField.php"
|
||||
enabled: true
|
||||
modified_at:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php"
|
||||
enabled: true
|
||||
created_at:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/CreatedAtField.php"
|
||||
enabled: true
|
||||
created_by:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/CreatedByField.php"
|
||||
enabled: true
|
||||
routable:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/RoutableField.php"
|
||||
enabled: true
|
||||
parsers:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/ParsesField.php"
|
||||
enabled: true
|
||||
visibility:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/VisibilityField.php"
|
||||
enabled: true
|
||||
uuid:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/UuidField.php"
|
||||
enabled: true
|
||||
id:
|
||||
path: "/src/flextype/Foundation/Entries/Fields/IdField.php"
|
||||
enabled: true
|
||||
|
||||
# Cache
|
||||
#
|
||||
|
Reference in New Issue
Block a user