2012-09-19 06:10:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Returns the temporary folder path that silverstripe should use for its cache files.
|
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package framework
|
|
|
|
* @subpackage core
|
|
|
|
*
|
2012-09-19 06:10:33 +02:00
|
|
|
* @param $base The base path to use for determining the temporary path
|
|
|
|
* @return string Path to temp
|
|
|
|
*/
|
|
|
|
function getTempFolder($base = null) {
|
2013-03-18 03:30:09 +01:00
|
|
|
$parent = getTempParentFolder($base);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-18 03:30:09 +01:00
|
|
|
// The actual temp folder is a subfolder of getTempParentFolder(), named by username
|
|
|
|
$subfolder = $parent . DIRECTORY_SEPARATOR . getTempFolderUsername();
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
if(!@file_exists($subfolder)) {
|
2013-03-18 03:30:09 +01:00
|
|
|
mkdir($subfolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subfolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns as best a representation of the current username as we can glean.
|
2013-11-29 05:12:47 +01:00
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage core
|
2013-03-18 03:30:09 +01:00
|
|
|
*/
|
|
|
|
function getTempFolderUsername() {
|
|
|
|
$user = getenv('APACHE_RUN_USER');
|
|
|
|
if(!$user) $user = getenv('USER');
|
|
|
|
if(!$user) $user = getenv('USERNAME');
|
|
|
|
if(!$user && function_exists('posix_getuid')) {
|
|
|
|
$userDetails = posix_getpwuid(posix_getuid());
|
|
|
|
$user = $userDetails['name'];
|
|
|
|
}
|
|
|
|
if(!$user) $user = 'unknown';
|
|
|
|
$user = preg_replace('/[^A-Za-z0-9_\-]/', '', $user);
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the parent folder of the temp folder.
|
|
|
|
* The temp folder will be a subfolder of this, named by username.
|
|
|
|
* This structure prevents permission problems.
|
2013-11-29 05:12:47 +01:00
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage core
|
2013-03-18 03:30:09 +01:00
|
|
|
*/
|
|
|
|
function getTempParentFolder($base = null) {
|
2012-09-19 06:10:33 +02:00
|
|
|
if(!$base && defined('BASE_PATH')) $base = BASE_PATH;
|
|
|
|
|
|
|
|
$tempPath = '';
|
|
|
|
$worked = true;
|
|
|
|
|
|
|
|
// first, try finding a silverstripe-cache dir built off the base path
|
2013-10-03 03:49:18 +02:00
|
|
|
$tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache';
|
2012-09-19 06:10:33 +02:00
|
|
|
if(@file_exists($tempPath)) {
|
2013-03-18 03:30:09 +01:00
|
|
|
if((fileperms($tempPath) & 0777) != 0777) {
|
|
|
|
@chmod($tempPath, 0777);
|
|
|
|
}
|
2012-09-19 06:10:33 +02:00
|
|
|
return $tempPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// failing the above, try finding a namespaced silverstripe-cache dir in the system temp
|
2013-10-03 03:49:18 +02:00
|
|
|
$cacheFolder = DIRECTORY_SEPARATOR . 'silverstripe-cache' . str_replace(array(' ', '/', ':', '\\'), '-', $base);
|
2012-09-19 06:10:33 +02:00
|
|
|
$tempPath = sys_get_temp_dir() . $cacheFolder;
|
|
|
|
if(!@file_exists($tempPath)) {
|
2013-03-18 03:30:09 +01:00
|
|
|
$oldUMask = umask(0);
|
|
|
|
$worked = @mkdir($tempPath, 0777);
|
|
|
|
umask($oldUMask);
|
|
|
|
|
|
|
|
// if the folder already exists, correct perms
|
|
|
|
} else {
|
|
|
|
if((fileperms($tempPath) & 0777) != 0777) {
|
|
|
|
@chmod($tempPath, 0777);
|
|
|
|
}
|
2012-09-19 06:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// failing to use the system path, attempt to create a local silverstripe-cache dir
|
|
|
|
if(!$worked) {
|
|
|
|
$worked = true;
|
2013-10-03 03:49:18 +02:00
|
|
|
$tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache';
|
2012-09-19 06:10:33 +02:00
|
|
|
if(!@file_exists($tempPath)) {
|
2013-03-18 03:30:09 +01:00
|
|
|
$oldUMask = umask(0);
|
|
|
|
$worked = @mkdir($tempPath, 0777);
|
|
|
|
umask($oldUMask);
|
2012-09-19 06:10:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-10-03 03:49:18 +02:00
|
|
|
}
|