This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.php
388 lines (342 loc) · 11.2 KB
/
query.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
// Settings
require_once('settings.php');
// Emoncms definitions
require_once('defs_emoncms.php');
// Create an user, asign feeds and inputs
//
// Parameters:
// $username: username
// $email: email of the user
// $password: password of the user
// $panelType: type of the panel
//
// Returns
// true: user, feeds and inputs successfully created
// *anything else*: error string
function create_linked_user($username, $email, $password, $panelType) {
// Global variables
global $redis_enabled, $redis_server;
// Connect to the DB
$ret = create_connection($connection);
if($ret !== true) {
return $ret;
}
// Connect to Redis
if($redis_enabled === true) {
$redis = new Redis();
if(!$redis->connect($redis_server)) {
$redis = false;
}
} else {
$redis = false;
}
// Validate input
$ret = validate_input($username, $email, $password, $panelType);
if($ret !== true) {
end_connection(true, $connection);
return $ret;
}
// Create user
if(create_user($username, $email, $password, $userid, $apikey, $connection) !== true) {
end_connection(true, $connection);
return 'Username already exists';
}
// Set the type of user profile
$prefix = 'data/' . $panelType;
// Create feeds
if(create_feeds($prefix . '_feeds.json', $feeds, $apikey) !== true) {
end_connection(true, $connection);
return 'Error while creating the feeds';
}
// Create inputs
if(create_inputs($prefix . '_inputs.json', $userid, $inputs, $connection, $redis) !== true) {
end_connection(true, $connection);
return 'Error while creating the inputs';
}
// Create processes
if(create_processes($prefix . '_processes.json', $feeds, $inputs, $apikey) !== true) {
end_connection(true, $connection);
return 'Error while creating the processes';
}
end_connection(false, $connection);
return true;
}
// Create a connection with the database
//
// Parameters:
// $connection: output parameter, connection
//
// Returns
// true: connection successfully created
// *anything else*: error string
function create_connection(&$connection) {
// Global variables
global $db_server, $db_username, $db_password, $db_name;
$connection = new mysqli($db_server, $db_username, $db_password, $db_name);
if($connection->connect_error) {
return false;
}
// Set charset to utf8
$connection->set_charset("utf8");
// Disable autocommit (begin transaction)
$connection->autocommit(FALSE);
return true;
}
// Ends the connection
//
// Parameters:
// $error: if true, the changes will be rolled back, otherwise, they will be commited
// $connection: connection to the database
function end_connection($error, &$connection) {
// Error, rollback changes
if($error === true) {
$connection->rollback();
}
// No error, commit the changes
else {
$connection->commit();
}
$connection->autocommit(TRUE);
$connection->close();
}
// Validate the input
//
// Parameters:
// $username: username
// $email: email of the user
// $password: password of the user
// $panelType: type of the panel ("PV" or "Consumption")
//
// Returns
// true: parameters valid
// *anything else*: error string
function validate_input($username, $email, $password, $panelType) {
// Global variables
global $user_profiles;
// Username
if((!isset($username)) || (strlen($username) == 0)) {
return 'Please provide an username';
}
if(!ctype_alnum($username)) {
return 'Username must contain only letters and numbers';
}
if(strlen($username) < 4) {
return 'Username must have at least 4 characters';
}
if(strlen($username) > 30) {
return 'Username cannot have more than 30 characters';
}
// Email
if((!isset($email)) || (strlen($email) == 0)) {
return 'Please provide an email';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return 'Please provide a valid email';
}
// Password
if((!isset($password)) || (strlen($password) == 0)) {
return 'Please provide a password';
}
if(strlen($password) < 4) {
return 'Password must have at least 4 characters';
}
if(strlen($password) > 30) {
return 'Password cannot have more than 30 characters';
}
// Panel Type
if((!isset($panelType)) || (strlen($panelType) == 0)) {
return 'Select a type of profile';
}
if(!isset($user_profiles[$panelType])) {
return 'Please select a valid profile';
}
return true;
}
// Create the user
//
// Parameters:
// $username: username, must be unique
// $email: email of the user
// $password: password of the user
// $userid: output parameter, id of the user created
// $apikey: output parameter, write API key of the user
// $connection: connection with the database
//
// Returns
// true: user successfully created
// false: error creating the user (already exists)
function create_user($username, $email, $password, &$userid, &$apikey, $connection) {
// Global variables
global $user_zone, $user_lang;
// Search if user exists
if($connection->query("SELECT * FROM users WHERE username='$username';")->num_rows != 0) {
return false;
}
// Rest of the parameters
$hash = hash('sha256', $password);
$salt = md5(uniqid(mt_rand(), true));
$password = hash('sha256', $salt . $hash);
$apikey_write = md5(uniqid(mt_rand(), true));
$apikey_read = md5(uniqid(mt_rand(), true));
$apikey = $apikey_write;
// Query
$sqlQuery = "INSERT INTO users (username, password, email, salt ,apikey_read, apikey_write, admin, timezone, language)
VALUES ('$username', '$password', '$email', '$salt', '$apikey_read', '$apikey_write', 0, '$user_zone', '$user_lang');";
if ($connection->query($sqlQuery) === FALSE) {
return false;
}
// Asign userid
$userid = $connection->insert_id;
return true;
}
// Create the feeds
//
// Parameters:
// $datafile: path to the feeds data
// $feeds: output parameter for the feeds, in the format 'feedName'=>'feedId'
// $apikey: write API key
//
// Returns
// true: feeds successfully created
// false: error creating the feeds
function create_feeds($datafile, &$feeds, $apikey) {
// Global variables
global $base_url, $user_interval;
// Read the feeds from file
$feedArray = json_decode(file_get_contents($datafile));
// Create each feed
foreach($feedArray as $feed) {
// Feed interval
if(property_exists($feed, "interval")) {
$feedInterval = $feed->interval;
} else {
$feedInterval = $user_interval;
}
// Query
$datatype = get_type_id($feed->type);
$engine = get_engine_id($feed->engine);
$url = str_replace(' ', '%20', $base_url . "/feed/create.json?tag=$feed->description&name=$feed->name&datatype=$datatype&engine=$engine&apikey=$apikey&options={\"interval\":$feedInterval}");
$result = json_decode(file_get_contents($url), true);
if($result["success"] !== true) {
return false;
}
// Assign the created feed id to the feeds array
$feeds[$feed->name] = $result["feedid"];
}
return true;
}
// Create the inputs
//
// Parameters:
// $datafile: path to the inputs data
// $userid: id of the user
// $inputs: output parameter for the inputs, in the format 'inputName'=>'inputId'
// $connection: connection with the database
// $redis: redis connection
//
// Returns
// true: inputs successfully created
// false: error creating the inputs
function create_inputs($datafile, $userid, &$inputs, $connection, $redis) {
// Global variables
global $user_node;
// Create the inputs from file
$inputArray = json_decode(file_get_contents($datafile));
// Create each input
foreach($inputArray as $input) {
// Input node
if(property_exists($input, "node")) {
$inputNode = $input->node;
} else {
$inputNode = $user_node;
}
// Query
$sqlQuery = "INSERT INTO input (userid, name, description, nodeid)
VALUES ($userid, '$input->name', '$input->description', $inputNode);";
if ($connection->query($sqlQuery) === FALSE) {
return false;
}
// Assign the created input id to the feeds array
$inputId = $connection->insert_id;
$inputs[$input->name] = $inputId;
// Redis query
if($redis !== false) {
$redis->sAdd("user:inputs:$userid", $inputId);
$redis->hMSet("input:$inputId",array('id'=>$inputId,'nodeid'=>$inputNode,'name'=>$input->name,'description'=>$input->description, 'processList'=>""));
}
}
return true;
}
// Create the processes
//
// Parameters:
// $datafile: path to the processes data
// $feeds: array of feed ids, in the format 'feedName'=>'feedId'
// $inputs: array of input ids, in the format 'inputName'=>'inputId'
// $apikey: write API key
//
// Returns
// true: processes successfully created
// false: error creating the processes
function create_processes($datafile, $feeds, $inputs, $apikey) {
// Global variables
global $base_url;
// Read the processes from file
$processArray = json_decode(file_get_contents($datafile));
// Create each process
foreach($processArray as $process) {
$inputId = $inputs[$process->input];
// Translate process
$processesStrings = array();
foreach($process->processes as $function) {
// Translate function
$functionId = get_function_id($function->function);
// Arguments
if(!empty($function->arguments)) {
$argumentsArray = array();
foreach($function->arguments as $argument) {
// Get the id of the feed name
if($argument->type == "feed") {
$argumentsArray[] = $feeds[$argument->value];
}
// Get the id of the input name
else if($argument->type == "input") {
$argumentsArray[] = $inputs[$argument->value];
}
// Get the value
else {
$argumentsArray[] = strval($argument->value);
}
}
$arguments = implode(':', $argumentsArray);
} else {
$arguments = "0";
}
$translatedFunction = strval($functionId) . ":$arguments";
// Add the translated string
$processesStrings[] = $translatedFunction;
}
$processes = implode(",", $processesStrings);
// Query
$postdata = http_build_query(
array(
'processlist' => "$processes"
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents("$base_url/input/process/set.json?inputid=$inputId&apikey=$apikey", false, $context);
if(($result == "false") || ($result === FALSE)) {
return false;
}
}
return true;
}
?>