From cb559c47829e38ace9f42987e5bbe2c59050173d Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 30 Aug 2019 08:22:05 -0400 Subject: [PATCH] Working on the logging of admin and ajax activity. Records the user id and the get or post request. --- controllers/admin.php | 30 +++++--- models/admin/logs/index.php | 131 +++++++++++++++++++++++++++++++++++ setup/adminMenus.php | 10 +++ setup/validActions.php | 5 +- views/admin/logs/header.html | 3 + views/admin/logs/index.html | 17 +++++ 6 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 models/admin/logs/index.php create mode 100644 views/admin/logs/header.html create mode 100644 views/admin/logs/index.html diff --git a/controllers/admin.php b/controllers/admin.php index 3641534d..8fb24339 100755 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -253,6 +253,8 @@ class glmMembersAdmin extends GlmPluginSupport public function glmMembersAdminAjax() { + $this->logUserActions(); + if ( GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE ) { trigger_error( glmAssociateMemoryUsage() . " - Start AJAX Controller", @@ -731,14 +733,7 @@ class glmMembersAdmin extends GlmPluginSupport $returnOutput = false, $forceAction = false) { - // TODO: setup audit logging for users - // $errorLogTime = date( 'Y-m-d H:i:s' ); - // if ( !empty( $_POST ) ) { - // error_log( $errorLogTime . " POST: " . serialize( $_POST ) . "\n", 3, '/var/www/develop/wp-content/error.log' ); - // } - // if ( !empty( $_GET ) ) { - // error_log( $errorLogTime . " GET: " . serialize( $_GET ). "\n", 3, '/var/www/develop/wp-content/error.log' ); - // } + $this->logUserActions(); if ( GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE ) { trigger_error( @@ -1127,4 +1122,23 @@ 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 ); + } + date_default_timezone_set( $cTimezone ); + } + + } diff --git a/models/admin/logs/index.php b/models/admin/logs/index.php new file mode 100644 index 00000000..40e30b3f --- /dev/null +++ b/models/admin/logs/index.php @@ -0,0 +1,131 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +/* + * This class performs the work for the default action of the "Logs" menu + * option. + * + */ +class GlmMembersAdmin_logs_index +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + /** + * Constructor + * + * This contractor sets up this model. At this time that only includes + * storing away the WordPress data object. + * + * @return object Class object + * + */ + public function __construct ( $wpdb, $config ) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + /** + * Perform Model Action + * + * This method does the work for this model and returns any resulting data + * + * @return array Status and data array + * + * 'status' + * + * True if successful and false if there was a fatal failure. + * + * 'menuItemRedirect' + * + * If not false, provides a menu item the controller should + * execute after this one. Normally if this is used, there would also be a + * modelRedirect value supplied as well. + * + * 'modelRedirect' + * + * If not false, provides an action the controller should execute after + * this one. + * + * 'view' + * + * A suggested view name that the controller should use instead of the + * default view for this model or false to indicate that the default view + * should be used. + * + * 'data' + * + * Data that the model is returning for use in merging with the view to + * produce output. + * + */ + public function modelAction ($actionData = false) + { + // Set the view file + $view = 'index.html'; + $fileData = false; + $logFile = WP_CONTENT_DIR . '/user.log'; + + if ( isset( $_REQUEST['option'] ) ) { + $option = filter_var( $_REQUEST['option'] ); + } + + switch( $option ) { + + default: + // get the log file contents + $fileData = file_get_contents( $logFile ); + + break; + + } + + // Setup the template data array + $templateData = array( + 'fileData' => $fileData, + ); + + // Return status, suggested view, and data to controller + return array( + 'status' => true, + 'menuItemRedirect' => false, + 'modelRedirect' => false, + 'view' => 'admin/logs/' . $view, + 'data' => $templateData, + ); + + } + + +} diff --git a/setup/adminMenus.php b/setup/adminMenus.php index 91458b74..e28a5335 100644 --- a/setup/adminMenus.php +++ b/setup/adminMenus.php @@ -78,6 +78,16 @@ if (current_user_can('glm_members_members')) { 'glm-members-admin-menu-member', function() {$this->controller('member');} ); + + // Add a submenu for the "Logs" + add_submenu_page( + $mainMenuSlug, + 'Logs', + 'Logs', + 'glm_members_member', + 'glm-members-admin-menu-logs', + function() {$this->controller('logs');} + ); } else { add_menu_page( $this->config['terms']['term_admin_menu_members'], diff --git a/setup/validActions.php b/setup/validActions.php index 53467cec..82b11d50 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -109,7 +109,10 @@ $glmMembersValidActions = array( ), 'import' => array( 'index' => 'glm-member-db', - ) + ), + 'logs' => array( + 'index' => 'glm-member-db', + ), ), 'frontActions' => array( 'members' => array( diff --git a/views/admin/logs/header.html b/views/admin/logs/header.html new file mode 100644 index 00000000..cfd19e89 --- /dev/null +++ b/views/admin/logs/header.html @@ -0,0 +1,3 @@ +
+ +
diff --git a/views/admin/logs/index.html b/views/admin/logs/index.html new file mode 100644 index 00000000..fc98eb97 --- /dev/null +++ b/views/admin/logs/index.html @@ -0,0 +1,17 @@ +{* + Log View File + + Displays the Entire log file. +*} + +{* Include Header *} +{include file='admin/logs/header.html'} +

Log File

+ +{* Display the Log $fileData *} +
+ {$fileData|nl2br} +
+ +{* Include Footer *} +{include file='admin/footer.html'} -- 2.17.1