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

TempFolder::getTempParentFolder()   C

Complexity

Conditions 10
Paths 62

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 30
nc 62
nop 1
dl 0
loc 51
rs 6
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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