reorganize code. Remove Server caching as it is done in Object.php.

This commit is contained in:
Isaac Connor 2020-11-11 11:49:44 -05:00
parent 0aef0adf48
commit 52c7cc5869
1 changed files with 46 additions and 54 deletions

View File

@ -6,7 +6,8 @@ $message='';
// INITIALIZE AND CHECK SANITY // INITIALIZE AND CHECK SANITY
// //
if ( !canView('System') ) $message = 'Insufficient permissions to view log entries for user '.$user['Username']; if ( !canView('System') )
$message = 'Insufficient permissions to view log entries for user '.$user['Username'];
// task must be set // task must be set
if ( !isset($_REQUEST['task']) ) { if ( !isset($_REQUEST['task']) ) {
@ -23,43 +24,6 @@ if ( $message ) {
return; return;
} }
// Search contains a user entered string to search on
$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
// Advanced search contains an array of "column name" => "search text" pairs
// Bootstrap table sends json_ecoded array, which we must decode
$advsearch = isset($_REQUEST['filter']) ? json_decode($_REQUEST['filter'], JSON_OBJECT_AS_ARRAY) : array();
// Sort specifies the name of the column to sort on
$sort = 'TimeKey';
if ( isset($_REQUEST['sort']) ) {
$sort = $_REQUEST['sort'];
if ( $sort == 'DateTime' ) $sort = 'TimeKey';
}
// Offset specifies the starting row to return, used for pagination
$offset = 0;
if ( isset($_REQUEST['offset']) ) {
if ( ( !is_int($_REQUEST['offset']) and !ctype_digit($_REQUEST['offset']) ) ) {
ZM\Error('Invalid value for offset: ' . $_REQUEST['offset']);
} else {
$offset = $_REQUEST['offset'];
}
}
// Order specifies the sort direction, either asc or desc
$order = (isset($_REQUEST['order']) and (strtolower($_REQUEST['order']) == 'asc')) ? 'ASC' : 'DESC';
// Limit specifies the number of rows to return
$limit = 100;
if ( isset($_REQUEST['limit']) ) {
if ( ( !is_int($_REQUEST['limit']) and !ctype_digit($_REQUEST['limit']) ) ) {
ZM\Error('Invalid value for limit: ' . $_REQUEST['limit']);
} else {
$limit = $_REQUEST['limit'];
}
}
// //
// MAIN LOOP // MAIN LOOP
// //
@ -69,7 +33,7 @@ switch ( $task ) {
createRequest(); createRequest();
break; break;
case 'query' : case 'query' :
$data = queryRequest($search, $advsearch, $sort, $offset, $order, $limit); $data = queryRequest();
break; break;
default : default :
ZM\Fatal('Unrecognised task '.$task); ZM\Fatal('Unrecognised task '.$task);
@ -105,9 +69,27 @@ function createRequest() {
} }
} }
function queryRequest($search, $advsearch, $sort, $offset, $order, $limit) { function queryRequest() {
global $Servers;
// Offset specifies the starting row to return, used for pagination
$offset = 0;
if ( isset($_REQUEST['offset']) ) {
if ( ( !is_int($_REQUEST['offset']) and !ctype_digit($_REQUEST['offset']) ) ) {
ZM\Error('Invalid value for offset: ' . $_REQUEST['offset']);
} else {
$offset = $_REQUEST['offset'];
}
}
// Limit specifies the number of rows to return
$limit = 100;
if ( isset($_REQUEST['limit']) ) {
if ( ( !is_int($_REQUEST['limit']) and !ctype_digit($_REQUEST['limit']) ) ) {
ZM\Error('Invalid value for limit: ' . $_REQUEST['limit']);
} else {
$limit = $_REQUEST['limit'];
}
}
// The table we want our data from // The table we want our data from
$table = 'Logs'; $table = 'Logs';
@ -117,11 +99,19 @@ function queryRequest($search, $advsearch, $sort, $offset, $order, $limit) {
// The names of columns shown in the log view that are NOT dB columns in the database // The names of columns shown in the log view that are NOT dB columns in the database
$col_alt = array('DateTime', 'Server'); $col_alt = array('DateTime', 'Server');
$sort = 'TimeKey';
if ( isset($_REQUEST['sort']) ) {
$sort = $_REQUEST['sort'];
if ( $sort == 'DateTime' ) $sort = 'TimeKey';
}
if ( !in_array($sort, array_merge($columns, $col_alt)) ) { if ( !in_array($sort, array_merge($columns, $col_alt)) ) {
ZM\Error('Invalid sort field: ' . $sort); ZM\Error('Invalid sort field: ' . $sort);
return; return;
} }
// Order specifies the sort direction, either asc or desc
$order = (isset($_REQUEST['order']) and (strtolower($_REQUEST['order']) == 'asc')) ? 'ASC' : 'DESC';
$col_str = implode(', ', $columns); $col_str = implode(', ', $columns);
$data = array(); $data = array();
$query = array(); $query = array();
@ -131,11 +121,17 @@ function queryRequest($search, $advsearch, $sort, $offset, $order, $limit) {
// There are two search bars in the log view, normal and advanced // There are two search bars in the log view, normal and advanced
// Making an exuctive decision to ignore the normal search, when advanced search is in use // Making an exuctive decision to ignore the normal search, when advanced search is in use
// Alternatively we could try to do both // Alternatively we could try to do both
//
// Advanced search contains an array of "column name" => "search text" pairs
// Bootstrap table sends json_ecoded array, which we must decode
$advsearch = isset($_REQUEST['filter']) ? json_decode($_REQUEST['filter'], JSON_OBJECT_AS_ARRAY) : array();
// Search contains a user entered string to search on
$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
if ( count($advsearch) ) { if ( count($advsearch) ) {
foreach ( $advsearch as $col=>$text ) { foreach ( $advsearch as $col=>$text ) {
if ( !in_array($col, array_merge($columns, $col_alt)) ) { if ( !in_array($col, array_merge($columns, $col_alt)) ) {
ZM\Error("'$col' is not a sortable column name"); ZM\Error("'$col' is not a searchable column name");
continue; continue;
} }
// Don't use wildcards on advanced search // Don't use wildcards on advanced search
@ -160,8 +156,6 @@ function queryRequest($search, $advsearch, $sort, $offset, $order, $limit) {
$query['sql'] = 'SELECT ' .$col_str. ' FROM `' .$table. '` ' .$where. ' ORDER BY ' .$sort. ' ' .$order. ' LIMIT ?, ?'; $query['sql'] = 'SELECT ' .$col_str. ' FROM `' .$table. '` ' .$where. ' ORDER BY ' .$sort. ' ' .$order. ' LIMIT ?, ?';
array_push($query['values'], $offset, $limit); array_push($query['values'], $offset, $limit);
//ZM\Warning('Calling the following sql query: ' .$query['sql']);
$data['totalNotFiltered'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table, 'Total'); $data['totalNotFiltered'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table, 'Total');
if ( $search != '' || count($advsearch) ) { if ( $search != '' || count($advsearch) ) {
$data['total'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table.$where , 'Total', $wherevalues); $data['total'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table.$where , 'Total', $wherevalues);
@ -169,18 +163,16 @@ function queryRequest($search, $advsearch, $sort, $offset, $order, $limit) {
$data['total'] = $data['totalNotFiltered']; $data['total'] = $data['totalNotFiltered'];
} }
if ( !$Servers ) $rows = array();
$Servers = ZM\Server::find(); $results = dbFetchAll($query['sql'], NULL, $query['values']);
$servers_by_Id = array(); if ( !$results ) {
# There is probably a better way to do this. return $data;
foreach ( $Servers as $server ) {
$servers_by_Id[$server->Id()] = $server;
} }
$rows = array(); foreach ( $results as $row ) {
foreach ( dbFetchAll($query['sql'], NULL, $query['values']) as $row ) {
$row['DateTime'] = strftime('%Y-%m-%d %H:%M:%S', intval($row['TimeKey'])); $row['DateTime'] = strftime('%Y-%m-%d %H:%M:%S', intval($row['TimeKey']));
$row['Server'] = ( $row['ServerId'] and isset($servers_by_Id[$row['ServerId']]) ) ? $servers_by_Id[$row['ServerId']]->Name() : ''; $Server = new ZM\Server($row['ServerId']);
$row['Server'] = $Server->Name();
// First strip out any html tags // First strip out any html tags
// Second strip out all characters that are not ASCII 32-126 (yes, 126) // Second strip out all characters that are not ASCII 32-126 (yes, 126)
$row['Message'] = preg_replace('/[^\x20-\x7E]/', '', strip_tags($row['Message'])); $row['Message'] = preg_replace('/[^\x20-\x7E]/', '', strip_tags($row['Message']));