From c0695eca2f95357f1b77d022c56d9bf8e395ed62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Tue, 28 Feb 2023 01:58:35 +0100 Subject: [PATCH] fix(Api): remove index, allow sending Cookies --- SeacmsApi.php | 45 ++++++++++++++++++++++++++++++-------------- index.php | 24 ----------------------- src/ApiAware.php | 2 +- src/JsonResponse.php | 14 +++++++++++++- 4 files changed, 45 insertions(+), 40 deletions(-) delete mode 100644 index.php diff --git a/SeacmsApi.php b/SeacmsApi.php index 37ca0ac..7c39641 100644 --- a/SeacmsApi.php +++ b/SeacmsApi.php @@ -2,6 +2,7 @@ // SPDX-License-Identifier: EUPL-1.2 // Authors: see README.md +use Pico; use SeaCMS\Api\ApiAware; use SeaCMS\Api\BadMethodException; use SeaCMS\Api\JsonResponse; @@ -19,16 +20,16 @@ class SeacmsApi extends AbstractPicoPlugin implements ApiAware const API_VERSION = 3; /** - * routes trigerred OnPageRendered + * api routes * @var array */ - protected $routesOnPageRendered ; + protected $routes ; /** * return api routes * @return array */ - public function registerOnPageRenderedApiRoutes():array + public function registerApiRoutes():array { return [ 'POST test' => 'api', @@ -66,14 +67,14 @@ class SeacmsApi extends AbstractPicoPlugin implements ApiAware */ public function onPluginsLoaded(array $plugins) { - $this->routesOnPageRendered = []; + $this->routes = []; foreach($plugins as $plugin){ if ($plugin instanceof ApiAware){ - $routes = $plugin->registerOnPageRenderedApiRoutes(); + $routes = $plugin->registerApiRoutes(); if (is_array($routes)){ foreach($routes as $route => $methodName){ if (is_string($methodName) && method_exists($plugin,$methodName)){ - $this->routesOnPageRendered[$route] = [$plugin,$methodName]; + $this->routes[$route] = [$plugin,$methodName]; } } } @@ -91,6 +92,17 @@ class SeacmsApi extends AbstractPicoPlugin implements ApiAware */ public function onPageRendered(&$output) { + $this->resolveApi($output); + } + + /** + * resolve api + * @param string $$output + * @return bool $outputChanged + */ + protected function resolveApi(string &$output): bool + { + $outputChanged = false; if (isset($_GET['api'])){ $route = $this->getPico()->getUrlParameter( 'api', @@ -104,10 +116,13 @@ class SeacmsApi extends AbstractPicoPlugin implements ApiAware ] ); $route = trim($route); + $callable = function() { + $this->getPico()->triggerEvent('sendCookies'); + }; if (empty($route)){ - $output = (new JsonResponse(404,['code'=>404,'reason'=>'Empty api route']))->send(); + $output = (new JsonResponse(404,['code'=>404,'reason'=>'Empty api route'],[],$callable))->send(); } elseif (!preg_match('/^[A-Za-z0-9_\-.\/]+$/',$route)) { - $output = (new JsonResponse(404,['code'=>404,'reason'=>"Route '$route' use forbidden characters !"]))->send(); + $output = (new JsonResponse(404,['code'=>404,'reason'=>"Route '$route' use forbidden characters !"],[],$callable))->send(); } else { ob_start(); $response = null; @@ -135,13 +150,15 @@ class SeacmsApi extends AbstractPicoPlugin implements ApiAware $content['rawOutput'] = $rawOutput; } $content = array_merge(['code'=>$code],$content); - $response = (new JsonResponse($code,$content)); + $response = (new JsonResponse($code,$content,[],$callable)); } elseif (!empty($rawOutput)) { $response->mergeInContent(compact(['rawOutput'])); } $output = $response->send(); } + $outputChanged = true; } + return $outputChanged; } /** @@ -178,10 +195,10 @@ class SeacmsApi extends AbstractPicoPlugin implements ApiAware } $searchingRoute = implode('/',$splittedRouteFiltered); $data = []; - if (array_key_exists("$method $searchingRoute",$this->routesOnPageRendered)){ - $data = $this->routesOnPageRendered["$method $searchingRoute"]; - } elseif (array_key_exists("$searchingRoute",$this->routesOnPageRendered)){ - $data = $this->routesOnPageRendered["$searchingRoute"]; + if (array_key_exists("$method $searchingRoute",$this->routes)){ + $data = $this->routes["$method $searchingRoute"]; + } elseif (array_key_exists("$searchingRoute",$this->routes)){ + $data = $this->routes["$searchingRoute"]; } if (!empty($data)){ return [ @@ -189,7 +206,7 @@ class SeacmsApi extends AbstractPicoPlugin implements ApiAware 'methodName' => $data[1], 'params' => $params ]; - } elseif (!$badMethod && array_key_exists((($method == 'GET') ? 'POST' : 'GET' )." $searchingRoute",$this->routesOnPageRendered)){ + } elseif (!$badMethod && array_key_exists((($method == 'GET') ? 'POST' : 'GET' )." $searchingRoute",$this->routes)){ $badMethod = true; } } diff --git a/index.php b/index.php deleted file mode 100644 index 988fb65..0000000 --- a/index.php +++ /dev/null @@ -1,24 +0,0 @@ -500,'reason'=>'bad url to run this file']))->send(); -} else { - chdir(dirname($_SERVER['SCRIPT_FILENAME'],5)); - $GLOBALS['PicoVendorsDirectoryRelativeLevels'] = 4; - include 'index.php'; -} \ No newline at end of file diff --git a/src/ApiAware.php b/src/ApiAware.php index 3662032..1148711 100644 --- a/src/ApiAware.php +++ b/src/ApiAware.php @@ -13,5 +13,5 @@ interface ApiAware * return api routes * @return array */ - public function registerOnPageRenderedApiRoutes():array; + public function registerApiRoutes():array; } diff --git a/src/JsonResponse.php b/src/JsonResponse.php index a345ef1..59fe02d 100644 --- a/src/JsonResponse.php +++ b/src/JsonResponse.php @@ -36,6 +36,12 @@ class JsonResponse implements JsonSerializable 503 => 'Service Unavailable', ]; + + /** + * callable to send cookies + * @var callable + */ + protected $callableToSendCookies; /** * HTTP CODE * @var int @@ -53,7 +59,7 @@ class JsonResponse implements JsonSerializable protected $headers; - public function __construct(int $code, array $content, array $headers = []){ + public function __construct(int $code, array $content, array $headers = [],$callableToSendCookies = null){ $this->code = array_key_exists($code, self::HTTP_CODES) ? $code : 501; // default $this->content = $content; $this->headers = array_merge([ @@ -65,6 +71,7 @@ class JsonResponse implements JsonSerializable 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, DELETE, PUT, PATCH', 'Access-Control-Max-Age' => '86400' ], $headers); + $this->callableToSendCookies = is_callable($callableToSendCookies) ? callableToSendCookies : null; } /** @@ -134,6 +141,11 @@ class JsonResponse implements JsonSerializable header($name.': '.$value); } + // cookies + if (!empty($this->callableToSendCookies)){ + call_user_func($this->callableToSendCookies); + } + // status $statusText = self::HTTP_CODES[$this->code]; $protocol = !empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';