| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles Web Application Firewall related functionality. |
| 4 |
* |
| 5 |
* @package WP_Defender\Controller |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Controller; |
| 9 |
|
| 10 |
use WP_Defender\Controller; |
| 11 |
use Calotes\Component\Response; |
| 12 |
use WP_Defender\Behavior\WPMUDEV; |
| 13 |
|
| 14 |
/** |
| 15 |
* Handles the settings, data, and other functionality related to the Web Application Firewall (WAF). |
| 16 |
*/ |
| 17 |
class WAF extends Controller { |
| 18 |
|
| 19 |
/** |
| 20 |
* The slug identifier for this controller. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $slug = 'wdf-waf'; |
| 25 |
/** |
| 26 |
* The WPMUDEV instance used for interacting with WPMUDEV services. |
| 27 |
* |
| 28 |
* @var WPMUDEV |
| 29 |
*/ |
| 30 |
private $wpmudev; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initializes the model and service, registers routes, and sets up scheduled events if the model is active. |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
$this->wpmudev = wd_di()->get( WPMUDEV::class ); |
| 37 |
|
| 38 |
// Return the constructor and do not register WAF page if Whitelable is enabled. |
| 39 |
if ( $this->wpmudev->is_whitelabel_enabled() ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$this->register_page( |
| 44 |
esc_html__( 'WAF', 'defender-security' ), |
| 45 |
$this->slug, |
| 46 |
array( $this, 'main_view' ), |
| 47 |
$this->parent_slug |
| 48 |
); |
| 49 |
add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) ); |
| 50 |
$this->register_routes(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Enqueues scripts and styles for this page. |
| 55 |
* Only enqueues assets if the page is active. |
| 56 |
*/ |
| 57 |
public function enqueue_assets() { |
| 58 |
if ( ! $this->is_page_active() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
wp_localize_script( 'def-waf', 'waf', $this->data_frontend() ); |
| 62 |
wp_enqueue_script( 'def-waf' ); |
| 63 |
$this->enqueue_main_assets(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Renders the main view for this page. |
| 68 |
*/ |
| 69 |
public function main_view() { |
| 70 |
$this->render( 'main' ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Retrieves the WAF status for a given site. |
| 75 |
* |
| 76 |
* @return bool Returns false on failure, true if WAF is enabled. |
| 77 |
*/ |
| 78 |
public function get_waf_status(): bool { |
| 79 |
$status = defender_get_hosting_feature_state( 'waf' ); |
| 80 |
return '' !== $status && true === (bool) $status; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Remove the cache and return latest data. |
| 85 |
* |
| 86 |
* @return Response |
| 87 |
* @defender_route |
| 88 |
*/ |
| 89 |
public function recheck(): Response { |
| 90 |
return new Response( true, array( 'waf' => $this->data_frontend()['waf'] ) ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Checks if the WAF dashboard widget should be shown. |
| 95 |
* |
| 96 |
* @return bool Whether to show the widget or not. |
| 97 |
*/ |
| 98 |
public function maybe_show_dashboard_widget(): bool { |
| 99 |
if ( |
| 100 |
// Not hosted on us. |
| 101 |
! $this->wpmudev->is_wpmu_hosting() |
| 102 |
// Pro. |
| 103 |
&& true === $this->wpmudev->is_pro() |
| 104 |
// Enable whitelabel. |
| 105 |
&& $this->wpmudev->is_whitelabel_enabled() |
| 106 |
) { |
| 107 |
// Hide it. |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Converts the current object state to an array. |
| 116 |
* |
| 117 |
* @return array The array representation of the object. |
| 118 |
*/ |
| 119 |
public function to_array(): array { |
| 120 |
return array( |
| 121 |
'waf' => array( |
| 122 |
'hosted' => $this->wpmudev->is_wpmu_hosting(), |
| 123 |
'status' => $this->get_waf_status(), |
| 124 |
'maybe_show' => $this->maybe_show_dashboard_widget(), |
| 125 |
), |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Removes settings for all submodules. |
| 131 |
*/ |
| 132 |
public function remove_settings() { |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Delete all the data & the cache. |
| 137 |
*/ |
| 138 |
public function remove_data() { |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Provides data for the frontend. |
| 143 |
* |
| 144 |
* @return array An array of data for the frontend. |
| 145 |
*/ |
| 146 |
public function data_frontend(): array { |
| 147 |
$site_id = $this->wpmudev->get_site_id(); |
| 148 |
|
| 149 |
return array_merge( |
| 150 |
array( |
| 151 |
'site_id' => $site_id, |
| 152 |
'waf' => array( |
| 153 |
'hosted' => $this->wpmudev->is_wpmu_hosting(), |
| 154 |
'status' => $this->get_waf_status(), |
| 155 |
), |
| 156 |
'hub_connector' => wd_di()->get( Hub_Connector::class )->data_frontend(), |
| 157 |
'antibot' => wd_di()->get( Antibot_Global_Firewall::class )->data_frontend(), |
| 158 |
), |
| 159 |
$this->dump_routes_and_nonces() |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Imports data into the model. |
| 165 |
* |
| 166 |
* @param array $data Data to be imported into the model. |
| 167 |
*/ |
| 168 |
public function import_data( array $data ) { |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Exports strings. |
| 173 |
* |
| 174 |
* @return array An array of strings. |
| 175 |
*/ |
| 176 |
public function export_strings(): array { |
| 177 |
return array(); |
| 178 |
} |
| 179 |
} |
| 180 |
|