| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component\Security_Tweaks\Servers; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use DOMXPath; |
| 7 |
use DOMDocument; |
| 8 |
|
| 9 |
class IIS_7 { |
| 10 |
/** |
| 11 |
* New htaccess file. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $new_htaccess_config = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Service type. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $type = null; |
| 23 |
|
| 24 |
public function __construct( $type ) { |
| 25 |
$this->type = $type; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Check whether the issue has been resolved or not. |
| 30 |
* |
| 31 |
* @return bool |
| 32 |
*/ |
| 33 |
public function check() { |
| 34 |
$url = ''; |
| 35 |
|
| 36 |
if ( 'prevent-php-executed' === $this->type ) { |
| 37 |
$dir = wp_upload_dir(); |
| 38 |
$url = $dir['baseurl'] . '/wp-defender/index.php'; |
| 39 |
} |
| 40 |
|
| 41 |
if ( 'protect-information' === $this->type ) { |
| 42 |
$url = defender_asset_url( '/languages/wpdef-default.pot' ); |
| 43 |
} |
| 44 |
|
| 45 |
return Server::ping_test_failed( $url ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Process the rule. |
| 50 |
* |
| 51 |
* @return bool|WP_Error |
| 52 |
*/ |
| 53 |
public function process() { |
| 54 |
$path = WP_CONTENT_DIR . '/uploads'; |
| 55 |
$filename = 'web.config'; |
| 56 |
|
| 57 |
if ( ! file_exists( $path . '/' . $filename ) ) { |
| 58 |
$fp = fopen( $path . '/' . $filename, 'w' ); |
| 59 |
fwrite( $fp, '<configuration/>' ); |
| 60 |
fclose( $fp ); |
| 61 |
} |
| 62 |
|
| 63 |
$formatxml = PHP_EOL; |
| 64 |
$formatxml .= " <handlers accessPolicy=\"Read\" />"; |
| 65 |
$formatxml .= PHP_EOL; |
| 66 |
|
| 67 |
$doc = new DOMDocument(); |
| 68 |
$doc->preserveWhiteSpace = true; |
| 69 |
|
| 70 |
if ( $doc->load( $path . '/' . $filename ) === false ) { |
| 71 |
return new WP_Error( |
| 72 |
'defender_file_not_editable', |
| 73 |
sprintf( |
| 74 |
__( 'The file %s could not be loaded.', 'wpdef' ), |
| 75 |
$filename |
| 76 |
) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
$xpath = new DOMXPath( $doc ); |
| 81 |
$read_accesspolicy = $xpath->query( '/configuration/system.webServer/handlers[starts-with(@accessPolicy,\'Read\')]' ); |
| 82 |
|
| 83 |
if ( $read_accesspolicy->length > 0 ) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
$xmlnodes = $xpath->query( '/configuration/system.webServer/handlers' ); |
| 88 |
|
| 89 |
if ( $xmlnodes->length > 0 ) { |
| 90 |
$handlers_node = $xmlnodes->item(0); |
| 91 |
} else { |
| 92 |
$handlers_node = $doc->createElement( 'handlers' ); |
| 93 |
$xmlnodes = $xpath->query( '/configuration/system.webServer' ); |
| 94 |
|
| 95 |
if ( $xmlnodes->length > 0 ) { |
| 96 |
$system_webServer_node = $xmlnodes->item(0); |
| 97 |
$handler_fragment = $doc->createDocumentFragment(); |
| 98 |
$handler_fragment->appendXML( $formatxml ); |
| 99 |
$system_webServer_node->appendChild( $handler_fragment ); |
| 100 |
} else { |
| 101 |
$system_webServer_node = $doc->createElement( 'system.webServer' ); |
| 102 |
$handler_fragment = $doc->createDocumentFragment(); |
| 103 |
$handler_fragment->appendXML( $formatxml ); |
| 104 |
$system_webServer_node->appendChild( $handler_fragment ); |
| 105 |
$xmlnodes = $xpath->query( '/configuration' ); |
| 106 |
|
| 107 |
if ( $xmlnodes->length > 0 ) { |
| 108 |
$config_node = $xmlnodes->item(0); |
| 109 |
$config_node->appendChild( $system_webServer_node ); |
| 110 |
} else { |
| 111 |
$config_node = $doc->createElement( 'configuration' ); |
| 112 |
$doc->appendChild( $config_node ); |
| 113 |
$config_node->appendChild( $system_webServer_node ); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$rule_fragment = $doc->createDocumentFragment(); |
| 119 |
$rule_fragment->appendXML( $formatxml ); |
| 120 |
$handlers_node->appendChild( $rule_fragment ); |
| 121 |
|
| 122 |
$doc->encoding = 'UTF-8'; |
| 123 |
$doc->formatOutput = true; |
| 124 |
saveDomDocument( $doc, $path .'/'. $filename ); |
| 125 |
|
| 126 |
$settings = array( |
| 127 |
'new_htaccess_config' => $this->get_new_htaccess_config(), |
| 128 |
); |
| 129 |
|
| 130 |
return update_site_option( "defender_security_tweeks_{$this->type}", $settings ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Revert the rule. |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
public function revert() { |
| 140 |
$path = WP_CONTENT_DIR . '/uploads'; |
| 141 |
$filename = 'web.config'; |
| 142 |
|
| 143 |
if ( ! file_exists( $path . '/' . $filename ) ) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
$doc = new DOMDocument(); |
| 148 |
$doc->preserveWhiteSpace = false; |
| 149 |
|
| 150 |
if ( $doc->load( $path . '/' . $filename ) === false ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
$xpath = new DOMXPath( $doc ); |
| 155 |
$handlers = $xpath->query( '/configuration/system.webServer/handlers[contains(@accessPolicy,\'Read\')]' ); |
| 156 |
|
| 157 |
if ( $handlers->length > 0 ) { |
| 158 |
$child = $handlers->item(0); |
| 159 |
$parent = $child->parentNode; |
| 160 |
$parent->removeChild( $child ); |
| 161 |
$doc->formatOutput = true; |
| 162 |
saveDomDocument( $doc, $path .'/'. $filename ); |
| 163 |
} |
| 164 |
|
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get the new HT config. |
| 170 |
* |
| 171 |
* @return array - $new_htaccess_config |
| 172 |
*/ |
| 173 |
public function get_new_htaccess_config() { |
| 174 |
return $this->new_htaccess_config; |
| 175 |
} |
| 176 |
} |
| 177 |
|