forked from glpi-project/glpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
113 lines (98 loc) · 3.31 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
/**
* High-level API entrypoint
*/
use Glpi\Api\HL\Controller\AbstractController;
use Glpi\Api\HL\Router;
use Glpi\Application\ErrorHandler;
use Glpi\Cache\CacheManager;
use Glpi\Http\JSONResponse;
use Glpi\Http\Request;
use Glpi\Http\Response;
/**
* @var GLPI $GLPI
* @var \Psr\SimpleCache\CacheInterface $GLPI_CACHE
*/
global $GLPI, $GLPI_CACHE;
define('GLPI_ROOT', __DIR__);
define('DO_NOT_CHECK_HTTP_REFERER', 1);
ini_set('session.use_cookies', 0);
include_once(GLPI_ROOT . '/inc/based_config.php');
// Init loggers
$GLPI = new GLPI();
$GLPI->initLogger();
$GLPI->initErrorHandler();
//init cache
$cache_manager = new CacheManager();
$GLPI_CACHE = $cache_manager->getCoreCacheInstance();
$method = $_SERVER['REQUEST_METHOD'];
$relative_uri = $_SERVER['PATH_INFO'] ?? '';
// Ensure uri starts with slash but does not end with a slash
$relative_uri = '/' . trim($relative_uri, '/');
// If the relative URI starts with /v1/ or is /v1 then we are dealing with a legacy API request
if (preg_match('/^\/v1(\/|$)/', $relative_uri)) {
// Include the legacy API entrypoint and then die
$api = new Glpi\Api\APIRest();
$api->call();
die();
}
include_once(GLPI_ROOT . '/inc/includes.php');
$supported_versions = Router::getAPIVersions(true);
if (preg_match('/^\/v\d+(\/|$)/', $relative_uri)) {
// A specific API version has been requested
//TODO Plan handling endpoints with specific versions
// For now, just remove the version prefix from the URI
$relative_uri = preg_replace('/^\/v\d+(\/|$)/', '/', $relative_uri);
}
$body = file_get_contents('php://input') ?? null;
$headers = getallheaders() ?? [];
$request = new Request($method, $relative_uri, $headers, $body);
$router = Router::getInstance();
try {
$response = $router->handleRequest($request);
$response->send();
} catch (\InvalidArgumentException $e) {
$response = new JSONResponse(
AbstractController::getErrorResponseBody(
AbstractController::ERROR_INVALID_PARAMETER,
$e->getMessage()
),
400
);
} catch (\Throwable $e) {
ErrorHandler::getInstance()->handleException($e);
$response = new Response(500);
$response->send();
}