public function call()
{
//parse http request and find parts
$this->request_uri = $_SERVER['REQUEST_URI'];
$this->verb = $_SERVER['REQUEST_METHOD'];
$path_info = str_replace("api/", "", trim($_SERVER['PATH_INFO'], '/'));
$this->url_elements = explode('/', $path_info);
// retrieve requested resource
$resource = trim(strval($this->url_elements[0]));
$is_inline_doc = strlen($resource) == 0 || $resource == "api";
// Add headers for CORS
$this->cors($this->verb);
// retrieve paramaters (in body, query_string, headers)
$this->parseIncomingParams($is_inline_doc);
// show debug if required
if (isset($this->parameters['debug'])) {
$this->debug = $this->parameters['debug'];
if (empty($this->debug)) {
$this->debug = 1;
}
if ($this->debug >= 2) {
$this->showDebug();
}
}
// retrieve session (if exist)
$this->retrieveSession();
// retrieve param who permit session writing
if (isset($this->parameters['session_write'])) {
$this->session_write = (bool) $this->parameters['session_write'];
}
// inline documentation (api/)
if ($is_inline_doc) {
return $this->inlineDocumentation("apirest.md");
} else {
if ($resource === "initSession") {
## DECLARE ALL ENDPOINTS ##
// login into glpi
$this->session_write = true;
return $this->returnResponse($this->initSession($this->parameters));
} else {
if ($resource === "killSession") {
// logout from glpi
$this->session_write = true;
return $this->returnResponse($this->killSession());
} else {
if ($resource === "changeActiveEntities") {
// change active entities
$this->session_write = true;
return $this->returnResponse($this->changeActiveEntities($this->parameters));
} else {
if ($resource === "getMyEntities") {
// get all entities of logged user
return $this->returnResponse($this->getMyEntities($this->parameters));
} else {
if ($resource === "getActiveEntities") {
// get curent active entity
return $this->returnResponse($this->getActiveEntities($this->parameters));
} else {
if ($resource === "changeActiveProfile") {
// change active profile
$this->session_write = true;
return $this->returnResponse($this->changeActiveProfile($this->parameters));
} else {
if ($resource === "getMyProfiles") {
// get all profiles of current logged user
return $this->returnResponse($this->getMyProfiles($this->parameters));
} else {
if ($resource === "getActiveProfile") {
// get current active profile
return $this->returnResponse($this->getActiveProfile($this->parameters));
} else {
if ($resource === "getFullSession") {
// get complete php session
return $this->returnResponse($this->getFullSession($this->parameters));
} else {
if ($resource === "getGlpiConfig") {
// get complete php var $CFG_GLPI
return $this->returnResponse($this->getGlpiConfig($this->parameters));
} else {
if ($resource === "listSearchOptions") {
// list searchOptions of an itemtype
$itemtype = $this->getItemtype(1);
return $this->returnResponse($this->listSearchOptions($itemtype, $this->parameters));
} else {
if ($resource === "getMultipleItems") {
// get multiple items (with various itemtype)
return $this->returnResponse($this->getMultipleItems($this->parameters));
} else {
if ($resource === "search") {
// Search on itemtype
self::checkSessionToken();
$itemtype = $this->getItemtype(1, true, true);
//clean stdObjects in parameter
$params = json_decode(json_encode($this->parameters), true);
//search
$response = $this->searchItems($itemtype, $params);
//add pagination headers
$additionalheaders = array();
$additionalheaders["Content-Range"] = $response['content-range'];
$additionalheaders["Accept-Range"] = $itemtype . " " . Toolbox::get_max_input_vars();
// diffent http return codes for complete or partial response
if ($response['count'] >= $response['totalcount']) {
$code = 200;
// full content
} else {
$code = 206;
// partial content
}
return $this->returnResponse($response, $code, $additionalheaders);
} else {
// commonDBTM manipulation
$itemtype = $this->getItemtype(0);
$id = $this->getId();
$additionalheaders = array();
$code = 200;
switch ($this->verb) {
default:
case "GET":
// retrieve item(s)
if ($id > 0 || $id == 0 && $itemtype == "Entity") {
$response = $this->getItem($itemtype, $id, $this->parameters);
if (isset($response['date_mod'])) {
$datemod = strtotime($response['date_mod']);
$additionalheaders['Last-Modified'] = gmdate("D, d M Y H:i:s", $datemod) . " GMT";
}
} else {
// return collection of items
$totalcount = 0;
$response = $this->getItems($itemtype, $this->parameters, $totalcount);
//add pagination headers
$range = [0, $_SESSION['glpilist_limit']];
if (isset($this->parameters['range'])) {
$range = explode("-", $this->parameters['range']);
// fix end range
if ($range[1] > $totalcount - 1) {
$range[1] = $totalcount - 1;
}
if ($range[1] - $range[0] + 1 < $totalcount) {
$code = 206;
// partial content
}
}
$additionalheaders["Content-Range"] = implode('-', $range) . "/" . $totalcount;
$additionalheaders["Accept-Range"] = $itemtype . " " . Toolbox::get_max_input_vars();
}
break;
case "POST":
// create item(s)
$response = $this->createItems($itemtype, $this->parameters);
$code = 201;
if (isset($response['id'])) {
// add a location targetting created element
$additionalheaders['location'] = self::$api_url . $itemtype . "/" . $response['id'];
} else {
// add a link header targetting created elements
$additionalheaders['link'] = "";
foreach ($response as $created_item) {
if ($created_item['id']) {
$additionalheaders['link'] .= self::$api_url . $itemtype . "/" . $created_item['id'] . ",";
}
}
// remove last comma
$additionalheaders['link'] = trim($additionalheaders['link'], ",");
}
break;
case "PUT":
// update item(s)
// if id is passed by query string, add it into input parameter
$input = (array) $this->parameters['input'];
if (($id > 0 || $id == 0 && $itemtype == "Entity") && !isset($input['id'])) {
$this->parameters['input']->id = $id;
}
$response = $this->updateItems($itemtype, $this->parameters);
break;
case "DELETE":
//delete item(s)
// if id is passed by query string, construct an object with it
if ($id !== false) {
//override input
$this->parameters['input'] = new stdClass();
$this->parameters['input']->id = $id;
}
$response = $this->deleteItems($itemtype, $this->parameters);
break;
}
return $this->returnResponse($response, $code, $additionalheaders);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
$this->messageLostError();
}