From 6fb2b39e018bbaaa64fc793996a67e1d1810de40 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 6 Sep 2019 16:15:01 -0400 Subject: [PATCH] Save date in file name. Ability to now select which log file date to view. Get the user name (login name) for user id. --- controllers/admin.php | 30 ++++++++++++++++---------- models/admin/logs/index.php | 43 ++++++++++++++++++++++++++++++++++--- views/admin/logs/index.html | 31 +++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 17 deletions(-) diff --git a/controllers/admin.php b/controllers/admin.php index 8fb24339..2a484042 100755 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -1124,18 +1124,26 @@ class glmMembersAdmin extends GlmPluginSupport public function logUserActions() { - $logFile = WP_CONTENT_DIR . '/user.log'; - $timeZone = get_option( 'timezone_string' ); - $cTimezone = date_default_timezone_get(); - date_default_timezone_set( $timeZone ); - - $errorLogTime = date( 'j-M-Y H:i:s e' ); $user_id = get_current_user_id(); - if ( !empty( $_POST ) ) { - error_log( "[{$errorLogTime}] USER: " . $user_id . " POST: " . serialize( $_POST ) . "\n", 3, $logFile ); - } - if ( !empty( $_GET ) ) { - error_log( "[{$errorLogTime}] USER: " . $user_id . " GET: " . serialize( $_GET ). "\n", 3, $logFile ); + if ( !empty( $user_id ) ) { + // $logFile = WP_CONTENT_DIR . '/user.log'; + $logFile = GLM_MEMBERS_PLUGIN_MEDIA_PATH . '/' . date('Y-m-d') . '-user.log'; + $timeZone = get_option( 'timezone_string' ); + $cTimezone = date_default_timezone_get(); + date_default_timezone_set( $timeZone ); + + $errorLogTime = date( 'j-M-Y H:i:s e' ); + + if ( !empty( $_POST ) ) { + $type = 'POST'; + $typeData = $_POST; + } + if ( !empty( $_GET ) ) { + $type = 'GET'; + $typeData = $_GET; + } + + error_log( "[{$errorLogTime}]\tIP: {$_SERVER['REMOTE_ADDR']}\tUSER: {$user_id}\t{$type}: " . serialize( $typeData ) . "\n", 3, $logFile ); } date_default_timezone_set( $cTimezone ); } diff --git a/models/admin/logs/index.php b/models/admin/logs/index.php index 40e30b3f..a41bfc00 100644 --- a/models/admin/logs/index.php +++ b/models/admin/logs/index.php @@ -93,19 +93,55 @@ class GlmMembersAdmin_logs_index public function modelAction ($actionData = false) { // Set the view file + $logFiles = array(); $view = 'index.html'; $fileData = false; - $logFile = WP_CONTENT_DIR . '/user.log'; + + // Defaults to today's date + $logFile = GLM_MEMBERS_PLUGIN_MEDIA_PATH . '/' . date('Y-m-d') . '-user.log'; + + if ( isset( $_REQUEST['logFile'] ) && $_REQUEST['logFile'] && is_readable( GLM_MEMBERS_PLUGIN_MEDIA_PATH . '/' . $_REQUEST['logFile'] ) ) { + $logFile = GLM_MEMBERS_PLUGIN_MEDIA_PATH . '/' . $_REQUEST['logFile']; + } if ( isset( $_REQUEST['option'] ) ) { $option = filter_var( $_REQUEST['option'] ); } + // Get a list of log files. + // Log files will be date then -user.log + $glmFiles = scandir( GLM_MEMBERS_PLUGIN_MEDIA_PATH ); + if ( !empty( $glmFiles ) ) { + foreach ( $glmFiles as $fileItem ) { + if ( preg_match( '/[0-9]{4}-[0-9]{2}-[0-9]{2}-user.log/', $fileItem ) ) { + $logFiles[] = $fileItem; + } + } + } + $logFiles = array_reverse( $logFiles ); + // echo '
$logFiles: ' . print_r( $logFiles, true ) . '
'; + switch( $option ) { default: + $logFileContent = array(); // get the log file contents - $fileData = file_get_contents( $logFile ); + $file = file( $logFile ); + + foreach ( $file as $lineNumber => $line ) { + $lineContents = explode( "\t", $line ); + foreach ( $lineContents as $key => &$item ) { + if ( preg_match( '/USER: ([0-9]*)/', $item, $userMatch ) ) { + // echo '
$userMatch: ' . print_r( $userMatch, true ) . '
'; + // get user name + if ( $userId = filter_var( $userMatch[1], FILTER_VALIDATE_INT ) ) { + $user = get_user_by( 'id', $userId ); + $item = 'USER: ' . $user->user_login; + } + } + } + $logFileContent[$lineNumber] = $lineContents; + } break; @@ -113,7 +149,8 @@ class GlmMembersAdmin_logs_index // Setup the template data array $templateData = array( - 'fileData' => $fileData, + 'fileData' => $logFileContent, + 'logFiles' => $logFiles, ); // Return status, suggested view, and data to controller diff --git a/views/admin/logs/index.html b/views/admin/logs/index.html index fc98eb97..ef76e092 100644 --- a/views/admin/logs/index.html +++ b/views/admin/logs/index.html @@ -8,10 +8,35 @@ {include file='admin/logs/header.html'}

Log File

+
+ + {if !empty($logFiles)} + + {/if} + + + +
{* Display the Log $fileData *} -
- {$fileData|nl2br} -
+ + + + + + + + {foreach $fileData as $lineNumber => $content} + + {foreach $content as $item} + + {/foreach} + + {/foreach} +
TimeIP AddressUser IdData
{$item}
{* Include Footer *} {include file='admin/footer.html'} -- 2.17.1