//<?
//error_reporting(E_ALL);
$tvip = "10.195.30.238"; //ЗАМЕНИТЬ!!!
$myip = "10.195.30.33"; //ЗАМЕНИТЬ!!!
$mymac = "00-1C-25-7C-D8-38"; // Я менял, думаю надо.
$appstring = "iphone..iapp.samsung"; //Оставляем
$tvappstring = "iphone.UE40JU6000.iapp.samsung"; //Я вставил свою модель
$remotename = "Perl Samsung Remote"; //Оставляем
//echo "Content-type: text/html\n\n";
if ((isset($_REQUEST["key"])) || (isset($_REQUEST["text"]))) {
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
$result = socket_connect($sock, $tvip, '55000');
if( $result === false)
die ("Could not create socket: \n");
//Normal remote keys
//KEY_0
//KEY_1
//KEY_2
//KEY_3
//KEY_4
//KEY_5
//KEY_6
//KEY_7
//KEY_8
//KEY_9
//KEY_UP
//KEY_DOWN
//KEY_LEFT
//KEY_RIGHT
//KEY_MENU
//KEY_PRECH
//KEY_GUIDE
//KEY_INFO
//KEY_RETURN
//KEY_CH_LIST
//KEY_EXIT
//KEY_ENTER
//KEY_SOURCE
//KEY_AD
//KEY_PLAY
//KEY_PAUSE
//KEY_MUTE
//KEY_PICTURE_SIZE
//KEY_VOLUP
//KEY_VOLDOWN
//KEY_TOOLS
//KEY_POWEROFF
//KEY_CHUP
//KEY_CHDOWN
//KEY_CONTENTS
//KEY_W_LINK //Media P
//KEY_RSS //Internet
//KEY_MTS //Dual
//KEY_CAPTION //Subt
//KEY_REWIND
//KEY_FF
//KEY_REC
//KEY_STOP
//Bonus buttons not on the normal remote:
//KEY_TV
//Don't work/wrong codes:
//KEY_CONTENT
//KEY_INTERNET
//KEY_PC
//KEY_HDMI1
//KEY_OFF
//KEY_POWER
//KEY_STANDBY
//KEY_DUAL
//KEY_SUBT
//KEY_CHANUP
//KEY_CHAN_UP
//KEY_PROGUP
//KEY_PROG_UP
$ipencoded = base64_encode($myip);
$macencoded = base64_encode($mymac);
$messagepart1 = chr(0x64) . chr(0x00) . chr(strlen($ipencoded)) . chr(0x00) . $ipencoded . chr(strlen($macencoded)) . chr(0x00) . $macencoded .
chr(strlen(base64_encode($remotename))) . chr(0x00) . base64_encode($remotename);
$part1 = chr(0x00) . chr(strlen($appstring)) . chr(0x00) . $appstring . chr(strlen($messagepart1)) . chr(0x00) . $messagepart1;
socket_write($sock, $part1, strlen($part1));
//echo $part1;
//echo "\n";
$messagepart2 = chr(0xc8) . chr(0x00);
$part2 = chr(0x00) . chr(strlen($appstring)) . chr(0x00) . $appstring . chr(strlen($messagepart2)) . chr(0x00) . $messagepart2;
socket_write($sock, $part2, strlen($part2));
//echo $part2;
//echo "\n";
}
//Preceding sections all first time only
if (isset($_REQUEST["key"])) {
//Send remote key
$key = "KEY_" . $_REQUEST["key"];
$messagepart3 = chr(0x00) . chr(0x00) . chr(0x00) . chr(strlen(base64_encode($key))) . chr(0x00) . base64_encode($key);
$part3 = chr(0x00) . chr(strlen($tvappstring)) . chr(0x00) . $tvappstring . chr(strlen($messagepart3)) . chr(0x00) . $messagepart3;
socket_write($sock,$part3,strlen($part3));
//$res = socket_read($sock, 40); //Читаем ответ 23 байта, не больше
echo $part3;
//var_dump($res);
echo "\n";
socket_close($sock);
} else if (isset($_REQUEST["text"])) {
//Send text, e.g. in YouTube app's search, N.B. NOT BBC iPlayer app.
$text = $_REQUEST["text"];
$messagepart3 = chr(0x01) . chr(0x00) . chr(strlen(base64_encode($text, ""))) . chr(0x00) . base64_encode($text, "");
$part3 = chr(0x01) . chr(strlen($appstring)) . chr(0x00) . $appstring . chr(strlen($messagepart3)) . chr(0x00) . $messagepart3;
socket_write($sock,$part3,strlen($part3));
//$res = socket_read($sock, 40); //Читаем ответ 23 байта, не больше
echo $part3;
//var_dump($res);
echo "\n";
socket_close($sock);
} else if (isset($_REQUEST["GetCurrentExternalSource"])) {
$command = "GetCurrentExternalSource";
$return = soaprequest($tvip, $command);
preg_match("/<ID>(.*?)<\/ID>/is",$return,$idCurrentExternalSource);
preg_match("/<CurrentExternalSource>(.*?)<\/CurrentExternalSource>/is",$return,$CurrentExternalSource);
echo 'Source='.$CurrentExternalSource[1].'<br>';
echo 'id='.$idCurrentExternalSource[1].'<br>';
} else if (isset($_REQUEST["GetMuteStatus"])) {
$command = "GetMuteStatus";
$return = soaprequest($tvip, $command);
preg_match("/<MuteStatus>(.*?)<\/MuteStatus>/is",$return,$val);
echo $val[1];
} else if (isset($_REQUEST["GetVolume"])) {
$command = "GetVolume";
$return = soaprequest($tvip, $command);
preg_match("/<Volume>(.*?)<\/Volume>/is",$return,$val);
echo $val[1];
}
function soaprequest($tvip, $command) {
if (strcmp($command,"GetVolume")===0) {
$Body='<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:GetVolume xmlns:ns0="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel></ns0:GetVolume></s:Body></s:Envelope>';
$Clen=strlen($Body);
$out ="POST /MainTVServer2/control/MainTVAgent2 HTTP/1.0\r\nHost: ".$tvip.":52235\r\nContent-Type: text/xml; charset='utf-8'\r\nContent-Length: ".$Clen."\r\n".'SOAPACTION: "SoapAction:urn:schemas-upnp-org:service:RenderingControl:1#'.$command.'"'."\r\nConnection: close\r\n\r\n";
} else {
$Body='<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:'.$command.' xmlns:u="urn:samsung.com:service:MainTVAgent2:1"></u:'.$command.'></s:Body></s:Envelope>';
$Clen=strlen($Body);
$out ="POST /MainTVServer2/control/MainTVAgent2 HTTP/1.0\r\nHost: ".$tvip.":52235\r\nContent-Type: text/xml; charset='utf-8'\r\nContent-Length: ".$Clen."\r\n".'SOAPACTION: "urn:samsung.com:service:MainTVAgent2:1#'.$command.'"'."\r\nConnection: close\r\n\r\n";
}
$fp = fsockopen($tvip, 52235,$errno,$errstr,15);
$result='';
if ($fp) {
$out .= $Body."\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp);
}
return $result;
}
?>