Failed Conditions
Push — master ( 89aa7e...49f87d )
by Adrien
09:57
created

File::realpath()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 13
nc 4
nop 1
dl 0
loc 28
ccs 14
cts 14
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
use InvalidArgumentException;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
use ZipArchive;
8
9
class File
10
{
11
    /**
12
     * Use Temp or File Upload Temp for temporary files.
13
     *
14
     * @var bool
15
     */
16
    protected static $useUploadTempDirectory = false;
17
18
    /**
19
     * Set the flag indicating whether the File Upload Temp directory should be used for temporary files.
20
     *
21
     * @param bool $useUploadTempDir Use File Upload Temporary directory (true or false)
22
     */
23 1
    public static function setUseUploadTempDirectory($useUploadTempDir): void
24
    {
25 1
        self::$useUploadTempDirectory = (bool) $useUploadTempDir;
26 1
    }
27
28
    /**
29
     * Get the flag indicating whether the File Upload Temp directory should be used for temporary files.
30
     *
31
     * @return bool Use File Upload Temporary directory (true or false)
32
     */
33 2
    public static function getUseUploadTempDirectory()
34
    {
35 2
        return self::$useUploadTempDirectory;
36
    }
37
38
    /**
39
     * Verify if a file exists.
40
     *
41
     * @param string $pFilename Filename
42
     *
43
     * @return bool
44
     */
45 11
    public static function fileExists($pFilename)
46
    {
47
        // Sick construction, but it seems that
48
        // file_exists returns strange values when
49
        // doing the original file_exists on ZIP archives...
50 11
        if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
51
            // Open ZIP file and verify if the file exists
52 6
            $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
53 6
            $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
54
55 6
            $zip = new ZipArchive();
56 6
            if ($zip->open($zipFile) === true) {
57 6
                $returnValue = ($zip->getFromName($archiveFile) !== false);
58 6
                $zip->close();
59
60 6
                return $returnValue;
61
            }
62
63
            return false;
64
        }
65
66 6
        return file_exists($pFilename);
67
    }
68
69
    /**
70
     * Returns canonicalized absolute pathname, also for ZIP archives.
71
     *
72
     * @param string $pFilename
73
     *
74
     * @return string
75
     */
76 128
    public static function realpath($pFilename)
77
    {
78
        // Returnvalue
79 128
        $returnValue = '';
80
81
        // Try using realpath()
82 128
        if (file_exists($pFilename)) {
83 12
            $returnValue = realpath($pFilename);
84
        }
85
86
        // Found something?
87 128
        if ($returnValue == '' || ($returnValue === null)) {
88 128
            $pathArray = explode('/', $pFilename);
89 128
            while (in_array('..', $pathArray) && $pathArray[0] != '..') {
90 4
                $iMax = count($pathArray);
91 4
                for ($i = 0; $i < $iMax; ++$i) {
92 4
                    if ($pathArray[$i] == '..' && $i > 0) {
93 4
                        unset($pathArray[$i], $pathArray[$i - 1]);
94
95 4
                        break;
96
                    }
97
                }
98
            }
99 128
            $returnValue = implode('/', $pathArray);
100
        }
101
102
        // Return
103 128
        return $returnValue;
104
    }
105
106
    /**
107
     * Get the systems temporary directory.
108
     *
109
     * @return string
110
     */
111 267
    public static function sysGetTempDir()
112
    {
113 267
        if (self::$useUploadTempDirectory) {
114
            //  use upload-directory when defined to allow running on environments having very restricted
115
            //      open_basedir configs
116
            if (ini_get('upload_tmp_dir') !== false) {
117
                if ($temp = ini_get('upload_tmp_dir')) {
118
                    if (file_exists($temp)) {
119
                        return realpath($temp);
120
                    }
121
                }
122
            }
123
        }
124
125 267
        return realpath(sys_get_temp_dir());
126
    }
127
128 247
    public static function temporaryFilename(): string
129
    {
130 247
        $filename = tempnam(self::sysGetTempDir(), 'phpspreadsheet');
131 247
        if ($filename === false) {
132
            throw new Exception('Could not create temporary file');
133
        }
134
135 247
        return $filename;
136
    }
137
138
    /**
139
     * Assert that given path is an existing file and is readable, otherwise throw exception.
140
     *
141
     * @param string $filename
142
     */
143 483
    public static function assertFile($filename): void
144
    {
145 483
        if (!is_file($filename)) {
146 6
            throw new InvalidArgumentException('File "' . $filename . '" does not exist.');
147
        }
148
149 477
        if (!is_readable($filename)) {
150
            throw new InvalidArgumentException('Could not open "' . $filename . '" for reading.');
151
        }
152 477
    }
153
}
154