16 import(
'lib.pkp.classes.handler.PKPHandler');
19 import(
'lib.pkp.classes.core.APIResponse');
20 import(
'classes.core.Services');
38 parent::__construct();
39 import(
'lib.pkp.classes.security.authorization.internal.ApiAuthorizationMiddleware');
40 import(
'lib.pkp.classes.security.authorization.internal.ApiTokenDecodingMiddleware');
41 import(
'lib.pkp.classes.security.authorization.internal.ApiCsrfMiddleware');
42 $this->_app = new \Slim\App(array(
44 'response' =>
function($c) {
49 'determineRouteBeforeAppMiddleware' =>
true,
56 $this->_app->add(
function ($request, $response, $next) {
57 $uri = $request->getUri();
58 $path = $uri->getPath();
59 if ($path !=
'/' && substr($path, -1) ==
'/') {
61 $uri = $uri->withPath(substr($path, 0, -1));
62 if($request->getMethod() ==
'GET') {
63 return $response->withRedirect((string)$uri, 301);
65 return $next($request->withUri($uri), $response);
68 return $next($request, $response);
73 $this->_app->add(
function($request, $response, $next) use($app, $handler) {
74 $uri = $request->getUri();
75 $endpoint = trim($request->getQueryParam(
'endpoint'));
76 $pathInfoEnabled =
Config::getVar(
'general',
'disable_path_info') ? false :
true;
77 $path = $uri->getPath();
78 if (!$pathInfoEnabled && !is_null($endpoint) && !isset($_SERVER[
'PATH_INFO']) && ($path ==
'/')) {
79 $basePath = $uri->getBasePath();
80 if($request->getMethod() ==
'GET') {
81 $uri = $uri->withPath($basePath . $endpoint);
82 return $response->withRedirect((
string)$uri, 301);
113 $uri = $uri->withPath($basePath . $endpoint);
114 $handler->_slimRequest = $request->withUri($uri);
115 return $app->process($handler->_slimRequest, $response);
117 } elseif ($pathInfoEnabled) {
121 $newUri = $uri->withPath($_SERVER[
'PATH_INFO']);
122 if ($uri != $newUri) {
123 $handler->_slimRequest = $request->withUri($newUri);
124 return $app->process($handler->_slimRequest, $response);
127 return $next($request, $response);
130 $this->_app->add(
function ($request, $response, $next) {
131 $response = $response->withHeader(
'Access-Control-Allow-Origin',
'*');
132 return $next($request, $response);
161 return $this->_slimRequest = $slimRequest;
168 public function getApp() {
182 if (!isset($this->_pathPattern)) {
206 foreach ($endpoints as $method => $definitions) {
207 foreach ($definitions as $parameters) {
208 $method = strtolower($method);
209 $pattern = $parameters[
'pattern'];
210 $handler = $parameters[
'handler'];
211 $roles = isset($parameters[
'roles']) ? $parameters[
'roles'] :
null;
212 $app->$method($pattern, $handler)->setName($handler[1]);
213 if (!is_null($roles) && is_array($roles)) {
237 public function getParameter($parameterName, $default =
null) {
239 if ($slimRequest ==
null) {
243 $route = $slimRequest->getAttribute(
'route');
246 if (!is_null($route)) {
247 $arguments = $route->getArguments();
248 if (isset($arguments[$parameterName])) {
249 return $arguments[$parameterName];
252 $queryParams = $slimRequest->getQueryParams();
253 if (isset($queryParams[$parameterName])) {
254 return $queryParams[$parameterName];
277 $schema = $schemaService->get($schema);
279 foreach ($params as $paramName => $paramValue) {
280 if (!property_exists($schema->properties, $paramName)) {
283 if (!empty($schema->properties->{$paramName}->multilingual)) {
284 foreach ($paramValue as $localeKey => $localeValue) {
285 $params[$paramName][$localeKey] = $this->_convertStringsToSchema(
287 $schema->properties->{$paramName}->type,
288 $schema->properties->{$paramName}
292 $params[$paramName] = $this->_convertStringsToSchema(
294 $schema->properties->{$paramName}->type,
295 $schema->properties->{$paramName}
313 private function _convertStringsToSchema($value, $type, $schema) {
315 if (is_string($value) && !strlen($value) && $type !==
'array') {
320 if (is_string($value)) {
321 if ($value ===
'true' || $value ===
'1') {
323 } elseif ($value ===
'false' || $value ===
'0') {
329 if (is_string($value) && ctype_digit($value)) {
334 if (is_string($value) && is_numeric($value)) {
335 return floatval($value);
339 if (is_array($value)) {
341 if (is_array($schema->items)) {
342 foreach ($schema->items as $i => $itemSchema) {
343 $newArray[$i] = $this->_convertStringsToSchema($value[$i], $itemSchema->type, $itemSchema);
346 foreach ($value as $i => $v) {
347 $newArray[$i] = $this->_convertStringsToSchema($v, $schema->items->type, $schema->items);
355 } elseif (is_string($value) && !strlen($value)) {
360 if (is_array($value)) {
362 foreach ($schema->properties as $propName => $propSchema) {
363 if (!isset($value[$propName])) {
366 $newObject[$propName] = $this->_convertStringsToSchema($value[$propName], $propSchema->type, $propSchema);
390 protected function _validateStatDates($params, $dateStartParam =
'dateStart', $dateEndParam =
'dateEnd') {
391 import(
'lib.pkp.classes.validation.ValidatorFactory');
397 'after_or_equal:' . STATISTICS_EARLIEST_DATE,
398 'before_or_equal:' . $dateEndParam,
402 'before_or_equal:yesterday',
403 'after_or_equal:' . $dateStartParam,
407 '*.date_format' =>
'invalidFormat',
408 $dateStartParam .
'.after_or_equal' =>
'tooEarly',
409 $dateEndParam .
'.before_or_equal' =>
'tooLate',
410 $dateStartParam .
'.before_or_equal' =>
'invalidRange',
411 $dateEndParam .
'.after_or_equal' =>
'invalidRange',
415 if ($validator->fails()) {
416 $errors = $validator->errors()->getMessages();
417 if ((!empty($errors[$dateStartParam]) && in_array(
'invalidFormat', $errors[$dateStartParam]))
418 || (!empty($errors[$dateEndParam]) && in_array(
'invalidFormat', $errors[$dateEndParam]))) {
419 return 'api.stats.400.wrongDateFormat';
421 if (!empty($errors[$dateStartParam]) && in_array(
'tooEarly', $errors[$dateStartParam])) {
422 return 'api.stats.400.earlyDateRange';
424 if (!empty($errors[$dateEndParam]) && in_array(
'tooLate', $errors[$dateEndParam])) {
425 return 'api.stats.400.lateDateRange';
427 if ((!empty($errors[$dateStartParam]) && in_array(
'invalidRange', $errors[$dateStartParam]))
428 || (!empty($errors[$dateEndParam]) && in_array(
'invalidRange', $errors[$dateEndParam]))) {
429 return 'api.stats.400.wrongDateRange';