forked from emoncms/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_model.php
241 lines (205 loc) · 10.8 KB
/
app_model.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
<?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;
private $settings;
public function __construct($mysqli, $settings)
{
$this->mysqli = $mysqli;
$this->settings = $settings;
}
// ----------------------------------------------------------------------------------------------
// Get metadata for available apps
// ----------------------------------------------------------------------------------------------
public function get_available()
{
$list = array();
$dirs = new RecursiveDirectoryIterator("Modules/app/apps/");
$it = new RecursiveIteratorIterator($dirs);
foreach($it as $file) {
// Restrict iteration to two levels
if ($it->getDepth() > 2) continue;
// Replace all backslashes to avoid conflicts with paths on windows machines
$file = str_replace('\\', '/', $file);
if(basename($file ,".json") == 'app') {
$dir = dirname($file);
$content = (array) json_decode(file_get_contents($file));
if (json_last_error() == 0 && array_key_exists("title", $content) && array_key_exists("description", $content)) {
$content['dir'] = stripslashes($dir.'/');
$id = basename($dir);
if ($id != 'template' && (!isset($this->settings) || !isset($this->settings['hidden'])
|| !in_array($id, $this->settings['hidden']))) {
$list[$id] = $content;
}
}
}
}
uasort($list, function($a1, $a2) {
if($a1['status'] == $a2['status'])
return strcmp($a1['title'], $a2['title']);
return strcmp($a1['status'], $a2['status']);
});
return $list;
}
// ----------------------------------------------------------------------------------------------
// Get user app list
// ----------------------------------------------------------------------------------------------
public function get_list($userid)
{
$userid = (int) $userid;
return $this->get($userid);
}
// ----------------------------------------------------------------------------------------------
// Add a new app to the user app list
// ----------------------------------------------------------------------------------------------
public function add($userid,$app,$name)
{
$userid = (int) $userid;
if (preg_replace('/[^\p{N}\p{L}]/u','',$app)!=$app) return array('success'=>false, "message"=>"Invalid app");
if (preg_replace('/[^\p{N}\p{L}_\s\-:]/u','',$name)!=$name) return array('success'=>false, "message"=>"Invalid app name");
// Load config from database
$applist = $this->get($userid);
if (isset($applist->$name)) return array('success'=>false, "message"=>"App already exist with name $name");
// Add new app to config
$applist->$name = new stdClass();
$applist->$name->app = $app;
$applist->$name->config = new stdClass();
// Save
if (!$this->set($userid,$applist)) return array('success'=>false, 'message'=>"Error on app_config update");
return array('success'=>true);
}
// ----------------------------------------------------------------------------------------------
// Remove app from user app list
// ----------------------------------------------------------------------------------------------
public function remove($userid,$name)
{
$userid = (int) $userid;
if (preg_replace('/[^\p{N}\p{L}_\s\-:]/u','',$name)!=$name) return array('success'=>false, "message"=>"Invalid app name");
// Load config from database
$applist = $this->get($userid);
if (!isset($applist->$name)) return array('success'=>false, "message"=>"App does not exist with name $name");
// Delete the app
unset($applist->$name);
// Save
if (!$this->set($userid,$applist)) return array('success'=>false, 'message'=>"Error on app_config update");
return array('success'=>true);
}
// ----------------------------------------------------------------------------------------------
// Set config off a particular app in the user app list
// ----------------------------------------------------------------------------------------------
public function set_config($userid,$name,$config)
{
$userid = (int) $userid;
if (preg_replace('/[^\p{N}\p{L}_\s\-:]/u','',$name)!=$name) return array('success'=>false, "message"=>"Invalid app name");
// Config sanitisation
$config = json_decode($config);
if (!$config || $config===null) return array('success'=>false, "message"=>"Could not parse json");
foreach ($config as $key=>$value) {
// Key must be lower case alphanumeric with no special char's
if (preg_replace("/[^a-zA-Z0-9_]/",'',$key)!=$key) return array('success'=>false, "message"=>"Invalid config key");
// Value type can not be object, array or resource
if (gettype($value)=="object") return array('success'=>false, "message"=>"Config value cannot be object");
if (gettype($value)=="array") return array('success'=>false, "message"=>"Config value cannot be array");
if (gettype($value)=="resource") return array('success'=>false, "message"=>"Config value cannot be resource");
// Value can be alphanumeric, either case plus selected char's
// using unicode categories for pattern match to allow for any currency character
// \pSc (S = symbols, c = currency)
// \pL (L = letters)
// \pP (P = punctuation)
// \pN (N = number)
// \W = white space (not unicode category)
// also using php's strip_tags() funciton to avoid html insertion (spaces required around greater than and less than)
//
// EXAMPLE:
// this input:
// <script> alert(123) </script> a is < than b but c is < > to b,/?€@#~$¢£¤¥৲৳৻૱௹฿៛₠₡₢₣₤₥₦₧₨₩₪₫₭₮₯₰₱₲₳₴₵₸₹₺₽
// would be recognized as:
// alert(123) a is < than b but c is < > to b,/?€@#~$¢£¤¥৲৳৻૱௹฿៛₠₡₢₣₤₥₦₧₨₩₪₫₭₮₯₰₱₲₳₴₵₸₹₺₽
if (preg_replace("/[^\pSc\pL\pP\pN\W]/u",'',strip_tags($value))!=$value) return array('success'=>false, "message"=>"Invalid config value");
}
// Load config from database
$applist = $this->get($userid);
if (!isset($applist->$name)) return array('success'=>false, "message"=>"App does not exist with name $name");
// Update the applist object with the configuration
$applist->$name->config = $config;
// Save
if (!$this->set($userid,$applist)) return array('success'=>false, 'message'=>"Error on app_config update");
return array('success'=>true);
}
// ----------------------------------------------------------------------------------------------
// Get the config off a particular app in the user app list
// ----------------------------------------------------------------------------------------------
public function get_config($userid,$name)
{
$userid = (int) $userid;
if (preg_replace('/[^\p{N}\p{L}_\s\-:]/u','',$name)!=$name) return array('success'=>false, "message"=>"Invalid app name");
$applist = $this->get($userid);
if (!isset($applist->$name)) return array('success'=>false, "message"=>"App does not exist with name $name");
return $applist->$name->config;
}
// ----------------------------------------------------------------------------------------------
// Private app config database entry set method
// ----------------------------------------------------------------------------------------------
private function set($userid,$applist)
{
// Re-encode for storage in db text field
$json = json_encode($applist);
// Alternative to json_encode($outdata,JSON_UNESCAPED_UNICODE), for php 5.3 support
$json = preg_replace_callback('/\\\\u(\w{4})/', function ($matches) {
return html_entity_decode('&#x' . $matches[1] . ';', ENT_COMPAT, 'UTF-8');
}, $json);
// Update database entry if it exists otherwise insert a new entry
$result = $this->mysqli->query("SELECT `userid` FROM app_config WHERE `userid`='$userid'");
if ($result->num_rows) {
$stmt = $this->mysqli->prepare("UPDATE app_config SET `data`=? WHERE `userid`=?");
$stmt->bind_param("si", $json, $userid);
if (!$stmt->execute()) {
return false;
}
return true;
} else {
$stmt = $this->mysqli->prepare("INSERT INTO app_config (`userid`,`data`) VALUES (?,?)");
$stmt->bind_param("is", $userid,$json);
if (!$stmt->execute()) {
return false;
}
return true;
}
}
// ----------------------------------------------------------------------------------------------
// Private app config database entry get method
// ----------------------------------------------------------------------------------------------
private function get($userid)
{
$result = $this->mysqli->query("SELECT `data` FROM app_config WHERE `userid`='$userid'");
if ($result && $row = $result->fetch_object()) {
$applist = json_decode($row->data);
if (gettype($applist)=="array" || $applist===null) $applist = new stdClass();
} else {
$applist = new stdClass();
}
// App list config migration
foreach ($applist as $name=>$appitem) {
if (!isset($applist->$name->config)) {
if (isset($this->available_apps[$name])) {
$applist->$name = new stdClass();
$applist->$name->app = $name;
$applist->$name->config = $appitem;
} else {
unset($applist->$name);
}
}
}
return $applist;
}
}