BUGFIX Use BASE_PATH and BASE_URL instead of data from $_SERVER.

API CHANGE: Determine default BASE_PATH/BASE_URL from the __FILE__ content, so that the script that initiated the Sapphire process doesn't matter.  This means that index.php doesn't need to manipulate those variables. (from r97731)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102528 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-04-12 23:15:04 +00:00
parent 40899c3bfb
commit 29ea7e0b98
6 changed files with 41 additions and 25 deletions

View File

@ -65,8 +65,8 @@ if(!isset($database) || !$database) {
// if SS_DATABASE_CHOOSE_NAME
if(defined('SS_DATABASE_CHOOSE_NAME') && SS_DATABASE_CHOOSE_NAME) {
$loopCount = (int)SS_DATABASE_CHOOSE_NAME;
$databaseDir = dirname($_SERVER['SCRIPT_FILENAME']);
for($i=0;$i<$loopCount;$i++) $databaseDir = dirname($databaseDir);
$databaseDir = BASE_PATH;
for($i=0;$i<$loopCount-1;$i++) $databaseDir = dirname($databaseDir);
$database = "SS_" . basename($databaseDir);
$database = str_replace('.','',$database);
}

View File

@ -117,8 +117,27 @@ if(!isset($_SERVER['HTTP_HOST'])) {
/**
* Define system paths
*/
define('BASE_PATH', rtrim(dirname(dirname($_SERVER['SCRIPT_FILENAME'])), DIRECTORY_SEPARATOR));
define('BASE_URL', rtrim(dirname(dirname($_SERVER['SCRIPT_NAME'])), DIRECTORY_SEPARATOR));
if(!defined('BASE_PATH')) {
// Assuming that this file is sapphire/core/Core.php we can then determine the base path
define('BASE_PATH', rtrim(dirname(dirname(dirname(__FILE__)))), DIRECTORY_SEPARATOR);
}
if(!defined('BASE_URL')) {
// Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting the common
// elements
if(substr($_SERVER['SCRIPT_FILENAME'],0,strlen(BASE_PATH)) == BASE_PATH) {
$urlSegmentToRemove = substr($_SERVER['SCRIPT_FILENAME'],strlen(BASE_PATH));
if(substr($_SERVER['SCRIPT_NAME'],-strlen($urlSegmentToRemove)) == $urlSegmentToRemove) {
$baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove));
define('BASE_URL', rtrim($baseURL, DIRECTORY_SEPARATOR));
}
}
// If that didn't work, failover to the old syntax. Hopefully this isn't necessary, and maybe
// if can be phased out?
if(!defined('BASE_URL')) {
define('BASE_URL', rtrim(dirname(dirname($_SERVER['SCRIPT_NAME'])), DIRECTORY_SEPARATOR));
}
}
define('MODULES_DIR', 'modules');
define('MODULES_PATH', BASE_PATH . '/' . MODULES_DIR);
define('THIRDPARTY_DIR', 'sapphire/thirdparty');
@ -222,10 +241,15 @@ function getSysTempDir() {
/**
* Returns the temporary folder that sapphire/silverstripe should use for its cache files
* This is loaded into the TEMP_FOLDER define on start up
*
* @param $base The base path to use as the basis for the temp folder name. Defaults to BASE_PATH,
* which is usually fine; however, the $base argument can be used to help test.
*/
function getTempFolder() {
if(preg_match('/^(.*)[\/\\\\]sapphire[\/\\\\][^\/\\\\]+$/', $_SERVER['SCRIPT_FILENAME'], $matches)) {
$cachefolder = "silverstripe-cache" . str_replace(array(' ', "/", ":", "\\"), "-", $matches[1]);
function getTempFolder($base = null) {
if(!$base) $base = BASE_PATH;
if($base) {
$cachefolder = "silverstripe-cache" . str_replace(array(' ', "/", ":", "\\"), "-", $base);
} else {
$cachefolder = "silverstripe-cache";
}

View File

@ -429,7 +429,7 @@ class Director {
static function baseURL() {
if(self::$alternateBaseURL) return self::$alternateBaseURL;
else {
$base = dirname(dirname($_SERVER['SCRIPT_NAME']));
$base = BASE_URL;
if($base == '/' || $base == '/.' || $base == '\\') $baseURL = '/';
else $baseURL = $base . '/';
@ -452,7 +452,7 @@ class Director {
*/
static function baseFolder() {
if(self::$alternateBaseFolder) return self::$alternateBaseFolder;
else return dirname(dirname($_SERVER['SCRIPT_FILENAME']));
else return BASE_PATH;
}
/**
@ -646,8 +646,7 @@ class Director {
* @return boolean
*/
public static function is_cli() {
return (!isset($_SERVER['HTTP_HOST']) && preg_match('/install\.php/', $_SERVER['SCRIPT_NAME']))
|| preg_match('/cli-script\.php/', $_SERVER['SCRIPT_NAME']);
return (php_sapi_name() == "cli");
}
////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -30,7 +30,7 @@ class DevelopmentAdmin extends Controller {
global $_FILE_TO_URL_MAPPING;
if(Director::is_cli()) {
if(isset($_FILE_TO_URL_MAPPING)) {
$fullPath = $testPath = $_SERVER['SCRIPT_FILENAME'];
$fullPath = $testPath = BASE_PATH;
while($testPath && $testPath != "/" && !preg_match('/^[A-Z]:\\\\$/', $testPath)) {
$matched = false;
if(isset($_FILE_TO_URL_MAPPING[$testPath])) {

View File

@ -20,23 +20,14 @@ class CoreTest extends SapphireTest {
if(file_exists($this->tempPath)) {
$this->assertEquals(getTempFolder(), $this->tempPath);
} else {
// Store the original variable so we know what to change it back to
$old = $_SERVER['SCRIPT_FILENAME'];
// A typical Windows location for where sites are stored on IIS
$_SERVER['SCRIPT_FILENAME'] = 'C:\\inetpub\\wwwroot\\silverstripe\\sapphire\\main.php';
$this->assertEquals(getTempFolder(), getSysTempDir() . '/silverstripe-cacheC--inetpub-wwwroot-silverstripe');
$this->assertEquals(getTempFolder('C:\\inetpub\\wwwroot\\silverstripe'), getSysTempDir() . '/silverstripe-cacheC--inetpub-wwwroot-silverstripe');
// A typical Mac OS X location for where sites are stored
$_SERVER['SCRIPT_FILENAME'] = '/Users/joebloggs/Sites/silverstripe/sapphire/main.php';
$this->assertEquals(getTempFolder(), getSysTempDir() . '/silverstripe-cache-Users-joebloggs-Sites-silverstripe');
$this->assertEquals(getTempFolder('/Users/joebloggs/Sites/silverstripe'), getSysTempDir() . '/silverstripe-cache-Users-joebloggs-Sites-silverstripe');
// A typical Linux location for where sites are stored
$_SERVER['SCRIPT_FILENAME'] = '/var/www/silverstripe/sapphire/main.php';
$this->assertEquals(getTempFolder(), getSysTempDir() . '/silverstripe-cache-var-www-silverstripe');
// Restore the SCRIPT_FILENAME variable back to the original
$_SERVER['SCRIPT_FILENAME'] = $old;
$this->assertEquals(getTempFolder('/var/www/silverstripe'), getSysTempDir() . '/silverstripe-cache-var-www-silverstripe');
}
}

View File

@ -13,7 +13,8 @@ class RSSFeedTest extends SapphireTest {
$origServer = $_SERVER;
$_SERVER['HTTP_HOST'] = 'www.example.org';
$_SERVER['SCRIPT_NAME'] = '/sapphire/main.php';
Director::setBaseURL('/');
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
$content = $rssFeed->feedContent();
@ -44,6 +45,7 @@ class RSSFeedTest extends SapphireTest {
$this->assertContains('<description>ItemB AltContent</description>', $content);
$this->assertContains('<description>ItemC AltContent</description>', $content);
Director::setBaseURL(null);
$_SERVER = $origServer;
}