<?php
  //  CONSTANTS  //
  $BaseUrl = "http://".$_SERVER['SERVER_NAME'];
  $root = $_SERVER['DOCUMENT_ROOT'];
  $bdnuss = "http://web-bdnuss-sys-dev.appliarmony.net";

  //  FUNCTIONS   //
  //tools//
  function decypher(string $name): ?string {
    $openssl = 'C:\Program Files\FireDaemon OpenSSL 3\bin\openssl.exe';
    $cmsFile = "F:\\Include\\dat\\$name.p7m";
    $certPem = "F:\\Include\\certs\\cert_only.pem";
    $keyPem  = "F:\\Include\\certs\\key_only.pem";

    foreach ([$openssl,$cmsFile,$certPem,$keyPem] as $p) {
      if (!is_file($p)) { error_log("Missing file: $p"); return null; }
    }

    $cmd = '"' . $openssl . '" cms -decrypt -inform PEM'
      . ' -in '    . escapeshellarg($cmsFile)
      . ' -recip ' . escapeshellarg($certPem)
      . ' -inkey ' . escapeshellarg($keyPem)
      . ' -out -';

    $spec = [
      0 => ['pipe','r'],   // stdin (unused)
      1 => ['pipe','w'],   // stdout -> texte déchiffré
      2 => ['pipe','w'],   // stderr -> erreurs OpenSSL
    ];
    $proc = proc_open($cmd, $spec, $pipes);
    if (!is_resource($proc)) { error_log('proc_open failed'); return null; }
    fclose($pipes[0]); // rien à envoyer en stdin

    $stdout = stream_get_contents($pipes[1]); fclose($pipes[1]);
    $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]);
    $code = proc_close($proc);

    if ($code !== 0) {
      error_log("OpenSSL failed (code $code): $stderr");
      return null;
    }
    return $stdout;
  }
  function PostJson($url, $content){
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    $retour = curl_exec($curl);
    curl_close($curl);
    return $retour;
  }

  //invoke//
  function Invoke_Infra($request)
  {
    error_reporting(E_ALL ^ E_WARNING);
    $user = "INFRA_dbo";
    $pwd = decypher("infra");
    $server = "DUN-PRD-R1MSSQL.armony.net\PRD";
    $database = "INFRA";
    $conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $pwd);
    if (!($conn)) {
      echo "Pas de connexion";
    }
    $rs = odbc_exec($conn, $request);
    if (str_contains(strtoupper($request), "SELECT")) {
      while ($row = odbc_fetch_array($rs)) {
        $answer[] = $row;
      }
    }
    if (isset($answer)) {
      return $answer;
    } else {
      if ($rs) {
        return "OK";
      } else {
        error_reporting(E_ALL);
        return "ERROR : " . odbc_errormsg($conn);
      }
    }

  }
  function Invoke_WebSelfInfra($request)
  {
    $user = "Scom-Write";
    $pwd = decypher("web-self-infra");
    $server = "DUN-PRD-R1SCOM.armony.net\OPS";
    $database = "web-self-infra";
    $conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $pwd);
    if (!($conn)) {
      echo "Pas de connexion";
    }
    $rs = odbc_exec($conn, $request);
    if (str_contains(strtoupper($request), "SELECT")) {
      while ($row = odbc_fetch_array($rs)) {
        $answer[] = $row;
      }
    }
    return $answer ?? null;
  }
  function Invoke_WebInfraReports($request)
  {
    $user = "admin";
    $pwd = decypher("web-infra-reports");
    $server = "dun-sup-s2entry.armony.net:3306";
    $database = "web-infra-reports";
    $conn = new mysqli($server, $user, $pwd, $database);
    $rs = $conn->query($request);
    while ($row = mysqli_fetch_array($rs)) {
      $answer[] = $row;
    }
    return $answer ?? null;

  }
  function Invoke_SCOMInfra($request)
  {
    $user = "Scom-Write";
    $pwd = decypher("ScomInfra");
    $server = "DUN-PRD-R1SCOM.armony.net\OPS";
    $database = "infra";
    $conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $pwd);
    if (!($conn)) {
      echo "Pas de connexion";
    }
    $rs = odbc_exec($conn, $request);
    if (str_contains(strtoupper($request), "SELECT")) {
      while ($row = odbc_fetch_array($rs)) {
        $answer[] = $row;
      }
    }
    return $answer ?? null;
  }
  function Invoke_Entry01($request)
  {
    $user = "infra";
    $pwd = decypher("entry01");
    $server = "dun-sup-entry01:3306";
    $database = "infra";
    $conn = new mysqli($server, $user, $pwd, $database);
    $rs = $conn->query($request);
    while ($row = mysqli_fetch_array($rs)) {
      $answer[] = $row;
    }
    return $answer ?? null;
  }
  function Invoke_WebInfraTools($request)
  {
    $user = "admin";
    $pwd = decypher("web-infra-tools");
    $server = "dun-sup-s2entry.armony.net:3306";
    $database = "webinfratools";
    $conn = new mysqli($server, $user, $pwd, $database);
    $rs = $conn->query($request);
    while ($row = mysqli_fetch_array($rs)) {
      $answer[] = $row;
    }
    return $answer ?? null;
  }
  function Invoke_GLPI($request)
  {
    $user="glpi_lect_seule";
    $pwd=decypher("glpi");
    $server="mys_glpi_prod.appliarmony.net";
    $database="glpi";
    $conn = new mysqli($server, $user, $pwd, $database);
    $rs = $conn->query($request);
    while ($row = mysqli_fetch_array($rs)) {
      $answer[] = $row;
    }
    return $answer ?? null;
  }
  function Invoke_aixcmdb($request)
  {
    $conn_string = "DRIVER={IBM DB2 ODBC DRIVER - F_DB2_clidriver};" .
      "DATABASE=AIXCMDB; " .
      "HOSTNAME=db2_aixcmdb.appliarmony.net;" .
      "PORT=50000; " .
      "PROTOCOL=TCPIP; " .
      "UID=aixcmdb;" .
      "AUTHENTICATION=SERVER;" .
      "PWD=".decypher('aixcmdb').";";

    $conn = odbc_connect($conn_string, "", "");
    if (!$conn) {
      $error_message = "ODBC Connect Error: " . odbc_errormsg() . " (" . odbc_error() . ")";
      echo "Pas de connexion : " . $error_message . "<br>";
      return "ERROR: Could not connect to DB2. Check connection string and ODBC setup.";
    }
    $rs = odbc_exec($conn, $request);
    if (str_contains(strtoupper($request), "SELECT")) {
      $answer = [];
      while ($row = odbc_fetch_array($rs)) {
        $answer[] = $row;
      }
    }
    if (isset($answer)) {
      return $answer;
    } else {
      if ($rs) {
        return "OK";
      } else {
        error_reporting(E_ALL);
        return "ERROR : " . odbc_errormsg($conn);
      }
    }
  }

  //DB Conn//
  function DB_SCCM(){
    $user = "infra";
    $pwd = decypher("SCCM");
    $conn = odbc_connect("Driver={SQL Server};Server=DUN-SMS-SRV01;Database=CM_SV1;", $user, $pwd);
    return $conn;
  }
  function DB_GLPI() {
    $username="glpi_lect_seule";
    $password=decypher( "glpi");
    $dbserver="mys_glpi_prod.appliarmony.net";
    $database="glpi";
    $conn = new mysqli($dbserver,$username,$password,$database);
    return $conn;
  }
  function DB_ENTRY01() {
    $username="infra";
    $password=decypher("entry01");
    $dbserver="dun-sup-entry01.armony.net";
    $database="infra";
    $conn = new mysqli($dbserver,$username,$password,$database);
    return $conn;
  }
  function DB_ENTRY02() {
    $username="admin";
    $password=decypher("web-infra-reports");
    $dbserver="dun-sup-s2entry.armony.net";
    $database="web-infra-reports";
    $conn = new mysqli($dbserver,$username,$password,$database);
    return $conn ;
  }
  function DB_INFRA() {
    $user="INFRA_dbo";
    $pwd=decypher("infra");
    $server="DUN-PRD-R1MSSQL.armony.net\PRD";
    $database="INFRA";
    $conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $pwd);
    return $conn ;
  }
  function DB_ZABBIX()
  {
    $host = 'aztprdzabbix52.armony.net';
    $dbname = 'zabbix';
    $user = 'patrick';
    $pass = decypher( 'zabbix');
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    return $pdo;
  }

?>

<!-- MODAL WAIT -->
<div class="modal fade bs-example-modal-sm" id="wait" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" style="padding-top: 15%;">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title mb-1 text-dark text-uppercase">
          <span class="text-center"><i class="bi bi-hourglass-split"></i><br> Work in progress ...</span>
        </h4>
      </div>
      <div class="modal-body">
        <div class="progress">
          <div class="progress-bar progress-bar-secondary progress-bar-striped progress-bar-animated" style="width: 100%"></div>
        </div>
      </div>
    </div>
  </div>
</div>