| 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 ( empty( $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 |
|