#!/usr/bin/php
<?php
    //This example logs into OpenSPOT, shows some information (if 'debug' is set to true)
    // and sends an SMS to a local tranceiver (via the modem, not via the network)
 
    // ----------- configuration :
    $debug = false;
    $server   = "http://my.openspot.ip.address";
    $password = "openspot";
 
    $sms_dst = 0000000;   // THE DESTINATION DMRid GOES HERE;
    $sms_src = 0000000;   // THE SOURCE DMRid GOES HERE;
    $sms_fmt = 2;         // format:  0 - ETSI, 1 - UDP, 2 - UDP/Chinese
 
    $msg="This is a test message from ON7LDS";
    // ---------------------------------------------------------------------------------
 
    $token=file_get_contents ("$server/gettok.cgi");
    $data=json_decode($token,true);
    if ($debug) print_r($data);
    $digest = hash("sha256", $data['token'].$password);  //binair
    if ($debug) echo "Digest : $digest\n\n";
 
    $data = array(
        'token' => $data['token'],
        'digest' => $digest
    );
 
    $opts=array(
        "http" => array(
            // http://www.php.net/manual/en/context.http.php
            'method'  => 'POST',
            'header'  => "Authorization: Bearer null\r\n".
                         "Content-type: application/json\r\n",
            'content' => json_encode($data)
        )
    );
    // Create the context for the request
    $context = stream_context_create($opts);
 
    $response = @file_get_contents("$server/login.cgi", false, $context);
 
    // Check for errors
    if($response === false){
        if ($debug) die('Failed '.print_r(error_get_last(),true)."\n");
        die("Login failed\n");
        }
    // Decode the response
    $responseData = json_decode($response, true);
    if ($debug) print_r($responseData);
    $jwt=$responseData['jwt'];
 
    if ($debug) echo "\njwt = $jwt\n\n";
 
    if ($debug) print "Authorisation status : \n" . @file_get_contents("$server/checkauth.cgi")."\n";
 
 
    $opts=array(
        "http" => array(
            'method'  => 'GET',
            'header'  => "Authorization: Bearer $jwt\r\n"
        )
    );
    $context = stream_context_create($opts);
 
 
    if ($debug) {
        $response = file_get_contents("$server/info.cgi", false, $context);
        $responseData = json_decode($response, true);
        print "Openspot Device Info :\n";
        print_r($responseData);
    }
 
 
    if ($debug) {
        $response = file_get_contents("$server/modemfreq.cgi", false, $context);
        $responseData = json_decode($response, true);
        print "Openspot Modem Frequency : \n";
        print_r($responseData);
    }
 
    $response = file_get_contents("$server/status-dmrsms.cgi", false, $context);
    $responseData = json_decode($response, true);
    if ($debug) { print "Openspot SMS status : \n"; print_r($responseData); }
 
    $data = array(
        "only_save" => 0,
        "intercept_net_msgs" => 1,
        "send_dstid" => $sms_dst,
        "send_calltype" => 0,<-><------>// 0 = private, 1 = group
        "send_srcid" => $sms_src,
        "send_format" => $sms_fmt,<---><------>// 0 - ETSI, 1 - UDP, 2 - UDP/Chinese
        "send_tdma_channel" => 0,
        "send_to_modem" => 1,
        "send_msg" => bin2hex(mb_convert_encoding($msg, "UTF-16"))
    );
    $opts=array(
        "http" => array(
            'method'  => 'POST',
            'header'  => "Authorization: Bearer $jwt\r\n".
                         "Content-type: application/json\r\n",
            'content' => json_encode($data)
        )
    );
    $smscontext = stream_context_create($opts);
 
    $response = @file_get_contents("$server/status-dmrsms.cgi", false, $smscontext);
 
    if ($response===false) {
        print "Send SMS failed\n\n";
    } else {
        print "SMS sent\n\n";
    }
?>