From a095fdaf84c2d6925b7126b35739c230ad5d0fac Mon Sep 17 00:00:00 2001 From: rboren Date: Tue, 27 Jul 2004 23:37:45 +0000 Subject: [PATCH] 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 --- wp-admin/admin-functions.php | 56 ++++++++++++++++++++++++++++++++++ wp-admin/options-permalink.php | 50 +++++++++++++++--------------- wp-includes/functions.php | 25 +++++++++++++++ 3 files changed, 105 insertions(+), 26 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 633922fc6c..6c42a4d75b 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -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; +} + ?> \ No newline at end of file diff --git a/wp-admin/options-permalink.php b/wp-admin/options-permalink.php index 85a1fc4133..65cc4f3018 100644 --- a/wp-admin/options-permalink.php +++ b/wp-admin/options-permalink.php @@ -44,6 +44,21 @@ if (isset($_POST['submit'])) {

+ + +

+ +

+ +

WordPress offers you the ability to create a custom URI structure for your permalinks and archives. The following “tags” are available:

')?> @@ -107,35 +122,18 @@ if (isset($_POST['submit'])) { if ($permalink_structure) { ?>

%s, these are the mod_rewrite rules you should have in your .htaccess file. Click in the field and press CTRL + a to select all.'), $permalink_structure) ?>

- -
+

-

- If your .htaccess file is writable by WordPress, you can edit it through your template interface.

'), 'templates.php?file=.htaccess') ?> + +

+ +

+
$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);