Make WordPress Core


Ignore:
Timestamp:
07/27/2004 11:37:45 PM (22 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.

File:
1 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?>
Note: See TracChangeset for help on using the changeset viewer.