Write rewrite rules to .htacces if .htaccess is writable. Create .htaccess if it does not exist and the directory is writable. Props to Owen Winkler.

git-svn-id: http://svn.automattic.com/wordpress/trunk@1489 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
rboren 2004-07-27 23:37:45 +00:00
parent c0a7433958
commit a095fdaf84
3 changed files with 105 additions and 26 deletions

View File

@ -417,4 +417,60 @@ function check_admin_referer() {
}
}
// insert_with_markers: Owen Winkler
// Inserts an array of strings into a file (.htaccess), placing it between
// BEGIN and END markers. Replaces existing marked info. Retains surrounding
// data. Creates file if none exists.
// Returns true on write success, false on failure.
function insert_with_markers($filename, $marker, $insertion) {
if (!file_exists($filename) || is_writeable($filename)) {
$markerdata = explode("\n", implode('', file($filename)));
$f = fopen($filename, 'w');
$foundit = false;
if ($markerdata) {
$state = true;
$newline = '';
foreach($markerdata as $markerline) {
if (strstr($markerline, "# BEGIN {$marker}")) $state = false;
if ($state) fwrite($f, "{$newline}{$markerline}");
if (strstr($markerline, "# END {$marker}")) {
fwrite($f, "{$newline}# BEGIN {$marker}");
if(is_array($insertion)) foreach($insertion as $insertline) fwrite($f, "{$newline}{$insertline}");
fwrite($f, "{$newline}# END {$marker}");
$state = true;
$foundit = true;
}
$newline = "\n";
}
}
if (!$foundit) {
fwrite($f, "# BEGIN {$marker}\n");
foreach($insertion as $insertline) fwrite($f, "{$insertline}\n");
fwrite($f, "# END {$marker}");
}
fclose($f);
return true;
} else {
return false;
}
}
// insert_with_markers: Owen Winkler
// Returns an array of strings from a file (.htaccess) from between BEGIN
// and END markers.
function extract_from_markers($filename, $marker) {
$result = array();
if($markerdata = explode("\n", implode('', file($filename))));
{
$state = false;
foreach($markerdata as $markerline) {
if(strstr($markerline, "# END {$marker}")) $state = false;
if($state) $result[] = $markerline;
if(strstr($markerline, "# BEGIN {$marker}")) $state = true;
}
}
return $result;
}
?>

View File

@ -44,6 +44,21 @@ if (isset($_POST['submit'])) {
<?php if (isset($_POST['submit'])) : ?>
<div class="updated"><p><?php _e('Permalink structure updated.'); ?></p></div>
<?php endif; ?>
<?php if(isset($_POST['rules'])) {
$rules = explode("\n", $_POST['rules']);
if(insert_with_markers(ABSPATH.'.htaccess', 'WordPress', $rules)) {
?>
<div class="updated" id="htupdate"><p><?php _e('mod_rewrite rules written to .htaccess.'); ?></p></div>
<?php
} else {
?>
<div class="updated" id="htupdate"><p><?php _e('Failed to write mod_rewrite rules to .htaccess.'); ?></p></div>
<?php
}
}
?>
<div class="wrap">
<h2><?php _e('Edit Permalink Structure') ?></h2>
<?php _e('<p>WordPress offers you the ability to create a custom URI structure for your permalinks and archives. The following &#8220;tags&#8221; are available:</p>')?>
@ -107,35 +122,18 @@ if (isset($_POST['submit'])) {
if ($permalink_structure) {
?>
<p><?php printf(__('Using the permalink structure value you currently have, <code>%s</code>, 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.'), $permalink_structure) ?></p>
<?php
$site_root = str_replace('http://', '', trim(get_settings('siteurl')));
$site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
$home_root = str_replace('http://', '', trim(get_settings('home')));
$home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
?>
<form action="">
<form action="options-permalink.php" method="post">
<p>
<textarea rows="5" style="width: 98%;">RewriteEngine On
RewriteBase <?php echo $home_root; ?>
<?php
$rewrite = rewrite_rules('', $permalink_structure);
$rules = '';
foreach ($rewrite as $match => $query) {
if (strstr($query, 'index.php')) {
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA]\n";
} else {
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA]\n";
}
}
echo apply_filters('rewrite_rules', $rules);
?>
<textarea rows="5" style="width: 98%;" name="rules"><?php echo mod_rewrite_rules($permalink_structure); ?>
</textarea>
</p>
<?php printf(__('<p>If your <code>.htaccess</code> file is writable by WordPress, you can <a href="%s">edit it through your template interface</a>.</p>'), 'templates.php?file=.htaccess') ?>
<?php
if ((! file_exists(ABSPATH.'.htaccess') && is_writable(ABSPATH)) || is_writable(ABSPATH.'.htaccess')) {
?>
<p class="submit">
<input type="submit" name="writerules" value="<?php _e('Write mod_rewrite rules to .htaccess &raquo;') ?>">
</p>
<?php } ?>
</form>
<?php

View File

@ -1352,6 +1352,31 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
return $rewrite;
}
function mod_rewrite_rules ($permalink_structure) {
$site_root = str_replace('http://', '', trim(get_settings('siteurl')));
$site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
$home_root = str_replace('http://', '', trim(get_settings('home')));
$home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
$rules = "RewriteEngine On\n";
$rules .= "RewriteBase $home_root\n";
$rewrite = rewrite_rules('', $permalink_structure);
foreach ($rewrite as $match => $query) {
if (strstr($query, 'index.php')) {
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA]\n";
} else {
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA]\n";
}
}
$rules = apply_filters('rewrite_rules', $rules);
return $rules;
}
function get_posts($args) {
global $wpdb;
parse_str($args, $r);