Have tiny_mce_gzip.php use local silverstripe-cache folder if available

This is a fix for ticket #7670. Some hosting situations don't
allow write access to the system temp path. tiny_mce_gzip.php is currently
using sys_get_temp_dir() by default, and not using a local silverstripe-cache
folder that may exist in the SilverStripe project.

This change moves the getTempFolder() function into a common file, and
includes that in core/Core.php, as well as thirdparty/tinymce/tiny_mce_gzip.php
so both locations share the same code to work out the temp path.
This commit is contained in:
Sean Harvey 2012-09-19 16:10:33 +12:00
parent e0beca198b
commit b075fa29c5
4 changed files with 74 additions and 66 deletions

View File

@ -176,11 +176,31 @@ define('THIRDPARTY_PATH', BASE_PATH . '/' . THIRDPARTY_DIR);
define('ASSETS_DIR', 'assets');
define('ASSETS_PATH', BASE_PATH . '/' . ASSETS_DIR);
///////////////////////////////////////////////////////////////////////////////
// INCLUDES
if(defined('CUSTOM_INCLUDE_PATH')) {
$includePath = CUSTOM_INCLUDE_PATH . PATH_SEPARATOR
. FRAMEWORK_PATH . PATH_SEPARATOR
. FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
. THIRDPARTY_PATH . PATH_SEPARATOR
. get_include_path();
} else {
$includePath = FRAMEWORK_PATH . PATH_SEPARATOR
. FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
. THIRDPARTY_PATH . PATH_SEPARATOR
. get_include_path();
}
set_include_path($includePath);
/**
* Define the temporary folder if it wasn't defined yet
*/
require_once 'core/TempPath.php';
if(!defined('TEMP_FOLDER')) {
define('TEMP_FOLDER', getTempFolder());
define('TEMP_FOLDER', getTempFolder(BASE_PATH));
}
/**
@ -212,24 +232,6 @@ mb_regex_encoding('UTF-8');
*/
gc_enable();
///////////////////////////////////////////////////////////////////////////////
// INCLUDES
if(defined('CUSTOM_INCLUDE_PATH')) {
$includePath = CUSTOM_INCLUDE_PATH . PATH_SEPARATOR
. FRAMEWORK_PATH . PATH_SEPARATOR
. FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
. THIRDPARTY_PATH . PATH_SEPARATOR
. get_include_path();
} else {
$includePath = FRAMEWORK_PATH . PATH_SEPARATOR
. FRAMEWORK_PATH . '/parsers' . PATH_SEPARATOR
. THIRDPARTY_PATH . PATH_SEPARATOR
. get_include_path();
}
set_include_path($includePath);
// Include the files needed the initial manifest building, as well as any files
// that are needed for the boostrap process on every request.
require_once 'cache/Cache.php';
@ -301,51 +303,6 @@ function getSysTempDir() {
return sys_get_temp_dir();
}
/**
* Returns the temporary folder that 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($base = null) {
if(!$base) $base = BASE_PATH;
$tempPath = '';
$worked = true;
// first, try finding a silverstripe-cache dir built off the base path
$tempPath = $base . '/silverstripe-cache';
if(@file_exists($tempPath)) {
return $tempPath;
}
// failing the above, try finding a namespaced silverstripe-cache dir in the system temp
$cacheFolder = '/silverstripe-cache' . str_replace(array(' ', '/', ':', '\\'), '-', $base);
$tempPath = sys_get_temp_dir() . $cacheFolder;
if(!@file_exists($tempPath)) {
$worked = @mkdir($tempPath);
}
// failing to use the system path, attempt to create a local silverstripe-cache dir
if(!$worked) {
$worked = true;
$tempPath = $base . '/silverstripe-cache';
if(!@file_exists($tempPath)) {
$worked = @mkdir($tempPath);
}
}
if(!$worked) {
throw new Exception(
'Permission problem gaining access to a temp folder. ' .
'Please create a folder named silverstripe-cache in the base folder ' .
'of the installation and ensure it has the correct permissions'
);
}
return $tempPath;
}
/**
* @deprecated 3.0 Please use {@link SS_ClassManifest::getItemPath()}.
*/

46
core/TempPath.php Normal file
View File

@ -0,0 +1,46 @@
<?php
/**
* Returns the temporary folder path that silverstripe should use for its cache files.
*
* @param $base The base path to use for determining the temporary path
* @return string Path to temp
*/
function getTempFolder($base = null) {
if(!$base && defined('BASE_PATH')) $base = BASE_PATH;
$tempPath = '';
$worked = true;
// first, try finding a silverstripe-cache dir built off the base path
$tempPath = $base . '/silverstripe-cache';
if(@file_exists($tempPath)) {
return $tempPath;
}
// failing the above, try finding a namespaced silverstripe-cache dir in the system temp
$cacheFolder = '/silverstripe-cache' . str_replace(array(' ', '/', ':', '\\'), '-', $base);
$tempPath = sys_get_temp_dir() . $cacheFolder;
if(!@file_exists($tempPath)) {
$worked = @mkdir($tempPath);
}
// failing to use the system path, attempt to create a local silverstripe-cache dir
if(!$worked) {
$worked = true;
$tempPath = $base . '/silverstripe-cache';
if(!@file_exists($tempPath)) {
$worked = @mkdir($tempPath);
}
}
if(!$worked) {
throw new Exception(
'Permission problem gaining access to a temp folder. ' .
'Please create a folder named silverstripe-cache in the base folder ' .
'of the installation and ensure it has the correct permissions'
);
}
return $tempPath;
}

View File

@ -17,7 +17,7 @@ class CoreTest extends SapphireTest {
public function testGetTempPathInProject() {
if(file_exists($this->tempPath)) {
$this->assertEquals(getTempFolder(), $this->tempPath);
$this->assertEquals(getTempFolder(BASE_PATH), $this->tempPath);
} else {
// A typical Windows location for where sites are stored on IIS
$this->assertEquals(getTempFolder('C:\\inetpub\\wwwroot\\silverstripe-test-project'), sys_get_temp_dir() . '/silverstripe-cacheC--inetpub-wwwroot-silverstripe-test-project');

View File

@ -9,12 +9,17 @@
* Contributing: http://tinymce.moxiecode.com/contributing
*/
$frameworkPath = rtrim(dirname(dirname(dirname(__FILE__))), DIRECTORY_SEPARATOR);
$basePath = rtrim(dirname($frameworkPath), DIRECTORY_SEPARATOR);
require_once $frameworkPath . '/core/TempPath.php';
// Handle incoming request if it's a script call
if (TinyMCE_Compressor::getParam("js")) {
// Default settings
$tinyMCECompressor = new TinyMCE_Compressor(array(
// CUSTOM SilverStripe
'cache_dir' => sys_get_temp_dir()
'cache_dir' => getTempFolder($basePath)
// CUSTOM END
));