97 lines
2.9 KiB
PHP
97 lines
2.9 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
// 1. Configuration - A ajuster selon ton environnement de prod
|
|
$api_url = "https://web-glpi-aim.process.dkm/glpi/apirest.php";
|
|
$user_token = "EgoVX0XYLK9SZi5dAyxRUPR8Te2rmmQbunaVpySO";
|
|
$app_token = "LQMIylSMr1qIAHh1OdYY8zP6MIyKEIKRt8mJbO6G";
|
|
|
|
// 2. Mapping des groupes et entités
|
|
$groups_mapping = [
|
|
"INFRADK" => 13,
|
|
"MIPSDK" => 50,
|
|
"NETWORKDK" => 60
|
|
];
|
|
$entities_mapping = [
|
|
"OT" => 1,
|
|
"IT" => 6
|
|
];
|
|
|
|
// 3. Récupération du JSON
|
|
$input = file_get_contents('php://input');
|
|
$data = json_decode($input, true);
|
|
|
|
if (!$data) {
|
|
echo json_encode(["RC" => 1, "stdout" => "Invalid JSON input"]);
|
|
exit;
|
|
}
|
|
|
|
// 4. Initialisation de la session GLPI
|
|
function callGLPI($url, $method = 'GET', $headers = [], $postFields = null) {
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Souvent nécessaire en interne
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
if ($postFields) curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
|
|
|
|
$result = curl_exec($ch);
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($err) return ["error" => $err];
|
|
return json_decode($result, true);
|
|
}
|
|
|
|
// Get Session Token
|
|
$session = callGLPI("$api_url/initSession", "GET", [
|
|
"App-Token: $app_token",
|
|
"Authorization: user_token $user_token"
|
|
]);
|
|
|
|
if (!isset($session['session_token'])) {
|
|
echo json_encode(["RC" => 1, "stdout" => "GLPI Auth Failed: " . json_encode($session)]);
|
|
exit;
|
|
}
|
|
|
|
$sess_token = $session['session_token'];
|
|
$headers = [
|
|
"Content-Type: application/json",
|
|
"App-Token: $app_token",
|
|
"Session-Token: $sess_token"
|
|
];
|
|
|
|
// 5. Création du Ticket
|
|
$groupId = $groups_mapping[$data['group']] ?? 13; //infra par défaut
|
|
$entityId = $entities_mapping[$data['entity']] ?? 1; //OT par défaut
|
|
|
|
$ticketPayload = json_encode([
|
|
"input" => [
|
|
"name" => $data['title'],
|
|
"content" => $data['description'] . "<br> Source: " . $data['source'],
|
|
"type" => 1,
|
|
"requesttypes_id" => 15,
|
|
"itilcategories_id" => 46,
|
|
"_groups_id_assign" => $groupId,
|
|
"urgency" => 3,
|
|
"impact" => 3,
|
|
"priority" => 3,
|
|
"entities_id" => $entityId,
|
|
"users_id_recipient" => 25255
|
|
]
|
|
]);
|
|
|
|
$response = callGLPI("$api_url/Ticket", "POST", $headers, $ticketPayload);
|
|
|
|
// 6. Fermeture session et retour
|
|
callGLPI("$api_url/killSession", "GET", $headers);
|
|
|
|
if (isset($response['id'])) {
|
|
echo json_encode(["RC" => 0, "stdout" => "Ticket created ID: " . $response['id']]);
|
|
} else {
|
|
echo json_encode(["RC" => 1, "stdout" => "API Error: " . json_encode($response)]);
|
|
}
|
|
|
|
|