get_headers_nonce() . ' ) return;' . "\n\n"; $contents .= 'if (!defined("RSSSL_HEADERS_ACTIVE")) define("RSSSL_HEADERS_ACTIVE", true);'."\n"; $contents .= "//RULES START\n".$rules; // write to advanced-header.php file if ( is_writable( WP_CONTENT_DIR ) ) { file_put_contents( $advanced_headers_file, $contents ); } $wpconfig_path = $this->find_wp_config_path(); $wpconfig = file_get_contents( $wpconfig_path ); if ( is_writable( $wpconfig_path ) && strpos( $wpconfig, 'advanced-headers.php' ) === false ) { // As WP_CONTENT_DIR is not defined at this point in the wp-config, we can't use that. // for those setups where the WP_CONTENT_DIR is not in the default location, we hardcode the path. if ( $use_dynamic_path ) { $rule = 'if (file_exists( ABSPATH . "wp-content/advanced-headers.php")) {' . "\n"; $rule .= "\t" . 'require_once ABSPATH . "wp-content/advanced-headers.php";' . "\n" . '}'; } else { $rule = 'if (file_exists(\'' . WP_CONTENT_DIR . '/advanced-headers.php\')) {' . "\n"; $rule .= "\t" . 'require_once \'' . WP_CONTENT_DIR . '/advanced-headers.php\';' . "\n" . '}'; } //if RSSSL comment is found, insert after $rsssl_comment = '//END Really Simple SSL Server variable fix'; if ( strpos($wpconfig, $rsssl_comment)!==false ) { $pos = strrpos($wpconfig, $rsssl_comment); $updated = substr_replace($wpconfig, $rsssl_comment."\n" . $rule . "\n", $pos, strlen($rsssl_comment)); } else { $updated = preg_replace( '/' . '<\?php' . '/', 'has_rules()) { return false; } return !defined('RSSSL_HEADERS_ACTIVE'); } /** * Show some notices * @param array $notices * * @return array */ public function notices( $notices ) { $notices['firewall-error'] = array( 'callback' => 'RSSSL_SECURITY()->firewall_manager->firewall_write_error', 'score' => 5, 'output' => array( 'wpconfig-notwritable' => array( 'title' => __("Firewall", "really-simple-ssl"), 'msg' => __("A firewall rule was enabled, but the wp-config.php is not writable.", "really-simple-ssl").' '.__("Please set the wp-config.php to writable until the rule has been written.", "really-simple-ssl"), 'icon' => 'open', 'dismissible' => true, ), 'advanced-headers-notwritable' => array( 'title' => __("Firewall", "really-simple-ssl"), 'msg' => __("A firewall rule was enabled, but /the wp-content/ folder is not writable.", "really-simple-ssl").' '.__("Please set the wp-content folder to writable:", "really-simple-ssl"), 'icon' => 'open', 'dismissible' => true, ), ), 'show_with_options' => [ 'disable_http_methods', ] ); $notices['firewall-active'] = array( 'condition' => ['RSSSL_SECURITY()->firewall_manager->firewall_active_error'], 'callback' => '_true_', 'score' => 5, 'output' => array( 'true' => array( 'title' => __("Firewall", "really-simple-ssl"), 'msg' => __("A firewall rule was enabled, but the firewall does not seem to get loaded correctly.", "really-simple-ssl").' '.__("Please check if the advanced-headers.php file is included in the wp-config.php, and exists in the wp-content folder.", "really-simple-ssl"), 'icon' => 'open', 'dismissible' => true, ), ), 'show_with_options' => [ 'disable_http_methods', ] ); return $notices; } /** * Admin is not always loaded here, so we define our own function * @return string|null */ public function find_wp_config_path() { //limit nr of iterations to 5 $i = 0; $maxiterations = 5; $dir = ABSPATH; do { $i++; if (file_exists($dir . "/wp-config.php")) { return $dir . "/wp-config.php"; } } while (($dir = realpath("$dir/..")) && ($i < $maxiterations)); return null; } /** * Clear the headers * @return void */ public function remove_advanced_headers() { if ( !rsssl_user_can_manage() ) { return; } $file = ABSPATH . 'wp-content/advanced-headers.php'; $wpconfig_path = $this->find_wp_config_path(); if ( is_writable( $wpconfig_path ) ) { $wpconfig = file_get_contents( $wpconfig_path ); $rule = "if ( file_exists('" . $file . "') ) { " . "\n"; $rule .= "\t" . "require_once '$file';" . "\n" . "}"; if ( strpos( $wpconfig, $rule ) !== false ) { $updated_wpconfig = str_replace( $rule, '', $wpconfig ); file_put_contents( $wpconfig_path, $updated_wpconfig ); } } if ( file_exists( $file ) ) { unlink( $file ); } } }