Completed
Push — develop ( c5339b...ea5663 )
by Adrien
19:12
created

File::fileExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 23
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.0856
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
use ZipArchive;
6
7
/**
8
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 *
24
 * @category   PhpSpreadsheet
25
 *
26
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
27
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
28
 */
29
class File
30
{
31
    /*
32
     * Use Temp or File Upload Temp for temporary files
33
     *
34
     * @protected
35
     * @var boolean
36
     */
37
38
    protected static $useUploadTempDirectory = false;
39
40
    /**
41
     * Set the flag indicating whether the File Upload Temp directory should be used for temporary files.
42
     *
43
     * @param bool $useUploadTempDir Use File Upload Temporary directory (true or false)
44
     */
45 1
    public static function setUseUploadTempDirectory($useUploadTempDir = false)
46
    {
47 1
        self::$useUploadTempDirectory = (bool) $useUploadTempDir;
48 1
    }
49
50
    /**
51
     * Get the flag indicating whether the File Upload Temp directory should be used for temporary files.
52
     *
53
     * @return bool Use File Upload Temporary directory (true or false)
54
     */
55 2
    public static function getUseUploadTempDirectory()
56
    {
57 2
        return self::$useUploadTempDirectory;
58
    }
59
60
    /**
61
     * Verify if a file exists.
62
     *
63
     * @param string $pFilename Filename
64
     *
65
     * @return bool
66
     */
67 7
    public static function fileExists($pFilename)
68
    {
69
        // Sick construction, but it seems that
70
        // file_exists returns strange values when
71
        // doing the original file_exists on ZIP archives...
72 7
        if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
73
            // Open ZIP file and verify if the file exists
74 2
            $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
75 2
            $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
76
77 2
            $zip = new ZipArchive();
78 2
            if ($zip->open($zipFile) === true) {
79 2
                $returnValue = ($zip->getFromName($archiveFile) !== false);
80 2
                $zip->close();
81
82 2
                return $returnValue;
83
            }
84
85
            return false;
86
        }
87
88 6
        return file_exists($pFilename);
89
    }
90
91
    /**
92
     * Returns canonicalized absolute pathname, also for ZIP archives.
93
     *
94
     * @param string $pFilename
95
     *
96
     * @return string
97
     */
98 10
    public static function realpath($pFilename)
99
    {
100
        // Returnvalue
101 10
        $returnValue = '';
102
103
        // 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...
104 10
        if (file_exists($pFilename)) {
105 4
            $returnValue = realpath($pFilename);
106
        }
107
108
        // Found something?
109 10
        if ($returnValue == '' || ($returnValue === null)) {
110 10
            $pathArray = explode('/', $pFilename);
111 10
            while (in_array('..', $pathArray) && $pathArray[0] != '..') {
112 2
                for ($i = 0; $i < count($pathArray); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
113 2
                    if ($pathArray[$i] == '..' && $i > 0) {
114 2
                        unset($pathArray[$i], $pathArray[$i - 1]);
115
116 2
                        break;
117
                    }
118
                }
119
            }
120 10
            $returnValue = implode('/', $pathArray);
121
        }
122
123
        // Return
124 10
        return $returnValue;
125
    }
126
127
    /**
128
     * Get the systems temporary directory.
129
     *
130
     * @return string
131
     */
132 42
    public static function sysGetTempDir()
133
    {
134 42
        if (self::$useUploadTempDirectory) {
135
            //  use upload-directory when defined to allow running on environments having very restricted
136
            //      open_basedir configs
137
            if (ini_get('upload_tmp_dir') !== false) {
138
                if ($temp = ini_get('upload_tmp_dir')) {
139
                    if (file_exists($temp)) {
140
                        return realpath($temp);
141
                    }
142
                }
143
            }
144
        }
145
146 42
        return realpath(sys_get_temp_dir());
147
    }
148
149
    /**
150
     * Assert that given path is an existing file and is readable, otherwise throw exception.
151
     *
152
     * @param string $filename
153
     *
154
     * @throws \InvalidArgumentException
155
     */
156 34
    public static function assertFile($filename)
157
    {
158 34
        if (!is_file($filename)) {
159 2
            throw new \InvalidArgumentException('File "' . $filename . '" does not exist.');
160
        }
161
162 32
        if (!is_readable($filename)) {
163
            throw new \InvalidArgumentException('Could not open "' . $filename . '" for reading.');
164
        }
165 32
    }
166
}
167