-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathinput_model.php
758 lines (654 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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');
15
16
class Input
{
17
private $mysqli;
18
private $feed;
19
private $redis;
20
private $log;
21
22
23
24
public function __construct($mysqli,$redis,$feed)
{
$this->mysqli = $mysqli;
25
$this->feed = $feed;
26
$this->redis = $redis;
27
$this->log = new EmonLogger(__FILE__);
28
29
30
31
32
}
public function create_input($userid, $nodeid, $name)
{
$userid = (int) $userid;
33
$nodeid = preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$nodeid);
34
// if (strlen($nodeid)>16) return false; // restriction placed on emoncms.org
35
$name = preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$name);
36
// if (strlen($name)>64) return false; // restriction placed on emoncms.org
37
$id = false;
38
39
40
41
42
43
44
if ($stmt = $this->mysqli->prepare("INSERT INTO input (userid,name,nodeid,description,processList) VALUES (?,?,?,'','')")) {
$stmt->bind_param("iss",$userid,$name,$nodeid);
$stmt->execute();
$stmt->close();
$id = $this->mysqli->insert_id;
45
46
47
48
49
if ($this->redis && $id>0) {
$this->redis->sAdd("user:inputs:$userid", $id);
$this->redis->hMSet("input:$id",array('id'=>$id,'nodeid'=>$nodeid,'name'=>$name,'description'=>"", 'processList'=>""));
}
50
} else {
51
$this->log->warn("create_input mysql error $userid $nodeid $name ".$this->mysqli->error);
52
}
53
54
55
56
57
58
59
60
61
return $id;
}
public function exists($inputid)
{
$inputid = (int) $inputid;
$result = $this->mysqli->query("SELECT id FROM input WHERE `id` = '$inputid'");
if ($result->num_rows == 1) return true; else return false;
}
62
63
64
public function access($userid,$inputid)
{
65
$userid = (int) $userid;
66
$inputid = (int) $inputid;
67
68
69
70
71
72
73
$stmt = $this->mysqli->prepare("SELECT id FROM input WHERE userid=? AND id=?");
$stmt->bind_param("ii",$userid,$inputid);
$stmt->execute();
$stmt->bind_result($id);
$result = $stmt->fetch();
$stmt->close();
74
75
76
if ($result && $id>0) return true; else return false;
}
77
78
79
80
public function exists_nodeid_name($userid,$nodeid,$name)
{
$userid = (int) $userid;
81
82
$nodeid = preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$nodeid);
$name = preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$name);
83
84
85
86
87
88
89
$stmt = $this->mysqli->prepare("SELECT id FROM input WHERE userid=? AND nodeid=? AND name=?");
$stmt->bind_param("iss",$userid,$nodeid,$name);
$stmt->execute();
$stmt->bind_result($id);
$result = $stmt->fetch();
$stmt->close();
90
91
if ($result && $id>0) return $id; else return false;
92
}
93
94
95
public function validate_access($dbinputs, $nodeid)
{
96
global $session, $settings;
97
98
99
100
101
102
$success=true;
$message = "";
if (isset($session['deviceid']) && isset($session['nodeid'])) {
if (!isset($dbinputs[$nodeid])) {
$success = false;
$message = "Device not initialized.";
103
} elseif ($nodeid != $session['nodeid']) {
104
105
106
$success = false;
$message = "Node '$nodeid' does not belong to device.";
}
107
} elseif (!isset($dbinputs[$nodeid]) && (count($dbinputs) >= $settings["input"]["max_node_id_limit"] )) {
108
$success = false;
109
$message = "Reached the maximal allowed number of different NodeIds, limit is ".$settings["input"]["max_node_id_limit"].". Node '$nodeid' was ignored.";
110
111
112
}
return array('success'=>$success, 'message'=>$message);
}
113
114
115
116
117
public function set_timevalue($id, $time, $value)
{
$id = (int) $id;
$time = (int) $time;
118
$value = $value; // Dont cast
119
120
121
122
if ($this->redis) {
$this->redis->hMset("input:lastvalue:$id", array('value' => $value, 'time' => $time));
} else {
123
124
125
if ($stmt = $this->mysqli->prepare("UPDATE input SET time = ?, value = ? WHERE id = ?")) {
$stmt->bind_param("idi", $time, $value, $id);
$stmt->execute();
126
$stmt->close();
127
}
128
}
129
130
}
131
// Used in conjunction with controller before calling another method
132
133
134
135
136
137
138
139
140
141
public function belongs_to_user($userid, $inputid)
{
$userid = (int) $userid;
$inputid = (int) $inputid;
$result = $this->mysqli->query("SELECT id FROM input WHERE userid = '$userid' AND id = '$inputid'");
if ($result->fetch_array()) return true; else return false;
}
public function set_fields($id,$fields)
{
142
143
$id = (int) $id;
if (!$this->exists($id)) return array('success'=>false, 'message'=>'Input does not exist');
144
$fields = json_decode(stripslashes($fields));
145
146
147
148
$success = false;
if (isset($fields->name)) {
149
if (preg_replace('/[^\p{N}\p{L}_\s\-]/u','',$fields->name)!=$fields->name) return array('success'=>false, 'message'=>'invalid characters in input name');
150
151
152
153
$stmt = $this->mysqli->prepare("UPDATE input SET name = ? WHERE id = ?");
$stmt->bind_param("si",$fields->name,$id);
if ($stmt->execute()) $success = true;
$stmt->close();
154
155
156
if ($this->redis) $this->redis->hset("input:$id",'name',$fields->name);
}
157
158
if (isset($fields->description)) {
159
if (preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$fields->description)!=$fields->description) return array('success'=>false, 'message'=>'invalid characters in input description');
160
161
162
163
$stmt = $this->mysqli->prepare("UPDATE input SET description = ? WHERE id = ?");
$stmt->bind_param("si",$fields->description,$id);
if ($stmt->execute()) $success = true;
$stmt->close();
164
165
166
if ($this->redis) $this->redis->hset("input:$id",'description',$fields->description);
}
167
168
if ($success){
169
170
171
172
173
return array('success'=>true, 'message'=>'Field updated');
} else {
return array('success'=>false, 'message'=>'Field could not be updated');
}
}
174
175
176
177
178
179
180
public function set_node_input_descriptions($userid,$nodeid,$names)
{
$names = explode(",",$names);
foreach ($names as $name) {
if (preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$name)!=$name) return array('success'=>false, 'message'=>'invalid characters in input description');
}
181
182
183
$userid = (int) $userid;
$nodeid = preg_replace('/[^\p{N}\p{L}_\s\-.]/u','',$nodeid);
184
185
186
187
188
$stmt = $this->mysqli->prepare("SELECT id,name FROM input WHERE userid=? AND nodeid=? ORDER BY id ASC");
$stmt->bind_param("is",$userid,$nodeid);
$stmt->execute();
$stmt->bind_result($id,$name);
189
190
191
192
193
194
195
196
197
198
$descriptions_by_id = array();
$index = 0;
while ($result = $stmt->fetch()) {
if (isset($names[$index])) {
$descriptions_by_id[$id] = $names[$index];
}
$index++;
}
$stmt->close();
199
200
201
202
203
204
foreach ($descriptions_by_id as $id=>$description) {
$stmt = $this->mysqli->prepare("UPDATE input SET description = ? WHERE id = ?");
$stmt->bind_param("si",$description,$id);
$stmt->execute();
$stmt->close();
205
206
207
208
if ($this->redis) $this->redis->hset("input:$id",'description',$description);
}
return array('success'=>true, 'message'=>'input descriptions updated');
209
}
210
211
212
// -----------------------------------------------------------------------------------------
213
214
215
// get_inputs, returns user inputs by node name and input name
// - last time and value not included
// - used by input/post, input/bulk input methods
216
// -----------------------------------------------------------------------------------------
217
218
219
220
221
222
223
224
public function get_inputs($userid)
{
if ($this->redis) {
return $this->redis_get_inputs($userid);
} else {
return $this->mysql_get_inputs($userid);
}
}
225
226
private function redis_get_inputs($userid)
227
228
229
230
231
{
$userid = (int) $userid;
$dbinputs = array();
$inputids = $this->redis->sMembers("user:inputs:$userid");
232
233
234
235
236
if ($inputids==null) {
$this->load_to_redis($userid);
$inputids = $this->redis->sMembers("user:inputs:$userid");
}
237
238
239
240
$pipe = $this->redis->multi(Redis::PIPELINE);
foreach ($inputids as $id) $row = $this->redis->hGetAll("input:$id");
$result = $pipe->exec();
241
242
foreach ($result as $row) {
243
244
245
if ($row['nodeid']==null) $row['nodeid'] = 0;
if (!isset($dbinputs[$row['nodeid']])) $dbinputs[$row['nodeid']] = array();
$dbinputs[$row['nodeid']][$row['name']] = array('id'=>$row['id'], 'processList'=>$row['processList']);
246
247
248
}
return $dbinputs;
}
249
250
private function mysql_get_inputs($userid)
251
252
253
{
$userid = (int) $userid;
$dbinputs = array();
254
$result = $this->mysqli->query("SELECT id,nodeid,name,description,processList FROM input WHERE `userid` = '$userid' ORDER BY nodeid,name asc");
255
256
257
258
259
260
261
262
while ($row = (array)$result->fetch_object())
{
if ($row['nodeid']==null) $row['nodeid'] = 0;
if (!isset($dbinputs[$row['nodeid']])) $dbinputs[$row['nodeid']] = array();
$dbinputs[$row['nodeid']][$row['name']] = array('id'=>$row['id'], 'processList'=>$row['processList']);
}
return $dbinputs;
}
263
264
// -----------------------------------------------------------------------------------------
265
266
267
268
269
270
271
272
273
// get_inputs_v2, returns user inputs by node name and input name
// - last time and value is included in the response
// - input id is not included in the response
//
// {"emontx":{
// "1":{"time":TIME,"value":100,"processList":""},
// "2":{"time":TIME,"value":200,"processList":""},
// "3":{"time":TIME,"value":300,"processList":""}
// }}
274
// -----------------------------------------------------------------------------------------
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
public function get_inputs_v2($userid)
{
if ($this->redis) {
return $this->redis_get_inputs_v2($userid);
} else {
return $this->mysql_get_inputs_v2($userid);
}
}
private function redis_get_inputs_v2($userid)
{
$userid = (int) $userid;
if (!$this->redis->exists("user:inputs:$userid")) $this->load_to_redis($userid);
$dbinputs = array();
$inputids = $this->redis->sMembers("user:inputs:$userid");
foreach ($inputids as $id)
{
$row = $this->redis->hGetAll("input:$id");
if ($row['nodeid']==null) $row['nodeid'] = 0;
296
297
$lastvalue = $this->redis->hmget("input:lastvalue:$id",array('time','value'));
298
299
300
301
302
303
304
305
306
307
if (!isset($lastvalue['time']) || !is_numeric($lastvalue['time']) || is_nan($lastvalue['time'])) {
$row['time'] = null;
} else {
$row['time'] = (int) $lastvalue['time'];
}
if (!isset($lastvalue['value']) || !is_numeric($lastvalue['value']) || is_nan($lastvalue['value'])) {
$row['value'] = null;
} else {
$row['value'] = (float) $lastvalue['value'];
}
308
309
310
311
312
313
314
315
316
317
318
319
if (!isset($dbinputs[$row['nodeid']])) $dbinputs[$row['nodeid']] = array();
$dbinputs[$row['nodeid']][$row['name']] = array('time'=>$row['time'], 'value'=>$row['value'], 'processList'=>$row['processList']);
}
return $dbinputs;
}
private function mysql_get_inputs_v2($userid)
{
$userid = (int) $userid;
$dbinputs = array();
320
$result = $this->mysqli->query("SELECT nodeid,name,description,processList,time,value FROM input WHERE `userid` = '$userid' ORDER BY nodeid,name asc");
321
322
323
324
while ($row = (array)$result->fetch_object())
{
if ($row['nodeid']==null) $row['nodeid'] = 0;
if (!isset($dbinputs[$row['nodeid']])) $dbinputs[$row['nodeid']] = array();
325
326
327
328
329
330
331
332
333
334
335
if (!is_numeric($row['time']) || is_nan($row['time'])) {
$row['time'] = null;
} else {
$row['time'] = (int) $row['time'];
}
if (!is_numeric($row['value']) || is_nan($row['value'])) {
$row['value'] = null;
} else {
$row['value'] = (float) $row['value'];
}
336
337
$dbinputs[$row['nodeid']][$row['name']] = array('time'=>$row['time'], 'value'=>$row['value'], 'processList'=>$row['processList']);
338
339
340
}
return $dbinputs;
}
341
342
// -----------------------------------------------------------------------------------------
343
// getlist: returns a list of user inputs (no grouping)
344
// -----------------------------------------------------------------------------------------
345
public function getlist($userid)
346
347
348
349
350
351
352
{
if ($this->redis) {
return $this->redis_getlist($userid);
} else {
return $this->mysql_getlist($userid);
}
}
353
354
private function redis_getlist($userid)
355
356
357
358
359
360
{
$userid = (int) $userid;
if (!$this->redis->exists("user:inputs:$userid")) $this->load_to_redis($userid);
$inputs = array();
$inputids = $this->redis->sMembers("user:inputs:$userid");
361
362
$pipe = $this->redis->multi(Redis::PIPELINE);
363
364
foreach ($inputids as $id)
{
365
366
367
368
$this->redis->hGetAll("input:$id");
$this->redis->hmget("input:lastvalue:$id",array('time','value'));
}
$result = $pipe->exec();
369
370
371
372
for ($i=0; $i<count($result); $i+=2) {
$row = $result[$i];
$lastvalue = $result[$i+1];
373
374
// $row["description"] = mb_convert_encoding($row["description"], 'UTF-8', mb_list_encodings());
$row["description"] = mb_convert_encoding($row["description"], 'UTF-8', 'auto');
375
if (!isset($lastvalue['time']) || !is_numeric($lastvalue['time']) || is_nan($lastvalue['time'])) {
376
377
$row['time'] = null;
} else {
378
$row['time'] = (int) $lastvalue['time'];
379
}
380
if (!isset($lastvalue['value']) || !is_numeric($lastvalue['value']) || is_nan($lastvalue['value'])) {
381
382
$row['value'] = null;
} else {
383
$row['value'] = (float) $lastvalue['value'];
384
}
385
$inputs[] = $row;
386
387
388
}
return $inputs;
}
389
390
private function mysql_getlist($userid)
391
392
393
{
$userid = (int) $userid;
$inputs = array();
394
$result = $this->mysqli->query("SELECT id,nodeid,name,description,processList,time,value FROM input WHERE `userid` = '$userid' ORDER BY nodeid,name asc");
395
while ($row = (array)$result->fetch_object()) $inputs[] = $row;
396
397
return $inputs;
}
398
399
// -----------------------------------------------------------------------------------------
400
401
402
403
public function get_name($id)
{
$id = (int) $id;
404
405
406
407
408
409
410
411
412
if ($this->redis) {
if (!$this->redis->exists("input:$id")) $this->load_input_to_redis($id);
return $this->redis->hget("input:$id",'name');
} else {
$result = $this->mysqli->query("SELECT name FROM input WHERE `id` = '$id'");
$row = $result->fetch_array();
return $row['name'];
}
413
414
}
415
416
417
418
419
420
421
422
423
424
425
426
public function get_details($id)
{
$id = (int) $id;
if ($this->redis) {
if (!$this->redis->exists("input:$id")) $this->load_input_to_redis($id);
return $this->redis->hGetAll("input:$id");
} else {
$result = $this->mysqli->query("SELECT nodeid,name,description FROM input WHERE `id` = '$id'");
return $result->fetch_array();
}
}
427
428
public function get_last_value($id)
{
429
$id = (int) $id;
430
431
if ($this->redis) {
432
$lastvalue = $this->redis->hget("input:lastvalue:$id",'value');
433
if (!isset($lastvalue) || !is_numeric($lastvalue) || is_nan($lastvalue)) {
434
435
436
437
$lastvalue = null;
} else {
$lastvalue = (float) $lastvalue;
}
438
return $lastvalue;
439
440
}
else {
441
442
443
444
$result = $this->mysqli->query("SELECT value FROM input WHERE `id` = '$id'");
$row = $result->fetch_array();
return $row['value'];
}
445
446
}
447
448
449
public function get_last_timevalue($id)
{
$id = (int) $id;
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
if ($this->redis) {
$lastvalue = $this->redis->hmget("input:lastvalue:$id", array('time','value'));
if (!isset($lastvalue['time']) || !is_numeric($lastvalue['time']) || is_nan($lastvalue['time'])) {
$lastvalue['time'] = null;
} else {
$lastvalue['time'] = (int) $lastvalue['time'];
}
if (!isset($lastvalue['value']) || !is_numeric($lastvalue['value']) || is_nan($lastvalue['value'])) {
$lastvalue['value'] = null;
} else {
$lastvalue['value'] = (float) $lastvalue['value'];
}
return $lastvalue;
}
else {
$result = $this->mysqli->query("SELECT time, value FROM input WHERE `id` = '$id'");
if ($result->num_rows > 0) {
$row = $result->fetch_array();
469
return array('time'=> (int) $row['time'], 'value'=> (float) $row['value']);
470
471
472
473
474
}
}
return null;
}
475
476
477
478
479
480
481
public function delete($userid, $inputid)
{
$userid = (int) $userid;
$inputid = (int) $inputid;
// Inputs are deleted permanentely straight away rather than a soft delete
// as in feeds - as no actual feed data will be lost
$this->mysqli->query("DELETE FROM input WHERE userid = '$userid' AND id = '$inputid'");
482
483
484
485
486
if ($this->redis) {
$this->redis->del("input:$inputid");
$this->redis->srem("user:inputs:$userid",$inputid);
}
487
488
return "input deleted";
}
489
490
// userid and inputids are checked in belongs_to_user and delete
491
492
493
494
495
public function delete_multiple($userid, $inputids) {
foreach ($inputids as $inputid) {
if ($this->belongs_to_user($userid, $inputid)) $this->delete($userid, $inputid);
}
return "inputs deleted";
496
497
498
499
}
public function clean($userid)
{
500
$userid = (int) $userid;
501
$n = 0;
502
503
$qresult = $this->mysqli->query("SELECT * FROM input WHERE `userid` = '$userid'");
while ($row = $qresult->fetch_array())
504
{
505
506
507
508
$inputid = $row['id'];
if ($row['processList']==NULL || $row['processList']=='')
{
$result = $this->mysqli->query("DELETE FROM input WHERE userid = '$userid' AND id = '$inputid'");
509
510
511
512
513
if ($this->redis) {
$this->redis->del("input:$inputid");
$this->redis->srem("user:inputs:$userid",$inputid);
}
514
$n++;
515
}
516
}
517
return "Deleted $n inputs";
518
}
519
520
public function clean_processlist_feeds($process_class,$userid)
521
522
{
$processes = $process_class->get_process_list();
523
$out = "";
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT id, processList FROM input WHERE `userid`='$userid'");
while ($row = $result->fetch_object())
{
$inputid = $row->id;
$processlist = $row->processList;
$pairs = explode(",",$processlist);
$pairsout = array();
for ($i=0; $i<count($pairs); $i++)
{
$valid = true;
$keyarg = explode(":",$pairs[$i]);
if (count($keyarg)==2) {
$key = $keyarg[0];
$arg = $keyarg[1];
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Map ids to process key names
if (isset($process_class->process_map[$key])) $key = $process_class->process_map[$key];
if (!isset($processes[$key])) {
$this->log->error("clean_processlist_feeds() Processor '".$processkey."' does not exists. Module missing?");
return false;
}
if ($processes[$key]["argtype"] == ProcessArg::FEEDID) {
if (!$this->feed->exist($arg)) $valid = false;
}
} else {
$valid = false;
}
if ($valid) $pairsout[] = $pairs[$i];
}
$processlist_after = implode(",",$pairsout);
if ($processlist_after!=$processlist) {
560
561
if ($this->redis)
$this->redis->hset("input:$inputid",'processList',$processlist_after);
562
563
564
565
566
567
$this->mysqli->query("UPDATE input SET processList = '$processlist_after' WHERE id='$inputid'");
$out .= "processlist for input $inputid changed from $processlist to $processlist_after\n";
}
}
return $out;
}
568
569
// -----------------------------------------------------------------------------------------
570
// Processlist functions
571
// -----------------------------------------------------------------------------------------
572
573
574
public function get_processlist($id)
{
$id = (int) $id;
575
576
577
578
579
580
581
582
583
584
585
586
if ($this->redis) {
if (!$this->redis->exists("input:$id")) $this->load_input_to_redis($id);
return $this->redis->hget("input:$id",'processList');
} else {
$result = $this->mysqli->query("SELECT processList FROM input WHERE `id` = '$id'");
$row = $result->fetch_array();
if (!$row['processList']) $row['processList'] = "";
return $row['processList'];
}
}
587
588
589
590
591
592
593
// Set_processlist is called from input_controller
// a processlist might look something like:
// 1:1,2:0.1,1:2,eventp__ifrategtequalskip:10
// Historically emoncms has used integer based processid's to reference the desired process function
// however emoncms also supports text based process reference and a number of processes
// are only available via the text based function reference.
// $process_list is a list of processes
594
595
public function set_processlist($userid, $id, $processlist, $process_list)
596
{
597
$userid = (int) $userid;
598
$id = (int) $id;
599
600
601
// Validate processlist
$pairs = explode(",",$processlist);
602
$pairs_out = array();
603
604
605
606
// Build map of processids where set
$map = array();
foreach ($process_list as $key=>$process) {
607
if (isset($process['id_num'])) $map[$process['id_num']] = $key;
608
}
609
610
611
612
613
foreach ($pairs as $pair)
{
$inputprocess = explode(":", $pair);
if (count($inputprocess)==2) {
614
615
// Verify process id
616
617
618
$processkey = $inputprocess[0];
// If key is in the map, switch to associated full process key
if (isset($map[$processkey])) $processkey = $map[$processkey];
619
620
621
// Load process
if (isset($process_list[$processkey])) {
622
$processarg = $process_list[$processkey]['argtype'];
623
624
625
626
if ($process_list[$processkey]['group']=="Deleted") {
return array('success'=>false, 'message'=>_("Process list contains depreciated process:$processkey, please delete process"));
}
627
628
// remap process back to use map id if available
629
630
if (isset($process_list[$processkey]['id_num']))
$processkey = $process_list[$processkey]['id_num'];
631
632
633
634
} else {
return array('success'=>false, 'message'=>_("Invalid process processid:$processkey"));
}
635
636
637
// Verify argument
$arg = $inputprocess[1];
638
639
// Check argument against process arg type
640
switch($processarg){
641
642
643
644
645
646
647
case ProcessArg::FEEDID:
$feedid = (int) $arg;
if (!$this->feed->access($userid,$feedid)) {
return array('success'=>false, 'message'=>_("Invalid feed"));
}
break;
648
649
650
651
652
653
654
655
656
657
case ProcessArg::INPUTID:
$inputid = (int) $arg;
if (!$this->access($userid,$inputid)) {
return array('success'=>false, 'message'=>_("Invalid input"));
}
break;
case ProcessArg::VALUE:
if (!is_numeric($arg)) {
658
return array('success'=>false, 'message'=>'Value is not numeric');
659
660
661
662
}
break;
case ProcessArg::TEXT:
663
664
if (preg_replace('/[^{}\p{N}\p{L}_\s\/.\-]/u','',$arg)!=$arg)
return array('success'=>false, 'message'=>'Invalid characters in arg');
665
break;
666
667
668
669
case ProcessArg::SCHEDULEID:
$scheduleid = (int) $arg;
if (!$this->schedule_access($userid,$scheduleid)) { // This should really be in the schedule model
670
return array('success'=>false, 'message'=>'Invalid schedule');
671
672
}
break;
673
674
675
676
677
case ProcessArg::NONE:
default:
$arg = false;
break;
678
}
679
680
$pairs_out[] = implode(":",array($processkey,$arg));
681
682
}
}
683
684
685
// rebuild processlist from verified content
$processlist_out = implode(",",$pairs_out);
686
687
$stmt = $this->mysqli->prepare("UPDATE input SET processList=? WHERE id=?");
688
$stmt->bind_param("si", $processlist_out, $id);
689
690
691
if (!$stmt->execute()) {
return array('success'=>false, 'message'=>_("Error setting processlist"));
}
692
693
if ($this->mysqli->affected_rows>0){
694
if ($this->redis) $this->redis->hset("input:$id",'processList',$processlist_out);
695
696
697
698
699
return array('success'=>true, 'message'=>'Input processlist updated');
} else {
return array('success'=>false, 'message'=>'Input processlist was not updated');
}
}
700
701
public function reset_processlist($id)
702
703
{
$id = (int) $id;
704
return $this->set_processlist($id, "");
705
706
}
707
// -----------------------------------------------------------------------------------------
708
// Redis cache loaders
709
// -----------------------------------------------------------------------------------------
710
711
private function load_input_to_redis($inputid)
{
712
$inputid = (int) $inputid;
713
$result = $this->mysqli->query("SELECT id,userid,nodeid,name,description,processList FROM input WHERE `id` = '$inputid' ORDER BY nodeid,name asc");
714
if ($result->num_rows > 0) {
715
$row = $result->fetch_object();
716
$userid = $row->userid;
717
718
719
720
721
722
723
724
725
726
$this->redis->sAdd("user:inputs:$userid", $row->id);
$this->redis->hMSet("input:$row->id",array(
'id'=>$row->id,
'nodeid'=>$row->nodeid,
'name'=>$row->name,
'description'=>$row->description,
'processList'=>$row->processList
));
}
727
728
729
730
}
private function load_to_redis($userid)
{
731
$userid = (int) $userid;
732
$result = $this->mysqli->query("SELECT id,userid,nodeid,name,description,processList FROM input WHERE `userid` = '$userid' ORDER BY nodeid,name asc");
733
734
735
736
737
738
739
740
741
742
743
while ($row = $result->fetch_object())
{
$this->redis->sAdd("user:inputs:$userid", $row->id);
$this->redis->hMSet("input:$row->id",array(
'id'=>$row->id,
'nodeid'=>$row->nodeid,
'name'=>$row->name,
'description'=>$row->description,
'processList'=>$row->processList
));
}
744
}
745
746
747
748
749
750
751
752
753
754
755
756
757
private function schedule_access($userid,$scheduleid)
{
$userid = (int) $userid;
$scheduleid = (int) $scheduleid;
$stmt = $this->mysqli->prepare("SELECT id FROM schedule WHERE userid=? AND id=?");
$stmt->bind_param("ii",$userid,$scheduleid);
$stmt->execute();
$stmt->bind_result($id);
$result = $stmt->fetch();
$stmt->close();
if ($result && $id>0) return true; else return false;
}
758
}