Save date in file name.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 6 Sep 2019 20:15:01 +0000 (16:15 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 6 Sep 2019 20:15:01 +0000 (16:15 -0400)
Ability to now select which log file date to view.
Get the user name (login name) for user id.

controllers/admin.php
models/admin/logs/index.php
views/admin/logs/index.html

index 8fb2433..2a48404 100755 (executable)
@@ -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 );
     }
index 40e30b3..a41bfc0 100644 (file)
@@ -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 '<pre>$logFiles: ' . print_r( $logFiles, true ) . '</pre>';
+
         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 '<pre>$userMatch: ' . print_r( $userMatch, true ) . '</pre>';
+                        // 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
index fc98eb9..ef76e09 100644 (file)
@@ -8,10 +8,35 @@
 {include file='admin/logs/header.html'}
 <h2>Log File</h2>
 
+<form method="post" action="{$thisUrl}?page={$thisPage}">
+
+    {if !empty($logFiles)}
+        <select id="logFile" name="logFile">
+            {foreach $logFiles as $fileName}
+                <option value="{$fileName}">{$fileName}</option>
+            {/foreach}
+        </select>
+    {/if}
+
+    <input type="submit" name="Load">
+
+</form>
 {* Display the Log $fileData *}
-<div>
-    {$fileData|nl2br}
-</div>
+<table class="wp-list-table widefat fixed striped">
+    <tr>
+        <th width="250">Time</th>
+        <th width="100">IP Address</th>
+        <th width="100">User Id</th>
+        <th>Data</th>
+    </tr>
+    {foreach $fileData as $lineNumber => $content}
+        <tr>
+            {foreach $content as $item}
+                <td style="padding: 5px;">{$item}</td>
+            {/foreach}
+        </tr>
+    {/foreach}
+</table>
 
 {* Include Footer *}
 {include file='admin/footer.html'}