示例#1
0
 /**
  * Removes old entries from the db
  */
 function purge_db()
 {
     // SimpleLogger()->debug("Simple History is running purge_db()");
     $do_purge_history = true;
     $do_purge_history = apply_filters("simple_history_allow_db_purge", $do_purge_history);
     $do_purge_history = apply_filters("simple_history/allow_db_purge", $do_purge_history);
     if (!$do_purge_history) {
         return;
     }
     $days = $this->get_clear_history_interval();
     // Never clear log if days = 0
     if (0 == $days) {
         return;
     }
     global $wpdb;
     $table_name = $wpdb->prefix . SimpleHistory::DBTABLE;
     $table_name_contexts = $wpdb->prefix . SimpleHistory::DBTABLE_CONTEXTS;
     // Get id of rows to delete
     $sql = "SELECT id FROM {$table_name} WHERE DATE_ADD(date, INTERVAL {$days} DAY) < now()";
     $ids_to_delete = $wpdb->get_col($sql);
     if (empty($ids_to_delete)) {
         // Nothing to delete
         return;
     }
     $sql_ids_in = implode(",", $ids_to_delete);
     // Add number of deleted rows to total_rows option
     $prev_total_rows = (int) get_option("simple_history_total_rows", 0);
     $total_rows = $prev_total_rows + sizeof($ids_to_delete);
     update_option("simple_history_total_rows", $total_rows);
     // Remove rows + contexts
     $sql_delete_history = "DELETE FROM {$table_name} WHERE id IN ({$sql_ids_in})";
     $sql_delete_history_context = "DELETE FROM {$table_name_contexts} WHERE history_id IN ({$sql_ids_in})";
     $wpdb->query($sql_delete_history);
     $wpdb->query($sql_delete_history_context);
     $message = _nx("Simple History removed one event that were older than {days} days", "Simple History removed {num_rows} events that were older than {days} days", "Database is being cleared automagically", "simple-history");
     SimpleLogger()->info($message, array("days" => $days, "num_rows" => sizeof($ids_to_delete)));
     $this->get_cache_incrementor(true);
 }
// in the log overview, even if the logged messages are different
for ($i = 0; $i < rand(1, 50); $i++) {
    SimpleLogger()->notice("User {username} edited page {pagename}", array("username" => "example_user_{$i}", "pagename" => "My test page", "_occasionsID" => "postID:24884,action:edited"));
}
// Events can have different "initiators",
// i.e. who was responsible for the logged event
// Initiator "WORDPRESS" means that WordPress did something on it's own
SimpleLogger()->info("WordPress updated itself from version {from_version} to {to_version}", array("from_version" => "3.8", "to_version" => "3.8.1", "_initiator" => SimpleLoggerLogInitiators::WORDPRESS));
// Initiator "WP_USER" means that a logged in user did someting
SimpleLogger()->info("Updated plugin {plugin_name} from version {plugin_from_version} to version {plugin_to_version}", array("plugin_name" => "Ninja Forms", "plugin_from_version" => "1.1", "plugin_to_version" => "1.1.2", "_initiator" => SimpleLoggerLogInitiators::WP_USER));
// // Initiator "WEB_USER" means that an unknown internet user did something
SimpleLogger()->warning("An attempt to login as user 'administrator' failed to login because the wrong password was entered", array("_initiator" => SimpleLoggerLogInitiators::WEB_USER));
// Use the "context array" to add  more data to your logged event
// Data can be used later on to show detailed info about a log entry
// and does not need to be shown on the overview screen
SimpleLogger()->info("Edited product '{pagename}'", array("pagename" => "We are hiring!", "_postType" => "product", "_userID" => 1, "_userLogin" => "jessie", "_userEmail" => "*****@*****.**", "_occasionsID" => "username:1,postID:24885,action:edited"));
// Test log cron things
/*
wp_schedule_event( time(), "hourly", "simple_history_cron_testhook");
*/
/*
wp_clear_scheduled_hook("simple_history_cron_testhook");
add_action( 'simple_history_cron_testhook', 'simple_history_cron_testhook_function' );
function simple_history_cron_testhook_function() {
	SimpleLogger()->info("This is a message inside a cron function");
}
*/
/*
add_action("init", function() {

	global $wp_current_filter;
 public function doLogTestThings()
 {
     // Add some data random back in time, to fill up the log to test much data
     for ($j = 0; $j < 50; $j++) {
         // between yesteday and a month back in time
         for ($i = 0; $i < rand(1, 30); $i++) {
             $str_date = date('Y-m-d H:i:s', strtotime("now -{$i}days"));
             SimpleLogger()->info('Entry with date in the past', array("_date" => $str_date, "_occasionsID" => "past_date:{$str_date}"));
         }
     }
     SimpleLogger()->info("This is a message sent to the log");
     // Second log entry with same info will make these two become an occasionGroup,
     // collapsing their entries into one expandable log item
     SimpleLogger()->info("This is a message sent to the log");
     // Log entries can be of different severity
     SimpleLogger()->info("User admin edited page 'About our company'");
     SimpleLogger()->warning("User 'Jessie' deleted user 'Kim'");
     SimpleLogger()->debug("Ok, cron job is running!");
     // Log entries can have placeholders and context
     // This makes log entried translatable and filterable
     for ($i = 0; $i < rand(1, 50); $i++) {
         SimpleLogger()->notice("User {username} edited page {pagename}", array("username" => "bonnyerden", "pagename" => "My test page", "_initiator" => SimpleLoggerLogInitiators::WP_USER, "_user_id" => rand(1, 20), "_user_login" => "loginname" . rand(1, 20), "_user_email" => "user" . rand(1, 20) . "@example.com"));
     }
     #return;
     // Log entried can have custom occasionsID
     // This will group items together and a log entry will only be shown once
     // in the log overview
     for ($i = 0; $i < rand(1, 50); $i++) {
         SimpleLogger()->notice("User {username} edited page {pagename}", array("username" => "admin", "pagename" => "My test page", "_occasionsID" => "username:1,postID:24884,action:edited"));
     }
     SimpleLogger()->info("WordPress updated itself from version {from_version} to {to_version}", array("from_version" => "3.8", "to_version" => "3.8.1", "_initiator" => SimpleLoggerLogInitiators::WORDPRESS));
     SimpleLogger()->info("Plugin {plugin_name} was updated from version {plugin_from_version} to version {plugin_to_version}", array("plugin_name" => "CMS Tree Page View", "plugin_from_version" => "4.0", "plugin_to_version" => "4.2", "_initiator" => SimpleLoggerLogInitiators::WORDPRESS));
     SimpleLogger()->info("Updated plugin {plugin_name} from version {plugin_from_version} to version {plugin_to_version}", array("plugin_name" => "Ninja Forms", "plugin_from_version" => "1.1", "plugin_to_version" => "1.1.2", "_initiator" => SimpleLoggerLogInitiators::WP_USER));
     SimpleLogger()->warning("An attempt to login as user 'administrator' failed to login because the wrong password was entered", array("_initiator" => SimpleLoggerLogInitiators::WEB_USER));
     SimpleLogger()->info("Updated plugin {plugin_name} from version {plugin_from_version} to version {plugin_to_version}", array("plugin_name" => "Simple Fields", "plugin_from_version" => "1.3.7", "plugin_to_version" => "1.3.8", "_initiator" => SimpleLoggerLogInitiators::WP_USER));
     SimpleLogger()->error("A JavaScript error was detected on page 'About us'", array("_initiator" => SimpleLoggerLogInitiators::WEB_USER));
     SimpleLogger()->debug("WP Cron 'my_test_cron_job' finished in 0.012 seconds", array("_initiator" => SimpleLoggerLogInitiators::WORDPRESS));
     for ($i = 0; $i < rand(50, 1000); $i++) {
         SimpleLogger()->warning('An attempt to login as user "{user_login}" failed to login because the wrong password was entered', array("user_login" => "admin", "_userID" => null, "_initiator" => SimpleLoggerLogInitiators::WEB_USER));
     }
     // Add more data to context array. Data can be used later on to show detailed info about a log entry.
     SimpleLogger()->info("Edited product '{pagename}'", array("pagename" => "We are hiring!", "_postType" => "product", "_userID" => 1, "_userLogin" => "jessie", "_userEmail" => "*****@*****.**", "_occasionsID" => "username:1,postID:24885,action:edited"));
     SimpleLogger()->debug("This is a message with no translation");
     SimpleLogger()->debug(__("Plugin"), array("comment" => "This message is 'Plugin' and should contain text domain 'default' since it's a translation that comes with WordPress"));
     SimpleLogger()->debug(__("Enter title of new page", "cms-tree-page-view"), array("comment" => "A translation used in CMS Tree Page View"));
 }
示例#4
0
 function test_logging()
 {
     global $wpdb;
     $table_name_simple_history = $wpdb->prefix . SimpleHistory::DBTABLE;
     $refl_log_levels = new ReflectionClass('SimpleLoggerLogLevels');
     $log_levels = (array) $refl_log_levels->getConstants();
     $refl_log_initiators = new ReflectionClass('SimpleLoggerLogInitiators');
     $log_initiators = (array) $refl_log_initiators->getConstants();
     foreach ($log_levels as $level_const => $level_str) {
         foreach ($log_initiators as $initiator_const => $initiator_str) {
             $message = "This is a message with log level {$level_str}";
             SimpleLogger()->log($level_str, $message, array("_initiator" => $initiator_str));
             // Last logged message in db should be the above
             $db_row = $wpdb->get_row("SELECT logger, level, message, initiator FROM {$table_name_simple_history} ORDER BY id DESC LIMIT 1", ARRAY_A);
             $expected_row = array('logger' => "SimpleLogger", 'level' => $level_str, 'message' => $message, 'initiator' => $initiator_str);
             $this->assertEquals($expected_row, $db_row, "logged event in db");
         }
     }
     // TODO: test logging with context
 }
 private function trigger($slug, $message, $level = 'error', $prefix = '')
 {
     if (empty($prefix)) {
         $prefix = $this->prefix;
     }
     // Trigger fail2ban
     $error_msg = sprintf('%s%s %s', $prefix, $slug, $this->esc_log($message));
     $this->enhanced_error_log($error_msg, $level);
     // Report to Sucuri Scan
     if (class_exists('SucuriScanEvent')) {
         if (true !== SucuriScanEvent::report_critical_event($error_msg)) {
             error_log('Sucuri Scan report event failure.');
         }
     }
     // Report to Simple History
     if (function_exists('SimpleLogger')) {
         $simple_level = $this->translate_apache_level($level);
         $context = array('_security' => 'WordPress fail2ban', '_server_request_method' => $this->esc_log($_SERVER['REQUEST_METHOD']));
         if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
             $context['_server_http_user_agent'] = $this->esc_log($_SERVER['HTTP_USER_AGENT']);
         }
         if (!class_exists('SimpleLogger')) {
             SimpleHistory::get_instance()->load_loggers();
         }
         SimpleLogger()->log($simple_level, $error_msg, $context);
     }
 }