| @@ -79,9 +79,9 @@ | ||
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | $spam_comments_ip = Spam_Comment::get_spam_comments_ip(); |
| 83 | - if ( ! empty( $spam_comments_ip ) ) { | |
| 83 | + if ( array() !== $spam_comments_ip ) { | |
| 84 | 84 | // Add spam comments IP to the compact log. |
| 85 | 85 | $this->log( $spam_comments_ip, 'spam-comment.log' ); |
| 86 | 86 | |
| 87 | 87 | foreach ( $spam_comments_ip as $ip => $count ) { |
| @@ -105,9 +105,9 @@ | ||
| 105 | 105 | $logs = array(); |
| 106 | 106 | // Retrieve the current list of blocked IPs from the site transient. |
| 107 | 107 | $ips = get_site_transient( \WP_Defender\Controller\Firewall_Logs::AKISMET_BLOCKED_IPS ); |
| 108 | 108 | // Ensure the retrieved data is an array; if not, initialize it as an empty array. |
| 109 | - if ( is_array( $ips ) && ! empty( $ips ) ) { | |
| 109 | + if ( is_array( $ips ) && array() !== $ips ) { | |
| 110 | 110 | $this->log( $ips, 'spam-comment.log' ); |
| 111 | 111 | |
| 112 | 112 | foreach ( $ips as $ip => $count ) { |
| 113 | 113 | if ( ! isset( $logs[ $ip ] ) ) { |
| @@ -118,8 +118,64 @@ | ||
| 118 | 118 | } |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | delete_site_transient( \WP_Defender\Controller\Firewall_Logs::AKISMET_BLOCKED_IPS ); |
| 122 | + | |
| 123 | + return array_values( $logs ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Fetch 404 Intelligence Firewall logs. | |
| 128 | + * | |
| 129 | + * @param int $from Fetch Logs from this time to current time. | |
| 130 | + * | |
| 131 | + * @return array | |
| 132 | + */ | |
| 133 | + public function get_404_intelligence_logs( int $from ): array { | |
| 134 | + global $wpdb; | |
| 135 | + | |
| 136 | + $table = $wpdb->base_prefix . ( new Lockout_Log() )->get_table(); | |
| 137 | + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery | |
| 138 | + $wpdb->prepare( | |
| 139 | + "SELECT IP, type, COUNT(*) AS frequency FROM {$table}" . // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 140 | + ' WHERE `date` >= %s AND type IN (%s, %s, %s)' . | |
| 141 | + ' GROUP BY IP, `type`', | |
| 142 | + $from, | |
| 143 | + \WP_Defender\Model\Lockout_Log::ERROR_404_XSS, | |
| 144 | + \WP_Defender\Model\Lockout_Log::ERROR_404_NON_EXISTENT_PLUGIN, | |
| 145 | + \WP_Defender\Model\Lockout_Log::ERROR_404_NON_EXISTENT_THEME | |
| 146 | + ), | |
| 147 | + ARRAY_A | |
| 148 | + ); | |
| 149 | + | |
| 150 | + $logs = array(); | |
| 151 | + if ( is_array( $results ) ) { | |
| 152 | + foreach ( $results as $row ) { | |
| 153 | + $frequency = (int) $row['frequency']; | |
| 154 | + | |
| 155 | + $type = ''; | |
| 156 | + switch ( $row['type'] ) { | |
| 157 | + case \WP_Defender\Model\Lockout_Log::ERROR_404_XSS: | |
| 158 | + $type = 'not_found_xss'; | |
| 159 | + break; | |
| 160 | + case \WP_Defender\Model\Lockout_Log::ERROR_404_NON_EXISTENT_PLUGIN: | |
| 161 | + $type = 'not_found_plugin'; | |
| 162 | + break; | |
| 163 | + case \WP_Defender\Model\Lockout_Log::ERROR_404_NON_EXISTENT_THEME: | |
| 164 | + $type = 'not_found_theme'; | |
| 165 | + break; | |
| 166 | + default: | |
| 167 | + continue 2; | |
| 168 | + } | |
| 169 | + | |
| 170 | + $ip = $row['IP']; | |
| 171 | + if ( ! isset( $logs[ $ip ] ) ) { | |
| 172 | + $logs[ $ip ] = array( 'ip' => $ip ); | |
| 173 | + } | |
| 174 | + | |
| 175 | + $logs[ $ip ]['reason'][ $type ] = $frequency; | |
| 176 | + } | |
| 177 | + } | |
| 122 | 178 | |
| 123 | 179 | return array_values( $logs ); |
| 124 | 180 | } |
| 125 | 181 | } |