Support IIS 7.0 URL Rewrite Module. Props ruslany. Hat tips to peaceablewhale, hakre, Denis-de-Bernardy, sivel. fixes #8974

git-svn-id: http://svn.automattic.com/wordpress/trunk@11350 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-05-16 02:04:36 +00:00
parent 5a6cf8b500
commit 24f847661f
4 changed files with 322 additions and 15 deletions

View File

@ -135,6 +135,34 @@ function save_mod_rewrite_rules() {
return false;
}
/**
* Updates the IIS web.config file with the current rules if it is writable.
* If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
*
* @since 2.8.0
*
* @return bool True if web.config was updated successfully
*/
function iis7_save_url_rewrite_rules(){
global $wp_rewrite;
$home_path = get_home_path();
$web_config_file = $home_path . 'web.config';
// Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
if ( ( ! file_exists($web_config_file) && win_is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable($web_config_file) ) {
if ( iis7_supports_permalinks() ) {
$rule = $wp_rewrite->iis7_url_rewrite_rules();
if ( ! empty($rule) ) {
return iis7_add_rewrite_rule($web_config_file, $rule);
} else {
return iis7_delete_rewrite_rule($web_config_file);
}
}
}
return false;
}
/**
* {@internal Missing Short Description}}
*
@ -370,4 +398,215 @@ function wp_menu_unfold() {
exit;
}
}
?>
/**
* Check if IIS 7 supports pretty permalinks
*
* @since 2.8.0
*
* @return bool
*/
function iis7_supports_permalinks() {
global $is_iis7;
$supports_permalinks = false;
if ( $is_iis7 ) {
/* First we check if the DOMDocument class exists. If it does not exist,
* which is the case for PHP 4.X, then we cannot easily update the xml configuration file,
* hence we just bail out and tell user that pretty permalinks cannot be used.
* This is not a big issue because PHP 4.X is going to be depricated and for IIS it
* is recommended to use PHP 5.X NTS.
* Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
* URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
* Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
* via ISAPI then pretty permalinks will not work.
*/
$supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && ( php_sapi_name() == 'cgi-fcgi' );
}
return apply_filters('iis7_supports_permalinks', $supports_permalinks);
}
/**
* Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
*
* @since 2.8.0
*
* @return bool
* @param string $filename The file path to the configuration file
*/
function iis7_rewrite_rule_exists($filename) {
if ( ! file_exists($filename) )
return false;
if ( ! class_exists('DOMDocument') )
return false;
$doc = new DOMDocument();
if ( $doc->load($filename) === false )
return false;
$xpath = new DOMXPath($doc);
$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
if ( $rules->length == 0 )
return false;
else
return true;
}
/**
* Delete WordPress rewrite rule from web.config file if it exists there
*
* @since 2.8.0
*
* @param string $filename Name of the configuration file
* @return bool
*/
function iis7_delete_rewrite_rule($filename) {
// If configuration file does not exist then rules also do not exist so there is nothing to delete
if ( ! file_exists($filename) )
return true;
if ( ! class_exists('DOMDocument') )
return false;
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
if ( $doc -> load($filename) === false )
return false;
$xpath = new DOMXPath($doc);
$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
if ( $rules->length > 0 ) {
$child = $rules->item(0);
$parent = $child->parentNode;
$parent->removeChild($child);
$doc->formatOutput = true;
saveDomDocument($doc, $filename);
}
return true;
}
/**
* Add WordPress rewrite rule to the IIS 7 configuration file.
*
* @since 2.8.0
*
* @param string $filename The file path to the configuration file
* @param string $rewrite_rule The XML fragment with URL Rewrite rule
* @return bool
*/
function iis7_add_rewrite_rule($filename, $rewrite_rule) {
if ( ! class_exists('DOMDocument') )
return false;
// If configuration file does not exist then we create one.
if ( ! file_exists($filename) ) {
$fp = fopen( $filename, 'w');
fwrite($fp, '<configuration/>');
fclose($fp);
}
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
if ( $doc->load($filename) === false )
return false;
$xpath = new DOMXPath($doc);
// First check if the rule already exists as in that case there is no need to re-add it
$wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
if ( $wordpress_rules->length > 0 )
return true;
// Check the XPath to the rewrite rule and create XML nodes if they do not exist
$xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
if ( $xmlnodes->length > 0 ) {
$rules_node = $xmlnodes->item(0);
} else {
$rules_node = $doc->createElement('rules');
$xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
if ( $xmlnodes->length > 0 ) {
$rewrite_node = $xmlnodes->item(0);
$rewrite_node->appendChild($rules_node);
} else {
$rewrite_node = $doc->createElement('rewrite');
$rewrite_node->appendChild($rules_node);
$xmlnodes = $xpath->query('/configuration/system.webServer');
if ( $xmlnodes->length > 0 ) {
$system_webServer_node = $xmlnodes->item(0);
$system_webServer_node->appendChild($rewrite_node);
} else {
$system_webServer_node = $doc->createElement('system.webServer');
$system_webServer_node->appendChild($rewrite_node);
$xmlnodes = $xpath->query('/configuration');
if ( $xmlnodes->length > 0 ) {
$config_node = $xmlnodes->item(0);
$config_node->appendChild($system_webServer_node);
} else {
$config_node = $doc->createElement('configuration');
$doc->appendChild($config_node);
$config_node->appendChild($system_webServer_node);
}
}
}
}
$rule_fragment = $doc->createDocumentFragment();
$rule_fragment->appendXML($rewrite_rule);
$rules_node->appendChild($rule_fragment);
$doc->formatOutput = true;
saveDomDocument($doc, $filename);
return true;
}
/**
* Saves the XML document into a file
*
* @since 2.8.0
*
* @param DOMDocument $doc
* @param string $filename
*/
function saveDomDocument($doc, $filename) {
$config = $doc->saveXML();
$config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
$fp = fopen($filename, 'w');
fwrite($fp, $config);
fclose($fp);
}
/**
* Workaround for Windows bug in is_writable() function
*
* @since 2.8.0
*
* @param object $path
* @return bool
*/
function win_is_writable($path) {
/* will work in despite of Windows ACLs bug
* NOTE: use a trailing slash for folders!!!
* see http://bugs.php.net/bug.php?id=27609
* see http://bugs.php.net/bug.php?id=30931
*/
if ( $path{strlen($path)-1} == '/' ) // recursively return a temporary file path
return win_is_writable($path . uniqid(mt_rand()) . '.tmp');
else if ( is_dir($path) )
return win_is_writable($path . '/' . uniqid(mt_rand()) . '.tmp');
// check tmp file for read/write capabilities
$rm = file_exists($path);
$f = @fopen($path, 'a');
if ($f===false)
return false;
fclose($f);
if ( ! $rm )
unlink($path);
return true;
}
?>

View File

@ -70,6 +70,7 @@ add_filter('admin_head', 'add_js');
include('admin-header.php');
$home_path = get_home_path();
$iis7_permalinks = iis7_supports_permalinks();
if ( isset($_POST['permalink_structure']) || isset($_POST['category_base']) ) {
check_admin_referer('update-permalink');
@ -100,12 +101,19 @@ $permalink_structure = get_option('permalink_structure');
$category_base = get_option('category_base');
$tag_base = get_option( 'tag_base' );
if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') )
$writable = true;
else
$writable = false;
if ( $iis7_permalinks ) {
if ( ( ! file_exists($home_path . 'web.config') && win_is_writable($home_path) ) || win_is_writable($home_path . 'web.config') )
$writable = true;
else
$writable = false;
} else {
if ( ( ! file_exists($home_path . '.htaccess') && is_writable($home_path) ) || is_writable($home_path . '.htaccess') )
$writable = true;
else
$writable = false;
}
if ($wp_rewrite->using_index_permalinks())
if ( $wp_rewrite->using_index_permalinks() )
$usingpi = true;
else
$usingpi = false;
@ -115,11 +123,21 @@ $wp_rewrite->flush_rules();
<?php if (isset($_POST['submit'])) : ?>
<div id="message" class="updated fade"><p><?php
if ( $permalink_structure && !$usingpi && !$writable )
_e('You should update your .htaccess now.');
else
_e('Permalink structure updated.');
?></p></div>
if ( $iis7_permalinks ) {
if ( $permalink_structure && ! $usingpi && ! $writable )
_e('You should update your web.config now');
else if ( $permalink_structure && ! $usingpi && $writable)
_e('Permalink structure updated. Remove write access on web.config file now!');
else
_e('Permalink structure updated');
} else {
if ( $permalink_structure && ! $usingpi && ! $writable )
_e('You should update your .htaccess now.');
else
_e('Permalink structure updated.');
}
?>
</p></div>
<?php endif; ?>
<div class="wrap">
@ -134,7 +152,7 @@ else
<?php
$prefix = '';
if ( ! got_mod_rewrite() )
if ( ! got_mod_rewrite() && ! $iis7_permalinks )
$prefix = '/index.php';
$structures = array(
@ -179,7 +197,7 @@ $structures = array(
</table>
<h3><?php _e('Optional'); ?></h3>
<?php if ($is_apache) : ?>
<?php if ( $is_apache || $iis7_permalinks ) : ?>
<p><?php _e('If you like, you may enter custom structures for your category and tag <abbr title="Universal Resource Locator">URL</abbr>s here. For example, using <kbd>topics</kbd> as your category base would make your category links like <code>http://example.org/topics/uncategorized/</code>. If you leave these blank the defaults will be used.') ?></p>
<?php else : ?>
<p><?php _e('If you like, you may enter custom structures for your category and tag <abbr title="Universal Resource Locator">URL</abbr>s here. For example, using <code>topics</code> as your category base would make your category links like <code>http://example.org/index.php/topics/uncategorized/</code>. If you leave these blank the defaults will be used.') ?></p>
@ -203,12 +221,23 @@ $structures = array(
<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
</p>
</form>
<?php if ( $permalink_structure && !$usingpi && !$writable ) : ?>
<p><?php _e('If your <code>.htaccess</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.') ?></p>
<?php if ($iis7_permalinks) :
if ( isset($_POST['submit']) && $permalink_structure && ! $usingpi && ! $writable ) : ?>
<p><?php _e('If your <code>web.config</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so this is the url rewrite rule you should have in your <code>web.config</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all. Then insert this rule inside of the <code>/&lt;configuration&gt;/&lt;system.webServer&gt;/&lt;rewrite&gt;/&lt;rules&gt;</code> element in <code>web.config</code> file.') ?></p>
<form action="options-permalink.php" method="post">
<?php wp_nonce_field('update-permalink') ?>
<p><textarea rows="10" class="large-text readonly" name="rules" id="rules" readonly="readonly"><?php echo wp_specialchars($wp_rewrite->iis7_url_rewrite_rules()); ?></textarea></p>
</form>
<p><?php _e('If you temporarily make your <code>web.config</code> file writable for us to generate rewrite rules automatically, do not forget to revert the permissions after rule has been saved.') ?></p>
<?php endif; ?>
<?php else :
if ( $permalink_structure && ! $usingpi && ! $writable ) : ?>
<p><?php _e('If your <code>.htaccess</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.') ?></p>
<form action="options-permalink.php" method="post">
<?php wp_nonce_field('update-permalink') ?>
<p><textarea rows="6" class="large-text readonly" name="rules" id="rules" readonly="readonly"><?php echo wp_specialchars($wp_rewrite->mod_rewrite_rules()); ?></textarea></p>
</form>
<?php endif; ?>
<?php endif; ?>
</div>

View File

@ -1695,6 +1695,36 @@ class WP_Rewrite {
return $rules;
}
/**
* Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
*
* Does not actually write to the web.config file, but creates the rules for
* the process that will.
*
* @since 2.8.0
* @access public
*
* @return string
*/
function iis7_url_rewrite_rules(){
if ( ! $this->using_permalinks()) {
return '';
}
$rules = "<rule name=\"wordpress\" patternSyntax=\"Wildcard\">\n";
$rules .= " <match url=\"*\" />\n";
$rules .= " <conditions>\n";
$rules .= " <add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" negate=\"true\" />\n";
$rules .= " <add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" negate=\"true\" />\n";
$rules .= " </conditions>\n";
$rules .= " <action type=\"Rewrite\" url=\"index.php\" />\n";
$rules .= "</rule>";
$rules = apply_filters('iis7_url_rewrite_rules', $rules);
return $rules;
}
/**
* Add a straight rewrite rule.
@ -1790,6 +1820,8 @@ class WP_Rewrite {
$this->wp_rewrite_rules();
if ( function_exists('save_mod_rewrite_rules') )
save_mod_rewrite_rules();
if ( function_exists('iis7_save_url_rewrite_rules') )
iis7_save_url_rewrite_rules();
}
/**

View File

@ -73,4 +73,11 @@ $is_apache = ((strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) || (strp
*/
$is_IIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false) ? true : false;
/**
* Whether the server software is IIS 7.X
* @global bool $is_IIS7
*/
$is_iis7 = ($is_IIS && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7.') !== false) ? true : false;
?>