Completed
Push — master ( 4ad6bd...3873e4 )
by Ingo
11:53
created

TempFolder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTempFolder() 0 13 2
B getTempFolderUsername() 0 19 7
C getTempParentFolder() 0 51 10
1
<?php
2
3
namespace SilverStripe\Core;
4
5
use Exception;
6
7
/**
8
 * Guesses location for temp folder
9
 */
10
class TempFolder
11
{
12
    /**
13
     * Returns the temporary folder path that silverstripe should use for its cache files.
14
     *
15
     * @param string $base The base path to use for determining the temporary path
16
     * @return string Path to temp
17
     */
18
    public static function getTempFolder($base)
19
    {
20
        $parent = static::getTempParentFolder($base);
21
22
        // The actual temp folder is a subfolder of getTempParentFolder(), named by username
23
        $subfolder = $parent . DIRECTORY_SEPARATOR . static::getTempFolderUsername();
24
25
        if (!@file_exists($subfolder)) {
26
            mkdir($subfolder);
27
        }
28
29
        return $subfolder;
30
    }
31
32
    /**
33
     * Returns as best a representation of the current username as we can glean.
34
     *
35
     * @return string
36
     */
37
    public static function getTempFolderUsername()
38
    {
39
        $user = getenv('APACHE_RUN_USER');
40
        if (!$user) {
41
            $user = getenv('USER');
42
        }
43
        if (!$user) {
44
            $user = getenv('USERNAME');
45
        }
46
        if (!$user && function_exists('posix_getpwuid') && function_exists('posix_getuid')) {
47
            $userDetails = posix_getpwuid(posix_getuid());
48
            $user = $userDetails['name'];
49
        }
50
        if (!$user) {
51
            $user = 'unknown';
52
        }
53
        $user = preg_replace('/[^A-Za-z0-9_\-]/', '', $user);
54
        return $user;
55
    }
56
57
    /**
58
     * Return the parent folder of the temp folder.
59
     * The temp folder will be a subfolder of this, named by username.
60
     * This structure prevents permission problems.
61
     *
62
     * @param string $base
63
     * @return string
64
     * @throws Exception
65
     */
66
    protected static function getTempParentFolder($base)
67
    {
68
        // first, try finding a silverstripe-cache dir built off the base path
69
        $tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache';
70
        if (@file_exists($tempPath)) {
71
            if ((fileperms($tempPath) & 0777) != 0777) {
72
                @chmod($tempPath, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
73
            }
74
            return $tempPath;
75
        }
76
77
        // failing the above, try finding a namespaced silverstripe-cache dir in the system temp
78
        $tempPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
79
            'silverstripe-cache-php' . preg_replace('/[^\w-\.+]+/', '-', PHP_VERSION) .
80
            str_replace(array(' ', '/', ':', '\\'), '-', $base);
81
        if (!@file_exists($tempPath)) {
82
            $oldUMask = umask(0);
83
            @mkdir($tempPath, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
84
            umask($oldUMask);
85
86
        // if the folder already exists, correct perms
87
        } else {
88
            if ((fileperms($tempPath) & 0777) != 0777) {
89
                @chmod($tempPath, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
90
            }
91
        }
92
93
        $worked = @file_exists($tempPath) && @is_writable($tempPath);
94
95
        // failing to use the system path, attempt to create a local silverstripe-cache dir
96
        if (!$worked) {
97
            $tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache';
98
            if (!@file_exists($tempPath)) {
99
                $oldUMask = umask(0);
100
                @mkdir($tempPath, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
101
                umask($oldUMask);
102
            }
103
104
            $worked = @file_exists($tempPath) && @is_writable($tempPath);
105
        }
106
107
        if (!$worked) {
108
            throw new Exception(
109
                'Permission problem gaining access to a temp folder. ' .
110
                'Please create a folder named silverstripe-cache in the base folder ' .
111
                'of the installation and ensure it has the correct permissions'
112
            );
113
        }
114
115
        return $tempPath;
116
    }
117
}
118