From 52c7cc5869484ef967654b29dbd25567d90b989f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 11 Nov 2020 11:49:44 -0500 Subject: [PATCH] reorganize code. Remove Server caching as it is done in Object.php. --- web/ajax/log.php | 100 ++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/web/ajax/log.php b/web/ajax/log.php index 22897fd04..60f0c1dc4 100644 --- a/web/ajax/log.php +++ b/web/ajax/log.php @@ -1,12 +1,13 @@ "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 // @@ -69,7 +33,7 @@ switch ( $task ) { createRequest(); break; case 'query' : - $data = queryRequest($search, $advsearch, $sort, $offset, $order, $limit); + $data = queryRequest(); break; default : ZM\Fatal('Unrecognised task '.$task); @@ -105,9 +69,27 @@ function createRequest() { } } -function queryRequest($search, $advsearch, $sort, $offset, $order, $limit) { - global $Servers; +function queryRequest() { + // 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 $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 $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)) ) { ZM\Error('Invalid sort field: ' . $sort); 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); $data = 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 // Making an exuctive decision to ignore the normal search, when advanced search is in use // 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) ) { foreach ( $advsearch as $col=>$text ) { 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; } // 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 ?, ?'; 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'); if ( $search != '' || count($advsearch) ) { $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']; } - if ( !$Servers ) - $Servers = ZM\Server::find(); - $servers_by_Id = array(); - # There is probably a better way to do this. - foreach ( $Servers as $server ) { - $servers_by_Id[$server->Id()] = $server; + $rows = array(); + $results = dbFetchAll($query['sql'], NULL, $query['values']); + if ( !$results ) { + return $data; } - $rows = array(); - foreach ( dbFetchAll($query['sql'], NULL, $query['values']) as $row ) { + foreach ( $results as $row ) { $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 // Second strip out all characters that are not ASCII 32-126 (yes, 126) $row['Message'] = preg_replace('/[^\x20-\x7E]/', '', strip_tags($row['Message']));