| @@ -234,19 +234,23 @@ | ||
| 234 | 234 | $tempDir = $upload_dir['basedir'] . '/bitforms/temp'; |
| 235 | 235 | $destinationDir = self::getEntriesFileUploadDir($formId, $entryID) . DIRECTORY_SEPARATOR; |
| 236 | 236 | self::createIndexFile($destinationDir); |
| 237 | 237 | |
| 238 | + $consumedFiles = []; | |
| 239 | + | |
| 238 | 240 | foreach ($submitted_data as $key => $data) { |
| 239 | 241 | if (isset($fields[$key]) && 'advanced-file-up' === $fields[$key]['type']) { |
| 240 | - $files = $data; | |
| 241 | 242 | $fldData = $submitted_data[$key]; |
| 242 | - $files = explode(',', $fldData); | |
| 243 | - if (is_array($files) && count($files) > 0) { | |
| 244 | - foreach ($files as $file) { | |
| 245 | - self::fileCopy($tempDir, $destinationDir, trim($file)); | |
| 243 | + // A repeater row (or a pre-split value) hands this field over as an array; explode() | |
| 244 | + // on an array is a TypeError on PHP 8, which would fatal mid-submission. | |
| 245 | + $files = \is_array($fldData) ? $fldData : explode(',', \is_scalar($fldData) ? (string) $fldData : ''); | |
| 246 | + foreach ($files as $file) { | |
| 247 | + $safeFile = \is_scalar($file) ? trim((string) $file) : ''; | |
| 248 | + if ('' === $safeFile) { | |
| 249 | + continue; | |
| 246 | 250 | } |
| 247 | - } else { | |
| 248 | - self::fileCopy($tempDir, $destinationDir, trim($files)); | |
| 251 | + self::fileCopy($tempDir, $destinationDir, $safeFile); | |
| 252 | + $consumedFiles[] = $safeFile; | |
| 249 | 253 | } |
| 250 | 254 | if (!empty($files)) { |
| 251 | 255 | $submitted_data[$key] = $files; |
| 252 | 256 | } |
| @@ -251,20 +255,68 @@ | ||
| 251 | 255 | $submitted_data[$key] = $files; |
| 252 | 256 | } |
| 253 | 257 | } |
| 254 | 258 | } |
| 259 | + | |
| 260 | + self::cleanupTempUploads($tempDir, $consumedFiles); | |
| 261 | + | |
| 262 | + return $submitted_data; | |
| 263 | + } | |
| 264 | + | |
| 265 | + /** | |
| 266 | + * Clear the shared temp upload staging area after a submission has taken what it needs. | |
| 267 | + * | |
| 268 | + * @param string $tempDir shared staging directory | |
| 269 | + * @param array $consumedFiles file names copied into the entry directory by this submission | |
| 270 | + * | |
| 271 | + * @return void | |
| 272 | + */ | |
| 273 | + private static function cleanupTempUploads($tempDir, array $consumedFiles) | |
| 274 | + { | |
| 255 | 275 | $tempBase = realpath($tempDir); |
| 256 | - if (false !== $tempBase) { | |
| 257 | - $tmpFiles = glob($tempBase . DIRECTORY_SEPARATOR . '*'); | |
| 258 | - foreach ((array) $tmpFiles as $tmpFile) { | |
| 259 | - $resolved = realpath($tmpFile); | |
| 260 | - if (false !== $resolved && 0 === strpos($resolved, $tempBase . DIRECTORY_SEPARATOR)) { | |
| 261 | - wp_delete_file($resolved); | |
| 262 | - } | |
| 276 | + if (false === $tempBase) { | |
| 277 | + return; | |
| 278 | + } | |
| 279 | + $boundary = $tempBase . DIRECTORY_SEPARATOR; | |
| 280 | + | |
| 281 | + // Never remove the directory-hardening files the uploads dir relies on. | |
| 282 | + $protected = ['index.php', 'index.html', '.htaccess']; | |
| 283 | + | |
| 284 | + foreach ($consumedFiles as $file) { | |
| 285 | + $resolved = realpath($boundary . $file); | |
| 286 | + if (false === $resolved || 0 !== strpos($resolved, $boundary) || !is_file($resolved)) { | |
| 287 | + continue; | |
| 263 | 288 | } |
| 289 | + if (\in_array(basename($resolved), $protected, true)) { | |
| 290 | + continue; | |
| 291 | + } | |
| 292 | + wp_delete_file($resolved); | |
| 264 | 293 | } |
| 265 | 294 | |
| 266 | - return $submitted_data; | |
| 295 | + // Sweep abandoned uploads. Anything still here after the retention window belongs to a form | |
| 296 | + // that was never submitted. Filterable so a site with very long multi-step forms can extend | |
| 297 | + // it; 0 or less disables the sweep entirely. | |
| 298 | + $retention = apply_filters('bitform_temp_upload_retention', DAY_IN_SECONDS); | |
| 299 | + $retention = is_numeric($retention) ? (int) $retention : DAY_IN_SECONDS; | |
| 300 | + if ($retention <= 0) { | |
| 301 | + return; | |
| 302 | + } | |
| 303 | + | |
| 304 | + $cutoff = time() - $retention; | |
| 305 | + $tmpFiles = glob($boundary . '*'); | |
| 306 | + foreach ((array) $tmpFiles as $tmpFile) { | |
| 307 | + $resolved = realpath($tmpFile); | |
| 308 | + if (false === $resolved || 0 !== strpos($resolved, $boundary) || !is_file($resolved)) { | |
| 309 | + continue; | |
| 310 | + } | |
| 311 | + if (\in_array(basename($resolved), $protected, true)) { | |
| 312 | + continue; | |
| 313 | + } | |
| 314 | + $modified = @filemtime($resolved); | |
| 315 | + if (false !== $modified && $modified < $cutoff) { | |
| 316 | + wp_delete_file($resolved); | |
| 317 | + } | |
| 318 | + } | |
| 267 | 319 | } |
| 268 | 320 | |
| 269 | 321 | private function getByteSizeByUnit($sizeString) |
| 270 | 322 | { |