<?php
error_reporting(E_ALL);
ini_set("display_errors","on");
date_default_timezone_set('Europe/Brussels');
define('time',$_SERVER['REQUEST_TIME']);
$c=json_decode(base64_decode($_REQUEST['c']),true);
$s=json_decode(base64_decode($_REQUEST['s']),true);
$i=json_decode(base64_decode($_REQUEST['i']),true);
$t=json_decode(base64_decode($_REQUEST['t']),true);
$r=str_replace(" ","_",key($c));
if(function_exists(key($c)))
key($c)();
elseif(function_exists($r))
$r();
//START USER FUNCTIONS
function pirhall()
{
global $s,$i,$t;
if($s['pirhall'=="On"])
{
if($s['lighthall']=='Off')
sw($i['hall'],'On','hall by pir',1000);
}
}
//END USERFUNCTIONS. Only change IP:PORT and ios,sms account details below
function sw($idx,$action="",$info="",$Usleep=600000)
{
lg("SWITCH ".$action." ".$info);
if(empty($action))
curl("http://127.0.0.1:8084/json.htm?type=command¶m=switchlight&idx=".$idx."&switchcmd=Toggle");
else
curl("http://127.0.0.1:8084/json.htm?type=command¶m=switchlight&idx=".$idx."&switchcmd=".$action);
usleep($Usleep);
}
function sl($idx,$level,$info="",$Usleep=600000)
{
lg("SETLEVEL ".$level." ".$info);
curl("http://127.0.0.1:8084/json.htm?type=command¶m=switchlight&idx=".$idx."&switchcmd=Set%20Level&level=".$level);
usleep($Usleep);
}
function ud($idx,$nvalue,$svalue,$info="",$Usleep=600000)
{
if(!in_array($idx, array(395,532,534)))
lg("UPDATE ".$nvalue." ".$svalue." ".$info);
curl('http://127.0.0.1:8084/json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
usleep($Usleep);
}
function setradiator($name,$dif,$koudst=false,$set)
{
$setpoint=$set-ceil($dif*4);
if($koudst==true)
$setpoint=28.0;
if($setpoint>28)
$setpoint=28.0;
elseif($setpoint<4)
$setpoint=4.0;
return round($setpoint,0).".0";
}
function double($idx,$action,$comment='',$wait=4000000)
{
sw($idx,$action,$comment,$wait);
sw($idx,$action,$comment.' repeat',0);
}
function telegram($msg,$silent=true,$to='Guy')
{
for($x=1;$x<=100;$x++)
{
$result=json_decode(curl('https://api.telegram.org/bot113592115:AAEZ-xCRhO-123456789_3YIr9irxI/sendMessage?chat_id=12345678&text='.$msg.'&disable_notification='.$silent,true));
if(isset($result->ok))
if($result->ok===true)
{
lg('telegram sent to Guy: '.$msg);
break;
}
else
{
lg('telegram sent failed');
}
sleep($x*3);
}
if($to=='Kirby')
for($x=1;$x<=100;$x++)
{
$result=json_decode(curl('https://api.telegram.org/bot113592115:AAEZ-xCRhO-123456789_3YIr9irxI/sendMessage?chat_id=2345678&text='.$msg.'&disable_notification='.$silent,true));
if(isset($result->ok))
if($result->ok===true)
{
lg('telegram sent to Kirby: '.$msg);
break;
}
else
{
lg('telegram sent failed');
}
sleep($x*3);
}
}
function lg($msg)
{
curl('http://127.0.0.1:8084/json.htm?type=command¶m=addlogmessage&message='.urlencode('=> '.$msg));
}
function ios($msg)
{
$appledevice='123456789EeBN1nZk0sD/ZHxYptWl12345678905kSRqROHYVNSUzmWV';
$appleid='your@apple.id';
$applepass='Y0ur@ppleP@ssw0rd';
require_once("findmyiphone.php");
$fmi=new FindMyiPhone($appleid,$applepass);
$fmi->playSound($appledevice,$msg);
sms($msg);
}
function sms($msg)
{
if(1==2)
{
$smsuser='clickatelluser';
$smspassword='clickatellpassword';
$smsapi=12345678;
$smstofrom=32412345678;
curl('http://api.clickatell.com/http/sendmsg?user='.$smsuser.'&password='.$smspassword.'&api_id='.$smsapi.'&to='.$smstofrom.'&text='.urlencode($msg).'&from='.$smstofrom.'');
usleep(500000);
}
}
function pingDomain($domain,$port)
{
$file=fsockopen($domain,$port,$errno,$errstr,10);
$status=0;
if(!$file)
$status=-1;
else
{
fclose($file);
$status=1;
}
return $status;
}
function RefreshZwave($node)
{
$last=cget('time-RefreshZwave'.$node);
cset('time-RefreshZwave'.$node,time);
if($last<time-10)
{
$devices=json_decode(file_get_contents('http://127.0.0.1:8084/json.htm?type=openzwavenodes&idx=3'),true);
foreach($devices['result'] as $devozw)
if($devozw['NodeID']==$node)
{
$device=$devozw['Description'].' '.$devozw['Name'];
break;
}
lg(' > Refreshing node '.$node.' '.$device);
for($k=1;$k<=5;$k++)
{
/* ControllerBusy(20);*/
$result=file_get_contents('http://127.0.0.1:8084/ozwcp/refreshpost.html',false,stream_context_create(array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query(array('fun'=>'racp','node'=>$node)),),)));
if($result==='OK')
{
cset('timerefresh-'.$device,time);
break;
}
sleep(1);
}
if(cget('timedeadnodes')<time-298)
{
cset('timedeadnodes',time);
foreach($devices as $node=>$data)
{
if($node=="result")
{
foreach($data as $index=>$eltsNode)
{
if($eltsNode["State"]=="Dead"&&!in_array($eltsNode['NodeID'],array(57)))
{
telegram('Node '.$eltsNode['NodeID'].' '.$eltsNode['Description'].' ('.$eltsNode['Name'].') marked as dead, reviving '.ZwaveCommand($eltsNode['NodeID'],'HasNodeFailed'));
ControllerBusy(10);
ZwaveCommand(1,'Cancel');
}
}
}
}
}
}
}
function Zwavecancelaction()
{
file_get_contents('http://127.0.0.1:8084/ozwcp/admpost.html',false,stream_context_create(array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query(array('fun'=>'cancel')),),)));
}
function ZwaveCommand($node,$command)
{
$cm=array('AssignReturnRoute'=>'assrr','DeleteAllReturnRoutes'=>'delarr','NodeNeighbourUpdate'=>'reqnnu','RefreshNodeInformation'=>'refreshnode','RequestNetworkUpdate'=>'reqnu','HasNodeFailed'=>'hnf','Cancel'=>'cancel');
$cm=$cm[$command];
for($k=1;$k<=5;$k++)
{
$result=file_get_contents('http://pass2php:pass2php@127.0.0.1:8084/ozwcp/admpost.html',false,stream_context_create(array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query(array('fun'=>$cm,'node'=>'node'.$node)),),)));
if($result=='OK')
break;
sleep(1);
}
return $result;
}
function ControllerBusy($retries)
{
for($k=1;$k<=$retries;$k++)
{
$result=file_get_contents('http://127.0.0.1:8084/ozwcp/poll.xml');
$p=xml_parser_create();
xml_parse_into_struct($p,$result,$vals,$index);
xml_parser_free($p);
foreach($vals as $val)
{
if($val['tag']=='ADMIN')
{
$result=$val['attributes']['ACTIVE'];
break;
}
}
if($result=='false')
break;
if($k==$retries)
{
ZwaveCommand(1,'Cancel');
break;
}
sleep(1);
}
}
function convertToHours($time)
{
if($time<600)
return substr(strftime('%M:%S',$time),1);
elseif($time>=600&&$time<3600)
return strftime('%M:%S',$time);
else
return strftime('%k:%M:%S',$time);
}
function curl($url)
{
$headers=array('Content-Type: application/json');
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($ch,CURLOPT_TIMEOUT,5);
$data=curl_exec($ch);
curl_close($ch);
return $data;
}
function cset($key,$value)
{
if(!$m=xsMemcached::Connect('127.0.0.1',11211))
{
die('Memcache failed to connect.');
}
$m->Set($key,$value);
}
function cget($key)
{
if(!$m=xsMemcached::Connect('127.0.0.1',11211))
{
die('Memcache failed to connect.');
}
return $m->Get($key);
}
class xsMemcached
{
private $Host;
private $Port;
private $Handle;
public static function Connect($Host,$Port,$Timeout=5)
{
$Ret=new self();
$Ret->Host=$Host;
$Ret->Port=$Port;
$ErrNo=$ErrMsg=NULL;
if(!$Ret->Handle=@fsockopen($Ret->Host,$Ret->Port,$ErrNo,$ErrMsg,$Timeout))
return false;
return $Ret;
}
public function Set($Key,$Value,$TTL=0)
{
return $this->SetOp($Key,$Value,$TTL,'set');
}
public function Get($Key)
{
$this->WriteLine('get '.$Key);
$Ret='';
$Header=$this->ReadLine();
if($Header=='END')
{
$Ret=0;
$this->SetOp($Key,0,0,'set');
return $Ret;
}
while(($Line=$this->ReadLine())!='END')
$Ret.=$Line;
if($Ret=='')
return false;
$Header=explode(' ',$Header);
if($Header[0]!='VALUE'||$Header[1]!=$Key)
throw new Exception('unexcpected response format');
$Meta=$Header[2];
$Len=$Header[3];
return $Ret;
}
public function Quit()
{
$this->WriteLine('quit');
}
private function SetOp($Key,$Value,$TTL,$Op)
{
$this->WriteLine($Op.' '.$Key.' 0 '.$TTL.' '.strlen($Value));
$this->WriteLine($Value);
return $this->ReadLine()=='STORED';
}
private function WriteLine($Command,$Response=false)
{
fwrite($this->Handle,$Command."\r\n");
if($Response)
return $this->ReadLine();
return true;
}
private function ReadLine()
{
return rtrim(fgets($this->Handle),"\r\n");
}
private function __construct()
{
}
}