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() |
|
|
|
|
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) { |
|
|
|
|
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
|
43 |
|
public static function sysGetTempDir() |
132
|
|
|
{ |
133
|
43 |
|
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
|
43 |
|
return realpath(sys_get_temp_dir()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Assert that given path is an existing file and is readable, otherwise throw exception. |
150
|
|
|
* |
151
|
|
|
* @param string $filename |
152
|
|
|
* |
153
|
|
|
* @throws \InvalidArgumentException |
154
|
|
|
*/ |
155
|
27 |
|
public static function assertFile($filename) |
156
|
|
|
{ |
157
|
27 |
|
if (!is_file($filename)) { |
158
|
2 |
|
throw new \InvalidArgumentException('File "' . $filename . '" does not exist.'); |
159
|
|
|
} |
160
|
|
|
|
161
|
25 |
|
if (!is_readable($filename)) { |
162
|
|
|
throw new \InvalidArgumentException('Could not open "' . $filename . '" for reading.'); |
163
|
|
|
} |
164
|
25 |
|
} |
165
|
|
|
} |
166
|
|
|
|
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.