-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalls.php
More file actions
108 lines (99 loc) · 3.36 KB
/
Copy pathcalls.php
File metadata and controls
108 lines (99 loc) · 3.36 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
<?php
/**
* Created by PhpStorm.
* User: emclain
* Date: 2/15/14
* Time: 1:01 PM
*
* This script is here to handle the callbacks of the client - primarily to store and retrieve the sensor
* locations on the map and handle map specific data.
*/
define('CONFIG_DIR', 'configs/');
if ( is_file(CONFIG_DIR.'sitedefaults.php'))
require_once(CONFIG_DIR.'sitedefaults.php');
if ( !isset($_REQUEST['cmd']) ) {
print "ERROR: No CMD\n";
exit;
}
if ( !isset($_REQUEST['screen']) ) {
print "ERROR: Screen is invalid\n";
exit;
}
if ( !isset($editPasswords) || !is_array($editPasswords) )
$editPasswords = array();
switch ( $_REQUEST['cmd'] ) {
case "saveLocation":
if ( !isset($_GET['sensor']) || !isset($_GET['top']) || !isset($_GET['left']) ) {
print "ERROR: locations invalid\n";
exit;
}
$screenName = $_GET['screen'];
$allowedToEdit = true;
if ( array_key_exists($screenName, $editPasswords) ) {
if ( isset($_GET['password']) && ($_GET['password'] == $editPasswords[$screenName]) ) {
$allowedToEdit = true;
} else {
$allowedToEdit = false;
}
}
if ( $allowedToEdit == true ) {
$screenConfig = json_decode(getScreenConfig($screenName), true);
$screenConfig['locations'][$_GET['sensor']] = array('top' => $_GET['top'], 'left' => $_GET['left']);
saveScreenConfig($screenName, json_encode($screenConfig));
}
break;
case "get":
$screenConfig = getScreenConfig($_GET['screen']);
sendJson($screenConfig);
break;
case "fetchData":
$screenConfig = getArrayScreenConfig($_GET['screen']);
if ( !array_key_exists('serverURL', $screenConfig) ) {
sendJson(array('ERROR'=>'Config missing serverURL'));
exit;
}
if ( ($jsonData = file_get_contents($screenConfig['serverURL'])) === FALSE ) {
sendJson(array('ERROR'=>'Failed to retrieve data'));
exit;
}
sendJson($jsonData);
break;
case "fetchGraph":
$screenConfig = getArrayScreenConfig($_GET['screen']);
if (!array_key_exists('graphiteURL', $screenConfig)) {
print "Error: Config missing graphiteURL<br />\n";
exit;
}
print file_get_contents($screenConfig['graphiteURL'].'?target='.$_GET['sensor'].$graphitePrefs);
break;
default:
print "ERROR: Invalid CMD\n";
exit;
}
function sendJson($jsonToSend) {
header('Content-Type: application/json');
if ( is_array($jsonToSend) ) {
echo json_encode($jsonToSend);
} else {
echo $jsonToSend;
}
}
function getScreenConfig($screenName) {
$fileName = CONFIG_DIR.$screenName.'_screen.php';
if ( !is_file($fileName) ) {
print "ERROR: Screen requested does not exist! $screenName\n";
exit;
}
return(file_get_contents($fileName));
}
function getArrayScreenConfig($screenName) {
return(json_decode(getScreenConfig($screenName), true));
}
function saveScreenConfig($screenName, $screenConfig) {
$fileName = CONFIG_DIR.$screenName.'_screen.php';
if ( !is_file($fileName) ) {
print "ERROR: Screen requested does not exist!\n";
exit;
}
file_put_contents($fileName, $screenConfig) or die ("Failed saving screen data");
}