| 1 |
<?php |
| 2 |
namespace Falcon; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
|
| 6 |
class Security extends Base { |
| 7 |
protected $features = [ |
| 8 |
'no_rest_api', |
| 9 |
'no_xmlrpc', |
| 10 |
'no_login_errors', |
| 11 |
'restrict_upload', |
| 12 |
'block_ai_bots', |
| 13 |
'force_login', |
| 14 |
]; |
| 15 |
|
| 16 |
public function no_rest_api(): void { |
| 17 |
remove_action( 'wp_head', 'rest_output_link_wp_head' ); |
| 18 |
remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' ); |
| 19 |
remove_action( 'template_redirect', 'rest_output_link_header', 11 ); |
| 20 |
|
| 21 |
add_filter( 'rest_authentication_errors', [ $this, 'no_public_rest_api' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
public function no_public_rest_api( $access ) { |
| 25 |
return is_user_logged_in() |
| 26 |
? $access |
| 27 |
: new WP_Error( 'rest_login_required', __( 'REST API restricted to authenticated users.', 'falcon' ), [ 'status' => rest_authorization_required_code() ] ); |
| 28 |
} |
| 29 |
|
| 30 |
public function no_xmlrpc(): void { |
| 31 |
add_filter( 'xmlrpc_enabled', '__return_false' ); |
| 32 |
add_filter( 'xmlrpc_methods', '__return_empty_array' ); |
| 33 |
add_filter( 'pings_open', '__return_false' ); |
| 34 |
} |
| 35 |
|
| 36 |
public function no_login_errors(): void { |
| 37 |
add_filter( 'login_errors', [ $this, 'custom_login_errors' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
public function custom_login_errors( $error ): string { |
| 41 |
return __( 'There is something wrong. Please try again.', 'falcon' ); |
| 42 |
} |
| 43 |
|
| 44 |
public function restrict_upload(): void { |
| 45 |
add_filter( 'upload_mimes', [ $this, 'restrict_upload_mimes' ] ); |
| 46 |
} |
| 47 |
|
| 48 |
public function restrict_upload_mimes(): array { |
| 49 |
return [ |
| 50 |
'jpg|jpeg' => 'image/jpeg', |
| 51 |
'gif' => 'image/gif', |
| 52 |
'png' => 'image/png', |
| 53 |
'webp' => 'image/webp', |
| 54 |
'mp4' => 'video/mp4', |
| 55 |
'pdf' => 'application/pdf', |
| 56 |
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 57 |
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 58 |
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
public function block_ai_bots(): void { |
| 63 |
add_action( 'do_robotstxt', [ $this, 'block_ai_bots_in_robots_txt' ] ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Block AI bots via robots.txt |
| 68 |
* |
| 69 |
* @link https://neil-clarke.com/block-the-bots-that-feed-ai-models-by-scraping-your-website/ |
| 70 |
*/ |
| 71 |
public function block_ai_bots_in_robots_txt(): void { |
| 72 |
$user_agents = [ |
| 73 |
'CCBot', |
| 74 |
'ChatGPT-User', |
| 75 |
'GPTBot', |
| 76 |
'Google-Extended', |
| 77 |
'anthropic-ai', |
| 78 |
'Omgilibot', |
| 79 |
'Omgili', |
| 80 |
'FacebookBot', |
| 81 |
'Bytespider', |
| 82 |
]; |
| 83 |
$content = []; |
| 84 |
foreach ( $user_agents as $user_agent ) { |
| 85 |
$content[] = "User-agent: $user_agent"; |
| 86 |
$content[] = 'Disallow: /'; |
| 87 |
$content[] = ''; |
| 88 |
} |
| 89 |
|
| 90 |
echo "\n", implode( "\n", $content ), "\n"; // phpcs:ignore |
| 91 |
} |
| 92 |
|
| 93 |
public function force_login(): void { |
| 94 |
add_action( 'template_redirect', [ $this, 'redirect_non_logged_in_users' ] ); |
| 95 |
} |
| 96 |
|
| 97 |
public function redirect_non_logged_in_users(): void { |
| 98 |
if ( is_user_logged_in() ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// Set the headers to prevent caching. |
| 103 |
nocache_headers(); |
| 104 |
|
| 105 |
wp_safe_redirect( wp_login_url(), 302 ); |
| 106 |
die; |
| 107 |
} |
| 108 |
} |
| 109 |
|