PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.0
Defender Security – Malware Scanner, Login Security & Firewall v6.2.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / model / class-antibot-global-firewall.php

class-antibot-global-firewall.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.0, at src/model/class-antibot-global-firewall.php

167 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The AntiBot Global Firewall model class.
4 *
5 * @package WP_Defender\Model
6 */
7
8 namespace WP_Defender\Model;
9
10 use WP_Defender\DB;
11
12 /**
13 * Class Antibot_Global_Firewall
14 *
15 * Provides methods to insert, truncate and retrieve IP(s) from database.
16 */
17 class Antibot_Global_Firewall extends DB {
18 /**
19 * The table name.
20 *
21 * @var string
22 */
23 protected $table = 'defender_antibot';
24
25 /**
26 * The IP address.
27 *
28 * @var string
29 * @defender_property
30 */
31 public $ip;
32
33 /**
34 * Status of the IP. If the IP is unlocked, the status will be true.
35 *
36 * @var int
37 * @defender_property
38 */
39 public $unlocked;
40
41 /**
42 * The time when the IP was unlocked.
43 *
44 * @var string
45 * @defender_property
46 */
47 public $unlocked_at;
48
49 /**
50 * Truncate the table when we fetch new data from API.
51 */
52 public function truncate() {
53 $orm = self::get_orm();
54 $orm->get_repository( self::class )->truncate();
55 }
56
57 /**
58 * Get IP record.
59 *
60 * @param string $ip The IP address.
61 *
62 * @return self|null
63 */
64 public function get_by_ip( $ip ) {
65 $orm = self::get_orm();
66
67 return $orm->get_repository( self::class )
68 ->where( 'ip', $ip )
69 ->first();
70 }
71
72 /**
73 * Bulk insert IP(s) into the database.
74 *
75 * @param iterable $ips The IP(s) to be inserted.
76 *
77 * @return void
78 */
79 public function bulk_insert( $ips ): void {
80 $batch_size = 1000;
81 $values = array();
82 $placeholders = array();
83 $counter = 0;
84
85 foreach ( $ips as $ip ) {
86 $values[] = $ip;
87 $placeholders[] = '(%s)';
88 ++$counter;
89
90 if ( $counter === $batch_size ) {
91 $this->execute_insert( $placeholders, $values );
92
93 $values = array();
94 $placeholders = array();
95 $counter = 0;
96 }
97 }
98
99 // Insert remaining records if any.
100 if ( $counter > 0 ) {
101 $this->execute_insert( $placeholders, $values );
102 }
103 }
104
105 /**
106 * Unlock IP(s) from the AntiBot Global Firewall.
107 *
108 * @param array $ips The IP(s) to be unlocked.
109 *
110 * @return int|false The number of rows affected, or false on error.
111 */
112 public function unlock_ips( array $ips ) {
113 if ( array() === $ips ) {
114 return false;
115 }
116
117 global $wpdb;
118
119 $table_name = $wpdb->prefix . $this->table;
120 $current_time = time();
121 $placeholders = implode( ', ', array_fill( 0, count( $ips ), '%s' ) );
122
123 return $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
124 $wpdb->prepare(
125 "UPDATE $table_name SET unlocked = 1, unlocked_at = %d WHERE ip IN ($placeholders)", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
126 array_merge( array( $current_time ), $ips )
127 )
128 );
129 }
130
131 /**
132 * Check if the blocklist table has any records.
133 *
134 * @return bool
135 */
136 public function has_records(): bool {
137 global $wpdb;
138
139 $table_name = $wpdb->prefix . $this->table;
140
141 $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM $table_name LIMIT 1" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
142
143 return $count > 0;
144 }
145
146 /**
147 * Executes the insert query with the provided values and placeholders.
148 *
149 * @param array $placeholders The placeholders for the insert query.
150 * @param array $values The values to be inserted.
151 *
152 * @return void
153 */
154 private function execute_insert( array $placeholders, array $values ): void {
155 global $wpdb;
156
157 $table_name = $wpdb->prefix . $this->table;
158
159 $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
160 $wpdb->prepare(
161 "INSERT INTO $table_name (ip) VALUES " . implode( ', ', $placeholders ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
162 $values
163 )
164 );
165 }
166 }
167