|
|
|
@ -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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|