@@ -76,191 +76,6 @@ public static function cf7a_check_dnsbl( $reverse_ip, $dnsbl ) {
7676 return checkdnsrr ( $ reverse_ip . '. ' . $ dnsbl . '. ' , 'A ' );
7777 }
7878
79- /* CF7_AntiSpam_Filters blacklists */
80-
81- /**
82- * It takes an IP address as a parameter, validates it, and then returns the row from the database that matches that IP
83- * address
84- *
85- * @param string $ip - The IP address to check.
86- *
87- * @return array|object|null - the row from the database that matches the IP address.
88- */
89- public static function cf7a_blacklist_get_ip ( $ ip ) {
90- $ ip = filter_var ( $ ip , FILTER_VALIDATE_IP );
91- if ( $ ip ) {
92- global $ wpdb ;
93- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
94- $ r = $ wpdb ->get_row ( $ wpdb ->prepare ( "SELECT * FROM %i WHERE ip = %s " , $ wpdb ->prefix . 'cf7a_blacklist ' , $ ip ) );
95- if ( $ r ) {
96- return $ r ;
97- }
98- }
99-
100- return null ;
101- }
102-
103- /**
104- * It gets the row from the database where the id is equal to the id passed to the function
105- *
106- * @param int $id The ID of the blacklist item.
107- *
108- * @return object|false the row from the database that matches the id.
109- */
110- public function cf7a_blacklist_get_id ( $ id ) {
111- if ( is_int ( $ id ) ) {
112- global $ wpdb ;
113- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
114- return $ wpdb ->get_row ( $ wpdb ->prepare ( "SELECT * FROM %i WHERE id = %s " , $ wpdb ->prefix . 'cf7a_blacklist ' , $ id ) );
115- }
116- }
117-
118- /**
119- * It adds an IP address to the blacklist.
120- *
121- * @param string $ip The IP address to ban.
122- * @param array $reason The reason why the IP is being banned.
123- * @param float $spam_score This is the number of points that will be added to the IP's spam score.
124- *
125- * @return bool true if the given id was banned
126- */
127- public function cf7a_ban_by_ip ( string $ ip , array $ reason = array (), $ spam_score = 1 ): bool {
128- $ ip = filter_var ( $ ip , FILTER_VALIDATE_IP );
129-
130- if ( $ ip ) {
131- global $ wpdb ;
132-
133- $ ip_row = self ::cf7a_blacklist_get_ip ( $ ip );
134-
135- if ( $ ip_row ) {
136- // if the ip is in the blacklist, update the status
137- $ status = isset ( $ ip_row ->status ) ? floatval ( $ ip_row ->status ) + floatval ( $ spam_score ) : 1 ;
138-
139- } else {
140- // if the ip is not in the blacklist, add it and initialize the status
141- $ status = floatval ( $ spam_score );
142- }
143-
144- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
145- $ r = $ wpdb ->replace (
146- $ wpdb ->prefix . 'cf7a_blacklist ' ,
147- array (
148- 'ip ' => $ ip ,
149- 'status ' => $ status ,
150- 'meta ' => serialize (
151- array (
152- 'reason ' => $ reason ,
153- 'meta ' => null ,
154- )
155- ),
156- ),
157- array ( '%s ' , '%d ' , '%s ' )
158- );
159-
160- if ( $ r > - 1 ) {
161- return true ;
162- }
163- }
164-
165- return false ;
166- }
167-
168- /**
169- * It deletes the IP address from the database
170- *
171- * @param string $ip The IP address to unban.
172- *
173- * @return int|false The number of rows deleted.
174- */
175- public function cf7a_unban_by_ip ( $ ip ) {
176- $ ip = filter_var ( $ ip , FILTER_VALIDATE_IP );
177-
178- if ( $ ip ) {
179- global $ wpdb ;
180-
181- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
182- $ r = $ wpdb ->delete (
183- $ wpdb ->prefix . 'cf7a_blacklist ' ,
184- array (
185- 'ip ' => $ ip ,
186- ),
187- array (
188- '%s ' ,
189- )
190- );
191-
192- return ! is_wp_error ( $ r ) ? $ r : $ wpdb ->last_error ;
193- }
194-
195- return false ;
196- }
197-
198- /**
199- * It deletes a row from the database table
200- *
201- * @param int $id The ID of the entry to delete.
202- *
203- * @return int The number of rows affected by the query.
204- */
205- public function cf7a_unban_by_id ( $ id ) {
206- $ id = intval ( $ id );
207-
208- global $ wpdb ;
209-
210- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
211- $ r = $ wpdb ->delete (
212- $ wpdb ->prefix . 'cf7a_blacklist ' ,
213- array (
214- 'id ' => $ id ,
215- ),
216- array (
217- '%d ' ,
218- )
219- );
220-
221- return ! is_wp_error ( $ r ) ? $ r : $ wpdb ->last_error ;
222- }
223-
224- /**
225- * It updates the status of all the users in the blacklist table by subtracting 1 from the status column.
226- *
227- * Then it deletes all the users whose status is 0.
228- * The status column is the number of days the user is banned for.
229- * So if the user is banned for 3 days, the status column will be 3. After the first day, the status column will be 2. After the second day, the status column will be 1. After the third day, the status column will be 0.
230- * When the status column is 0, the user is unbanned.
231- *
232- * The function returns true if the user is unbanned.
233- *
234- * @return true.
235- */
236- public function cf7a_cron_unban () {
237- global $ wpdb ;
238-
239- /* We remove 1 from the status column */
240- $ status_decrement = 1 ;
241-
242- /* Below 0 is not anymore a valid status for a blacklist entry, so we can remove it */
243- $ lower_bound = 0 ;
244-
245- $ blacklist_table = $ wpdb ->prefix . 'cf7a_blacklist ' ;
246-
247- /* removes a status count at each balcklisted ip */
248- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
249- $ updated = $ wpdb ->query ( $ wpdb ->prepare ( "UPDATE %i SET `status` = `status` - %d " , $ blacklist_table , $ status_decrement ) );
250- cf7a_log ( "Status updated for blacklisted (score -1) - $ updated users " , 1 );
251-
252- /* when the line has 0 in status, we can remove it from the blacklist */
253- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
254- $ updated_deletion = $ wpdb ->delete (
255- $ blacklist_table ,
256- array ( 'status ' => $ lower_bound ),
257- array ( '%d ' )
258- );
259- cf7a_log ( "Removed {$ updated_deletion } users from blacklist " , 1 );
260-
261- return true ;
262- }
263-
26479 /**
26580 * Checks the length of a string and returns a specific part of it based on a given index.
26681 *
@@ -539,7 +354,7 @@ public function cf7a_spam_filter( $spam ) {
539354 * Checking if the IP address was already blacklisted - no mercy 😎
540355 */
541356 if ( $ remote_ip && $ options ['max_attempts ' ] ) {
542- $ ip_data = self ::cf7a_blacklist_get_ip ( $ remote_ip );
357+ $ ip_data = CF7_Antispam_Blacklist ::cf7a_blacklist_get_ip ( $ remote_ip );
543358 $ ip_data_status = isset ( $ ip_data ->status ) ? intval ( $ ip_data ->status ) : 0 ;
544359 $ max_attempts = intval ( $ options ['max_attempts ' ] );
545360
@@ -1043,7 +858,8 @@ public function cf7a_spam_filter( $spam ) {
1043858
1044859 /* If the auto-store ip is enabled (and NOT in extended debug mode) */
1045860 if ( $ options ['autostore_bad_ip ' ] ) {
1046- if ( self ::cf7a_ban_by_ip ( $ remote_ip , $ reason , round ( $ spam_score ) ) ) {
861+ $ blacklist = new CF7_Antispam_Blacklist ();
862+ if ( CF7_Antispam_Blacklist::cf7a_ban_by_ip ( $ remote_ip , $ reason , round ( $ spam_score ) ) ) {
1047863 /* Log the antispam result in extended debug mode */
1048864 cf7a_log ( "Ban for $ remote_ip - results - " . $ reasons_for_ban , 2 );
1049865 } else {
0 commit comments