This repository was archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmodule.php
More file actions
123 lines (95 loc) · 3.95 KB
/
Copy pathmodule.php
File metadata and controls
123 lines (95 loc) · 3.95 KB
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
<?
include __DIR__ . "/../libs/WebHookModule.php";
class EgiGeoZone extends WebHookModule {
public function __construct($InstanceID) {
parent::__construct($InstanceID, "egigeozone");
}
public function Create() {
//Never delete this line!
parent::Create();
//Properties
$this->RegisterPropertyString("Username", "");
$this->RegisterPropertyString("Password", "");
}
public function ApplyChanges() {
//Never delete this line!
parent::ApplyChanges();
//Cleanup old hook script
$id = @IPS_GetObjectIDByIdent("Hook", $this->InstanceID);
if($id > 0) {
IPS_DeleteScript($id, true);
}
}
/**
* This function will be called by the hook control. Visibility should be protected!
*/
protected function ProcessHookData() {
//Never delete this line!
parent::ProcessHookData();
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($_GET['device']) || !isset($_GET['id']) || !isset($_GET['name'])) {
$this->SendDebug("EgiGeoZone", "Malformed data: ".print_r($_GET, true), 0);
return;
}
//Adding a missing '+' in front of the timezone to get a strtotime() convertable string
//Example:
//"2016-08-12T17:40:13 0000" => "2016-08-12T17:40:13 +0000"
if(strlen($_GET['date']) == 24){
$_GET['date'] = implode("+", str_split($_GET['date'], 20));
}
$deviceID = $this->CreateInstanceByIdent($this->InstanceID, $this->ReduceGUIDToIdent($_GET['device']), "Device");
SetValue($this->CreateVariableByIdent($deviceID, "Latitude", "Latitude", 2), floatval($_GET['latitude']));
SetValue($this->CreateVariableByIdent($deviceID, "Longitude", "Longitude", 2), floatval($_GET['longitude']));
SetValue($this->CreateVariableByIdent($deviceID, "Timestamp", "Timestamp", 1, "~UnixTimestamp"), intval(strtotime($_GET['date'])));
SetValue($this->CreateVariableByIdent($deviceID, $this->ReduceToAllowedIdent($_GET['name']), utf8_decode($_GET['name']), 0, "~Presence"), intval($_GET['entry']) > 0);
}
private function ReduceGUIDToIdent($guid) {
return str_replace(Array("{", "-", "}"), "", $guid);
}
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;
}
//Replaces all unallowed Chars of a String with "_"
//Allowed Chars: "a..z", "A..Z", "_", "0..9"
private function ReduceToAllowedIdent($String) {
for($i = 0; $i < strlen($String) ; $i++) {
$val = ord($String[$i]);
// Between (1..9) Between (A..Z) Underscore (_) Between (a..z)
if (!(($val >= 48 && $val <= 57) || ($val >= 65 && $val <= 90) || ($val == 95) || ($val >= 97 && $val <= 122))) {
$String[$i] = "_";
}
}
return $String;
}
}
?>