| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles quarantine related actions. |
| 4 |
* |
| 5 |
* @package WP_Defender\Controller |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Controller; |
| 9 |
|
| 10 |
use WP_Defender\Controller; |
| 11 |
use Calotes\Component\Request; |
| 12 |
use Calotes\Component\Response; |
| 13 |
use WP_Defender\Component\Quarantine as Quarantine_Component; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handles quarantine related actions. |
| 17 |
*/ |
| 18 |
class Quarantine extends Controller { |
| 19 |
|
| 20 |
/** |
| 21 |
* Quarantine component. |
| 22 |
* |
| 23 |
* @var Quarantine_Component |
| 24 |
*/ |
| 25 |
private $quarantine_component; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initializes the model and service, registers routes, and sets up scheduled events if the model is active. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->register_routes(); |
| 32 |
$this->quarantine_component = wd_di()->get( Quarantine_Component::class ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Provides data for the frontend. |
| 37 |
* |
| 38 |
* @return array An array of data for the frontend. |
| 39 |
*/ |
| 40 |
public function data_frontend(): array { |
| 41 |
$quarantine_directory = array( |
| 42 |
'url' => $this->quarantine_component->quarantine_directory_url(), |
| 43 |
'permission' => $this->quarantine_component::QUARANTINE_DIRECTORY_PERMISSION, |
| 44 |
'is_quarantine_directory_url_forbidden' => $this->quarantine_component->is_quarantine_directory_url_forbidden(), |
| 45 |
); |
| 46 |
|
| 47 |
return array_merge( |
| 48 |
array( |
| 49 |
'list' => $this->quarantine_component->quarantine_collection(), |
| 50 |
'cron_schedules' => $this->quarantine_component->cron_schedules(), |
| 51 |
'quarantine_directory' => $quarantine_directory, |
| 52 |
), |
| 53 |
$this->dump_routes_and_nonces() |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Converts the current object state to an array. |
| 59 |
* |
| 60 |
* @return array The array representation of the object. |
| 61 |
*/ |
| 62 |
public function to_array(): array { |
| 63 |
return array(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Imports data into the model. |
| 68 |
* |
| 69 |
* @param array $data Data to be imported into the model. |
| 70 |
*/ |
| 71 |
public function import_data( array $data ) { |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Removes settings for all submodules. |
| 76 |
*/ |
| 77 |
public function remove_settings() { |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Delete all the data & the cache. |
| 82 |
*/ |
| 83 |
public function remove_data() { |
| 84 |
$this->quarantine_component->on_uninstall(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Exports strings. |
| 89 |
* |
| 90 |
* @return array An array of strings. |
| 91 |
*/ |
| 92 |
public function export_strings(): array { |
| 93 |
return array(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Restore the quarantined file. |
| 98 |
* |
| 99 |
* @param Request $request Request object. |
| 100 |
* |
| 101 |
* @return Response |
| 102 |
* @defender_route |
| 103 |
*/ |
| 104 |
public function restore_file( Request $request ) { |
| 105 |
$data = $request->get_data( |
| 106 |
array( |
| 107 |
'id' => array( |
| 108 |
'type' => 'int', |
| 109 |
), |
| 110 |
) |
| 111 |
); |
| 112 |
|
| 113 |
$action = $this->quarantine_component->restore_file( $data['id'] ); |
| 114 |
|
| 115 |
if ( isset( $action['success'] ) && true === $action['success'] ) { |
| 116 |
return new Response( |
| 117 |
true, |
| 118 |
array( |
| 119 |
'message' => $action['message'], |
| 120 |
'file_id' => $data['id'], |
| 121 |
'success' => true, |
| 122 |
'quarantine_collection' => $this->quarantine_component->quarantine_collection(), |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
return new Response( |
| 128 |
false, |
| 129 |
array( |
| 130 |
'message' => $action['message'], |
| 131 |
'file_id' => $data['id'], |
| 132 |
'success' => false, |
| 133 |
) |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get quarantine collection. |
| 139 |
* |
| 140 |
* @return Response |
| 141 |
* @defender_route |
| 142 |
*/ |
| 143 |
public function quarantine_collection() { |
| 144 |
$data = $this->quarantine_component->quarantine_collection(); |
| 145 |
|
| 146 |
return new Response( |
| 147 |
true, |
| 148 |
array( |
| 149 |
'list' => $data, |
| 150 |
) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Delete quarantined file. |
| 156 |
* |
| 157 |
* @param Request $request Request object. |
| 158 |
* |
| 159 |
* @return Response |
| 160 |
* @defender_route |
| 161 |
*/ |
| 162 |
public function delete_file( Request $request ): Response { |
| 163 |
$data = $request->get_data( |
| 164 |
array( |
| 165 |
'id' => array( |
| 166 |
'type' => 'int', |
| 167 |
), |
| 168 |
'file_name' => array( |
| 169 |
'type' => 'string', |
| 170 |
), |
| 171 |
) |
| 172 |
); |
| 173 |
|
| 174 |
$action = $this->quarantine_component->delete_quarantined_file( $data['id'] ); |
| 175 |
|
| 176 |
if ( $action ) { |
| 177 |
return new Response( |
| 178 |
true, |
| 179 |
array( |
| 180 |
'message' => sprintf( |
| 181 |
/* translators: 1: Filename with extension */ |
| 182 |
esc_html__( 'Deleted %1$s permanently.', 'defender-security' ), |
| 183 |
'<strong>' . $data['file_name'] . '</strong>' |
| 184 |
), |
| 185 |
'file_id' => $data['id'], |
| 186 |
'success' => true, |
| 187 |
'quarantine_collection' => $this->quarantine_component->quarantine_collection(), |
| 188 |
) |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
return new Response( |
| 193 |
false, |
| 194 |
array( |
| 195 |
'message' => |
| 196 |
sprintf( |
| 197 |
/* translators: %s: Filename with extension */ |
| 198 |
esc_html__( |
| 199 |
'Deleting %s failed.', |
| 200 |
'defender-security' |
| 201 |
), |
| 202 |
'<strong>' . $data['file_name'] . '</strong>' |
| 203 |
), |
| 204 |
'file_id' => $data['id'], |
| 205 |
'success' => false, |
| 206 |
) |
| 207 |
); |
| 208 |
} |
| 209 |
} |
| 210 |
|