-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathfeed_controller.php
369 lines (330 loc) · 17.5 KB
/
feed_controller.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?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 feed_controller()
{
global $mysqli, $redis, $user, $session, $route, $settings;
$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";
if (!$user_timezone = $user->get_timezone($session['userid'])) {
$user_timezone = 'UTC';
}
$process = new Process($mysqli,$input,$feed,$user_timezone);
// Specialised 2 way feed sync API
if ($route->action == "sync" && $session['write']) {
$route->format = "json";
return $feed->sync($session['userid'],file_get_contents('php://input'));
}
if ($route->format == 'html')
{
if ($route->action=="") {
$route->action = "view";
}
textdomain("messages");
if (($route->action == "view" || $route->action == "list")) {
if (!$session['read'] && !$session['public_userid']) {
return "";
}
return view("Modules/feed/Views/feedlist_view.php");
} elseif ($route->action == "api") {
if (!$session['read'] && !$session['public_userid']) {
return "";
}
require "Modules/feed/feed_api_obj.php";
return view("Lib/api_tool_view.php",array("title"=>_("Feed API"), "api"=>feed_api_obj(), "selected_api"=>8));
} elseif (!$session['read']) {
return ''; // empty strings force user back to login
}
else {
return EMPTY_ROUTE;
} // this string displays error
}
elseif ($route->format == 'json')
{
// Public actions available on public feeds.
if ($route->action == "list")
{
if ($session['public_userid']) {
return $feed->get_user_public_feeds($session['public_userid'],get("meta",false,0));
} elseif (isset($_GET['userid'])) {
return $feed->get_user_public_feeds((int)$_GET['userid'],get("meta",false,0));
} elseif ($session['read']) {
return $feed->get_user_feeds($session['userid'],get("meta",false,0));
} else {
return false;
}
} elseif ($route->action == "listwithmeta" && $session['read']) {
return $feed->get_user_feeds($session['userid'],1);
} elseif ($route->action == "getid" && $session['read']) {
$route->format = "text";
if (isset($_GET["tag"]) && isset($_GET["name"])) {
return $feed->exists_tag_name($session['userid'],get("tag"),get("name"));
} elseif (isset($_GET["name"])) {
return $feed->get_id($session['userid'],get("name"));
} else {
return false;
}
} elseif ($route->action == "create" && $session['write']) {
return $feed->create($session['userid'],get('tag'),get('name'),get('engine'),json_decode(get('options')),get('unit'));
} elseif ($route->action == "updatesize" && $session['write']) {
return $feed->update_user_feeds_size($session['userid']);
} elseif ($route->action == "buffersize" && $session['write']) {
return $feed->get_buffer_size();
// To "fetch" multiple feed values in a single request
// http://emoncms.org/feed/fetch.json?ids=123,567,890
} elseif ($route->action == "fetch") {
$result = [];
$feedids = (array) (explode(",",(get('ids'))));
for ($i=0; $i<count($feedids); $i++) {
$feedid = (int) $feedids[$i];
if ($feed->exist($feedid)) { // if the feed exists
$f = $feed->get($feedid);
if ($f['public'] || ($session['userid']>0 && $f['userid']==$session['userid'] && $session['read'])) {
$result[$i] = $feed->get_value($feedid); // null is a valid response
} else { $result[$i] = false; }
} else { $result[$i] = false; } // false means feed not found
}
return $result;
// ----------------------------------------------------------------------------
// Multi feed actions
// ----------------------------------------------------------------------------
} elseif (in_array($route->action,array("data","average","csvexport"))) {
// get data for a list of existing feeds
$result = array('success'=>false, 'message'=>'bad parameters');
// return $_REQUEST;
$singular = false;
$feedids = array();
$results = array();
if (isset($_GET['id'])) {
$feedids = explode(",", get('id'));
$singular = true;
} elseif (isset($_GET['ids'])) {
$feedids = explode(",", get('ids'));
}
$start = get('start',true);
$end = get('end',true);
$interval = get('interval',false,0);
$timezone = get('timezone',false,$user_timezone);
$timeformat = get('timeformat',false,'unixms');
$csv = get('csv',false,0);
$skipmissing = get('skipmissing',false,0);
$limitinterval = get('limitinterval',false,0);
$dp = get('dp',false,-1);
$averages = array();
if (isset($_GET['average'])) {
$averages = explode(",",get('average'));
}
$deltas = array();
if (isset($_GET['delta'])) {
$deltas = explode(",",get('delta'));
}
// Backwards compatibility
if ($route->action=="average") $average = 1; else $average = 0;
if ($route->action=="csvexport") $csv = 1;
if (isset($_GET['mode'])) $interval = $_GET['mode'];
$multi_csv = false;
if ($csv && count($feedids)>1) {
$csv = false;
$multi_csv = true;
}
if (!empty($feedids)) {
$missing = array();
foreach($feedids as $index => $feedid) {
if ($feed->exist($feedid)) { // if the feed exists
$f = $feed->get($feedid);
// if public or belongs to user
if ($f['public'] || ($session['userid']>0 && $f['userid']==$session['userid'] && $session['read']))
{
$results[$index] = array('feedid'=>$feedid);
if (!isset($_GET['split'])) {
if (isset($averages[$index]) && $averages[$index]) $average = $averages[$index];
if (isset($deltas[$index]) && $deltas[$index]) $delta = $deltas[$index]; else $delta = 0;
$results[$index]['data'] = $feed->get_data($feedid,$start,$end,$interval,$average,$timezone,$timeformat,$csv,$skipmissing,$limitinterval,$delta,$dp);
} else {
$results[$index]['data'] = $feed->get_data_DMY_time_of_day($feedid,$start,$end,$interval,$timezone,$timeformat,get('split'));
}
}
} else {
$missing[] = intval($feedid); //add feed id to array of missing ids
}
}
if (!empty($missing)) {
// return error if any feed ids not found
if (count($missing) === 1) // if just one feed not found, return its id
return array('success'=>false, 'message'=> "feed $missing[0] does not exist", 'feeds' => $missing);
else
return array('success'=>false, 'message'=> count($missing) .' feeds do not exist', 'feeds' => $missing);
} else {
if ($singular && count($results)==1) {
return $results[0]['data'];
} else {
if ($multi_csv) {
return $feed->csv_export_multi($feedids,$results,$timezone,$timeformat);
} else {
return $results;
}
}
// @todo: return array for each feed's data
// and a single array for each interval timestamp
}
} else {
// no ids passed
return array('success'=>false, 'message'=>'no ids given');
}
return $result;
// ----------------------------------------------------------------------------
} else {
$feedid = (int) get('id');
// Actions that operate on a single existing feed that all use the feedid to select:
// First we load the meta data for the feed that we want
if ($feed->exist($feedid)) // if the feed exists
{
$f = $feed->get($feedid);
// if public or belongs to user
if ($f['public'] || ($session['userid']>0 && $f['userid']==$session['userid'] && $session['read']))
{
if ($route->action == "timevalue") {
return $feed->get_timevalue($feedid);
} elseif ($route->action == "value") {
// null is a valid response
return $feed->get_value($feedid,get('time'));
} elseif ($route->action == "get") {
// '/[^\w\s-]/'
return $feed->get_field($feedid,get('field'));
} elseif ($route->action == "aget") {
return $feed->get($feedid);
} elseif ($route->action == "getmeta") {
return $feed->get_meta($feedid);
} elseif ($route->action == "getfeedsize") {
return $feed->get_feed_size($feedid);
} elseif ($route->action == "export") {
if ($f['engine']==Engine::MYSQL || $f['engine']==Engine::MYSQLMEMORY) {
return $feed->mysqltimeseries_export($feedid,get('start'));
} elseif ($f['engine']==Engine::PHPTIMESERIES) {
return $feed->phptimeseries_export($feedid,get('start'));
} elseif ($f['engine']==Engine::PHPFINA) {
return $feed->phpfina_export($feedid,get('start'));
}
}
}
// write session required
if (isset($session['write']) && $session['write'] && $session['userid']>0 && $f['userid']==$session['userid'])
{
// Storage engine agnostic
// Set feed meta fields
if ($route->action == 'set') {
// if tag or name changed check new combination is unique
$fields = json_decode(get('fields'), true);
if (!empty($fields['tag']) || !empty($fields['name'])) {
$original_name = $feed->get_field($feedid, 'name');
$original_tag = $feed->get_field($feedid, 'tag');
// use original tag/name if no new value given
$new_name = !empty($fields['name']) ? $fields['name'] : $original_name;
$new_tag = !empty($fields['tag']) ? $fields['tag'] : $original_tag;
// exists_tag_name returns false if not found
$unique = $feed->exists_tag_name($session['userid'], $new_tag, $new_name) === false;
// update if tag:name unique else return error;
return $unique ? $feed->set_feed_fields($feedid, get('fields')) : array('success'=>false, 'message'=>'fields tag:name must be unique');
}else{
// update if no tag/name change
return $feed->set_feed_fields($feedid, get('fields'));
}
// insert available here for backwards compatibility
} elseif ($route->action == "insert" || $route->action == "update" || $route->action == "post") {
$join = get('join',false,false);
// Single data point
if (isset($_GET['time']) || isset($_GET['value'])) {
return $feed->post($feedid,time(),get("time"),get("value"),$join);
}
// Single or multiple datapoints via json format
// Format: [[UNIXTIME,VALUE],[UNIXTIME,VALUE],[UNIXTIME,VALUE]]
$data = false;
if (isset($_GET['data'])) {
$data = json_decode($_GET['data']);
} elseif (isset($_POST['data'])) {
$data = json_decode($_POST['data']);
} else {
return array('success'=>false, 'message'=>'missing data parameter');
}
if ($data==null) {
return array('success'=>false, 'message'=>'error decoding json');
}
if (!$data || count($data)==0) {
return array('success'=>false, 'message'=>'empty data object');
}
return $feed->post_multiple($feedid,$data,$join);
// Delete feed
} elseif ($route->action == "delete") {
return $feed->delete($feedid);
// scale range for PHPFINA
// added by Alexandre CUER - january 2019
} elseif ($route->action == "scalerange") {
if ($f['engine'] == Engine::PHPFINA) {
return $feed->EngineClass(Engine::PHPFINA)->scalerange($feedid,get("start"),get("end"),get("value"));
} else {
return "scalerange only supported by phpfina engine";
}
// Clear feed
} elseif ($route->action == "clear") {
return $feed->clear($feedid);
// Trim feed
} elseif ($route->action == "trim") {
if (!filter_var(get('start_time'), FILTER_VALIDATE_INT)) {
return false;
}
$start_time = filter_var(get('start_time'), FILTER_SANITIZE_NUMBER_INT);
return $feed->trim($feedid, $start_time);
// Process
} elseif ($route->action == "process") {
if ($f['engine']!=Engine::VIRTUALFEED) {
return array('success'=>false, 'message'=>'Feed is not Virtual');
} elseif ($route->subaction == "get") {
return $feed->get_processlist($feedid);
} elseif ($route->subaction == "set") {
return $feed->set_processlist($session['userid'], $feedid, post('processlist'),$process->get_process_list());
} elseif ($route->subaction == "reset") {
return $feed->reset_processlist($feedid);
}
// Fast bulk uploader
} elseif ($route->action == "upload") {
// Start time and interval
if (isset($_GET['start']) && isset($_GET['interval']) && isset($_GET['npoints'])) {
return $feed->upload_fixed_interval($feedid,get("start"),get("interval"),get("npoints"));
} elseif (isset($_GET['npoints'])) {
return $feed->upload_variable_interval($feedid,get("npoints"));
}
} elseif ($route->action == "deletedatapoint") {
if ($f['engine']==Engine::MYSQL || $f['engine']==Engine::MYSQLMEMORY) {
return $feed->mysqltimeseries_delete_data_point($feedid,get('feedtime'));
} else {
return "deletedatapoint only supported by mysqltimeseries engine";
}
} elseif ($route->action == "deletedatarange") {
if ($f['engine']==Engine::MYSQL || $f['engine']==Engine::MYSQLMEMORY) {
return $feed->mysqltimeseries_delete_data_range($feedid,get('start'),get('end'));
} else {
return "deletedatarange only supported by mysqltimeseries engine";
}
}
}
}
else
{
return array('success'=>false, 'message'=>'Feed does not exist');
}
}
}
return array('content'=>EMPTY_ROUTE);
}