From 4eaa2f6001c7d65c7baff7d9831e371784e7f37b Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 1 Dec 2009 10:26:08 +0000 Subject: [PATCH] BUGFIX #4661 Fix SS temporary directory bug with Windows environments - the directory would always be "silverstripe-cache" instead of a namespaced one so that multiple SS sites don't conflict with eachother git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@94134 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/Core.php | 37 ++++++++++++++++------------- tests/CoreTest.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 tests/CoreTest.php diff --git a/core/Core.php b/core/Core.php index a3539756d..10ac6160e 100755 --- a/core/Core.php +++ b/core/Core.php @@ -179,22 +179,7 @@ Debug::loadErrorHandlers(); /////////////////////////////////////////////////////////////////////////////// // HELPER FUNCTIONS -/** - * Returns the temporary folder that sapphire/silverstripe should use for its cache files - * This is loaded into the TEMP_FOLDER define on start up - */ -function getTempFolder() { - if(preg_match('/^(.*)\/sapphire\/[^\/]+$/', $_SERVER['SCRIPT_FILENAME'], $matches)) { - $cachefolder = "silverstripe-cache" . str_replace(array(' ',"/",":", "\\"),"-", $matches[1]); - } else { - $cachefolder = "silverstripe-cache"; - } - - $ssTmp = BASE_PATH . "/silverstripe-cache"; - if(@file_exists($ssTmp)) { - return $ssTmp; - } - +function getSysTempDir() { if(function_exists('sys_get_temp_dir')) { $sysTmp = sys_get_temp_dir(); } elseif(isset($_ENV['TMP'])) { @@ -204,6 +189,26 @@ function getTempFolder() { unlink($tmpFile); $sysTmp = dirname($tmpFile); } + return $sysTmp; +} + +/** + * Returns the temporary folder that sapphire/silverstripe should use for its cache files + * This is loaded into the TEMP_FOLDER define on start up + */ +function getTempFolder() { + if(preg_match('/^(.*)[\/\\\\]sapphire[\/\\\\][^\/\\\\]+$/', $_SERVER['SCRIPT_FILENAME'], $matches)) { + $cachefolder = "silverstripe-cache" . str_replace(array(' ', "/", ":", "\\"), "-", $matches[1]); + } else { + $cachefolder = "silverstripe-cache"; + } + + $ssTmp = BASE_PATH . "/silverstripe-cache"; + if(@file_exists($ssTmp)) { + return $ssTmp; + } + + $sysTmp = getSysTempDir(); $worked = true; $ssTmp = "$sysTmp/$cachefolder"; diff --git a/tests/CoreTest.php b/tests/CoreTest.php new file mode 100644 index 000000000..be18dc2b7 --- /dev/null +++ b/tests/CoreTest.php @@ -0,0 +1,58 @@ +tempPath = $tempPath = Director::baseFolder() . '/silverstripe-cache'; + $this->renamedTempPath = $renamedTempPath = Director::baseFolder() . '/silverstripe-cache-1'; + + // If the test failed (PHP error) this will need to be renamed + if(file_exists($renamedTempPath)) { + rename($renamedTempPath, $tempPath); + } + if(!file_exists($tempPath)) { + mkdir($tempPath); + } + } + + public function testGetTempPathInProject() { + $this->assertEquals(getTempFolder(), $this->tempPath); + rename($this->tempPath, $this->tempPath . '-1'); + + // 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'); + + // 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'); + + // 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; + } + + public function tearDown() { + parent::tearDown(); + if(file_exists($this->renamedTempPath)) { + rename($this->renamedTempPath, Director::baseFolder() . '/silverstripe-cache'); + } + } + +} \ No newline at end of file