Skip to content

Commit

Permalink
Merge pull request #76 from erommel/master
Browse files Browse the repository at this point in the history
Added "Export to CSV" buttons for 404 Event Logs, Account Activity Lo…
  • Loading branch information
Arsenal21 authored Feb 6, 2017
2 parents fa67154 + d316ad9 commit 2426fbb
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 56 deletions.
94 changes: 89 additions & 5 deletions all-in-one-wp-security/admin/wp-security-admin-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,101 @@ class AIOWPSecurity_Admin_Init
var $filescan_menu;
var $misc_menu;

function __construct()
{
function __construct() {
//This class is only initialized if is_admin() is true
$this->admin_includes();
add_action('admin_menu', array(&$this, 'create_admin_menus'));
//handle CSV download
add_action('admin_init', array(&$this, 'aiowps_csv_download'));

//make sure we are on our plugin's menu pages
if (isset($_GET['page']) && strpos($_GET['page'], AIOWPSEC_MENU_SLUG_PREFIX ) !== false ) {
if (isset($_GET['page']) && strpos($_GET['page'], AIOWPSEC_MENU_SLUG_PREFIX) !== false) {
add_action('admin_print_scripts', array(&$this, 'admin_menu_page_scripts'));
add_action('admin_print_styles', array(&$this, 'admin_menu_page_styles'));
add_action('init', array( &$this, 'init_hook_handler_for_admin_side'));
add_action('admin_print_styles', array(&$this, 'admin_menu_page_styles'));
add_action('init', array(&$this, 'init_hook_handler_for_admin_side'));
}
}

private function aiowps_output_csv($items, $export_keys, $filename='data.csv') {
header("Content-Type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=".$filename);
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen('php://output', 'w'); //open output stream

fputcsv($output, $export_keys); //let's put column names first

foreach ($items as $item) {
unset($csv_line);
foreach ($export_keys as $key => $value) {
if (isset($item[$key])) {
$csv_line[] = $item[$key];
}
}
fputcsv($output, $csv_line);
}
}

function aiowps_csv_download() {
global $aio_wp_security;
if (isset($_POST['aiowpsec_export_acct_activity_logs_to_csv'])) { //Export account activity logs
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'aiowpsec-export-acct-activity-logs-to-csv-nonce')) {
$aio_wp_security->debug_logger->log_debug("Nonce check failed for export account activity logs to CSV!", 4);
die(__('Nonce check failed for export account activity logs to CSV!', 'all-in-one-wp-security-and-firewall'));
}
include_once 'wp-security-list-acct-activity.php';
$acct_activity_list = new AIOWPSecurity_List_Account_Activity();
$acct_activity_list->prepare_items(true);
//Let's build a list of items we want to export and give them readable names
$export_keys = array(
'user_id' => 'User ID',
'user_login' => 'Username',
'login_date' => 'Login Date',
'logout_date' => 'Logout Date',
'login_ip' => 'IP'
);
$this->aiowps_output_csv($acct_activity_list->items, $export_keys, 'account_activity_logs.csv');
exit();
}
if (isset($_POST['aiowps_export_failed_login_records_to_csv'])) {//Export failed login records
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'aiowpsec-export-failed-login-records-to-csv-nonce')) {
$aio_wp_security->debug_logger->log_debug("Nonce check failed for export failed login records to CSV!", 4);
die(__('Nonce check failed for export failed login records to CSV!', 'all-in-one-wp-security-and-firewall'));
}
include_once 'wp-security-list-login-fails.php';
$failed_login_list = new AIOWPSecurity_List_Login_Failed_Attempts();
$failed_login_list->prepare_items(true);
$export_keys = array(
'login_attempt_ip' => 'Login IP Range',
'user_id' => 'User ID',
'user_login' => 'Username',
'failed_login_date' => 'Date',
);
$this->aiowps_output_csv($failed_login_list->items, $export_keys, 'failed_login_records.csv');
exit();
}
if (isset($_POST['aiowps_export_404_event_logs_to_csv'])) {//Export 404 event logs
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'aiowpsec-export-404-event-logs-to-csv-nonce')) {
$aio_wp_security->debug_logger->log_debug("Nonce check failed for export 404 event logs to CSV!", 4);
die(__('Nonce check failed for export 404 event logs to CSV!', 'all-in-one-wp-security-and-firewall'));
}
include_once 'wp-security-list-404.php'; //For rendering the AIOWPSecurity_List_Table in tab1
$event_list_404 = new AIOWPSecurity_List_404(); //For rendering the AIOWPSecurity_List_Table in tab1
$event_list_404->prepare_items(true);
$export_keys = array(
'id' => 'ID',
'event_type' => 'Event Type',
'ip_or_host' => 'IP Address',
'url' => 'Attempted URL',
'referer_info' => 'Referer',
'event_date' => 'Date',
'status' => 'Lock Status',
);
$this->aiowps_output_csv($event_list_404->items, $export_keys, '404_event_logs.csv');
exit();
}
}

Expand Down
13 changes: 13 additions & 0 deletions all-in-one-wp-security/admin/wp-security-firewall-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,19 @@ function render_tab6()
</form>
</div></div>
<div class="postbox">
<h3 class="hndle"><label for="title"><?php _e('Export to CSV', 'all-in-one-wp-security-and-firewall'); ?></label></h3>
<div class="inside">
<form action="" method="POST">
<?php wp_nonce_field('aiowpsec-export-404-event-logs-to-csv-nonce'); ?>
<table class="form-table">
<tr valign="top">
<span class="description"><?php _e('Click this button if you wish to download this log in CSV format.', 'all-in-one-wp-security-and-firewall'); ?></span>
</tr>
</table>
<input type="submit" name="aiowps_export_404_event_logs_to_csv" value="<?php _e('Export to CSV', 'all-in-one-wp-security-and-firewall')?>" class="button-primary"/>
</form>
</div></div>
<div class="postbox">
<h3 class="hndle"><label for="title"><?php _e('Delete All 404 Event Logs', 'all-in-one-wp-security-and-firewall'); ?></label></h3>
<div class="inside">
<form action="" method="POST">
Expand Down
20 changes: 11 additions & 9 deletions all-in-one-wp-security/admin/wp-security-list-404.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function delete_404_event_records($entries) {
}
}

function prepare_items() {
function prepare_items($ignore_pagination=false) {
/**
* First, lets decide how many records per page to show
*/
Expand Down Expand Up @@ -305,15 +305,17 @@ function prepare_items() {
$row['status'] = '';
$new_data[] = $row;
}
$current_page = $this->get_pagenum();
$total_items = count($new_data);
$new_data = array_slice($new_data, (($current_page - 1) * $per_page), $per_page);
if (!$ignore_pagination) {
$current_page = $this->get_pagenum();
$total_items = count($new_data);
$new_data = array_slice($new_data, (($current_page - 1) * $per_page), $per_page);
$this->set_pagination_args(array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items / $per_page) //WE have to calculate the total number of pages
));
}
$this->items = $new_data;
$this->set_pagination_args(array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items / $per_page) //WE have to calculate the total number of pages
));
}

}
44 changes: 23 additions & 21 deletions all-in-one-wp-security/admin/wp-security-list-acct-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function delete_login_activity_records($entries)
}
}

function prepare_items() {
function prepare_items($ignore_pagination = false) {
/**
* First, lets decide how many records per page to show
*/
Expand All @@ -148,33 +148,35 @@ function prepare_items() {
$sortable = $this->get_sortable_columns();

$this->_column_headers = array($columns, $hidden, $sortable);

$this->process_bulk_action();
global $wpdb;

global $wpdb;
$login_activity_table = AIOWPSEC_TBL_USER_LOGIN_ACTIVITY;

/* -- Ordering parameters -- */
//Parameters that are going to be used to order the result
/* -- Ordering parameters -- */
//Parameters that are going to be used to order the result

isset($_GET["orderby"]) ? $orderby = strip_tags($_GET["orderby"]): $orderby = '';
isset($_GET["order"]) ? $order = strip_tags($_GET["order"]): $order = '';
isset($_GET["orderby"]) ? $orderby = strip_tags($_GET["orderby"]) : $orderby = '';
isset($_GET["order"]) ? $order = strip_tags($_GET["order"]) : $order = '';

$orderby = !empty($orderby) ? esc_sql($orderby) : 'login_date';
$order = !empty($order) ? esc_sql($order) : 'DESC';
$orderby = !empty($orderby) ? esc_sql($orderby) : 'login_date';
$order = !empty($order) ? esc_sql($order) : 'DESC';

$orderby = AIOWPSecurity_Utility::sanitize_value_by_array($orderby, $sortable);
$order = AIOWPSecurity_Utility::sanitize_value_by_array($order, array('DESC' => '1', 'ASC' => '1'));

$data = $wpdb->get_results($wpdb->prepare("SELECT * FROM $login_activity_table ORDER BY $orderby $order LIMIT %d", 50), ARRAY_A); //Get the last 50 records
$current_page = $this->get_pagenum();
$total_items = count($data);
$data = array_slice($data,(($current_page-1)*$per_page),$per_page);

$data = $wpdb->get_results($wpdb->prepare("SELECT * FROM $login_activity_table ORDER BY $orderby $order LIMIT %d", 50), ARRAY_A); //Get the last 50 records
if (!$ignore_pagination) {
$current_page = $this->get_pagenum();
$total_items = count($data);
$data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
$this->set_pagination_args(array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items / $per_page) //WE have to calculate the total number of pages
));
}
$this->items = $data;
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
) );
}
}
}
44 changes: 23 additions & 21 deletions all-in-one-wp-security/admin/wp-security-list-login-fails.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function delete_login_failed_records($entries)
}
}

function prepare_items() {
function prepare_items($ignore_pagination = false) {
/**
* First, lets decide how many records per page to show
*/
Expand All @@ -148,32 +148,34 @@ function prepare_items() {
$sortable = $this->get_sortable_columns();

$this->_column_headers = array($columns, $hidden, $sortable);

$this->process_bulk_action();
global $wpdb;

global $wpdb;
$failed_logins_table_name = AIOWPSEC_TBL_FAILED_LOGINS;

/* -- Ordering parameters -- */
//Parameters that are going to be used to order the result
isset($_GET["orderby"]) ? $orderby = strip_tags($_GET["orderby"]): $orderby = '';
isset($_GET["order"]) ? $order = strip_tags($_GET["order"]): $order = '';
/* -- Ordering parameters -- */
//Parameters that are going to be used to order the result
isset($_GET["orderby"]) ? $orderby = strip_tags($_GET["orderby"]) : $orderby = '';
isset($_GET["order"]) ? $order = strip_tags($_GET["order"]) : $order = '';

$orderby = !empty($orderby) ? esc_sql($orderby) : 'failed_login_date';
$order = !empty($order) ? esc_sql($order) : 'DESC';
$orderby = !empty($orderby) ? esc_sql($orderby) : 'failed_login_date';
$order = !empty($order) ? esc_sql($order) : 'DESC';

$orderby = AIOWPSecurity_Utility::sanitize_value_by_array($orderby, $sortable);
$order = AIOWPSecurity_Utility::sanitize_value_by_array($order, array('DESC' => '1', 'ASC' => '1'));

$data = $wpdb->get_results("SELECT * FROM $failed_logins_table_name ORDER BY $orderby $order", ARRAY_A);
$current_page = $this->get_pagenum();
$total_items = count($data);
$data = array_slice($data,(($current_page-1)*$per_page),$per_page);
$data = $wpdb->get_results("SELECT * FROM $failed_logins_table_name ORDER BY $orderby $order", ARRAY_A);
if (!$ignore_pagination) {
$current_page = $this->get_pagenum();
$total_items = count($data);
$data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
$this->set_pagination_args(array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items / $per_page) //WE have to calculate the total number of pages
));
}
$this->items = $data;
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
) );
}
}
}
}
30 changes: 30 additions & 0 deletions all-in-one-wp-security/admin/wp-security-user-login-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,19 @@ function render_tab2()
</form>
</div></div>
<div class="postbox">
<h3 class="hndle"><label for="title"><?php _e('Export to CSV', 'all-in-one-wp-security-and-firewall'); ?></label></h3>
<div class="inside">
<form action="" method="POST">
<?php wp_nonce_field('aiowpsec-export-failed-login-records-to-csv-nonce'); ?>
<table class="form-table">
<tr valign="top">
<span class="description"><?php _e('Click this button if you wish to download this log in CSV format.', 'all-in-one-wp-security-and-firewall'); ?></span>
</tr>
</table>
<input type="submit" name="aiowps_export_failed_login_records_to_csv" value="<?php _e('Export to CSV', 'all-in-one-wp-security-and-firewall')?>" class="button-primary"/>
</form>
</div></div>
<div class="postbox">
<h3 class="hndle"><label for="title"><?php _e('Delete All Failed Login Records', 'all-in-one-wp-security-and-firewall'); ?></label></h3>
<div class="inside">
<form action="" method="POST">
Expand Down Expand Up @@ -447,6 +460,10 @@ function render_tab4()
$acct_activity_list->delete_login_activity_records(strip_tags($_REQUEST['activity_login_rec']));
}
}
if (isset($_POST['aiowpsec_export_to_csv'])) {
echo'yo';
die;
}
?>
<div class="aio_blue_box">
<?php
Expand All @@ -471,6 +488,19 @@ function render_tab4()
<?php $acct_activity_list->display(); ?>
</form>
</div></div>
<div class="postbox">
<h3 class="hndle"><label for="title"><?php _e('Export to CSV', 'all-in-one-wp-security-and-firewall'); ?></label></h3>
<div class="inside">
<form action="" method="POST">
<?php wp_nonce_field('aiowpsec-export-acct-activity-logs-to-csv-nonce'); ?>
<table class="form-table">
<tr valign="top">
<span class="description"><?php _e('Click this button if you wish to download this log in CSV format.', 'all-in-one-wp-security-and-firewall'); ?></span>
</tr>
</table>
<input type="submit" name="aiowpsec_export_acct_activity_logs_to_csv" value="<?php _e('Export to CSV', 'all-in-one-wp-security-and-firewall')?>" class="button-primary"/>
</form>
</div></div>
<?php
}

Expand Down
1 change: 1 addition & 0 deletions all-in-one-wp-security/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ None
== Changelog ==

= TODO - 4.2.6 =
- Added "Export to CSV" buttons for 404 Event Logs, Account Activity Logs and Failed Login Records.
- Fixed bug - Replaced date_i18n with current_time to prevent cases where some localisations produce foreign characters in date stamp output.
- Added a new feature to add Honeypot to the WordPress's user registration form (this can help reduce registration attempts by robots).

Expand Down

0 comments on commit 2426fbb

Please sign in to comment.