From 29ea7e0b982e2acc9f444ee3632aa96cb32f6ca7 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 12 Apr 2010 23:15:04 +0000 Subject: [PATCH] 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 --- conf/ConfigureFromEnv.php | 4 ++-- core/Core.php | 34 +++++++++++++++++++++++++++++----- core/control/Director.php | 7 +++---- dev/DevelopmentAdmin.php | 2 +- tests/CoreTest.php | 15 +++------------ tests/api/RSSFeedTest.php | 4 +++- 6 files changed, 41 insertions(+), 25 deletions(-) diff --git a/conf/ConfigureFromEnv.php b/conf/ConfigureFromEnv.php index 17637f132..d96ff4a25 100644 --- a/conf/ConfigureFromEnv.php +++ b/conf/ConfigureFromEnv.php @@ -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); } diff --git a/core/Core.php b/core/Core.php index 1becf0d86..f73a20d80 100755 --- a/core/Core.php +++ b/core/Core.php @@ -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"; } diff --git a/core/control/Director.php b/core/control/Director.php index 9f0aa4d50..8c5777cfa 100755 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -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"); } //////////////////////////////////////////////////////////////////////////////////////////// diff --git a/dev/DevelopmentAdmin.php b/dev/DevelopmentAdmin.php index 7f3d03076..92b272279 100644 --- a/dev/DevelopmentAdmin.php +++ b/dev/DevelopmentAdmin.php @@ -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])) { diff --git a/tests/CoreTest.php b/tests/CoreTest.php index ebf78de5b..330ce7d28 100644 --- a/tests/CoreTest.php +++ b/tests/CoreTest.php @@ -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'); } } diff --git a/tests/api/RSSFeedTest.php b/tests/api/RSSFeedTest.php index 517b26379..73528aa88 100755 --- a/tests/api/RSSFeedTest.php +++ b/tests/api/RSSFeedTest.php @@ -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('ItemB AltContent', $content); $this->assertContains('ItemC AltContent', $content); + Director::setBaseURL(null); $_SERVER = $origServer; }