array(
'name' => 'Any',
'value' => '',
),
self::ALA_LOGIN_SUCCESS => array(
'name' => 'Login Successful',
'value' => 'success',
),
self::ALA_LOGIN_ERROR => array(
'name' => 'Login Failed',
'value' => 'error',
),
self::ALA_LOGOUT => array(
'name' => 'Log out',
'value' => 'logout',
),
self::ALA_PASSWORD_RESET => array(
'name' => 'Password reset',
'value' => 'password-reset',
),
);
public function __construct() {
$this->settings();
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
add_action( 'wp_login', array( &$this, 'wp_login' ), 10, 2);
add_action( 'wp_logout', array( &$this, 'wp_logout' ) );
add_action( 'wp_login_failed', array( &$this, 'wp_login_failed' ) );
add_action( 'password_reset', array( &$this, 'password_reset' ), 10, 2 );
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_scripts' ) );
add_action( 'wp', array( &$this, 'cron' ) );
add_action( self::$cron_tag, array( &$this, 'delete_logs' ), 10, 1 );
add_filter( 'plugin_action_links', array( &$this, 'settings_link' ), 10, 2);
register_activation_hook( __FILE__, array( &$this, 'db_install' ) );
register_uninstall_hook( __FILE__, array( 'AspexiLoginAudit', 'uninstall' ) );
register_deactivation_hook( __FILE__, array( &$this, 'clear_cron' ) );
load_plugin_textdomain( 'aspexiloginaudit', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
public function settings() {
$config_default = array(
'log_info_type' => 'success|error',
'logs_days' => '0',
'logs_per_page' => $this->logs_per_page,
'remove_data_on_uninstall' => 'on',
);
if ( ! get_option( 'aspexiloginaudit_options' ) )
add_option( 'aspexiloginaudit_options', $config_default, '', 'yes' );
$this->config = get_option( 'aspexiloginaudit_options' );
}
public function admin_menu() {
add_submenu_page( 'options-general.php', __( 'Aspexi Login Audit', 'aspexiloginaudit' ), __( 'Aspexi Login Audit', 'aspexiloginaudit' ), 'manage_options', basename(__FILE__), array( &$this, 'admin_page' ) );
}
public function admin_scripts() {
if ( isset( $_REQUEST['page'] ) && basename(__FILE__) == $_REQUEST['page'] ) {
wp_enqueue_style('aspexi-login-audit-admin', ASPEXILOGINAUDIT_URL . '/aspexi-login-audit.css');
wp_enqueue_style( 'wp-jquery-ui-dialog' );
wp_enqueue_script( 'aspexi-login-audit-admin', ASPEXILOGINAUDIT_URL . '/js/aspexi-login-audit-admin.js', array( 'jquery', 'jquery-ui-dialog', 'jquery-ui-tooltip' ) );
wp_localize_script( 'aspexi-login-audit-admin', 'ala', array(
'pro_url' => $this->get_pro_url(),
) );
}
}
public function admin_page() {
if ( !current_user_can( 'manage_options' ) )
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
if ( isset( $_REQUEST['ala_form_submit'] ) && check_admin_referer( plugin_basename(__FILE__), 'ala_nonce_name' ) ) {
if( ! (int)$_REQUEST['ala_logs_days'] < 0 )
$this->add_error( __( 'Missing Keep logs for days. Settings not saved.', 'aspexiloginaudit' ) );
if (!$this->has_errors()) {
$ala_request_options = array();
$logStatuses = array();
foreach ($this->statuses as $key => $status) {
if ($key == 0) continue;
if (isset($_REQUEST['ala_log_info_type_' . $status['value']]))
$logStatuses[] = sanitize_text_field($_REQUEST['ala_log_info_type_' . $status['value']]);
}
$ala_request_options['log_info_type'] = implode('|', $logStatuses);
$ala_request_options['logs_days'] = isset( $_REQUEST['ala_logs_days'] ) ? absint( $_REQUEST['ala_logs_days'] ) : 0;
$ala_request_options['remove_data_on_uninstall'] = isset( $_REQUEST['ala_remove_data_on_uninstall'] ) ? sanitize_key( $_REQUEST['ala_remove_data_on_uninstall'] ) : '';
$_logs_days = absint( $this->config['logs_days'] );
$this->config = array_merge($this->config, $ala_request_options);
update_option('aspexiloginaudit_options', $this->config, 'yes');
// Clear logs if needed
if( $ala_request_options['logs_days'] > 0 && $_logs_days != $ala_request_options['logs_days'] )
$this->delete_logs(true);
$this->add_message(__('Settings saved.', 'aspexiloginaudit'));
}
}
$_page = 1;
if ( isset( $_REQUEST['ala_filter_submit'] ) && check_admin_referer( plugin_basename(__FILE__), 'ala_nonce_name' ) ) {
if ( isset( $_REQUEST['ala_delete_logs'] ) )
$this->delete_logs();
$_filters = array();
if( isset( $_REQUEST['ala_filter_status'] ) )
foreach ($this->statuses as $key => $status)
if ( $_REQUEST['ala_filter_status'] == $status['value'])
$_filters['status'] = $key;
if (isset($_GET['ala_page']) && $_GET['ala_page'] > 0) {
$_page = absint($_GET['ala_page']);
} elseif (isset($_POST['ala_page']) && $_POST['ala_page'] > 0) {
$_page = absint($_POST['ala_page']);
} else {
$_page = 1;
}
$_count = $this->get_logs( array_merge( (array) $_filters, array( 'count_only' => 'yes' ) ) );
$_max_pages = ceil( $_count / $this->get_logs_per_page() );
if( $_page > $_max_pages )
$_page = $_max_pages;
$_filters['page'] = $_page;
if( isset( $_REQUEST['ala_logs_per_page'] ) && (int) $_REQUEST['ala_logs_per_page'] > 0 ) {
$this->config['logs_per_page'] = (int) $_REQUEST['ala_logs_per_page'];
update_option('aspexiloginaudit_options', $this->config, 'yes');
}
}
?>
get_logs( $filters );
if ($loginInfo != false): ?>
statuses as $status)
if ($status['value'] == $request_status_foreach)
$request_status = $status['value'];
}
if (!empty($request_status)) {
$logs = array();
foreach ($loginInfo as $log) {
if ($log->status == $request_status)
$logs[] = $log;
}
}
?>
status) {
case self::ALA_LOGIN_SUCCESS:
$log_status = 'Login Successful';
$tr_color = 'background-color: rgba(0, 255, 0, 0.05);';
break;
case self::ALA_LOGIN_ERROR:
$log_status = 'Login Failed';
$tr_color = 'background-color: rgba(255, 0, 0, 0.05);';
break;
case self::ALA_LOGOUT:
$log_status = 'Log out';
$tr_color = 'background-color: rgba(0, 0, 255, 0.05);';
break;
case self::ALA_PASSWORD_RESET:
$log_status = 'Password reset';
$tr_color = 'background-color: rgba(126, 0, 126, 0.05);';
break;
}
?>
user_email, 45).''.$logs->user.'' . (0 != $logs->user_id ? implode(', ', get_userdata($logs->user_id)->roles) : '') . ' '; ?>
user_email ) ? __( 'Unknown', 'aspexiloginaudit' ) : '' . $logs->user_email . ' ' ); ?>
get_ip_url( $logs->ip || $this->get_ip() != $logs->ip ) ? 'block' : '' ) ?> Block ip == 'unknown' ) ? __( 'Unknown', 'aspexiloginaudit' ) : $logs->ip ; ?>
referer; ?>
user_agent; ?>
date . ' ' . get_option('gmt_offset') . ' hours')); ?>
get_pro_link(); ?>
get_delete_logs_button($filters); ?>
get_logs_per_page_filter( $filters ); ?>
get_pagination($this->get_logs( array_merge( (array) $filters, array( 'count_only' => 'yes' ) ) ), $_page, $filters); ?>
get_delete_logs_button($filters); ?>
get_logs_per_page_filter( $filters ); ?>
get_pagination($this->get_logs( array_merge( (array) $filters, array( 'count_only' => 'yes' ) ) ), $_page, $filters); ?>
config['logs_per_page'] );
$filters_hidden = $this->get_filters_hidden( $filters );
$prev = '
'.$filters_hidden.'
'.wp_nonce_field( plugin_basename( __FILE__ ), 'ala_nonce_name' ).'
';
$next = '
'.$filters_hidden.'
'.wp_nonce_field( plugin_basename( __FILE__ ), 'ala_nonce_name' ).'
';
$page_info = '
'.$filters_hidden.'
'.wp_nonce_field( plugin_basename( __FILE__ ), 'ala_nonce_name' ).' / '.$_max_pages;
if( $page > 1 ) {
if( $page < $_max_pages )
$ret .= $prev.' '.$page_info.' '.$next;
else
$ret .= $prev.' '.$page_info;
} else {
if( $page < $_max_pages )
$ret .= $page_info.' '.$next;
}
$ret = ''.$ret.'
';
return $ret;
}
public function get_logs( $filters = array(), $no_limit = false )
{
global $wpdb;
$_where = '';
$extra = '';
$_select = '*';
if( isset( $filters['status'] ) && $filters['status'] != self::ALA_LOGIN_ANY ) {
$_where = ' AND status = ' . $filters['status'];
}
if( strlen( $_where ) )
$extra .= "WHERE hidden=0".$_where;
else
$extra .= "WHERE hidden=0";
$offset = '';
if( isset( $filters['page'] ) && $filters['page'] > 1 )
$offset = $wpdb->prepare( ' OFFSET %d', (int) ( ( $filters['page']-1 ) * $this->get_logs_per_page() ) );
$limit = $no_limit ? '' : ' LIMIT '.$wpdb->prepare( '%d', $this->get_logs_per_page() ).$offset;
$sql = 'SELECT * FROM ' . $wpdb->prefix . self::$table_name . ' ' . $extra . ' ORDER BY date DESC';
$sql_final = 'SELECT '.$_select.' FROM ' . $wpdb->prefix . self::$table_name . ' ' . $extra . ' ORDER BY date DESC'.$limit;
if( isset( $filters['count_only'] ) && 'yes' == $filters['count_only'] ) {
$sql = str_replace( '*', 'COUNT(*)', $sql );
return $wpdb->get_var( $sql );
}
else
return $wpdb->get_results( $sql_final );
}
public function get_filters_hidden( $filters ) {
$_status_input = '';
if( isset( $filters['status'] ) )
$_status_input = ' ';
return $_status_input;
}
public function get_pro_url() {
return 'http://aspexi.com/downloads/aspexi-login-audit/?src=free_plugin';
}
public function get_pro_link() {
return ''.__( 'Get PRO version', 'aspexiloginaudit' ).' ';
}
public function settings_link( $action_links, $plugin_file ) {
if( $plugin_file == plugin_basename(__FILE__) ) {
$pro_link = $this->get_pro_link();
array_unshift( $action_links, $pro_link );
$settings_link = '' . __("Settings") . ' ';
array_unshift( $action_links, $settings_link );
}
return $action_links;
}
public function wp_login( $user_login, WP_User $user ) {
$logInfoTypes = explode( '|', $this->config['log_info_type'] );
if( in_array( 'success', $logInfoTypes ) )
$hidden = 0;
else
$hidden = 1;
global $wpdb;
$wpdb->insert($wpdb->prefix . self::$table_name, array(
'status' => self::ALA_LOGIN_SUCCESS,
'user' => $user->user_login,
'user_id' => $user->ID,
'user_email' => $user->user_email,
'ip' => $this->get_ip(),
'user_agent' => $this->get_user_agent(),
'referer' => $this->get_referer(),
'date' => date('Y-m-d H:i:s'),
'email_sent' => '0',
'hidden' => $hidden
));
}
public function wp_logout()
{
$user = wp_get_current_user();
$logInfoTypes = explode( '|', $this->config['log_info_type'] );
if( in_array( 'logout', $logInfoTypes ) )
$hidden = 0;
else
$hidden = 1;
global $wpdb;
$wpdb->insert($wpdb->prefix . self::$table_name, array(
'status' => self::ALA_LOGOUT,
'user' => $user->user_login,
'user_id' => $user->ID,
'user_email' => $user->user_email,
'ip' => $this->get_ip(),
'user_agent' => $this->get_user_agent(),
'referer' => $this->get_referer(),
'date' => date('Y-m-d H:i:s'),
'email_sent' => '0',
'hidden' => $hidden
));
}
public function password_reset($user, $new_pass)
{
$logInfoTypes = explode( '|', $this->config['log_info_type'] );
if( in_array( 'password-reset', $logInfoTypes ) )
$hidden = 0;
else
$hidden = 1;
global $wpdb;
$wpdb->insert($wpdb->prefix . self::$table_name, array(
'status' => self::ALA_PASSWORD_RESET,
'user' => $user->user_login,
'user_id' => $user->ID,
'user_email' => $user->user_email,
'ip' => $this->get_ip(),
'user_agent' => $this->get_user_agent(),
'referer' => $this->get_referer(),
'date' => date('Y-m-d H:i:s'),
'email_sent' => '0',
'hidden' => $hidden
));
}
public function wp_login_failed($username) {
$user_email = '';
$user = get_user_by('login', $username);
if ($user != false)
$user_email = $user->user_email;
$logInfoTypes = explode( '|', $this->config['log_info_type'] );
if( in_array( 'error', $logInfoTypes ) )
$hidden = 0;
else
$hidden = 1;
global $wpdb;
$wpdb->insert( $wpdb->prefix . self::$table_name, array(
'status' => self::ALA_LOGIN_ERROR,
'user' => $username,
'user_id' => $user != false ? $user->ID : '',
'user_email' => $user != false ? $user->user_email : '',
'ip' => $this->get_ip(),
'user_agent' => $this->get_user_agent(),
'referer' => $this->get_referer(),
'date' => date('Y-m-d H:i:s'),
'email_sent' => '0',
'hidden' => $hidden
));
}
public function get_ip_url( $ip ) {
if( $ip && false === !filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) )
return 'http://www.ip-adress.com/ip_tracer/'.$ip;
else
return false;
}
public function get_ip() {
$indices = array(
'HTTP_CF_CONNECTING_IP',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_VIA',
'REMOTE_ADDR'
);
foreach ( $indices as $index ) {
if ( empty( $_SERVER[$index] ) ) {
continue;
}
$ip = filter_var( $_SERVER[$index], FILTER_VALIDATE_IP );
if ( ! empty( $ip ) ) {
break;
}
}
$ip = esc_sql( (string) $ip );
return strlen( $ip ) ? $ip : 'unknown';
}
public function get_user_agent() {
return ( isset( $_SERVER['HTTP_USER_AGENT'] ) ? esc_html( $_SERVER['HTTP_USER_AGENT'] ) : '' );
}
public function get_referer() {
return ( isset( $_SERVER['HTTP_REFERER'] ) ? esc_html( $_SERVER['HTTP_REFERER'] ) : '' );
}
public function uninstall() {
global $wpdb;
$options = get_option( 'aspexiloginaudit_options' );
if ($options['remove_data_on_uninstall'] == 'on') {
if ( is_multisite() ) {
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
$original_blog_id = get_current_blog_id();
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . self::$table_name );
}
switch_to_blog( $original_blog_id );
} else {
$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . self::$table_name );
}
}
delete_option('aspexiloginaudit_options');
wp_clear_scheduled_hook( self::$cron_tag );
}
protected function add_message( $message ) {
$message = trim( $message );
if( strlen( $message ) )
$this->messages[] = $message;
}
protected function add_error( $error ) {
$error = trim( $error );
if( strlen( $error ) )
$this->errors[] = $error;
}
public function has_errors() {
return count( $this->errors );
}
public function display_admin_notices( $echo = false ) {
$ret = '';
foreach( (array)$this->errors as $error ) {
$ret .= '';
}
foreach( (array)$this->messages as $message ) {
$ret .= '';
}
if( $echo )
echo $ret;
else
return $ret;
}
public function delete_logs($cron = false) {
global $wpdb;
if( $cron ) {
$logs_days = (int)$this->config['logs_days'];
if( 0 < $logs_days )
return $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . self::$table_name . ' WHERE date < "' . date_i18n('Y-m-d H:i:s') . '" - INTERVAL ' . $logs_days . ' DAY');
} else
return $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . self::$table_name );
}
public function cron() {
$logs_days = (int) $this->config['logs_days'];
$next_timestamp = wp_next_scheduled( self::$cron_tag, array( true ) );
// Check if we should ever initiate WP cron
if( 0 < $logs_days && ! $next_timestamp ) {
// hourly, twicedaily, daily available only
wp_schedule_event( time(), 'daily', self::$cron_tag, array( true ) );
} else if ( 0 == $logs_days || ! $logs_days ) {
// if next scheduled exists - remove it
if( $next_timestamp ) {
wp_unschedule_event( $next_timestamp, self::$cron_tag, array( true ) );
}
}
}
public function clear_cron() {
wp_clear_scheduled_hook( self::$cron_tag );
}
public function get_delete_logs_button( $filters = array() ) {
$ret = '';
$url = ASPEXILOGINAUDIT_ADMIN_URL.'&logs=true';
$filters_hidden = $this->get_filters_hidden( $filters );
$form = '
'.$filters_hidden.'
'.wp_nonce_field( plugin_basename( __FILE__ ), 'ala_nonce_name' ).'
';
return ''.$form.'
';
}
public function get_logs_per_page_filter( $filters = array() ) {
$ret = '';
$url = ASPEXILOGINAUDIT_ADMIN_URL.'&logs=true';
$filters_hidden = $this->get_filters_hidden( $filters );
$form = '
'.$filters_hidden.'
'.wp_nonce_field( plugin_basename( __FILE__ ), 'ala_nonce_name' ).'
' . __( 'Logs per page', 'aspexiloginaudit' ) . ':
';
$ret = ''.$form.'
';
return $ret;
}
public function get_logs_per_page() {
return ( isset( $this->config['logs_per_page'] ) && (int)$this->config['logs_per_page'] > 0 ) ? (int)$this->config['logs_per_page'] : $this->logs_per_page;
}
public function db_install() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . self::$table_name;
if( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) {
$sql = "CREATE TABLE " . $table_name . " (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
status TINYINT UNSIGNED NOT NULL,
user VARCHAR(255) NOT NULL,
user_id INT NOT NULL,
user_email VARCHAR(255) NOT NULL,
ip VARCHAR(46) NOT NULL,
user_agent VARCHAR(255) NOT NULL,
referer VARCHAR(255) NOT NULL,
ip_blocked INT DEFAULT 0,
email_sent INT DEFAULT 0,
hidden INT DEFAULT 0,
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
}
}
/* Let's start the show */
global $aspexi_login_audit;
$aspexi_login_audit = new AspexiLoginAudit();
}