<?php

include("functions.php");
//include_once(__DIR__ . '/../library/php/licenseHandler/licenseHandler.php');

  if(isset($_GET['init'])){

    // richiamo la funziona che verifica che webapp sia già stato installato e che abbia in futuro la licenza.
    echo initWEBAPP();

  }


  if(isset($_GET['InstallationStep'])){

    echo InstallationStep($_POST['step']);

  }


  if(isset($_GET['saveDBInformation'])){

    echo saveDBInformation($_POST['ip'] , $_POST['user'] , $_POST['password'] , $_POST['schema'] , $_POST['prefix']);

  }


  if(isset($_GET['saveSharedPath'])){

    echo verifyShared($_POST['sharedPath']);

  }

  if(isset($_GET['executeInstallationWebApp'])){

    echo executeInstallationWebApp($_POST['username'] , $_POST['password']);

  }


// Fay 01-2021

if (isset($_GET['display_dbsettings'])) {

  echo setupBadDBStartup(0);
}

if (isset($_GET['executeDcv'])) {

  echo executeDcv($_POST['ip'], $_POST['user'], $_POST['pass'], $_POST['name']);
}

if (isset($_GET['RUNdatabaseCheck'])) {

  echo RUNdatabaseCheck($_POST['ip'], $_POST['user'], $_POST['pass'], $_POST['name'],$_POST['prefix'], $_POST['sharedPath'], $_POST['username'], $_POST['password']);
}

/*if (isset($_GET['updateConfigDB'])) {

  echo updateConfigDB($_POST['ip'], $_POST['user'], $_POST['pass'], $_POST['name']);
  
}
*/

if (isset($_GET['checkStatus'])) {

  include("check_session.php");
  include("../db_connection.php");

  if (
    $_SESSION['OLD_DB_CONFIG']['IP'] != $db_connection['db-ip'] ||
    $_SESSION['OLD_DB_CONFIG']['USER'] != $db_connection['user'] ||
    $_SESSION['OLD_DB_CONFIG']['PASSWORD'] != $db_connection['password'] ||
    $_SESSION['OLD_DB_CONFIG']['SCHEMA'] != $db_connection['db-name']) {

    echo "1";
  } else {

  echo "0";
  }
  
}


if (isset($_GET['RUNdbInstallation'])) {

  echo RUNdbInstallation();

}


if(isset($_GET['registerLicense'])){

  echo registerLicense($_FILES['license']);

}

if(isset($_GET['get2LicenseKey'])){

  $file = __DIR__ . "/license-token.key";

  $k = generateBitonliveKey(1, file_get_contents(__DIR__ . "/../version.txt"));
 
  file_put_contents( $file , generateBitonliveKey(1, file_get_contents(__DIR__ . "/../version.txt")));

  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename="'.basename($file).'"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  readfile( $file );
  //ob_end_clean(); // disabilito echo della licenza */

}


if (isset($_GET['updateLicense'])) {

  include("check_session.php");
  include("../include/setsession.php");
  include("../library/php/functions.php");
  
  include_once(__DIR__ . "/../library/php/licenseHandler/licenseUpdater.php");
  $endpoint_url = "sede.bitonlive.net";
  $endpoint_port = "7998";
  // LANCIO IL CHECK AGGIORNAMENTO LICENZA.
  $res = autoLicenseUpdate($endpoint_url, $endpoint_port);
  if($res != true) echo webapp_translator($res);
  else echo $res;
 

}

?>