-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathinput_controller.php
More file actions
206 lines (183 loc) · 8.56 KB
/
input_controller.php
File metadata and controls
206 lines (183 loc) · 8.56 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
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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
function input_controller()
{
global $mysqli, $redis, $user, $session, $route, $settings, $param, $device, $path;
// requires at least read access
if (!isset($session['read'])) return false;
if (!$session['read']) return false;
$result = false;
require_once "Modules/feed/feed_model.php";
$feed = new Feed($mysqli,$redis, $settings["feed"]);
require_once "Modules/input/input_model.php";
$input = new Input($mysqli,$redis, $feed);
require_once "Modules/process/process_model.php";
$process = new Process($mysqli,$input,$feed,$user->get_timezone($session['userid']));
if (!$device) {
if (file_exists("Modules/device/device_model.php")) {
require_once "Modules/device/device_model.php";
$device = new Device($mysqli,$redis);
}
}
require_once "Modules/input/input_methods.php";
$inputMethods = new InputMethods($mysqli,$redis,$user,$input,$feed,$process,$device);
// Change default route to json
$route->format = "json";
// Write access level
if ($session["write"])
{
// ------------------------------------------------------------------------
// input/post
// ------------------------------------------------------------------------
if ($route->action == "post") {
$result = $inputMethods->post($session['userid']);
if ($result=="ok") {
if ($param->exists('fulljson')) $result = '{"success": true}';
if ($param->sha256base64_response) $result = $param->sha256base64_response;
} else {
$result = '{"success": false, "message": "'.str_replace("\"","'",$result).'"}';
$log = new EmonLogger(__FILE__);
$log->error($result." for User: ".$session['userid']);
}
return $result;
}
// ------------------------------------------------------------------------
// input/bulk
// ------------------------------------------------------------------------
elseif ($route->action == 'bulk') {
$result = $inputMethods->bulk($session['userid']);
if ($result=="ok") {
if ($param->exists('fulljson')) {
$result = '{"success": true}';
}
if ($param->sha256base64_response) {
$result = $param->sha256base64_response;
}
} else {
$result = '{"success": false, "message": "'.str_replace("\"","'",$result).'"}';
$log = new EmonLogger(__FILE__);
$log->error($result." for User: ".$session['userid']);
}
return $result;
}
// ------------------------------------------------------------------------
elseif ($route->action == "clean") {
$route->format = 'text';
return $input->clean($session['userid']);
} elseif ($route->action == "cleanprocesslistfeeds") {
$route->format = 'text';
return $input->clean_processlist_feeds($process,$session['userid']);
} elseif ($route->action == "set-node-input-descriptions") {
if (isset($_GET['node']) && isset($_GET['names'])) {
return $input->set_node_input_descriptions($session['userid'],$_GET['node'],$_GET['names']);
}
} elseif (isset($_GET['inputid']) && $input->belongs_to_user($session['userid'],get("inputid"))) {
if ($route->action == 'set') {
return $input->set_fields(get('inputid'),get('fields'));
} elseif ($route->action == "delete") {
return $input->delete($session['userid'],get("inputid"));
} elseif ($route->action == "process") {
if ($route->subaction == "get") {
return $input->get_processlist(get("inputid"));
} elseif ($route->subaction == "set") {
return $input->set_processlist($session['userid'], get('inputid'), post('processlist'),$process);
} elseif ($route->subaction == "reset") {
return $input->reset_processlist(get("inputid"));
}
}
}
// Multiple input actions - permissions are checked within model
elseif (isset($_GET['inputids'])) {
if ($route->action == "delete") {
$inputids = json_decode(get('inputids'));
if ($inputids!=null) {
return $input->delete_multiple($session['userid'],$inputids);
}
}
}
// Enable and disable input creation for session user
elseif ($route->action == "disable") {
return $input->disable_input_creation($session['userid']);
} elseif ($route->action == "enable") {
return $input->enable_input_creation($session['userid']);
} elseif ($route->action == "isdisabled") {
return $input->is_creation_disabled($session['userid']);
}
// -------------------------------------------------------------------------
// HTML Web pages
// -------------------------------------------------------------------------
elseif ($route->action == 'api') {
$route->format = "html";
textdomain("messages");
return view("Modules/input/Views/input_api.php", array());
} elseif ($route->action == 'view') {
$route->format = "html";
textdomain("messages");
$device_module = false;
if (file_exists("Modules/device")) {
$device_module = true;
}
return view("Modules/input/Views/input_view.php", array(
'path' => $path,
'device_module' => $device_module,
'feedviewpath' => $settings['interface']['feedviewpath']
));
}
}
// Read access
// --------------------------------------------
// Fetch inputs by node and node variable names
// --------------------------------------------
// input/get full list
// input/get?node=emontx {"power1":{"time":0,"value":0},"power2":{"time":0,"value":0},"power3":{"time":0,"value":0}}
// input/get/emontx {"power1":{"time":0,"value":0},"power2":{"time":0,"value":0},"power3":{"time":0,"value":0}}
// input/get?node=emontx&name=power1 {"time":0,"value":0}
// input/get/emontx/power1 {"time":0,"value":0}
if ($route->action == "get") {
$dbinputs = $input->get_inputs_v2($session['userid']);
if (!$route->subaction && !isset($_GET['node'])) {
return $dbinputs;
} else {
// Node
if ($route->subaction) { $nodeid = $route->subaction; } else { $nodeid = get('node'); }
$nodeid = preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$nodeid);
// If no node variable name specified return all node variables
if (!$route->subaction2 && !isset($_GET['name'])) {
if (isset($dbinputs[$nodeid])) {
return $dbinputs[$nodeid];
} else {
return "Node does not exist";
}
} else {
// Property
if ($route->subaction2) { $name = $route->subaction2; } else { $name = get('name'); }
$name = preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$name);
if (isset($dbinputs[$nodeid])) {
if (isset($dbinputs[$nodeid][$name])) {
return $dbinputs[$nodeid][$name];
} else {
return "Node variable does not exist";
}
} else {
return "Node does not exist";
}
}
}
} elseif ($route->action == "list") {
return $input->getlist($session['userid']);
} elseif ($route->action == "getinputs") {
return $input->get_inputs($session['userid']);
} elseif ($route->action == "get_inputs") {
return $input->get_inputs($session['userid']);
}
return array('content'=>$result);
}