Completed
Push — develop ( 03f96a...8c66af )
by Adrien
19:36
created

File::getUseUploadTempDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class File
28
{
29
    /*
30
     * Use Temp or File Upload Temp for temporary files
31
     *
32
     * @protected
33
     * @var    boolean
34
     */
35
36
    protected static $useUploadTempDirectory = false;
37
38
    /**
39
     * Set the flag indicating whether the File Upload Temp directory should be used for temporary files.
40
     *
41
     * @param     bool    $useUploadTempDir        Use File Upload Temporary directory (true or false)
42
     */
43 1
    public static function setUseUploadTempDirectory($useUploadTempDir = false)
44
    {
45 1
        self::$useUploadTempDirectory = (bool) $useUploadTempDir;
46 1
    }
47
48
    /**
49
     * Get the flag indicating whether the File Upload Temp directory should be used for temporary files.
50
     *
51
     * @return     bool    Use File Upload Temporary directory (true or false)
52
     */
53 2
    public static function getUseUploadTempDirectory()
54
    {
55 2
        return self::$useUploadTempDirectory;
56
    }
57
58
    /**
59
     * Verify if a file exists.
60
     *
61
     * @param     string    $pFilename    Filename
62
     *
63
     * @return bool
64
     */
65 7
    public static function fileExists($pFilename)
66
    {
67
        // Sick construction, but it seems that
68
        // file_exists returns strange values when
69
        // doing the original file_exists on ZIP archives...
70 7
        if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
71
            // Open ZIP file and verify if the file exists
72 2
            $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
73 2
            $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
74
75 2
            $zipClass = \PhpOffice\PhpSpreadsheet\Settings::getZipClass();
76 2
            $zip = new $zipClass();
77 2
            if ($zip->open($zipFile) === true) {
78 2
                $returnValue = ($zip->getFromName($archiveFile) !== false);
79 2
                $zip->close();
80
81 2
                return $returnValue;
82
            }
83
84
            return false;
85
        }
86
            // Regular file_exists
87 6
            return file_exists($pFilename);
88
    }
89
90
    /**
91
     * Returns canonicalized absolute pathname, also for ZIP archives.
92
     *
93
     * @param string $pFilename
94
     *
95
     * @return string
96
     */
97 10
    public static function realpath($pFilename)
98
    {
99
        // Returnvalue
100 10
        $returnValue = '';
101
102
        // 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...
103 10
        if (file_exists($pFilename)) {
104 4
            $returnValue = realpath($pFilename);
105
        }
106
107
        // Found something?
108 10
        if ($returnValue == '' || ($returnValue === null)) {
109 10
            $pathArray = explode('/', $pFilename);
110 10
            while (in_array('..', $pathArray) && $pathArray[0] != '..') {
111 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...
112 2
                    if ($pathArray[$i] == '..' && $i > 0) {
113 2
                        unset($pathArray[$i], $pathArray[$i - 1]);
114
115 2
                        break;
116
                    }
117
                }
118
            }
119 10
            $returnValue = implode('/', $pathArray);
120
        }
121
122
        // Return
123 10
        return $returnValue;
124
    }
125
126
    /**
127
     * Get the systems temporary directory.
128
     *
129
     * @return string
130
     */
131 40
    public static function sysGetTempDir()
132
    {
133 40
        if (self::$useUploadTempDirectory) {
134
            //  use upload-directory when defined to allow running on environments having very restricted
135
            //      open_basedir configs
136
            if (ini_get('upload_tmp_dir') !== false) {
137
                if ($temp = ini_get('upload_tmp_dir')) {
138
                    if (file_exists($temp)) {
139
                        return realpath($temp);
140
                    }
141
                }
142
            }
143
        }
144
145
        // sys_get_temp_dir is only available since PHP 5.2.1
146
        // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
147 40
        if (!function_exists('sys_get_temp_dir')) {
148 View Code Duplication
            if ($temp = getenv('TMP')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
                if ((!empty($temp)) && (file_exists($temp))) {
150
                    return realpath($temp);
151
                }
152
            }
153 View Code Duplication
            if ($temp = getenv('TEMP')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
                if ((!empty($temp)) && (file_exists($temp))) {
155
                    return realpath($temp);
156
                }
157
            }
158 View Code Duplication
            if ($temp = getenv('TMPDIR')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
                if ((!empty($temp)) && (file_exists($temp))) {
160
                    return realpath($temp);
161
                }
162
            }
163
164
            // trick for creating a file in system's temporary dir
165
            // without knowing the path of the system's temporary dir
166
            $temp = tempnam(__FILE__, '');
167
            if (file_exists($temp)) {
168
                unlink($temp);
169
170
                return realpath(dirname($temp));
171
            }
172
173
            return null;
174
        }
175
176
        // use ordinary built-in PHP function
177
        //    There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
178
        //        be called if we're running 5.2.1 or earlier
179 40
        return realpath(sys_get_temp_dir());
180
    }
181
182
    /**
183
     * Assert that given path is an existing file and is readable, otherwise throw exception.
184
     *
185
     * @param string $filename
186
     *
187
     * @throws \InvalidArgumentException
188
     */
189 27
    public static function assertFile($filename)
190
    {
191 27
        if (!is_file($filename)) {
192 2
            throw new \InvalidArgumentException('File "' . $filename . '" does not exist.');
193
        }
194
195 25
        if (!is_readable($filename)) {
196
            throw new \InvalidArgumentException('Could not open "' . $filename . '" for reading.');
197
        }
198 25
    }
199
}
200