class-itsec-four-oh-four-log.php 5.93 KB
Newer Older
John Punzalan's avatar
John Punzalan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
<?php

/**
 * Log 404 errors for Intrusion Detection Module
 *
 * @package    iThemes-Security
 * @subpackage Intrusion-Detection
 * @since      4.0
 */
final class ITSEC_Four_Oh_Four_Log extends ITSEC_WP_List_Table {

	function __construct() {

		parent::__construct(
			array(
				'singular' => 'itsec_four_oh_four_log_item',
				'plural'   => 'itsec_four_oh_four_log_items',
				'ajax'     => true
			)
		);

	}

	/**
	 * Define first time column
	 *
	 * @param array $item array of row data
	 *
	 * @return string formatted output
	 *
	 **/
	function column_first_time( $item ) {

		return $item['first_time'];

	}

	/**
	 * Define time column
	 *
	 * @param array $item array of row data
	 *
	 * @return string formatted output
	 *
	 **/
	function column_last_time( $item ) {

		return $item['last_time'];

	}

	/**
	 * Define count column
	 *
	 * @param array $item array of row data
	 *
	 * @return string formatted output
	 *
	 **/
	function column_count( $item ) {

		return $item['count'];

	}

	/**
	 * Define uri column
	 *
	 * @param array $item array of row data
	 *
	 * @return string formatted output
	 *
	 **/
	function column_uri( $item ) {

		global $itsec_logger;

		$items = $itsec_logger->get_events( 'four_oh_four', array( 'log_url' => $item['uri'] ) );

		echo '<a href="itsec_404_details_' . $item['id'] . '" class="dialog">' . $item['uri'] . '</a>';

		echo '<div id="itsec_404_details_' . $item['id'] . '" style="display:none;">';

		echo '<h3>' . __( 'Details for ' . $item['uri'], 'better-wp-security' ) . '</h3>';

		echo '<ol class="file_change_detail_list">';

		foreach ( $items as $item => $details ) {
			$data = maybe_unserialize( $details['log_data'] );
			echo '<li class="404_detail"><strong>' . __( 'Time', 'better-wp-security' ) . '</strong>: ' . $details['log_date'] . '<br /><strong>' . __( 'Host', 'better-wp-security' ) . '</strong>: ' . $details['log_host'] . '<br /><strong>' . __( 'Referrer', 'better-wp-security' ) . '</strong>: ' . esc_html( $details['log_referrer'] ) . '<br /><strong>' . __( 'Query', 'better-wp-security' ) . '</strong>: ' . esc_html( $data['query_string'] ) . '</li>';
		}

		echo '</ol>';

		echo '</div>';

	}

	/**
	 * Define Columns
	 *
	 * @return array array of column titles
	 */
	public function get_columns() {

		return array(
			'uri'        => __( 'Location', 'better-wp-security' ),
			'count'      => __( 'Count', 'better-wp-security' ),
			'first_time' => __( 'First Recorded', 'better-wp-security' ),
			'last_time'  => __( 'Last Recorded', 'better-wp-security' ),
		);

	}

	/**
	 * Define Sortable Columns
	 *
	 * @return array of column titles that can be sorted
	 */
	public function get_sortable_columns() {

		$order = ( empty( $_GET['order'] ) ) ? false : true;

		$sortable_columns = array(
			'uri'        => array( 'uri', $order ),
			'count'      => array( 'count', $order ),
			'first_time' => array( 'first_time', $order ),
			'last_time'  => array( 'last_time', $order ),
		);

		return $sortable_columns;

	}

	/**
	 * Prepare data for table
	 *
	 * @return void
	 */
	public function prepare_items() {

		global $itsec_logger;

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

		$items = $itsec_logger->get_events( 'four_oh_four' );

		$table_data = array();

		foreach ( $items as $item ) { //loop through and group 404s

			if ( isset( $table_data[ $item['log_url'] ] ) ) {

				$table_data[ $item['log_url'] ]['id']         = $item['log_id'];
				$table_data[ $item['log_url'] ]['count']      = $table_data[ $item['log_url'] ]['count'] + 1;
				$table_data[ $item['log_url'] ]['last_time']  = strtotime( $table_data[ $item['log_url'] ]['last_time'] ) > strtotime( $item['log_date'] ) ? $table_data[ $item['log_url'] ]['last_time'] : sanitize_text_field( $item['log_date'] );
				$table_data[ $item['log_url'] ]['first_time'] = strtotime( $table_data[ $item['log_url'] ]['first_time'] ) < strtotime( $item['log_date'] ) ? $table_data[ $item['log_url'] ]['first_time'] : sanitize_text_field( $item['log_date'] );
				$table_data[ $item['log_url'] ]['uri']        = sanitize_text_field( $item['log_url'] );

			} else {

				$table_data[ $item['log_url'] ]['id']         = $item['log_id'];
				$table_data[ $item['log_url'] ]['count']      = 1;
				$table_data[ $item['log_url'] ]['last_time']  = sanitize_text_field( $item['log_date'] );
				$table_data[ $item['log_url'] ]['first_time'] = sanitize_text_field( $item['log_date'] );
				$table_data[ $item['log_url'] ]['uri']        = sanitize_text_field( $item['log_url'] );

			}

		}

		usort( $table_data, array( $this, 'sortrows' ) );

		$per_page     = 20; //20 items per page
		$current_page = $this->get_pagenum();
		$total_items  = count( $table_data );

		$table_data = array_slice( $table_data, ( ( $current_page - 1 ) * $per_page ), $per_page );

		$this->items = $table_data;

		$this->set_pagination_args(
			array(
				'total_items' => $total_items,
				'per_page'    => $per_page,
				'total_pages' => ceil( $total_items / $per_page )
			)
		);

	}

	/**
	 * Sorts rows by count in descending order
	 *
	 * @param array $a first array to compare
	 * @param array $b second array to compare
	 *
	 * @return int comparison result
	 */
	function sortrows( $a, $b ) {

		// If no sort, default to count
		$orderby = ( ! empty( $_GET['orderby'] ) ) ? esc_attr( $_GET['orderby'] ) : 'last_time';

		// If no order, default to desc
		$order = ( ! empty( $_GET['order'] ) ) ? esc_attr( $_GET['order'] ) : 'desc';

		if ( $orderby == 'count' ) {

			if ( intval( $a[ $orderby ] ) < intval( $b[ $orderby ] ) ) {
				$result = - 1;
			} elseif ( intval( $a[ $orderby ] ) === intval( $b[ $orderby ] ) ) {
				$result = 0;
			} else {
				$result = 1;
			}

		} else {

			// Determine sort order
			$result = strcmp( $a[ $orderby ], $b[ $orderby ] );

		}

		// Send final sort direction to usort
		return ( $order === 'asc' ) ? $result : - $result;

	}

}