prefix . 'aisfg_log'; } /** * Register hooks. */ public static function init() { add_action( 'aisfg_purge', array( __CLASS__, 'purge' ) ); if ( ! wp_next_scheduled( 'aisfg_purge' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'aisfg_purge' ); } self::maybe_upgrade(); } /** * Schema sql. */ public static function schema_sql() { global $wpdb; $charset = $wpdb->get_charset_collate(); $log = self::table(); $ban = AISFG_Ban::table(); return array( "CREATE TABLE $log ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL, last_at datetime DEFAULT NULL, form_id bigint(20) unsigned NOT NULL DEFAULT 0, decision varchar(10) NOT NULL, reasons varchar(255) NOT NULL DEFAULT '', ip_masked varchar(45) NOT NULL DEFAULT '', hits int(10) unsigned NOT NULL DEFAULT 1, PRIMARY KEY (id), KEY created_at (created_at), KEY ip_time (ip_masked, created_at) ) $charset;", "CREATE TABLE $ban ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, ip varchar(45) NOT NULL, reason varchar(40) NOT NULL DEFAULT '', strikes int(10) unsigned NOT NULL DEFAULT 0, blocked bigint(20) unsigned NOT NULL DEFAULT 0, first_at datetime NOT NULL, last_at datetime NOT NULL, expires_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY ip (ip), KEY expires_at (expires_at) ) $charset;", ); } /** * Create or upgrade tables. */ public static function install() { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; foreach ( self::schema_sql() as $sql ) { dbDelta( $sql ); } update_option( 'aisfg_db_version', self::DB_VERSION, false ); } /** * Maybe upgrade. */ public static function maybe_upgrade() { if ( get_option( 'aisfg_db_version' ) !== self::DB_VERSION ) { self::install(); } } /** * Mask ip. * * @param mixed $ip Parameter. */ public static function mask_ip( $ip ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- inet_pton returns false on invalid input. $bin = @inet_pton( (string) $ip ); if ( false === $bin ) { return ''; } if ( 4 === strlen( $bin ) ) { $bin[3] = "\0"; } else { $bin = substr( $bin, 0, 6 ) . str_repeat( "\0", 10 ); } return (string) inet_ntop( $bin ); } /** * Log. * * @param mixed $form_id Parameter. * @param mixed $decision Parameter. * @param array $reasons Parameter. * @param mixed $ip Parameter. */ public static function log( $form_id, $decision, array $reasons, $ip ) { global $wpdb; $t = self::table(); $now = gmdate( 'Y-m-d H:i:s' ); $r = substr( implode( ',', $reasons ), 0, 255 ); $m = self::mask_ip( $ip ); if ( in_array( $decision, array( 'block', 'monitor' ), true ) ) { $since = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- aisfg_log custom table; no WP API $updated = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET hits = hits + 1, last_at = %s WHERE form_id = %d AND decision = %s AND reasons = %s AND ip_masked = %s AND created_at >= %s ORDER BY id DESC LIMIT 1', $t, $now, (int) $form_id, $decision, $r, $m, $since ) ); if ( $updated ) { return; } } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- aisfg_log custom table; no WP API $wpdb->insert( $t, array( 'created_at' => $now, 'last_at' => $now, 'form_id' => (int) $form_id, 'decision' => $decision, 'reasons' => $r, 'ip_masked' => $m, 'hits' => 1, ), array( '%s', '%s', '%d', '%s', '%s', '%s', '%d' ) ); } /** * Delete expired rows. */ public static function purge() { global $wpdb; $days = (int) AISFG_Settings::get()['retention_days']; $cut = gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- aisfg_log custom table; retention purge $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE created_at < %s', self::table(), $cut ) ); AISFG_Ban::purge(); } }