<?php

// Libreria per comunicare la licenza da software a server
// c/o stefano riva

// target nostro live server
// license.bitonlive.net
// 7998

//$endpoint_url = "sede.bitonlive.net";
//$endpoint_port = "7998";

//autoLicenseUpdate();

function autoLicenseUpdate($endpoint_url,$endpoint_port){

    include_once(__DIR__ . "/licenseHandler.php");

    // prendiamo la licenza.
    $endpoint_url = "sede.bitonlive.net";
    $endpoint_port = "7998";

    $licenseKey = generateBitonliveKey(0, file_get_contents(__DIR__ . "/../../../version.txt"));
    $operating_system = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'win' : 'linux';
    $id_license = "";

    // MI SERVE IL SERIALE IN USO ORA SE ABBIAMO GIà UNA LICENZA x sapere se dobbiamo aggiornarla
    if(file_exists(__DIR__ . "/../../../lic.bitonlive")){

        $lic_payload = checkBitonliveLicense([144], __DIR__ . "/../../../lic.bitonlive", true);

       // print_r($lic_payload);
        if(isset($lic_payload['id'])) $id_license = $lic_payload['id'];

        
    }
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
    CURLOPT_URL => $endpoint_url . ':'.$endpoint_port.'/api/v1/update/license',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 5,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
        "softwarekey" : "'.trim(preg_replace('/\s+/', '', ($licenseKey))).'",
        "id" : "'.$id_license.'",
        "target_os" : "'.$operating_system.'"
    }',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',       
    ),
    ));


    $response = curl_exec($curl);

    if(!$response || empty($response)) return 'impossibile raggiungere il server';

    $response = json_decode($response, true);    
    
    if(isset($response['error'])){

        //return $licenseKey . " " . $response['error'];
        return 'aggiornamento licenza fallito'; //$response['error']; // false la richiesta della nuvoa licenza è andata in errore.

    } else {

        if(!isset($response['new_license']) || (
            isset($response['new_license']) && empty($response['new_license'])
        )) 
        
        return "licenza non trovata"; //false; // non abbiamo la licenza... o il campo è vuoto lol

        // abbiamo la licenza NUOVA  da installare...
        file_put_contents(__DIR__ . "/../../../lic.bitonlive" , $response['new_license']);

        // registro la licenza 
        if(isset($response['registration']) && $response['registration'])
            registerLicenseOnServer($endpoint_url,$endpoint_port, $response['id_ref']);

        return true;


    }
   
    curl_close($curl);

}

function registerLicenseOnServer($endpoint_url,$endpoint_port, $id_ref){
  
    $lic_content = file_get_contents(__DIR__ . "/../../../lic.bitonlive");

    $operating_system = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'win' : 'linux';
    
    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL =>  $endpoint_url . ':'.$endpoint_port.'/api/v1/register/license',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{        
        "id_ref" : "'.$id_ref.'"
    }',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',       
    ),
    ));

    $response = curl_exec($curl);
    if(!$response || empty($response)) return "Impossibile raggiungere il server";

    $response = json_decode($response, true);

    if(isset($response['error'])){

        return "aggiornamento licenza fallito"; //false

    } else {

        return true;

    }

}


?>