-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathAppConfig_model.php
More file actions
94 lines (75 loc) · 2.63 KB
/
AppConfig_model.php
File metadata and controls
94 lines (75 loc) · 2.63 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
<?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');
class AppConfig
{
private $mysqli;
public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}
public function checktable(){
$sql = 'SHOW TABLES LIKE "app_config"';
$result = $this->mysqli->query($sql);
if ($result->num_rows>0) return true;
return false;
}
public function set($userid,$json)
{
$userid = (int) $userid;
//var_dump ($json);
$data = json_decode($json);
if (!$data) return array('success'=>false);
// Input sanitisation
$outdata = array();
$filter = "/[^A-Za-z0-9&£€$.,]/";
foreach ($data as $appname=>$properties)
{
$appname = preg_replace($filter,'',$appname);
if (gettype($properties)=="object") {
$outdata[$appname] = array();
foreach ($properties as $property=>$value)
{
$property = preg_replace($filter,'',$property);
if (gettype($value)=="array") {
$tmp = array();
foreach ($value as $val) $tmp[] = (int) $val;
$value = $tmp;
} else {
$value = preg_replace($filter,'',$value);
}
$outdata[$appname][$property] = $value;
}
}
}
// Re-encode for storage in db text field
$json = json_encode($outdata);
$result = $this->mysqli->query("SELECT `userid` FROM app_config WHERE `userid`='$userid'");
if ($result->num_rows) {
$this->mysqli->query("UPDATE app_config SET `data`='$json' WHERE `userid`='$userid'");
return true;
} else {
$this->mysqli->query("INSERT INTO app_config (`userid`,`data`) VALUES ('$userid','$json')");
return true;
}
return array('success'=>false);
}
public function get($userid)
{
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT `data` FROM app_config WHERE `userid`='$userid'");
if ($row = $result->fetch_array()) {
return json_decode($row['data']);
} else {
return false;
}
}
}