[wp-cvs] wordpress/wp-admin admin-functions.php, 1.35,
1.36 options-permalink.php, 1.37, 1.38
Ryan Boren
rboren at users.sourceforge.net
Tue Jul 27 23:37:47 UTC 2004
Update of /cvsroot/cafelog/wordpress/wp-admin
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18486/wp-admin
Modified Files:
admin-functions.php options-permalink.php
Log Message:
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.
Index: options-permalink.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-admin/options-permalink.php,v
retrieving revision 1.37
retrieving revision 1.38
diff -C2 -d -r1.37 -r1.38
*** options-permalink.php 19 Jun 2004 04:23:58 -0000 1.37
--- options-permalink.php 27 Jul 2004 23:37:44 -0000 1.38
***************
*** 45,48 ****
--- 45,63 ----
<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>
***************
*** 108,140 ****
?>
<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="">
<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>
</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') ?>
</form>
--- 123,138 ----
?>
<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>
! <form action="options-permalink.php" method="post">
<p>
! <textarea rows="5" style="width: 98%;" name="rules"><?php echo mod_rewrite_rules($permalink_structure); ?>
</textarea>
</p>
! <?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 »') ?>">
! </p>
! <?php } ?>
</form>
Index: admin-functions.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-admin/admin-functions.php,v
retrieving revision 1.35
retrieving revision 1.36
diff -C2 -d -r1.35 -r1.36
*** admin-functions.php 6 Jul 2004 18:14:42 -0000 1.35
--- admin-functions.php 27 Jul 2004 23:37:43 -0000 1.36
***************
*** 418,420 ****
--- 418,476 ----
}
+ // 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
More information about the cvs
mailing list