From a2e10130b5b590204eb761117fd2adeee7530386 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 10 Aug 2021 12:46:01 +0300 Subject: [PATCH] feat(endpoints): remove old codebase for endpoints #565 --- src/flextype/core/Endpoints/entries.php | 672 ---------- src/flextype/core/Endpoints/images.php | 72 -- src/flextype/core/Endpoints/media.php | 1418 ---------------------- src/flextype/core/Endpoints/registry.php | 114 -- 4 files changed, 2276 deletions(-) delete mode 100644 src/flextype/core/Endpoints/entries.php delete mode 100644 src/flextype/core/Endpoints/images.php delete mode 100644 src/flextype/core/Endpoints/media.php delete mode 100644 src/flextype/core/Endpoints/registry.php diff --git a/src/flextype/core/Endpoints/entries.php b/src/flextype/core/Endpoints/entries.php deleted file mode 100644 index 4ea754ee..00000000 --- a/src/flextype/core/Endpoints/entries.php +++ /dev/null @@ -1,672 +0,0 @@ -file(PATH['project'] . '/tokens/content/' . $token . '/token.yaml')->exists(); -} - -/** - * Fetch entry(content) - * - * endpoint: GET /api/content - * - * Query: - * id - [REQUIRED] - Unique identifier of the entry(content). - * token - [REQUIRED] - Valid Entries token. - * filter - [OPTIONAL] - Select items in collection by given conditions. - * - * Returns: - * An array of entry item objects. - */ -app()->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=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0100'])); - } - - // Set variables - $id = $query['id']; - $token = $query['token']; - $options = $query['options'] ?? []; - $method = $query['options']['method'] ?? ''; - - if (registry()->get('flextype.settings.api.content.enabled')) { - // Validate content token - if (validateEntriesToken($token)) { - $content_token_file_path = PATH['project'] . '/tokens/content/' . $token . '/token.yaml'; - - // Set content token file - if ($content_token_file_data = serializers()->yaml()->decode(filesystem()->file($content_token_file_path)->get())) { - if ( - $content_token_file_data['state'] === 'disabled' || - ($content_token_file_data['limit_calls'] !== 0 && $content_token_file_data['calls'] >= $content_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // override content.fetch.result - registry()->set('flextype.settings.entries.content.fields.content.fetch.result', 'toArray'); - - if (isset($method) && - strpos($method, 'fetch') !== false && - is_callable([content(), $method])) { - $fetchFromCallbackMethod = $method; - } else { - $fetchFromCallbackMethod = 'fetch'; - } - - // Get fetch result - $response_data['data'] = content()->{$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($content_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($content_token_file_data, ['calls' => $content_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0102']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0102'])); - } - - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Create entry - * - * endpoint: POST /api/content - * - * 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/content', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0101']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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 (registry()->get('flextype.settings.api.content.enabled')) { - // Validate content and access token - if (validateEntriesToken($token) && validate_access_token($access_token)) { - $content_token_file_path = PATH['project'] . '/tokens/content/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set content and access token file - if ( - ($content_token_file_data = serializers()->yaml()->decode(filesystem()->file($content_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $content_token_file_data['state'] === 'disabled' || - ($content_token_file_data['limit_calls'] !== 0 && $content_token_file_data['calls'] >= $content_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Create entry - $create_entry = content()->create($id, $data); - - if ($create_entry) { - $response_data['data'] = content()->fetch($id); - } else { - $response_data['data'] = []; - } - - // Set response code - $response_code = $create_entry ? 200 : 404; - - // Update calls counter - filesystem()->file($content_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($content_token_file_data, ['calls' => $content_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0102']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0102'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Update entry - * - * endpoint: PATCH /api/content - * - * 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/content', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0101']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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 (registry()->get('flextype.settings.api.content.enabled')) { - // Validate content and access token - if (validateEntriesToken($token) && validate_access_token($access_token)) { - $content_token_file_path = PATH['project'] . '/tokens/content/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set content and access token file - if ( - ($content_token_file_data = serializers()->yaml()->decode(filesystem()->file($content_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $content_token_file_data['state'] === 'disabled' || - ($content_token_file_data['limit_calls'] !== 0 && $content_token_file_data['calls'] >= $content_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Update entry - $update_entry = content()->update($id, $data); - - if ($update_entry) { - $response_data['data'] = content()->fetch($id); - } else { - $response_data['data'] = []; - } - - // Set response code - $response_code = $update_entry ? 200 : 404; - - // Update calls counter - filesystem()->file($content_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($content_token_file_data, ['calls' => $content_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0102']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0102'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Move entry - * - * endpoint: PUT /api/content - * - * 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/content', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0101']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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 (registry()->get('flextype.settings.api.content.enabled')) { - // Validate content and access token - if (validateEntriesToken($token) && validate_access_token($access_token)) { - $content_token_file_path = PATH['project'] . '/tokens/content/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set content and access token file - if ( - ($content_token_file_data = serializers()->yaml()->decode(filesystem()->file($content_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $content_token_file_data['state'] === 'disabled' || - ($content_token_file_data['limit_calls'] !== 0 && $content_token_file_data['calls'] >= $content_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Move entry - $move_entry = content()->move($id, $new_id); - - // Get entry data - if ($move_entry) { - $response_data['data'] = content()->fetch($new_id); - } else { - $response_data['data'] = []; - } - - // Set response code - $response_code = $move_entry ? 200 : 404; - - // Update calls counter - filesystem()->file($content_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($content_token_file_data, ['calls' => $content_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0102']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0102'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Copy entry(content) - * - * endpoint: PUT /api/content/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/content/copy', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0101']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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 (registry()->get('flextype.settings.api.content.enabled')) { - // Validate content and access token - if (validateEntriesToken($token) && validate_access_token($access_token)) { - $content_token_file_path = PATH['project'] . '/tokens/content/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set content and access token file - if ( - ($content_token_file_data = serializers()->yaml()->decode(filesystem()->file($content_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $content_token_file_data['state'] === 'disabled' || - ($content_token_file_data['limit_calls'] !== 0 && $content_token_file_data['calls'] >= $content_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Copy entry - $copy_entry = content()->copy($id, $new_id, true); - - // Get entry data - if ($copy_entry === null) { - $response_data['data'] = content()->fetch($new_id); - } else { - $response_data['data'] = []; - } - - // Set response code - $response_code = $copy_entry === null ? 200 : 404; - - // Update calls counter - filesystem()->file($content_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($content_token_file_data, ['calls' => $content_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0102']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0102'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Delete entry - * - * endpoint: DELETE /api/content - * - * 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/content', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0101']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0101']['http_status_code'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - - if (registry()->get('flextype.settings.api.content.enabled')) { - // Validate content and access token - if (validateEntriesToken($token) && validate_access_token($access_token)) { - $content_token_file_path = PATH['project'] . '/tokens/content/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set content and access token file - if ( - ($content_token_file_data = serializers()->yaml()->decode(filesystem()->file($content_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $content_token_file_data['state'] === 'disabled' || - ($content_token_file_data['limit_calls'] !== 0 && $content_token_file_data['calls'] >= $content_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Delete entry - $delete_entry = content()->delete($id); - - // Set response code - $response_code = $delete_entry ? 204 : 404; - - // Update calls counter - filesystem()->file($content_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($content_token_file_data, ['calls' => $content_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0102']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0102'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($delete_entry)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); \ No newline at end of file diff --git a/src/flextype/core/Endpoints/images.php b/src/flextype/core/Endpoints/images.php deleted file mode 100644 index 50661d4c..00000000 --- a/src/flextype/core/Endpoints/images.php +++ /dev/null @@ -1,72 +0,0 @@ -get('/api/images/{path:.+}', function ($path, Request $request, Response $response) { - - // Get Query Params - $queryParams = $request->getQueryParams(); - - // Check is images api enabled - if (! registry()->get('flextype.settings.api.images.enabled')) { - return getApiResponseWithError($response, 400); - } - - // Check is token param exists - if (! isset($queryParams['token'])) { - return getApiResponseWithError($response, 400); - } - - // Check is token exists - if (! tokens()->has($queryParams['token'])) { - return getApiResponseWithError($response, 401); - } - - // Fetch token - $tokenData = tokens()->fetch($queryParams['token']); - - // Check token state and limit_calls - if ($tokenData['state'] === 'disabled' || - ($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])) { - return getApiResponseWithError($response, 400); - } - - // Update token calls - tokens()->update($queryParams['token'], ['calls' => $tokenData['calls'] + 1]); - - // Check is file exists - if (! filesystem()->file(PATH['project'] . '/uploads/' . $path)->exists()) { - return getApiResponseWithError($response, 404); - } - - // Return image response - return container()->get('images')->getImageResponse($path, $queryParams); -}); diff --git a/src/flextype/core/Endpoints/media.php b/src/flextype/core/Endpoints/media.php deleted file mode 100644 index c8c6bcce..00000000 --- a/src/flextype/core/Endpoints/media.php +++ /dev/null @@ -1,1418 +0,0 @@ -file(PATH['project'] . '/tokens/media/' . $token . '/token.yaml')->exists(); -} - -/** - * Fetch file(s) - * - * endpoint: GET /api/media/files - * - * Query: - * path - [REQUIRED] - Unique identifier of the file path. - * token - [REQUIRED] - Valid Files token. - * - * Returns: - * An array of file item objects. - */ -app()->get('/api/media/files', function (Request $request, Response $response) use ($apiErrors) { - // Get Query Params - $query = $request->getQueryParams(); - - if (! isset($query['id']) || ! isset($query['token'])) { - return $response->withStatus($apiErrors['0500']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0500'])); - } - - // Set variables - $id = $query['id']; - $token = $query['token']; - $options = $query['options'] ?? []; - $method = $query['options']['method'] ?? ''; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate delivery token - if (validate_media_token($token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - - // Set delivery token file - if ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Create files array - $files = []; - - if (isset($method) && - strpos($method, 'fetch') !== false && - is_callable([flextype('media')->files(), $method])) { - $fetchFromCallbackMethod = $method; - } else { - $fetchFromCallbackMethod = 'fetch'; - } - - // Get fetch result - $files = flextype('media')->files()->{$fetchFromCallbackMethod}($id, $options); - $files = ($files instanceof Arrays) ? $files->toArray() : $files; - - // Write response data - $response_data = []; - $response_data['data'] = $files; - - // Set response code - $response_code = count($response_data['data']) > 0 ? 200 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Upload media file - * - * endpoint: POST /api/media/files - * - * Body: - * folder - [REQUIRED] - The folder you're targetting. - * token - [REQUIRED] - Valid Files token. - * access_token - [REQUIRED] - Valid Access token. - * file - [REQUIRED] - Raw file data (multipart/form-data). - * - * Returns: - * Returns the file object for the file that was just uploaded. - */ -flextype()->post('/api/media/files', function (Request $request, Response $response) use ($apiErrors) { - // Get Post Data - $post_data = (array) $request->getParsedBody(); - - if (! isset($post_data['token']) || ! isset($post_data['access_token']) || ! isset($post_data['folder']) || ! isset($_FILES['file'])) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $folder = $post_data['folder']; - $file = $_FILES['file']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Create file - $create_file = flextype('media')->files()->upload($file, $folder); - - $response_data['data'] = []; - - if ($create_file) { - $response_data['data'] = flextype('media')->files()->fetch($folder . '/' . basename($create_file)); - } - - // Set response code - $response_code = filesystem()->file($create_file)->exists() ? 200 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - - -/** - * Rename media file - * - * endpoint: PUT /api/media/files - * - * Body: - * id - [REQUIRED] - Unique identifier of the file. - * new_id - [REQUIRED] - New Unique identifier of the file. - * token - [REQUIRED] - Valid Entries token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the file object for the file that was just created. - */ -flextype()->put('/api/media/files', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - $new_id = $post_data['new_id']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Rename file - $rename_file = flextype('media')->files()->move($id, $new_id); - - $response_data['data'] = []; - - if ($rename_file) { - $response_data['data'] = flextype('media')->files()->fetch($new_id); - } - - // Set response code - $response_code = $rename_file === true ? 200 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Copy media file - * - * endpoint: PUT /api/media/files - * - * Body: - * id - [REQUIRED] - Unique identifier of the file. - * new_id - [REQUIRED] - New Unique identifier of the file. - * token - [REQUIRED] - Valid Entries token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the file object for the file that was just created. - */ -flextype()->put('/api/media/files/copy', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - $new_id = $post_data['new_id']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Copy file - $copy_file = flextype('media')->files()->copy($id, $new_id); - - $response_data['data'] = []; - - if ($copy_file) { - $response_data['data'] = flextype('media')->files()->fetch($new_id); - } - - // Set response code - $response_code = $copy_file === true ? 200 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Delete file - * - * endpoint: DELETE /api/media/files - * - * Body: - * id - [REQUIRED] - Unique identifier of the file. - * token - [REQUIRED] - Valid Entries token. - * access_token - [REQUIRED] - Valid Authentication token. - * - * Returns: - * Returns an empty body with HTTP status 204 - */ -flextype()->delete('/api/media/files', function (Request $request, Response $response) use ($apiErrors) { - // Get Post Data - $post_data = (array) $request->getParsedBody(); - - if (! isset($post_data['id']) || ! isset($post_data['access_token']) || ! isset($post_data['id'])) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Delete file - $delete_file = flextype('media')->files()->delete($id); - - // Set response code - $response_code = $delete_file ? 204 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($delete_file)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Update file meta information - * - * endpoint: PATCH /api/media/files/meta - * - * Body: - * path - [REQUIRED] - File path. - * field - [REQUIRED] - Field name. - * value - [REQUIRED] - Field value. - * token - [REQUIRED] - Valid Entries token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the file object for the file that was just updated. - */ -flextype()->patch('/api/media/files/meta', function (Request $request, Response $response) use ($apiErrors) { - // 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['field']) || ! isset($post_data['value'])) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - $field = $post_data['field']; - $value = $post_data['value']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Update file meta - $update_file_meta = flextype('media')->files()->meta()->update($id, $field, $value); - - $response_data['data'] = []; - - if ($update_file_meta) { - $response_data['data'] = flextype('media')->files()->fetch($id); - } - - // Set response code - $response_code = $update_file_meta ? 200 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Add file meta information - * - * endpoint: POST /api/media/files/meta - * - * Body: - * path - [REQUIRED] - File path. - * field - [REQUIRED] - Field name. - * value - [REQUIRED] - Field value. - * token - [REQUIRED] - Valid Entries token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the file object for the file that was just created. - */ -flextype()->post('/api/media/files/meta', function (Request $request, Response $response) use ($apiErrors) { - // 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['field']) || ! isset($post_data['value'])) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - $field = $post_data['field']; - $value = $post_data['value']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Add file meta - $add_file_meta = flextype('media')->files()->meta()->add($id, $field, $value); - - $response_data['data'] = []; - - if ($add_file_meta) { - $response_data['data'] = flextype('media')->files()->fetch($id); - } - - // Set response code - $response_code = $add_file_meta ? 200 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - - -/** - * Delete file meta information - * - * endpoint: DELETE /api/media/files/meta - * - * Body: - * path - [REQUIRED] - File path. - * field - [REQUIRED] - Field name. - * token - [REQUIRED] - Valid Entries token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the file object for the file that was just created. - */ -flextype()->delete('/api/media/files/meta', function (Request $request, Response $response) use ($apiErrors) { - // 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['field'])) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - $field = $post_data['field']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $files_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($files_token_file_data = serializers()->yaml()->decode(filesystem()->file($files_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $files_token_file_data['state'] === 'disabled' || - ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls']) - ) { - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['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($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Delete file meta - $delete_file_meta = flextype('media')->files()->meta()->delete($id, $field); - - $response_data['data'] = []; - - if ($delete_file_meta) { - $response_data['data'] = flextype('media')->files()->fetch($id); - } - - // Set response code - $response_code = $delete_file_meta ? 200 : 404; - - // Update calls counter - filesystem()->file($files_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0502']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0502'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - - -/** - * Fetch folders(s) - * - * endpoint: GET /api/media/folders - * - * Query: - * path - [REQUIRED] - Folder path. - * collection - [OPTIONAL] - Collection or single. - * token - [REQUIRED] - Valid Folders token. - * - * Returns: - * An array of folder(s) item objects. - */ -app()->get('/api/media/folders', function (Request $request, Response $response) use ($apiErrors) { - // Get Query Params - $query = $request->getQueryParams(); - - if (! isset($query['id']) || ! isset($query['token'])) { - return $response->withStatus($apiErrors['0600']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0600'])); - } - - // Set variables - $id = $query['id']; - $token = $query['token']; - $options = $query['options'] ?? []; - $method = $query['method'] ?? ''; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate delivery token - if (validate_media_token($token)) { - $folders_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - - // Set delivery token file - if ($folders_token_file_data = serializers()->yaml()->decode(filesystem()->file($folders_token_file_path)->get())) { - if ( - $folders_token_file_data['state'] === 'disabled' || - ($folders_token_file_data['limit_calls'] !== 0 && $folders_token_file_data['calls'] >= $folders_token_file_data['limit_calls']) - ) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Create folders array - $folders = []; - - if (isset($method) && - strpos($method, 'fetch') !== false && - is_callable([flextype('media')->folders(), $method])) { - $fetchFromCallbackMethod = $method; - } else { - $fetchFromCallbackMethod = 'fetch'; - } - - // Get fetch result - $folders = flextype('media')->folders()->{$fetchFromCallbackMethod}($id, $options); - $folders = ($folders instanceof Arrays) ? $folders->toArray() : $folders; - - // Write response data - $response_data = []; - $response_data['data'] = $folders; - - // Set response code - $response_code = count($response_data['data']) > 0 ? 200 : 404; - - // Update calls counter - filesystem()->file($folders_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($folders_token_file_data, ['calls' => $folders_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0602']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0602'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - - -/** - * Create folder - * - * endpoint: PUT /api/media/folders - * - * Body: - * path - [REQUIRED] - New folder path. - * token - [REQUIRED] - Valid Folders token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the folder object for the folder that was just created. - */ -flextype()->post('/api/media/folders', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0601']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0601'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $folders_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($folders_token_file_data = serializers()->yaml()->decode(filesystem()->file($folders_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $folders_token_file_data['state'] === 'disabled' || - ($folders_token_file_data['limit_calls'] !== 0 && $folders_token_file_data['calls'] >= $folders_token_file_data['limit_calls']) - ) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - 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($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Create folder - $create_folder = flextype('media')->folders()->create($id); - - $response_data = []; - - if ($create_folder) { - $response_data['data'] = flextype('media')->folders()->fetch($id); - } - - // Set response code - $response_code = $create_folder ? 200 : 404; - - // Update calls counter - filesystem()->file($folders_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($folders_token_file_data, ['calls' => $folders_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0602']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0602'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Copy folder - * - * endpoint: PUT /api/media/folders/copy - * - * Body: - * path - [REQUIRED] - Unique identifier of the folder. - * new_path - [REQUIRED] - New Unique identifier of the folder. - * token - [REQUIRED] - Valid Folder token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the folders object for the folders that was just copied. - */ -flextype()->put('/api/media/folders/copy', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0601']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0601'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - $new_id = $post_data['new_id']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $folders_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($folders_token_file_data = serializers()->yaml()->decode(filesystem()->file($folders_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $folders_token_file_data['state'] === 'disabled' || - ($folders_token_file_data['limit_calls'] !== 0 && $folders_token_file_data['calls'] >= $folders_token_file_data['limit_calls']) - ) { - return $response->withStatus($apiErrors['0601']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0601'])); - } - - 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($apiErrors['0601']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0601'])); - } - - // Copy folder - $copy_folder = flextype('media')->folders()->copy($id, $new_id); - - $response_data = []; - - if ($copy_folder) { - $response_data['data'] = flextype('media')->folders()->fetch($new_id); - } else { - $response_data['data'] = $copy_folder; - } - - // Set response code - $response_code = $copy_folder ? 200 : 404; - - // Update calls counter - filesystem()->file($folders_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($folders_token_file_data, ['calls' => $folders_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0602']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0602'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** - * Move folder - * - * endpoint: PUT /api/media/folders - * - * Body: - * path - [REQUIRED] - Unique identifier of the folder. - * new_pah - [REQUIRED] - New Unique identifier of the folder. - * token - [REQUIRED] - Valid Folder token. - * access_token - [REQUIRED] - Valid Access token. - * - * Returns: - * Returns the folders object for the folders that was just renamed. - */ -flextype()->put('/api/media/folders', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0601']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0601'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - $new_id = $post_data['new_id']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $folders_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($folders_token_file_data = serializers()->yaml()->decode(filesystem()->file($folders_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $folders_token_file_data['state'] === 'disabled' || - ($folders_token_file_data['limit_calls'] !== 0 && $folders_token_file_data['calls'] >= $folders_token_file_data['limit_calls']) - ) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - 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($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Rename folder - $move_folder = flextype('media')->folders()->move($id, $new_id); - - $response_data = []; - - if ($move_folder) { - $response_data['data'] = flextype('media')->folders()->fetch($new_id); - } - - // Set response code - $response_code = $move_folder ? 200 : 404; - - // Update calls counter - filesystem()->file($folders_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($folders_token_file_data, ['calls' => $folders_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0602']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0602'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($response_data)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); - -/** -* Delete folder -* -* endpoint: DELETE /api/media/folders -* -* Body: -* id - [REQUIRED] - Unique identifier of the folder. -* token - [REQUIRED] - Valid Folders token. -* access_token - [REQUIRED] - Valid Authentication token. -* -* Returns: -* Returns an empty body with HTTP status 204 -*/ -flextype()->delete('/api/media/folders', function (Request $request, Response $response) use ($apiErrors) { - // 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($apiErrors['0601']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0601'])); - } - - // Set variables - $token = $post_data['token']; - $access_token = $post_data['access_token']; - $id = $post_data['id']; - - if (registry()->get('flextype.settings.api.media.enabled')) { - // Validate files and access token - if (validate_media_token($token) && validateAccessToken($access_token)) { - $folders_token_file_path = PATH['project'] . '/tokens/media/' . $token . '/token.yaml'; - $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; - - // Set files and access token file - if ( - ($folders_token_file_data = serializers()->yaml()->decode(filesystem()->file($folders_token_file_path)->get())) && - ($access_token_file_data = serializers()->yaml()->decode(filesystem()->file($access_token_file_path)->get())) - ) { - if ( - $folders_token_file_data['state'] === 'disabled' || - ($folders_token_file_data['limit_calls'] !== 0 && $folders_token_file_data['calls'] >= $folders_token_file_data['limit_calls']) - ) { - return $response->withStatus($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - 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($apiErrors['0501']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0501'])); - } - - // Delete folder - $delete_folder = flextype('media')->folders()->delete($id); - - // Set response code - $response_code = $delete_folder ? 204 : 404; - - // Update calls counter - filesystem()->file($folders_token_file_path)->put(serializers()->yaml()->encode(array_replace_recursive($folders_token_file_data, ['calls' => $folders_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0602']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0602'])); - } - - // Return response - return $response - ->withStatus($response_code) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($delete_folder)); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -}); diff --git a/src/flextype/core/Endpoints/registry.php b/src/flextype/core/Endpoints/registry.php deleted file mode 100644 index c4b1fe3d..00000000 --- a/src/flextype/core/Endpoints/registry.php +++ /dev/null @@ -1,114 +0,0 @@ -file(PATH['project'] . '/tokens/registry/' . $token . '/token.yaml')->exists(); -} - -/** - * Fetch registry item - * - * endpoint: GET /api/registry - * - * Query: - * id - [REQUIRED] - Unique identifier of the registry item. - * token - [REQUIRED] - Valid Registry token. - * - * Returns: - * An array of registry item objects. - */ -app()->get('/api/registry', function (Request $request, Response $response) use ($apiErrors) { - // Get Query Params - $query = $request->getQueryParams(); - - if (! isset($query['id']) || ! isset($query['token'])) { - return $response->withStatus($apiErrors['0300']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0300'])); - } - - // Set variables - $id = $query['id']; - $token = $query['token']; - - if (registry()->get('flextype.settings.api.registry.enabled')) { - // Validate token - if (validate_registry_token($token)) { - $registry_token_file_path = PATH['project'] . '/tokens/registry/' . $token . '/token.yaml'; - - // Set token file - if ($registry_token_file_data = serializers()->yaml()->decode(filesystem()->file($registry_token_file_path)->get())) { - if ( - $registry_token_file_data['state'] === 'disabled' || - ($registry_token_file_data['limit_calls'] !== 0 && $registry_token_file_data['calls'] >= $registry_token_file_data['limit_calls']) - ) { - return $response->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - // Fetch registry - if (registry()->has($id)) { - $response_data['data']['key'] = $id; - $response_data['data']['value'] = registry()->get($id); - - // Set response code - $response_code = 200; - } else { - $response_data = []; - $response_code = 404; - } - - // Update calls counter - filesystem()->file($registry_token_file_path) - ->put(serializers()->yaml()->encode(array_replace_recursive($registry_token_file_data, ['calls' => $registry_token_file_data['calls'] + 1]))); - - if ($response_code === 404) { - // Return response - return $response - ->withStatus($apiErrors['0302']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0302'])); - } - - // Return response - return $response - ->withJson($response_data, $response_code); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); - } - - return $response - ->withStatus($apiErrors['0003']['http_status_code']) - ->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset')) - ->write(serializers()->json()->encode($apiErrors['0003'])); -});