Don't make multiple .htaccess files, just work on the soingle one so we don';t need permissions on base folder. (merged from 2.0.2 branch, r37927)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/phpinstaller/trunk@40480 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
aoneil 2007-08-20 00:30:16 +00:00
parent 4e0fdcb5cf
commit c31b87df69
2 changed files with 187 additions and 28 deletions

View File

@ -624,8 +624,6 @@ RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
TEXT
;
$rewriteAlternative = $rewrite;
str_replace("sapphire", $_SERVER['DOCUMENT_ROOT'] . '/sapphire', $rewriteAlternative);
$baseURL = dirname($_SERVER['SCRIPT_NAME']);
if($baseURL == "/") {
@ -634,36 +632,92 @@ TEXT
if(file_exists('.htaccess')) {
$htaccess = file_get_contents('.htaccess');
copy('.htaccess', '.htaccess_orig');
if(strpos($htaccess, '### SILVERSTRIPE START ###') === false && strpos($htaccess, '### SILVERSTRIPE END ###') === false) {
$htaccess .= "\n### SILVERSTRIPE START ###\n### SILVERSTRIPE END ###\n";
}
if(strpos($htaccess, '### SILVERSTRIPE START ###') !== false && strpos($htaccess, '### SILVERSTRIPE END ###') !== false) {
echo "<li>Existing .htaccess found, merging in SilverStripe rewrite rules</li>";
$start = substr($htaccess, 0, strpos($htaccess, '### SILVERSTRIPE START ###')) . "### SILVERSTRIPE START ###\n";
$end = "\n" . substr($htaccess, strpos($htaccess, '### SILVERSTRIPE END ###'));
}
}
$this->createFile('.htaccess_rewrite', $start . $rewrite . $end);
$this->createFile('.htaccess_alternative', $start . $rewriteAlternative . $end);
$this->createFile('.htaccess', $start . $rewrite . $end);
}
function createHtaccessAlternative() {
$start = "### SILVERSTRIPE START ###\n";
$end= "\n### SILVERSTRIPE END ###";
$rewrite = <<<TEXT
RewriteEngine On
RewriteRule \.js$ - [L]
RewriteRule \.css$ - [L]
RewriteRule \.png$ - [L]
RewriteRule \.jpg$ - [L]
RewriteRule \.gif$ - [L]
RewriteRule \.php$ - [L]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* $_SERVER[DOCUMENT_ROOT]/sapphire/main.php?url=%1&%{QUERY_STRING} [L]
TEXT
;
$baseURL = dirname($_SERVER['SCRIPT_NAME']);
if($baseURL == "/") {
$baseURL = "";
}
if(file_exists('.htaccess')) {
$htaccess = file_get_contents('.htaccess');
if(strpos($htaccess, '### SILVERSTRIPE START ###') === false && strpos($htaccess, '### SILVERSTRIPE END ###') === false) {
$htaccess .= "\n### SILVERSTRIPE START ###\n### SILVERSTRIPE END ###\n";
}
if(strpos($htaccess, '### SILVERSTRIPE START ###') !== false && strpos($htaccess, '### SILVERSTRIPE END ###') !== false) {
$start = substr($htaccess, 0, strpos($htaccess, '### SILVERSTRIPE START ###')) . "### SILVERSTRIPE START ###\n";
$end = "\n" . substr($htaccess, strpos($htaccess, '### SILVERSTRIPE END ###'));
}
}
$this->createFile('.htaccess', $start . $rewrite . $end);
}
function restoreHtaccess() {
$start = "### SILVERSTRIPE START ###\n";
$end= "\n### SILVERSTRIPE END ###";
if(file_exists('.htaccess')) {
$htaccess = file_get_contents('.htaccess');
if(strpos($htaccess, '### SILVERSTRIPE START ###') === false && strpos($htaccess, '### SILVERSTRIPE END ###') === false) {
$htaccess .= "\n### SILVERSTRIPE START ###\n### SILVERSTRIPE END ###\n";
}
if(strpos($htaccess, '### SILVERSTRIPE START ###') !== false && strpos($htaccess, '### SILVERSTRIPE END ###') !== false) {
$start = substr($htaccess, 0, strpos($htaccess, '### SILVERSTRIPE START ###')) . "### SILVERSTRIPE START ###\n";
$end = "\n" . substr($htaccess, strpos($htaccess, '### SILVERSTRIPE END ###'));
}
}
$this->createFile('.htaccess', $start . $end);
}
function checkModRewrite() {
copy('../.htaccess_rewrite', '../.htaccess');
if($this->performModRewriteTest() == true) {
return true;
}
copy('../.htaccess_alternative', '../.htaccess');
$this->createHtaccessAlternative();
if($this->performModRewriteTest() == false) {
echo "<li>ERROR: mod_rewrite not working, redirecting to mod_rewrite test page</li>";
if(file_exists('../.htaccess_orig')) {
copy('../.htaccess_orig', '../.htaccess');
} else {
unlink('../.htaccess');
}
$this->restoreHtaccess();
echo "I will now try and direct you to <a href=\"rewritetest.php\">rewritetest</a> to troubleshoot mod_rewrite</p>
<script>setTimeout(function() { window.location.href = 'rewritetest.php'; }, 1000);</script>
";
@ -689,14 +743,14 @@ TEXT
// Workaround for 'URL file-access is disabled in the server configuration' using curl
if(function_exists('curl_init')) {
$ch = curl_init($location);
$fp = @fopen("temp", "w");
$fp = @fopen(dirname(tempnam('adfadsfdas','')) . '/rewritetest', "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$testrewriting = file_get_contents('temp');
unlink('temp');
$testrewriting = file_get_contents(dirname(tempnam('adfadsfdas','')) . '/rewritetest');
unlink(dirname(tempnam('adfadsfdas','')) . '/rewritetest');
if($testrewriting == 'OK') {
return true;
}

View File

@ -1,8 +1,6 @@
<?php
if(file_exists('.htaccess_rewrite')) {
copy(".htaccess_rewrite", ".htaccess");
}
createHtaccess();
$baseURL = dirname($_SERVER['SCRIPT_NAME']);
if($baseURL == "/") {
@ -16,7 +14,7 @@ if(isset($_REQUEST['force'])) {
$modRewriteWorking = performModRewriteTest();
if(!$modRewriteWorking) {
copy(".htaccess_alternative", ".htaccess");
createHtaccessAlternative();
$modRewriteWorking = performModRewriteTest();
}
@ -26,10 +24,8 @@ if(isset($_REQUEST['force'])) {
<script>setTimeout(function() { window.location.href = 'home/successfullyinstalled'; }, 1000);</script>
";
} else {
unlink('.htaccess');
if(file_exists('.htaccess_orig')) {
copy('.htaccess_orig', '.htaccess');
}
restoreHtaccess();
echo "mod_rewrite doesn't appear to be working. Make sure:" .
"<ul>" .
"<li>mod_rewrite is enabled in your httpd.conf</li>" .
@ -57,14 +53,14 @@ function performModRewriteTest() {
// Workaround for 'URL file-access is disabled in the server configuration' using curl
if(function_exists('curl_init')) {
$ch = curl_init($location);
$fp = @fopen("temp", "w");
$fp = @fopen(dirname(tempnam('adfadsfdas','')) . '/rewritetest', "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$testrewriting = file_get_contents('temp');
unlink('temp');
$testrewriting = file_get_contents(dirname(tempnam('adfadsfdas','')) . '/rewritetest');
unlink(dirname(tempnam('adfadsfdas','')) . '/rewritetest');
if($testrewriting == 'OK') {
return true;
}
@ -72,4 +68,113 @@ function performModRewriteTest() {
return false;
}
function createHtaccess() {
$start = "### SILVERSTRIPE START ###\n";
$end= "\n### SILVERSTRIPE END ###";
$rewrite = <<<TEXT
RewriteEngine On
RewriteRule \.js$ - [L]
RewriteRule \.css$ - [L]
RewriteRule \.png$ - [L]
RewriteRule \.jpg$ - [L]
RewriteRule \.gif$ - [L]
RewriteRule \.php$ - [L]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
TEXT
;
$baseURL = dirname($_SERVER['SCRIPT_NAME']);
if($baseURL == "/") {
$baseURL = "";
}
if(file_exists('.htaccess')) {
$htaccess = file_get_contents('.htaccess');
if(strpos($htaccess, '### SILVERSTRIPE START ###') === false && strpos($htaccess, '### SILVERSTRIPE END ###') === false) {
$htaccess .= "\n### SILVERSTRIPE START ###\n### SILVERSTRIPE END ###\n";
}
if(strpos($htaccess, '### SILVERSTRIPE START ###') !== false && strpos($htaccess, '### SILVERSTRIPE END ###') !== false) {
$start = substr($htaccess, 0, strpos($htaccess, '### SILVERSTRIPE START ###')) . "### SILVERSTRIPE START ###\n";
$end = "\n" . substr($htaccess, strpos($htaccess, '### SILVERSTRIPE END ###'));
}
}
createFile('.htaccess', $start . $rewrite . $end);
}
function createHtaccessAlternative() {
$start = "### SILVERSTRIPE START ###\n";
$end= "\n### SILVERSTRIPE END ###";
$rewrite = <<<TEXT
RewriteEngine On
RewriteRule \.js$ - [L]
RewriteRule \.css$ - [L]
RewriteRule \.png$ - [L]
RewriteRule \.jpg$ - [L]
RewriteRule \.gif$ - [L]
RewriteRule \.php$ - [L]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* $_SERVER[DOCUMENT_ROOT]/sapphire/main.php?url=%1&%{QUERY_STRING} [L]
TEXT
;
$baseURL = dirname($_SERVER['SCRIPT_NAME']);
if($baseURL == "/") {
$baseURL = "";
}
if(file_exists('.htaccess')) {
$htaccess = file_get_contents('.htaccess');
if(strpos($htaccess, '### SILVERSTRIPE START ###') === false && strpos($htaccess, '### SILVERSTRIPE END ###') === false) {
$htaccess .= "\n### SILVERSTRIPE START ###\n### SILVERSTRIPE END ###\n";
}
if(strpos($htaccess, '### SILVERSTRIPE START ###') !== false && strpos($htaccess, '### SILVERSTRIPE END ###') !== false) {
$start = substr($htaccess, 0, strpos($htaccess, '### SILVERSTRIPE START ###')) . "### SILVERSTRIPE START ###\n";
$end = "\n" . substr($htaccess, strpos($htaccess, '### SILVERSTRIPE END ###'));
}
}
createFile('.htaccess', $start . $rewrite . $end);
}
function restoreHtaccess() {
$start = "### SILVERSTRIPE START ###\n";
$end= "\n### SILVERSTRIPE END ###";
if(file_exists('.htaccess')) {
$htaccess = file_get_contents('.htaccess');
if(strpos($htaccess, '### SILVERSTRIPE START ###') === false && strpos($htaccess, '### SILVERSTRIPE END ###') === false) {
$htaccess .= "\n### SILVERSTRIPE START ###\n### SILVERSTRIPE END ###\n";
}
if(strpos($htaccess, '### SILVERSTRIPE START ###') !== false && strpos($htaccess, '### SILVERSTRIPE END ###') !== false) {
$start = substr($htaccess, 0, strpos($htaccess, '### SILVERSTRIPE START ###')) . "### SILVERSTRIPE START ###\n";
$end = "\n" . substr($htaccess, strpos($htaccess, '### SILVERSTRIPE END ###'));
}
}
createFile('.htaccess', $start . $end);
}
function getBaseDir() {
return dirname($_SERVER['SCRIPT_FILENAME']) . '/';
}
function createFile($filename, $content) {
$base = getBaseDir();
if(($fh = fopen($base . $filename, 'w')) && fwrite($fh, $content) && fclose($fh))
return true;
}
?>