forked from sktaylortrash/TCPLightingWebInterface-MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude.php
234 lines (202 loc) · 6.91 KB
/
include.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/*
*
* PHP includes
*
*/
error_reporting(0);
include "config.inc.php";
function isLocalIPAddress($IPAddress){
if($IPAddress == '127.0.0.1'){return true;}
return ( !filter_var($IPAddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) );
}
$REMOTE_IP = getenv('HTTP_CLIENT_IP')?: getenv('HTTP_X_FORWARDED_FOR')?: getenv('HTTP_X_FORWARDED')?: getenv('HTTP_FORWARDED_FOR')?: getenv('HTTP_FORWARDED')?: getenv('REMOTE_ADDR');
$CURRENT_PAGE_SCRIPT = basename($_SERVER["SCRIPT_FILENAME"], '.php');
/*Function to Print Array*/
function pa($array){
echo '<pre>'.print_r($array,true).'</pre>';
}
function getCurlReturn($postDataString){
$URL = SCHEME."://".LIGTHING_BRIDGE_IP.":".LIGHTING_BRIDGE_PORT."/gwr/gop.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataString);
if (SCHEME == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function xmlToArray($string){
$xml = simplexml_load_string($string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
return $array;
}
function getDevices(){
$CMD = "cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>".TOKEN."</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
$DATA = $array["gwrcmd"]["gdata"]["gip"]["room"];
$DEVICES = array();
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
foreach($DATA as $room){
if( ! is_array($room["device"]) ){
//$DEVICES[] = $room["device"]; //singular device in a room
}else{
$device = (array)$room["device"];
if( isset($device["did"]) ){
//item is singular device
$DEVICES[] = $room["device"];
}else{
for( $x = 0; $x < sizeof($device); $x++ ){
if( isset($device[$x]) && is_array($device[$x]) && ! empty($device[$x]) ){
$DEVICES[] = $device[$x];
}
}
}
}
}
return $DEVICES;
}
function getDevicesMQTT(){
$CMD = "cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>".TOKEN."</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
$DATA = $array["gwrcmd"]["gdata"]["gip"]["room"];
$DEVICES = array();
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
foreach($DATA as $room){
if( ! is_array($room["device"]) ){
//$DEVICES[] = $room["device"]; //singular device in a room
}else{
$device = (array)$room["device"];
if( isset($device["did"]) ){
//item is singular device
$DEVICES[] = $room["device"];
}else{
for( $x = 0; $x < sizeof($device); $x++ ){
if( isset($device[$x]) && is_array($device[$x]) && ! empty($device[$x]) ){
$DEVICES[] = $device[$x];
}
}
}
}
}
return $DEVICES;
}
function pageHeader($title){
global $CURRENT_PAGE_SCRIPT;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>TCP Control Script</title>
<link rel="apple-touch-icon" sizes="180x180" href="css/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" href="css/favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="css/favicons/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="css/favicons/manifest.json">
<link rel="mask-icon" href="css/favicons/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="css/favicons/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="apple-mobile-web-app-title" content="TCP Lighting">
<meta name="application-name" content="TCP Lighting">
<meta name="msapplication-config" content="css/favicons/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" href="css/jquery-ui.min.css" />
<link rel="stylesheet" href="css/style.css" />
<script src="js/libs.js"></script>
<script>
//local IP
<?php
if( USE_LOCAL_API_IP ){
global $REMOTE_IP;
if( isLocalIPAddress($REMOTE_IP) ){
?>
var API_IP = '<?php echo LOCAL_URL; ?>';
<?php
}else{
?>
var API_IP = '<?php echo $_SERVER['REQUEST_URI']; ?>';
<?php
}
}else{
?>
var API_IP = '<?php echo $_SERVER['REQUEST_URI']; ?>';
<?php
}
?>
</script>
<script src="js/scripts.js"></script>
<title><?php echo $title; ?></title>
</head>
<body>
<div id="headerBar">
<!-- check if not index -->
<?php if( $CURRENT_PAGE_SCRIPT != "index" ){ ?>
<a href="index.php">Home</a>
<a href="index.php#scenes">Scenes</a>
<?php }else{ ?>
<a href="#">Home</a>
<a href="#scenes">Scenes</a>
<?php } ?>
<a target="_blank" href="https://github.com/sktaylortrash/TCPLightingWebInterface-MQTT">GitHub Link</a>
<!-- <a target="_blank" href="https://github.com/bren1818/TCPLightingWebInterface/wiki">Wiki</a>-->
<a target="_blank" href="MQTTGenerator.php">MQTT Script Generator</a>
<a target="_blank" href="mqttstate.php">Publish Current Device States</a>
<a href="queryBuilder.php">IFTTT Query Builder</a>
<?php if( LOG_ACTIONS == 1 || LOG_API_CALLS == 1){
echo '<a href="viewLogs.php">View Logs</a>';
}
?>
<div class="clear"></div>
</div>
<?php
}
function pageFooter(){
?>
<div id="toolBar"><a href="scheduler.php">Lighting Scheduler</a> | <a href="createDevice.php">Create Virtual Device</a> | <a href="setDateTime.php">Set Bridge Date Time</a> | <a href="discoverBulbs.php">Search for New Bulbs. (Beta)</a> </div>
</body>
</html>
<?php
}
if( ! file_exists(LOG_DIR) ){
mkdir( LOG_DIR, 0755, true);
}
function APILog($string){
if( LOG_API_CALLS == 1 ){
file_put_contents( LOG_DIR . DIRECTORY_SEPARATOR . "API-Request.log", date('Y-m-d H:i:s '). $string . "\n", FILE_APPEND | LOCK_EX);
}
}
function SCHEDLog($string){
if( LOG_ACTIONS == 1 ){
file_put_contents( LOG_DIR . DIRECTORY_SEPARATOR . "Schedule-Actioned.log", date('Y-m-d H:i:s '). $string . "\n", FILE_APPEND | LOCK_EX);
}
}
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
/*do some checks based on parameters above*/
if( ALLOW_EXTERNAL_API_ACCESS == 1 ){
//allow external
}else{
//if not allowing external access, check you're a local IP
if( ! isLocalIPAddress($REMOTE_IP) ){
//if not local you can only hit the API
if( $CURRENT_PAGE_SCRIPT != "api" ){
echo "This application is restricted to the internal network.";
exit;
}
}
}
?>