PluginProbe
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin / 6.5.1.7
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin v6.5.1.7
6.5.1.7 6.5.1.6 6.5.1.5 6.5.1.4 6.5.1.3 6.5.1.2 6.5.1.1 6.5.0.9 6.5.0.8 6.5.0.7 6.5.0.6 trunk 3.4.2.40 3.4.2.41 3.4.2.42 3.4.2.43 3.4.2.44 3.4.2.45 3.4.2.46 3.4.2.47 3.4.2.48 3.4.2.49 3.4.2.50 6.3.2 6.3.3.1 All 47 releases
wpdatatables / lib / phpoffice / phpspreadsheet / src / PhpSpreadsheet / Shared / File.php

File.php in wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin 6.5.1.7, at lib/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Shared/File.php

198 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PhpOffice\PhpSpreadsheet\Shared;
4
5 use PhpOffice\PhpSpreadsheet\Exception;
6 use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
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 public static function setUseUploadTempDirectory(bool $useUploadTempDir): void
22 {
23 self::$useUploadTempDirectory = (bool) $useUploadTempDir;
24 }
25
26 /**
27 * Get the flag indicating whether the File Upload Temp directory should be used for temporary files.
28 */
29 public static function getUseUploadTempDirectory(): bool
30 {
31 return self::$useUploadTempDirectory;
32 }
33
34 // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
35 // Section 4.3.7
36 // Looks like there might be endian-ness considerations
37 private const ZIP_FIRST_4 = [
38 "\x50\x4b\x03\x04", // what it looks like on my system
39 "\x04\x03\x4b\x50", // what it says in documentation
40 ];
41
42 private static function validateZipFirst4(string $zipFile): bool
43 {
44 $contents = @file_get_contents($zipFile, false, null, 0, 4);
45
46 return in_array($contents, self::ZIP_FIRST_4, true);
47 }
48
49 /**
50 * Verify if a file exists.
51 */
52 public static function fileExists(string $filename): bool
53 {
54 // Sick construction, but it seems that
55 // file_exists returns strange values when
56 // doing the original file_exists on ZIP archives...
57 if (strtolower(substr($filename, 0, 6)) == 'zip://') {
58 // Open ZIP file and verify if the file exists
59 $zipFile = substr($filename, 6, strrpos($filename, '#') - 6);
60 $archiveFile = substr($filename, strrpos($filename, '#') + 1);
61
62 if (self::validateZipFirst4($zipFile)) {
63 $zip = new ZipArchive();
64 $res = $zip->open($zipFile);
65 if ($res === true) {
66 $returnValue = ($zip->getFromName($archiveFile) !== false);
67 $zip->close();
68
69 return $returnValue;
70 }
71 }
72
73 return false;
74 }
75
76 return file_exists($filename);
77 }
78
79 /**
80 * Returns canonicalized absolute pathname, also for ZIP archives.
81 */
82 public static function realpath(string $filename): string
83 {
84 // Returnvalue
85 $returnValue = '';
86
87 // Try using realpath()
88 if (file_exists($filename)) {
89 $returnValue = realpath($filename) ?: '';
90 }
91
92 // Found something?
93 if ($returnValue === '') {
94 $pathArray = explode('/', $filename);
95 while (in_array('..', $pathArray) && $pathArray[0] != '..') {
96 $iMax = count($pathArray);
97 for ($i = 0; $i < $iMax; ++$i) {
98 if ($pathArray[$i] == '..' && $i > 0) {
99 unset($pathArray[$i], $pathArray[$i - 1]);
100
101 break;
102 }
103 }
104 }
105 $returnValue = implode('/', $pathArray);
106 }
107
108 // Return
109 return $returnValue;
110 }
111
112 /**
113 * Get the systems temporary directory.
114 */
115 public static function sysGetTempDir(): string
116 {
117 $path = sys_get_temp_dir();
118 if (self::$useUploadTempDirectory) {
119 // use upload-directory when defined to allow running on environments having very restricted
120 // open_basedir configs
121 if (ini_get('upload_tmp_dir') !== false) {
122 if ($temp = ini_get('upload_tmp_dir')) {
123 if (file_exists($temp)) {
124 $path = $temp;
125 }
126 }
127 }
128 }
129
130 return realpath($path) ?: '';
131 }
132
133 public static function temporaryFilename(): string
134 {
135 $filename = tempnam(self::sysGetTempDir(), 'phpspreadsheet');
136 if ($filename === false) {
137 throw new Exception('Could not create temporary file');
138 }
139
140 return $filename;
141 }
142
143 /**
144 * Assert that given path is an existing file and is readable, otherwise throw exception.
145 */
146 public static function assertFile(string $filename, string $zipMember = ''): void
147 {
148 if (!is_file($filename)) {
149 throw new ReaderException('File "' . $filename . '" does not exist.');
150 }
151
152 if (!is_readable($filename)) {
153 throw new ReaderException('Could not open "' . $filename . '" for reading.');
154 }
155
156 if ($zipMember !== '') {
157 $zipfile = "zip://$filename#$zipMember";
158 if (!self::fileExists($zipfile)) {
159 // Has the file been saved with Windoze directory separators rather than unix?
160 $zipfile = "zip://$filename#" . str_replace('/', '\\', $zipMember);
161 if (!self::fileExists($zipfile)) {
162 throw new ReaderException("Could not find zip member $zipfile");
163 }
164 }
165 }
166 }
167
168 /**
169 * Same as assertFile, except return true/false and don't throw Exception.
170 */
171 public static function testFileNoThrow(string $filename, ?string $zipMember = null): bool
172 {
173 if (!is_file($filename)) {
174 return false;
175 }
176 if (!is_readable($filename)) {
177 return false;
178 }
179 if ($zipMember === null) {
180 return true;
181 }
182 // validate zip, but don't check specific member
183 if ($zipMember === '') {
184 return self::validateZipFirst4($filename);
185 }
186
187 $zipfile = "zip://$filename#$zipMember";
188 if (self::fileExists($zipfile)) {
189 return true;
190 }
191
192 // Has the file been saved with Windoze directory separators rather than unix?
193 $zipfile = "zip://$filename#" . str_replace('/', '\\', $zipMember);
194
195 return self::fileExists($zipfile);
196 }
197 }
198