1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Returns the temporary folder path that silverstripe should use for its cache files. |
4
|
|
|
* |
5
|
|
|
* @package framework |
6
|
|
|
* @subpackage core |
7
|
|
|
* |
8
|
|
|
* @param $base The base path to use for determining the temporary path |
9
|
|
|
* @return string Path to temp |
10
|
|
|
*/ |
11
|
|
|
function getTempFolder($base = null) { |
12
|
|
|
$parent = getTempParentFolder($base); |
13
|
|
|
|
14
|
|
|
// The actual temp folder is a subfolder of getTempParentFolder(), named by username |
15
|
|
|
$subfolder = $parent . DIRECTORY_SEPARATOR . getTempFolderUsername(); |
16
|
|
|
|
17
|
|
|
if(!@file_exists($subfolder)) { |
18
|
|
|
mkdir($subfolder); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
return $subfolder; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns as best a representation of the current username as we can glean. |
26
|
|
|
* |
27
|
|
|
* @package framework |
28
|
|
|
* @subpackage core |
29
|
|
|
*/ |
30
|
|
|
function getTempFolderUsername() { |
31
|
|
|
$user = getenv('APACHE_RUN_USER'); |
32
|
|
|
if(!$user) $user = getenv('USER'); |
33
|
|
|
if(!$user) $user = getenv('USERNAME'); |
34
|
|
|
if(!$user && function_exists('posix_getuid')) { |
35
|
|
|
$userDetails = posix_getpwuid(posix_getuid()); |
36
|
|
|
$user = $userDetails['name']; |
37
|
|
|
} |
38
|
|
|
if(!$user) $user = 'unknown'; |
39
|
|
|
$user = preg_replace('/[^A-Za-z0-9_\-]/', '', $user); |
40
|
|
|
return $user; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the parent folder of the temp folder. |
45
|
|
|
* The temp folder will be a subfolder of this, named by username. |
46
|
|
|
* This structure prevents permission problems. |
47
|
|
|
* |
48
|
|
|
* @package framework |
49
|
|
|
* @subpackage core |
50
|
|
|
*/ |
51
|
|
|
function getTempParentFolder($base = null) { |
52
|
|
|
if(!$base && defined('BASE_PATH')) $base = BASE_PATH; |
53
|
|
|
|
54
|
|
|
$worked = true; |
55
|
|
|
|
56
|
|
|
// first, try finding a silverstripe-cache dir built off the base path |
57
|
|
|
$tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache'; |
58
|
|
|
if(@file_exists($tempPath)) { |
59
|
|
|
if((fileperms($tempPath) & 0777) != 0777) { |
60
|
|
|
@chmod($tempPath, 0777); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
return $tempPath; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// failing the above, try finding a namespaced silverstripe-cache dir in the system temp |
66
|
|
|
$tempPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . |
67
|
|
|
'silverstripe-cache-php' . preg_replace('/[^\w-\.+]+/', '-', PHP_VERSION) . |
68
|
|
|
str_replace(array(' ', '/', ':', '\\'), '-', $base); |
69
|
|
|
if(!@file_exists($tempPath)) { |
70
|
|
|
$oldUMask = umask(0); |
71
|
|
|
$worked = @mkdir($tempPath, 0777); |
72
|
|
|
umask($oldUMask); |
73
|
|
|
|
74
|
|
|
// if the folder already exists, correct perms |
75
|
|
|
} else { |
76
|
|
|
if((fileperms($tempPath) & 0777) != 0777) { |
77
|
|
|
@chmod($tempPath, 0777); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// failing to use the system path, attempt to create a local silverstripe-cache dir |
82
|
|
|
if(!$worked) { |
83
|
|
|
$worked = true; |
84
|
|
|
$tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache'; |
85
|
|
|
if(!@file_exists($tempPath)) { |
86
|
|
|
$oldUMask = umask(0); |
87
|
|
|
$worked = @mkdir($tempPath, 0777); |
88
|
|
|
umask($oldUMask); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if(!$worked) { |
93
|
|
|
throw new Exception( |
94
|
|
|
'Permission problem gaining access to a temp folder. ' . |
95
|
|
|
'Please create a folder named silverstripe-cache in the base folder ' . |
96
|
|
|
'of the installation and ensure it has the correct permissions' |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $tempPath; |
101
|
|
|
} |
102
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: