This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmodule.php
135 lines (116 loc) · 4.26 KB
/
module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?
class Geofency extends IPSModule
{
public function Create()
{
//Never delete this line!
parent::Create();
$this->RegisterPropertyString("Username", "");
$this->RegisterPropertyString("Password", "");
}
public function ApplyChanges()
{
//Never delete this line!
parent::ApplyChanges();
$sid = $this->RegisterScript("Hook", "Hook", "<? //Do not delete or modify.\ninclude(IPS_GetKernelDirEx().\"scripts/__ipsmodule.inc.php\");\ninclude(\"../modules/SymconMisc/Geofency/module.php\");\n(new Geofency(".$this->InstanceID."))->ProcessHookData();");
$this->RegisterHook("/hook/geofency", $sid);
}
private function RegisterHook($Hook, $TargetID)
{
$ids = IPS_GetInstanceListByModuleID("{015A6EB8-D6E5-4B93-B496-0D3F77AE9FE1}");
if(sizeof($ids) > 0) {
$hooks = json_decode(IPS_GetProperty($ids[0], "Hooks"), true);
$found = false;
foreach($hooks as $index => $hook) {
if($hook['Hook'] == "/hook/geofency") {
if($hook['TargetID'] == $TargetID)
return;
$hooks[$index]['TargetID'] = $TargetID;
$found = true;
}
}
if(!$found) {
$hooks[] = Array("Hook" => "/hook/geofency", "TargetID" => $TargetID);
}
IPS_SetProperty($ids[0], "Hooks", json_encode($hooks));
IPS_ApplyChanges($ids[0]);
}
}
/**
* This function will be available automatically after the module is imported with the module control.
* Using the custom prefix this function will be callable from PHP and JSON-RPC through:
*
* GEO_ProcessHookData($id);
*
*/
public function ProcessHookData()
{
if($_IPS['SENDER'] == "Execute") {
echo "This script cannot be used this way.";
return;
}
if((IPS_GetProperty($this->InstanceID, "Username") != "") || (IPS_GetProperty($this->InstanceID, "Password") != "")) {
if(!isset($_SERVER['PHP_AUTH_USER']))
$_SERVER['PHP_AUTH_USER'] = "";
if(!isset($_SERVER['PHP_AUTH_PW']))
$_SERVER['PHP_AUTH_PW'] = "";
if(($_SERVER['PHP_AUTH_USER'] != IPS_GetProperty($this->InstanceID, "Username")) || ($_SERVER['PHP_AUTH_PW'] != IPS_GetProperty($this->InstanceID, "Password"))) {
header('WWW-Authenticate: Basic Realm="Geofency WebHook"');
header('HTTP/1.0 401 Unauthorized');
echo "Authorization required";
return;
}
}
if(!isset($_POST['device']) || !isset($_POST['id']) || !isset($_POST['name'])) {
IPS_LogMessage("Geofency", "Malformed data: ".print_r($_POST, true));
return;
}
$deviceID = $this->CreateInstanceByIdent($this->InstanceID, $this->ReduceGUIDToIdent($_POST['device']), "Device");
SetValue($this->CreateVariableByIdent($deviceID, "Latitude", "Latitude", 2), floatval($_POST['latitude']));
SetValue($this->CreateVariableByIdent($deviceID, "Longitude", "Longitude", 2), floatval($_POST['longitude']));
SetValue($this->CreateVariableByIdent($deviceID, "Timestamp", "Timestamp", 1, "~UnixTimestamp"), intval(strtotime($_POST['date'])));
SetValue($this->CreateVariableByIdent($deviceID, $this->ReduceGUIDToIdent($_POST['id']), utf8_decode($_POST['name']), 0, "~Presence"), intval($_POST['entry']) > 0);
}
private function ReduceGUIDToIdent($guid) {
return str_replace(Array("{", "-", "}"), "", $guid);
}
private function CreateCategoryByIdent($id, $ident, $name)
{
$cid = @IPS_GetObjectIDByIdent($ident, $id);
if($cid === false)
{
$cid = IPS_CreateCategory();
IPS_SetParent($cid, $id);
IPS_SetName($cid, $name);
IPS_SetIdent($cid, $ident);
}
return $cid;
}
private function CreateVariableByIdent($id, $ident, $name, $type, $profile = "")
{
$vid = @IPS_GetObjectIDByIdent($ident, $id);
if($vid === false)
{
$vid = IPS_CreateVariable($type);
IPS_SetParent($vid, $id);
IPS_SetName($vid, $name);
IPS_SetIdent($vid, $ident);
if($profile != "")
IPS_SetVariableCustomProfile($vid, $profile);
}
return $vid;
}
private function CreateInstanceByIdent($id, $ident, $name, $moduleid = "{485D0419-BE97-4548-AA9C-C083EB82E61E}")
{
$iid = @IPS_GetObjectIDByIdent($ident, $id);
if($iid === false)
{
$iid = IPS_CreateInstance($moduleid);
IPS_SetParent($iid, $id);
IPS_SetName($iid, $name);
IPS_SetIdent($iid, $ident);
}
return $iid;
}
}
?>