mainwp-child/class/class-mainwp-security.php

279 lines
8.2 KiB
PHP
Raw Normal View History

2015-10-15 22:52:37 +10:00
<?php
2020-05-05 20:13:38 +07:00
namespace MainWP\Child;
2015-10-15 22:52:37 +10:00
class MainWP_Security {
2020-05-05 20:13:38 +07:00
/**
* Method get_class_name()
*
* Get Class Name.
*
* @return object
*/
public static function get_class_name() {
return __CLASS__;
}
2020-05-06 20:22:11 +07:00
public static function fix_all() {
2020-03-26 14:05:04 +00:00
self::remove_wp_version();
self::remove_rsd();
self::remove_wlw();
self::remove_php_reporting();
self::remove_registered_versions();
self::remove_generator_version();
self::remove_readme();
2015-10-15 22:52:37 +10:00
2020-05-05 20:13:38 +07:00
add_filter( 'style_loader_src', array( self::get_class_name(), 'remove_script_versions' ), PHP_INT_MAX );
add_filter( 'style_loader_src', array( self::get_class_name(), 'remove_theme_versions' ), PHP_INT_MAX );
add_filter( 'script_loader_src', array( self::get_class_name(), 'remove_script_versions' ), PHP_INT_MAX );
add_filter( 'script_loader_src', array( self::get_class_name(), 'remove_theme_versions' ), PHP_INT_MAX );
2015-10-15 22:52:37 +10:00
}
2020-04-21 20:09:08 +02:00
// Prevent listing wp-content, wp-content/plugins, wp-content/themes, wp-content/uploads.
2015-10-15 22:52:37 +10:00
private static $listingDirectories = null;
2020-05-06 20:22:11 +07:00
private static function init_listing_directories() {
2020-03-26 14:05:04 +00:00
if ( null === self::$listingDirectories ) {
2020-03-26 19:45:07 +00:00
$wp_upload_dir = wp_upload_dir();
2020-03-26 14:05:04 +00:00
self::$listingDirectories = array(
2015-10-15 22:52:37 +10:00
WP_CONTENT_DIR,
WP_PLUGIN_DIR,
get_theme_root(),
$wp_upload_dir['basedir'],
);
}
}
public static function prevent_listing_ok() {
2020-05-06 20:22:11 +07:00
self::init_listing_directories();
2020-03-26 14:05:04 +00:00
foreach ( self::$listingDirectories as $directory ) {
2015-10-15 22:52:37 +10:00
$file = $directory . DIRECTORY_SEPARATOR . 'index.php';
if ( ! file_exists( $file ) ) {
return false;
}
}
return true;
}
public static function prevent_listing() {
2020-05-06 20:22:11 +07:00
self::init_listing_directories();
2020-03-26 14:05:04 +00:00
foreach ( self::$listingDirectories as $directory ) {
2015-10-15 22:52:37 +10:00
$file = $directory . DIRECTORY_SEPARATOR . 'index.php';
if ( ! file_exists( $file ) ) {
$h = fopen( $file, 'w' );
2020-05-05 20:13:38 +07:00
fwrite( $h, "<?php \n" );
fwrite( $h, "header(\$_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden' );\n" );
fwrite( $h, "die( '403 Forbidden' );\n" );
2015-10-15 22:52:37 +10:00
fclose( $h );
}
}
}
public static function get_security_option( $option ) {
$security = get_option( 'mainwp_security' );
return ! empty( $security ) && isset( $security[ $option ] ) && ( true === $security[ $option ] );
}
2020-04-21 20:09:08 +02:00
// Removed wp-version.
2015-10-15 22:52:37 +10:00
public static function remove_wp_version_ok() {
return ! ( has_action( 'wp_head', 'wp_generator' ) || has_filter( 'wp_head', 'wp_generator' ) );
}
public static function remove_wp_version( $force = false ) {
if ( $force || self::get_security_option( 'wp_version' ) ) {
remove_action( 'wp_head', 'wp_generator' );
remove_filter( 'wp_head', 'wp_generator' );
}
}
2020-04-21 20:09:08 +02:00
// Removed Really Simple Discovery meta tag.
2015-10-15 22:52:37 +10:00
public static function remove_rsd_ok() {
return ( ! has_action( 'wp_head', 'rsd_link' ) );
}
public static function remove_rsd( $force = false ) {
if ( $force || self::get_security_option( 'rsd' ) ) {
remove_action( 'wp_head', 'rsd_link' );
}
}
2020-04-21 20:09:08 +02:00
// Removed Windows Live Writer meta tag.
2015-10-15 22:52:37 +10:00
public static function remove_wlw_ok() {
return ( ! has_action( 'wp_head', 'wlwmanifest_link' ) );
}
public static function remove_wlw( $force = false ) {
if ( $force || self::get_security_option( 'wlw' ) ) {
remove_action( 'wp_head', 'wlwmanifest_link' );
}
}
2020-04-21 20:09:08 +02:00
// File permissions not secure.
2015-10-15 22:52:37 +10:00
private static $permission_checks = null;
private static function init_permission_checks() {
2020-03-26 14:05:04 +00:00
if ( null === self::$permission_checks ) {
self::$permission_checks = array(
2020-03-26 15:29:54 +00:00
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . '../' => '0755',
2015-10-15 22:52:37 +10:00
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . '../wp-includes' => '0755',
2020-03-26 15:29:54 +00:00
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . '../.htaccess' => '0644',
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'index.php' => '0644',
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'js/' => '0755',
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'themes' => '0755',
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' => '0755',
WP_CONTENT_DIR . DIRECTORY_SEPARATOR . '../wp-admin' => '0755',
WP_CONTENT_DIR => '0755',
2015-10-15 22:52:37 +10:00
);
}
}
2020-04-21 20:09:08 +02:00
// Database error reporting turned on/off.
2015-10-15 22:52:37 +10:00
public static function remove_database_reporting_ok() {
global $wpdb;
return ( false === $wpdb->show_errors );
}
public static function remove_database_reporting() {
global $wpdb;
$wpdb->hide_errors();
$wpdb->suppress_errors();
}
2020-04-21 20:09:08 +02:00
// PHP error reporting turned on/off.
2015-10-15 22:52:37 +10:00
public static function remove_php_reporting_ok() {
return ! ( ( ( 0 != ini_get( 'display_errors' ) ) && ( 'off' != ini_get( 'display_errors' ) ) ) || ( ( 0 != ini_get( 'display_startup_errors' ) ) && ( 'off' != ini_get( 'display_startup_errors' ) ) ) );
2015-10-15 22:52:37 +10:00
}
public static function remove_php_reporting( $force = false ) {
if ( $force || self::get_security_option( 'php_reporting' ) ) {
2020-05-07 23:22:05 +07:00
error_reporting( 0 ); //phpcs:ignore -- to custom display errors
2020-05-07 19:34:36 +07:00
ini_set( 'display_errors', 'off' ); //phpcs:ignore -- to custom display errors
ini_set( 'display_startup_errors', 0 ); //phpcs:ignore -- to custom
2015-10-15 22:52:37 +10:00
}
}
2020-04-21 20:09:08 +02:00
// Removed version information for scripts/stylesheets.
2015-10-15 22:52:37 +10:00
public static function remove_scripts_version_ok() {
return self::get_security_option( 'scripts_version' );
}
public static function remove_script_versions( $src ) {
if ( self::get_security_option( 'scripts_version' ) ) {
if ( strpos( $src, '?ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
2015-10-15 22:52:37 +10:00
}
return $src;
}
return $src;
}
2018-09-27 19:52:32 +02:00
public static function remove_registered_versions_ok() {
return self::get_security_option( 'registered_versions' );
}
public static function remove_registered_versions() {
if ( self::get_security_option( 'registered_versions' ) ) {
2018-09-27 19:52:32 +02:00
global $wp_styles;
2020-03-26 17:03:00 +00:00
if ( $wp_styles instanceof WP_Styles ) {
2018-09-27 19:52:32 +02:00
foreach ( $wp_styles->registered as $handle => $style ) {
$wp_styles->registered[ $handle ]->ver = null;
}
2018-09-27 19:52:32 +02:00
}
global $wp_scripts;
2018-09-27 19:52:32 +02:00
if ( $wp_scripts instanceof WP_Scripts ) {
foreach ( $wp_scripts->registered as $handle => $script ) {
$wp_scripts->registered[ $handle ]->ver = null;
}
2018-09-27 19:52:32 +02:00
}
}
}
2016-02-15 22:08:39 +01:00
public static function remove_generator_version_ok() {
return self::get_security_option( 'generator_version' );
}
public static function remove_generator_version( $force = false ) {
if ( $force || self::get_security_option( 'generator_version' ) ) {
$types = array( 'html', 'xhtml', 'atom', 'rss2', 'rdf', 'comment', 'export' );
foreach ( $types as $type ) {
2016-02-17 20:38:44 +01:00
add_filter( 'get_the_generator_' . $type, array( 'MainWP_Security', 'custom_the_generator' ), 10, 2 );
2016-02-15 22:08:39 +01:00
}
}
}
2016-02-17 20:38:44 +01:00
public static function custom_the_generator( $generator, $type = '' ) {
2016-02-15 22:08:39 +01:00
return '';
}
2015-10-15 22:52:37 +10:00
public static function remove_theme_versions( $src ) {
2017-01-14 13:16:52 +01:00
if ( self::get_security_option( 'styles_version' ) ) {
2015-10-15 22:52:37 +10:00
if ( strpos( $src, '?ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
2015-10-15 22:52:37 +10:00
}
return $src;
}
return $src;
}
public static function remove_readme( $force = false ) {
2020-04-21 20:09:08 +02:00
// to prevent remove readme.html file on WPE hosts.
if ( MainWP_Helper::is_wp_engine() ) {
return true;
}
2015-10-15 22:52:37 +10:00
if ( $force || self::get_security_option( 'readme' ) ) {
2020-04-23 19:53:22 +02:00
if ( file_exists( ABSPATH . 'readme.html' ) ) {
if ( ! unlink( ABSPATH . 'readme.html' ) ) {
2020-05-06 00:47:59 +07:00
MainWP_Helper::get_wp_filesystem();
2015-10-15 22:52:37 +10:00
global $wp_filesystem;
if ( ! empty( $wp_filesystem ) ) {
$wp_filesystem->delete( ABSPATH . 'readme.html' );
2020-04-23 19:53:22 +02:00
if ( file_exists( ABSPATH . 'readme.html' ) ) {
2020-04-21 20:09:08 +02:00
// prevent repeat delete.
self::update_security_option( 'readme', false );
2016-04-27 21:54:28 +02:00
}
2015-10-15 22:52:37 +10:00
}
}
}
}
}
public static function remove_readme_ok() {
return ! file_exists( ABSPATH . 'readme.html' );
}
public static function remove_styles_version_ok() {
return self::get_security_option( 'styles_version' );
}
2020-04-21 20:09:08 +02:00
// Admin user name is not admin.
2015-10-15 22:52:37 +10:00
public static function admin_user_ok() {
$user = get_user_by( 'login', 'admin' );
2020-03-26 17:03:00 +00:00
if ( ! $user ) {
2020-03-26 19:51:58 +00:00
return true;
}
2015-10-15 22:52:37 +10:00
if ( 10 !== $user->wp_user_level && ( ! isset( $user->user_level ) || 10 !== $user->user_level ) && ! user_can( $user, 'level_10' ) ) {
return true;
}
return false;
2015-10-15 22:52:37 +10:00
}
2016-02-15 22:08:39 +01:00
public static function update_security_option( $key, $value ) {
$security = get_option( 'mainwp_security' );
2020-04-21 20:09:08 +02:00
if ( ! empty( $key ) ) {
2020-03-26 15:29:54 +00:00
$security[ $key ] = $value;
}
2016-02-15 22:08:39 +01:00
MainWP_Helper::update_option( 'mainwp_security', $security, 'yes' );
}
2015-10-15 22:52:37 +10:00
}