Completed
Push — develop ( d383bc...d9bd45 )
by Adrien
23:59
created

File::fileExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 22
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
use InvalidArgumentException;
6
use ZipArchive;
7
8
class File
9
{
10
    /*
11
     * Use Temp or File Upload Temp for temporary files
12
     *
13
     * @protected
14
     * @var boolean
15
     */
16
17
    protected static $useUploadTempDirectory = false;
18
19
    /**
20
     * Set the flag indicating whether the File Upload Temp directory should be used for temporary files.
21
     *
22
     * @param bool $useUploadTempDir Use File Upload Temporary directory (true or false)
23
     */
24 1
    public static function setUseUploadTempDirectory($useUploadTempDir)
25
    {
26 1
        self::$useUploadTempDirectory = (bool) $useUploadTempDir;
27 1
    }
28
29
    /**
30
     * Get the flag indicating whether the File Upload Temp directory should be used for temporary files.
31
     *
32
     * @return bool Use File Upload Temporary directory (true or false)
33
     */
34 2
    public static function getUseUploadTempDirectory()
35
    {
36 2
        return self::$useUploadTempDirectory;
37
    }
38
39
    /**
40
     * Verify if a file exists.
41
     *
42
     * @param string $pFilename Filename
43
     *
44
     * @return bool
45
     */
46 7
    public static function fileExists($pFilename)
47
    {
48
        // Sick construction, but it seems that
49
        // file_exists returns strange values when
50
        // doing the original file_exists on ZIP archives...
51 7
        if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
52
            // Open ZIP file and verify if the file exists
53 2
            $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
54 2
            $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
55
56 2
            $zip = new ZipArchive();
57 2
            if ($zip->open($zipFile) === true) {
58 2
                $returnValue = ($zip->getFromName($archiveFile) !== false);
59 2
                $zip->close();
60
61 2
                return $returnValue;
62
            }
63
64
            return false;
65
        }
66
67 6
        return file_exists($pFilename);
68
    }
69
70
    /**
71
     * Returns canonicalized absolute pathname, also for ZIP archives.
72
     *
73
     * @param string $pFilename
74
     *
75
     * @return string
76
     */
77 15
    public static function realpath($pFilename)
78
    {
79
        // Returnvalue
80 15
        $returnValue = '';
81
82
        // Try using realpath()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83 15
        if (file_exists($pFilename)) {
84 5
            $returnValue = realpath($pFilename);
85
        }
86
87
        // Found something?
88 15
        if ($returnValue == '' || ($returnValue === null)) {
89 15
            $pathArray = explode('/', $pFilename);
90 15
            while (in_array('..', $pathArray) && $pathArray[0] != '..') {
91 2
                $iMax = count($pathArray);
92 2
                for ($i = 0; $i < $iMax; ++$i) {
93 2
                    if ($pathArray[$i] == '..' && $i > 0) {
94 2
                        unset($pathArray[$i], $pathArray[$i - 1]);
95
96 2
                        break;
97
                    }
98
                }
99
            }
100 15
            $returnValue = implode('/', $pathArray);
101
        }
102
103
        // Return
104 15
        return $returnValue;
105
    }
106
107
    /**
108
     * Get the systems temporary directory.
109
     *
110
     * @return string
111
     */
112 46
    public static function sysGetTempDir()
113
    {
114 46
        if (self::$useUploadTempDirectory) {
115
            //  use upload-directory when defined to allow running on environments having very restricted
116
            //      open_basedir configs
117
            if (ini_get('upload_tmp_dir') !== false) {
118
                if ($temp = ini_get('upload_tmp_dir')) {
119
                    if (file_exists($temp)) {
120
                        return realpath($temp);
121
                    }
122
                }
123
            }
124
        }
125
126 46
        return realpath(sys_get_temp_dir());
127
    }
128
129
    /**
130
     * Assert that given path is an existing file and is readable, otherwise throw exception.
131
     *
132
     * @param string $filename
133
     *
134
     * @throws InvalidArgumentException
135
     */
136 74
    public static function assertFile($filename)
137
    {
138 74
        if (!is_file($filename)) {
139 3
            throw new InvalidArgumentException('File "' . $filename . '" does not exist.');
140
        }
141
142 71
        if (!is_readable($filename)) {
143
            throw new InvalidArgumentException('Could not open "' . $filename . '" for reading.');
144
        }
145 71
    }
146
}
147