PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.8.1
Defender Security – Malware Scanner, Login Security & Firewall v5.8.1
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 5.8.1, at src/model/class-antibot-global-firewall.php

152 lines 3.3 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 * Executes the insert query with the provided values and placeholders.
133 *
134 * @param array $placeholders The placeholders for the insert query.
135 * @param array $values The values to be inserted.
136 *
137 * @return void
138 */
139 private function execute_insert( array $placeholders, array $values ): void {
140 global $wpdb;
141
142 $table_name = $wpdb->prefix . $this->table;
143
144 $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
145 $wpdb->prepare(
146 "INSERT INTO $table_name (ip) VALUES " . implode( ', ', $placeholders ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
147 $values
148 )
149 );
150 }
151 }
152