-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathadmin_controller.php
336 lines (295 loc) · 14 KB
/
admin_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
<?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 admin_controller()
{
global $mysqli,$session,$route,$updatelogin,$allow_emonpi_admin, $log_filename, $log_enabled, $redis;
$result = "<br><div class='alert-error' style='top:0px; left:0px; width:100%; height:100%; text-align:center; padding-top:100px; padding-bottom:100px; border-radius:4px;'><h4>"._('Admin re-authentication required')."</h4></div>";
// Allow for special admin session if updatelogin property is set to true in settings.php
// Its important to use this with care and set updatelogin to false or remove from settings
// after the update is complete.
if ($updatelogin===true) {
$route->format = 'html';
if ($route->action == 'db')
{
$applychanges = false;
if (isset($_GET['apply']) && $_GET['apply']==true) $applychanges = true;
require_once "Lib/dbschemasetup.php";
$updates = array(array(
'title'=>"Database schema", 'description'=>"",
'operations'=>db_schema_setup($mysqli,load_db_schema(),$applychanges)
));
return array('content'=>view("Modules/admin/update_view.php", array('applychanges'=>$applychanges, 'updates'=>$updates)));
}
}
if ($session['admin']) {
if ($route->format == 'html') {
if ($route->action == 'view') $result = view("Modules/admin/admin_main_view.php", array());
else if ($route->action == 'db')
{
$applychanges = get('apply');
if (!$applychanges) $applychanges = false;
else $applychanges = true;
require_once "Lib/dbschemasetup.php";
$updates = array();
$updates[] = array(
'title'=>"Database schema",
'description'=>"",
'operations'=>db_schema_setup($mysqli,load_db_schema(),$applychanges)
);
$error = !empty($updates[0]['operations']['error']) ? $updates[0]['operations']['error']: '';
$result = view("Modules/admin/update_view.php", array('applychanges'=>$applychanges, 'updates'=>$updates, 'error'=>$error));
}
else if ($route->action == 'users' && $session['write'])
{
$result = view("Modules/admin/userlist_view.php", array());
}
else if ($route->action == 'setuser' && $session['write'])
{
$_SESSION['userid'] = intval(get('id'));
header("Location: ../user/view");
}
else if ($route->action == 'downloadlog')
{
if ($log_enabled) {
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($log_filename) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
flush();
if (file_exists($log_filename)) {
readfile($log_filename);
}
else
{
echo($log_filename . " does not exist!");
}
exit;
}
}
else if ($route->action == 'getlog')
{
$route->format = "text";
if ($log_enabled) {
ob_start();
// PHP replacement for tail starts here
// full path to text file
define("TEXT_FILE", $log_filename);
// number of lines to read from the end of file
define("LINES_COUNT", 25);
function read_file($file, $lines) {
//global $fsize;
$handle = fopen($file, "r");
$linecounter = $lines;
$pos = -2;
$beginning = false;
$text = array();
while ($linecounter > 0) {
$t = " ";
while ($t != "\n") {
if(!empty($handle) && fseek($handle, $pos, SEEK_END) == -1) {
$beginning = true;
break;
}
if(!empty($handle)) $t = fgetc($handle);
$pos --;
}
$linecounter --;
if ($beginning) {
rewind($handle);
}
$text[$lines-$linecounter-1] = fgets($handle);
if ($beginning) break;
}
fclose ($handle);
return array_reverse($text);
}
$fsize = round(filesize(TEXT_FILE)/1024/1024,2);
$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {
echo $line;
} //End PHP replacement for Tail
$result = trim(ob_get_clean());
} else {
$result = "Log is disabled.";
}
}
else if ($allow_emonpi_admin && $route->action == 'emonpi') {
//put $update_logfile here so it can be referenced in other if statements
//before it was only accesable in the update subaction
//placed some other variables here as well so they are grouped
//together for the emonpi action even though they might not be used
//in the subaction
$update_logfile = "/home/pi/data/emonpiupdate.log";
$backup_logfile = "/home/pi/data/emonpibackup.log";
$update_flag = "/tmp/emoncms-flag-update";
$backup_flag = "/tmp/emonpibackup";
$update_script = "/home/pi/emonpi/service-runner-update.sh";
$backup_file = "/home/pi/data/backup.tar.gz";
if ($route->subaction == 'update' && $session['write'] && $session['admin']) {
$route->format = "text";
// Get update argument e.g. 'emonpi' or 'rfm69pi'
$argument="";
if (isset($_POST['argument'])) {
$argument = $_POST['argument'];
}
$redis->rpush("service-runner","$update_script $argument>$update_logfile");
$result = "service-runner trigger sent";
}
if ($route->subaction == 'getupdatelog' && $session['admin']) {
$route->format = "text";
ob_start();
passthru("cat " . $update_logfile);
$result = trim(ob_get_clean());
}
if ($route->subaction == 'downloadupdatelog' && $session['admin'])
{
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($update_logfile) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
flush();
if (file_exists($update_logfile))
{
ob_start();
readfile($update_logfile);
echo(trim(ob_get_clean()));
}
else
{
echo($update_logfile . " does not exist!");
}
exit;
}
if ($route->subaction == 'backup' && $session['write'] && $session['admin']) {
$route->format = "text";
$fh = @fopen($backup_flag,"w");
if (!$fh) $result = "ERROR: Can't write the flag $backup_flag.";
else $result = "Update flag file $backup_flag created. Update will start on next cron call in " . (60 - (time() % 60)) . "s...";
@fclose($fh);
}
if ($route->subaction == 'getbackuplog' && $session['admin']) {
$route->format = "text";
ob_start();
passthru("cat " . $backup_logfile);
$result = trim(ob_get_clean());
}
if ($route->subaction == 'downloadbackuplog' && $session['admin'])
{
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($backup_logfile) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
flush();
if (file_exists($backup_logfile)) {
ob_start();
readfile($backup_logfile);
echo(trim(ob_get_clean()));
}
else
{
echo($backup_logfile . " does not exist!");
}
exit;
}
if ($route->subaction == "downloadbackup" && $session['write'] && $session['admin']) {
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"" . basename($backup_file) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
readfile($backup_file);
exit;
}
if ($route->subaction == 'fs' && $session['admin'])
{
if (isset($_POST['argument'])) {
$argument = $_POST['argument'];
}
if ($argument == 'ro'){
$result = passthru('rpi-ro');
}
if ($argument == 'rw'){
$result = passthru('rpi-rw');
}
}
}
}
else if ($route->format == 'json')
{
if ($route->action == 'redisflush' && $session['write'])
{
$redis->flushDB();
$result = array('used'=>$redis->info()['used_memory_human'], 'dbsize'=>$redis->dbSize());
}
else if ($route->action == 'numberofusers')
{
$route->format = "text";
$result = $mysqli->query("SELECT COUNT(*) FROM users");
$row = $result->fetch_array();
$result = (int) $row[0];
}
else if ($route->action == 'userlist')
{
$limit = "";
if (isset($_GET['page']) && isset($_GET['perpage'])) {
$page = (int) $_GET['page'];
$perpage = (int) $_GET['perpage'];
$offset = $page * $perpage;
$limit = "LIMIT $perpage OFFSET $offset";
}
$orderby = "id";
if (isset($_GET['orderby'])) {
if ($_GET['orderby']=="id") $orderby = "id";
if ($_GET['orderby']=="username") $orderby = "username";
if ($_GET['orderby']=="email") $orderby = "email";
if ($_GET['orderby']=="email_verified") $orderby = "email_verified";
}
$order = "DESC";
if (isset($_GET['order'])) {
if ($_GET['order']=="decending") $order = "DESC";
if ($_GET['order']=="ascending") $order = "ASC";
}
$search = false;
$searchstr = "";
if (isset($_GET['search'])) {
$search = $_GET['search'];
$search_out = preg_replace('/[^\p{N}\p{L}_\s-@.]/u','',$search);
if ($search_out!=$search || $search=="") {
$search = false;
}
if ($search!==false) $searchstr = "WHERE username LIKE '%$search%' OR email LIKE '%$search%'";
}
$data = array();
$result = $mysqli->query("SELECT id,username,email,email_verified FROM users $searchstr ORDER BY $orderby $order ".$limit);
while ($row = $result->fetch_object()) $data[] = $row;
$result = $data;
}
else if ($route->action == 'setuser' && $session['write'])
{
$_SESSION['userid'] = intval(get('id'));
header("Location: ../user/view");
}
else if ($route->action == 'setuserfeed' && $session['write'])
{
$feedid = (int) get("id");
$result = $mysqli->query("SELECT userid FROM feeds WHERE id=$feedid");
$row = $result->fetch_object();
$userid = $row->userid;
$_SESSION['userid'] = $userid;
header("Location: ../user/view");
}
}
}
return array('content'=>$result);
}