WordPress.org

Make WordPress Core

Changeset 1489


Ignore:
Timestamp:
07/27/2004 11:37:45 PM (16 years ago)
Author:
rboren
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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-functions.php

    r1454 r1489  
    418418}
    419419
     420// insert_with_markers: Owen Winkler
     421// Inserts an array of strings into a file (.htaccess), placing it between
     422// BEGIN and END markers.  Replaces existing marked info.  Retains surrounding
     423// data.  Creates file if none exists.
     424// Returns true on write success, false on failure.
     425function insert_with_markers($filename, $marker, $insertion) {
     426    if (!file_exists($filename) || is_writeable($filename)) {
     427        $markerdata = explode("\n", implode('', file($filename)));
     428        $f = fopen($filename, 'w');
     429        $foundit = false;
     430        if ($markerdata) {
     431            $state = true;
     432            $newline = '';
     433            foreach($markerdata as $markerline) {
     434                if (strstr($markerline, "# BEGIN {$marker}")) $state = false;
     435                if ($state) fwrite($f, "{$newline}{$markerline}");
     436                if (strstr($markerline, "# END {$marker}")) {
     437                    fwrite($f, "{$newline}# BEGIN {$marker}");
     438                    if(is_array($insertion)) foreach($insertion as $insertline) fwrite($f, "{$newline}{$insertline}");
     439                    fwrite($f, "{$newline}# END {$marker}");
     440                    $state = true;
     441                    $foundit = true;
     442                }
     443                $newline = "\n";
     444            }
     445        }
     446        if (!$foundit) {
     447            fwrite($f, "# BEGIN {$marker}\n");
     448            foreach($insertion as $insertline) fwrite($f, "{$insertline}\n");
     449            fwrite($f, "# END {$marker}");             
     450        }
     451        fclose($f);
     452        return true;
     453    } else {
     454        return false;
     455    }
     456}
     457
     458// insert_with_markers: Owen Winkler
     459// Returns an array of strings from a file (.htaccess) from between BEGIN
     460// and END markers.
     461function extract_from_markers($filename, $marker) {
     462    $result = array();
     463    if($markerdata = explode("\n", implode('', file($filename))));
     464 {
     465     $state = false;
     466     foreach($markerdata as $markerline) {
     467         if(strstr($markerline, "# END {$marker}")) $state = false;
     468         if($state) $result[] = $markerline;
     469         if(strstr($markerline, "# BEGIN {$marker}")) $state = true;
     470     }
     471 }
     472
     473 return $result;
     474}
     475
    420476?>
  • trunk/wp-admin/options-permalink.php

    r1438 r1489  
    4545<div class="updated"><p><?php _e('Permalink structure updated.'); ?></p></div>
    4646<?php endif; ?>
     47
     48<?php if(isset($_POST['rules'])) {
     49        $rules = explode("\n", $_POST['rules']);
     50        if(insert_with_markers(ABSPATH.'.htaccess', 'WordPress', $rules)) {
     51?>
     52<div class="updated" id="htupdate"><p><?php _e('mod_rewrite rules written to .htaccess.'); ?></p></div>
     53<?php
     54        } else {
     55?>
     56<div class="updated" id="htupdate"><p><?php _e('Failed to write mod_rewrite rules to .htaccess.'); ?></p></div>
     57<?php
     58        }
     59    }
     60?>
     61
    4762<div class="wrap">
    4863  <h2><?php _e('Edit Permalink Structure') ?></h2>
     
    108123?>
    109124  <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>
    110   <?php
    111 $site_root = str_replace('http://', '', trim(get_settings('siteurl')));
    112 $site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
    113 if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
    114 
    115 $home_root = str_replace('http://', '', trim(get_settings('home')));
    116 $home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
    117 if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
    118 
    119 ?>
    120 <form action="">
     125<form action="options-permalink.php" method="post">
    121126    <p>
    122         <textarea rows="5" style="width: 98%;">RewriteEngine On
    123 RewriteBase <?php echo $home_root; ?>
    124 <?php
    125 $rewrite = rewrite_rules('', $permalink_structure);
    126 $rules = '';
    127 foreach ($rewrite as $match => $query) {
    128     if (strstr($query, 'index.php')) {
    129         $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA]\n";
    130     } else {
    131         $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA]\n";
    132     }
    133 }
    134 echo apply_filters('rewrite_rules', $rules);
    135 ?>
     127<textarea rows="5" style="width: 98%;" name="rules"><?php echo mod_rewrite_rules($permalink_structure); ?>
    136128</textarea>
    137129    </p>
    138     <?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') ?>
     130<?php
     131if ((! file_exists(ABSPATH.'.htaccess') && is_writable(ABSPATH)) || is_writable(ABSPATH.'.htaccess')) {
     132?>
     133    <p class="submit">
     134        <input type="submit" name="writerules" value="<?php _e('Write mod_rewrite rules to .htaccess &raquo;') ?>">
     135    </p>
     136<?php } ?>
    139137</form>
    140138 
  • trunk/wp-includes/functions.php

    r1480 r1489  
    13531353}
    13541354
     1355function mod_rewrite_rules ($permalink_structure) {
     1356    $site_root = str_replace('http://', '', trim(get_settings('siteurl')));
     1357    $site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
     1358    if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';
     1359   
     1360    $home_root = str_replace('http://', '', trim(get_settings('home')));
     1361    $home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
     1362    if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';
     1363   
     1364    $rules = "RewriteEngine On\n";
     1365    $rules .= "RewriteBase $home_root\n";
     1366    $rewrite = rewrite_rules('', $permalink_structure);
     1367    foreach ($rewrite as $match => $query) {
     1368        if (strstr($query, 'index.php')) {
     1369            $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA]\n";
     1370        } else {
     1371            $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA]\n";
     1372        }
     1373    }
     1374
     1375    $rules = apply_filters('rewrite_rules', $rules);
     1376
     1377    return $rules;
     1378}
     1379
    13551380function get_posts($args) {
    13561381    global $wpdb;
Note: See TracChangeset for help on using the changeset viewer.