silverstripe-installer/rewritetest.php
aoneil c31b87df69 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
2007-08-20 00:30:16 +00:00

181 lines
5.7 KiB
PHP

<?php
createHtaccess();
$baseURL = dirname($_SERVER['SCRIPT_NAME']);
if($baseURL == "/") {
$baseURL = "";
}
if(isset($_REQUEST['force'])) {
echo "Forced continue, attempting to redirect to <a href=\"home/successfullyinstalled\">home/successfullyinstalled</a>.
<script>setTimeout(function() { window.location.href = 'home/successfullyinstalled'; }, 1000);</script>";
} else {
$modRewriteWorking = performModRewriteTest();
if(!$modRewriteWorking) {
createHtaccessAlternative();
$modRewriteWorking = performModRewriteTest();
}
if($modRewriteWorking) {
echo "mod_rewrite is working! I will now try and direct you to
<a href=\"home/successfullyinstalled\">home/successfullyinstalled</a> to confirm that the installation was successful.
<script>setTimeout(function() { window.location.href = 'home/successfullyinstalled'; }, 1000);</script>
";
} else {
restoreHtaccess();
echo "mod_rewrite doesn't appear to be working. Make sure:" .
"<ul>" .
"<li>mod_rewrite is enabled in your httpd.conf</li>" .
"<li>AllowOverride is enabled for the current path.</li>" .
"</ul>" .
"Please check these options, then refresh this page." .
"If you believe that your configuration is correct, <a href=\"rewritetest.php?force=1\">click here to proceed anyway.</a>";
}
}
function performModRewriteTest() {
$baseURL = dirname($_SERVER['SCRIPT_NAME']);
if($baseURL == "/") {
$baseURL = "";
}
// Check if mod_rewrite works properly
$location = 'http://' . (isset($_SERVER['PHP_AUTH_USER']) ? "$_SERVER[PHP_AUTH_USER]:$_SERVER[PHP_AUTH_PW]@" : '') . $_SERVER['HTTP_HOST'] . $baseURL . '/InstallerTest/testRewrite';
@$testrewriting = file_get_contents($location);
if($testrewriting == 'OK') {
return true;
}
// Workaround for 'URL file-access is disabled in the server configuration' using curl
if(function_exists('curl_init')) {
$ch = curl_init($location);
$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(dirname(tempnam('adfadsfdas','')) . '/rewritetest');
unlink(dirname(tempnam('adfadsfdas','')) . '/rewritetest');
if($testrewriting == 'OK') {
return true;
}
}
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;
}
?>