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
126 lines (107 loc) · 4.2 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
<?
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();
$this->RegisterHook("/hook/geofency");
}
private function RegisterHook($WebHook) {
$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'] == $WebHook) {
if($hook['TargetID'] == $this->InstanceID)
return;
$hooks[$index]['TargetID'] = $this->InstanceID;
$found = true;
}
}
if(!$found) {
$hooks[] = Array("Hook" => $WebHook, "TargetID" => $this->InstanceID);
}
IPS_SetProperty($ids[0], "Hooks", json_encode($hooks));
IPS_ApplyChanges($ids[0]);
}
}
/**
* This function will be called by the hook control. Visibility should be protected!
*/
protected 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'])) {
$this->SendDebug("Geofency", "Malformed data: ".print_r($_POST, true), 0);
return;
}
$this->SendDebug("GeoFency", "Array POST: ".print_r($_POST, true), 0);
$deviceID = $this->CreateInstanceByIdent($this->InstanceID, $this->ReduceGUIDToIdent($_POST['device']), "Device");
SetValue($this->CreateVariableByIdent($deviceID, "Latitude", "Latitude", 2), $this->ParseFloat($_POST['latitude']));
SetValue($this->CreateVariableByIdent($deviceID, "Longitude", "Longitude", 2), $this->ParseFloat($_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;
}
private function ParseFloat($floatString) {
$LocaleInfo = localeconv();
$floatString = str_replace(".", $LocaleInfo["decimal_point"], $floatString);
$floatString = str_replace(",", $LocaleInfo["decimal_point"], $floatString);
return floatval($floatString);
}
}
?>