<?php

$_portal_uri = 'http://DOMAIN.iptvportal.ru/';

$_auth_uri = 'https://admin.DOMAIN.iptvportal.ru/api/jsonrpc/';
$_jsonsql_uri = 'https://admin.DOMAIN.iptvportal.ru/api/jsonsql/';
$_username = 'ADMIN_LOGIN';
$_password = 'ADMIN_PASSWORD';

$_iptvportal_header = null;


#$_msg_to = 'all'; // Message for ALL
#$_msg_to = 'user-12345'; // Message for User 12345
$_msg_to = 'term-1027BE03008F'; // Message for MAC 00:1a:79:05:72:6b

$_msg_type = 'text=TEXT MESSAGE TEXT MESSAGE TEXT MESSAGE TEXT MESSAGE TEXT MESSAGE TEXT MESSAGE TEXT MESSAGE';
//$_msg_type = 'image=http://iptvportal.ru/wp-content/uploads/logo_iptvportal_221x61.png';
//$_msg_type = 'video=http://iptvportal.ru/wp-content/uploads/180405_FinancialCharts_06_360p.mp4';
//$_msg_type = 'stream=http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8';

$_msg_duration = 15; // duration = 15 sec.

$_msg_repeat = 10; // repeat = 10



//-------------------------------------------------------------------------------------------------------------------------

function send($url, $data, $extra_headers=null) {
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    if (isset($extra_headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $extra_headers);
    }
    //echo "HTTP fetching '$url'...\n" . chr(10);
    $content=curl_exec($ch);
    $http_code=curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($content === false) {
        $err_msg="HTTP error: $http_code (" . curl_error($ch) . ')' . '\n';
        echo $err_msg . chr(10);
        throw new Exception($err_msg);
    }
    if ($http_code != 200) {
        $err_msg="HTTP request failed ($http_code)\n";
        echo $err_msg . chr(10);
        throw new Exception($err_msg);
    }
    //echo "HTTP OK ($http_code)\n";
    curl_close($ch);
    return $content;
}

function jsonrpc_call($url, $method, $params, $extra_headers=null) {
    static $req_id=1;

    $req=array(
            "jsonrpc"=>'2.0',
            "id"=>$req_id++,
            "method"=>$method,
            "params"=>$params
    );
    $req=json_encode($req);
    $res=send($url, $req, $extra_headers=$extra_headers);
    //echo $res . chr(10);
    $res=json_decode($res, true);
    if (!isset($res)) {
        echo "error: not result\n" . chr(10);
        return null;
    } else if (!array_key_exists('result', $res) || !isset($res ['result'])) {
        print_r($res ['error']);
        return null;
    } else {
        return $res ['result'];
    }
    return $res;
}

function jsonsql_call($cmd, $params) {
    global $_jsonsql_uri, $_iptvportal_header;
    //echo 'iptvportal_header: ';	print_r ($_iptvportal_header);
    return jsonrpc_call($_jsonsql_uri, $cmd, $params, $extra_headers=$_iptvportal_header);
}

function authorize_user($auth_uri, $username, $password) {
    global $_iptvportal_header;
    $res=jsonrpc_call($auth_uri, $cmd="authorize_user", $params=array(
            'username'=>$username,
            'password'=>$password
    ));
    if (isset($res) && array_key_exists('session_id', $res)) {
        $_iptvportal_header=array('Iptvportal-Authorization: ' . 'sessionid=' . $res ['session_id']);
    }
    return $res;
}

//-------------------------------------------------------------------------------------------------------------------------------

$user=authorize_user($auth_uri=$_auth_uri, $username=$_username, $password=$_password);


$url = $_portal_uri.'pub?id='.$_msg_to;
$data = $_msg_type;

if (isset($_msg_duration)) {
	$data = $data.'|t='.$_msg_duration;
}
if (isset($_msg_repeat)) {
	$data = $data.'|r='.$_msg_repeat;
}

$extra_headers = array('Iptvportal-Authorization: ' . 'sessionid=' . $user['session_id']);
$ch=curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $extra_headers);
curl_setopt($ch, CURLOPT_HEADER, $extra_headers);
//echo "HTTP fetching '$url'...\n" . chr(10);
//print_r ($ch);
$content=curl_exec($ch);
$http_code=curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($content === false) {
	$err_msg="HTTP error: $http_code (" . curl_error($ch) . ')' . '\n';
	echo $err_msg . chr(10) . '</br>';
	//throw new Exception($err_msg);
}
if ($http_code != 200) {
	$err_msg="HTTP request failed ($http_code)\n";
	echo $err_msg . chr(10) . '</br>';
	//throw new Exception($err_msg);
}
//echo "HTTP OK ($http_code)\n" . '</br>';
curl_close($ch);
//print_r ($content);



?>