initial commit
This commit is contained in:
125
Glpi/Assign-Ticket.php
Normal file
125
Glpi/Assign-Ticket.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
// Bypass Auth on API Call
|
||||
define('IS_API', true);
|
||||
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
||||
if (in_array($origin,[
|
||||
'http://infra-tools.appliarmony.net',
|
||||
'https://infra-tools.appliarmony.net',
|
||||
'https://web-infra-reports.process.dkm'
|
||||
], true)) {
|
||||
header("Access-Control-Allow-Origin: $origin");
|
||||
header("Vary: Origin");
|
||||
}
|
||||
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type, Accept');
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
|
||||
|
||||
// Web-Service Common
|
||||
include $_SERVER['DOCUMENT_ROOT'] . "/include/all.php";
|
||||
$rc=999;$stdout="?";$actor=$_SERVER['COMPUTERNAME'];$done=0;
|
||||
$input = file_get_contents('php://input');
|
||||
$json = json_decode($input, true);
|
||||
$SourceUser = $json['source'] ?? $_SERVER['CURRENTUSER'] ?? null;
|
||||
|
||||
// Main
|
||||
|
||||
$ticketId = (int)$json['ticketId'];
|
||||
$userId = (int)$json['userId'];
|
||||
|
||||
// 1) initSession
|
||||
$initPayload = $glpiUserToken
|
||||
? ['user_token' => $glpiUserToken]
|
||||
: ['login' => 'glpi', 'password' => 'glpi'];
|
||||
$ch = curl_init(rtrim($glpiUrl, '/') . '/initSession');
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Content-Type: application/json',
|
||||
'App-Token: ' . $glpiAppToken,
|
||||
],
|
||||
CURLOPT_POSTFIELDS => json_encode($initPayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_SSL_VERIFYHOST => false,
|
||||
]);
|
||||
$initResp = curl_exec($ch);
|
||||
if ($initResp === false) {
|
||||
die('initSession cURL error: ' . curl_error($ch));
|
||||
}
|
||||
$initJson = json_decode($initResp, true);
|
||||
curl_close($ch);
|
||||
|
||||
if (empty($initJson['session_token'])) {
|
||||
die("initSession failed:\n$initResp");
|
||||
}
|
||||
$sessionToken = $initJson['session_token'];
|
||||
|
||||
// 2) Update the ticket using POST
|
||||
$tuPayload = [
|
||||
'input' => [
|
||||
'tickets_id' => $ticketId,
|
||||
'users_id' => $userId,
|
||||
'type' => 2 // 1=requester, 2=assigned, 3=observer
|
||||
]
|
||||
];
|
||||
|
||||
$ch = curl_init(rtrim($glpiUrl, '/') . '/Ticket_User');
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Content-Type: application/json',
|
||||
'App-Token: ' . $glpiAppToken,
|
||||
'Session-Token: ' . $sessionToken,
|
||||
],
|
||||
CURLOPT_POSTFIELDS => json_encode($tuPayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_SSL_VERIFYHOST => false,
|
||||
]);
|
||||
$tuResp = curl_exec($ch);
|
||||
$tuCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
if ($tuResp === false) {
|
||||
die('Ticket_User add cURL error: ' . curl_error($ch));
|
||||
}
|
||||
curl_close($ch);
|
||||
|
||||
$rc=0;$done=1;$stdout="Ticket $ticketId assigned to $userId ";
|
||||
|
||||
// 3) If Zabbix Ticket --> Call acknowledge WS
|
||||
$rs = Invoke_glpi("SELECT name, content FROM glpi_tickets WHERE id=$ticketId");
|
||||
$ticketName = $rs[0]['name'] ?? '';
|
||||
$ticketContent = $rs[0]['content'] ?? '';
|
||||
|
||||
if (preg_match('/eventid=(\d+)/', $ticketContent, $m)) {
|
||||
$eventId = $m[1];
|
||||
$payload = json_encode(['eventId' => $eventId]);
|
||||
|
||||
// Detect Zabbix source
|
||||
if (str_contains($ticketName, 'ZABBIX OT |')) {
|
||||
// Zabbix OT Case
|
||||
postJson("$BaseUrl/Zabbix/Acknowledge-OT-Event.php", $payload);
|
||||
$stdout .= "| Zabbix OT Acknowledged (Event $eventId)";
|
||||
}
|
||||
elseif (str_contains($ticketName, 'ZABBIX IT |')) {
|
||||
// Zabbix IT Case
|
||||
postJson("$BaseUrl/Zabbix/Acknowledge-IT-Event.php", $payload);
|
||||
$stdout .= "| Zabbix IT Acknowledged (Event $eventId)";
|
||||
}
|
||||
}
|
||||
|
||||
//Answer
|
||||
$response = [
|
||||
"RC" => $rc,
|
||||
"stdout" => $stdout,
|
||||
];
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
// Log to dedicated Table
|
||||
log2DB();
|
||||
|
||||
?>
|
||||
|
||||
|
||||
|
||||
96
Glpi/Create-Ticket.php
Normal file
96
Glpi/Create-Ticket.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?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)]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user