-
Notifications
You must be signed in to change notification settings - Fork 10
/
admin_dl.php
79 lines (63 loc) · 2.68 KB
/
admin_dl.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
<?php
$start_time = microtime(true);
require_once( 'common.php' );
require_once( 'user.php' );
$util::logInfo( 'admin_dl: Start' );
$reply['result'] = false;
$uname = (isset($_REQUEST['user'])) ? $_REQUEST['user'] : null; // Set uname to chosen user name (or null if not chosen)
$session = (isset($_REQUEST['session'])) ? $_REQUEST['session'] : null; // Set session to chosen session id (or null if not chosen)
$login = new USER( $uname, $session );
$util::logDebug( 'admin_dl: Test user ' . $login->getName() );
if( ! $login->isLoggedin() || ! $login->isSiteAdmin() ){
$util::logError( "admin_dl: called by someone other than admin" );
die();
}
//$util::logDebug( 'admin_dl: User OK' );
function backupAllTables(){
global $util;
// QQQ Really should make a global database connection that everyone shares instead of reinstantiating it all over the place.
// QQQ That means doing it like I have done the util with static functiond and everything.
// QQQ Need an abstract-ish version that has generic stuff and a project level one that extends the vase class
$database = new Database();
// QQQ Really should make a global database connection that everyone shares instead of reinstantiating it all over the place.
$now = date( 'Y-m-d-H-i', time() );
$util::logInfo( 'admin_dl: backupAllTables: Backup starting.' );
$tableList = array( 'hvac_cycles', 'hvac_status', 'locations', 'location_data', 'meters', 'meter_data', 'run_times', 'setpoints', 'thermostats', 'thermostat_data', 'users' );
foreach( $tableList as $tableName ){
$util::logInfo( 'admin_dl: backupAllTables: Backup starting for table: (' . $tableName . ')' );
$database->backupOneTable( $tableName, $now );
}
$util::logInfo( 'admin_dl: backupAllTables: Backup complete.' );
return true;
}
$validActions = [ 'backup'
,'clean_logs' ];
if( isset( $_GET[ 'action' ] ) ){
$action = $_GET[ 'action' ];
$reply['action'] = $action;
if( ! in_array( $action, $validActions, true ) ){
$util::logError( "admin_dl: front end is trying invalid action: ($action)" );
}
else{
switch( $action ){
case 'clean_logs':
$howMany = $util::logClean();
$reply['result'] = true;
$reply['clean_logs'] = $howMany;
break;
case 'backup':
$reply['result'] = backupAllTables();
// $reply['backup'] =
break;
default:
$util::logError( "admin_dl: action deemed valid, but unsupported: ($action)" );
break;
}
}
}
$answer = array();
$answer[ 'result' ] = $reply;
echo json_encode( array( 'answer' => $answer), JSON_NUMERIC_CHECK );
$log::logInfo( 'admin_dl: execution time was ' . (microtime(true) - $start_time) . ' seconds.' );
return 0;
?>