1
0
Files
breadcrumb-the-shire/core/Service/Import/ImportTempFileService.php

140 lines
4.3 KiB
PHP
Raw Normal View History

<?php
namespace MintyPHP\Service\Import;
class ImportTempFileService
{
public const MAX_UPLOAD_BYTES = 10485760; // 10 MB
private const TMP_SUBDIR = '/imports/tmp';
private const MAX_FILE_AGE_SECONDS = 86400;
/**
* @param array<string, mixed> $upload
* @return array{ok:bool, path?:string, code?:string}
*/
2026-02-23 12:58:19 +01:00
public function storeUploadedFile(array $upload): array
{
$tmpName = (string) ($upload['tmp_name'] ?? '');
$name = (string) ($upload['name'] ?? '');
$size = (int) ($upload['size'] ?? 0);
$error = (int) ($upload['error'] ?? UPLOAD_ERR_NO_FILE);
if ($error === UPLOAD_ERR_NO_FILE || $tmpName === '') {
return ['ok' => false, 'code' => 'upload_missing'];
}
if ($error !== UPLOAD_ERR_OK) {
return ['ok' => false, 'code' => 'invalid_file_type'];
}
if ($size <= 0) {
return ['ok' => false, 'code' => 'empty_csv'];
}
if ($size > self::MAX_UPLOAD_BYTES) {
return ['ok' => false, 'code' => 'upload_too_large'];
}
2026-02-23 12:58:19 +01:00
if (!$this->isAllowedExtension($name)) {
return ['ok' => false, 'code' => 'invalid_file_type'];
}
2026-02-23 12:58:19 +01:00
$dir = $this->tmpDir();
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
return ['ok' => false, 'code' => 'unexpected_error'];
}
2026-03-06 00:44:52 +01:00
// Lazy cleanup on each upload — removes abandoned files without needing a separate cron job.
2026-02-23 12:58:19 +01:00
$this->cleanupExpired();
try {
$target = $dir . '/import_' . bin2hex(random_bytes(16)) . '.csv';
} catch (\Throwable $exception) {
$target = $dir . '/import_' . str_replace('.', '', uniqid('', true)) . '.csv';
}
2026-03-06 00:44:52 +01:00
// Real uploads use move_uploaded_file (validates PHP upload context).
// Fallback rename/copy path handles test uploads without a real HTTP context.
$moved = false;
if (is_uploaded_file($tmpName)) {
$moved = @move_uploaded_file($tmpName, $target);
} elseif (is_file($tmpName) && is_readable($tmpName)) {
$moved = @rename($tmpName, $target);
if (!$moved) {
$moved = @copy($tmpName, $target);
}
}
if (!$moved) {
return ['ok' => false, 'code' => 'invalid_file_type'];
}
@chmod($target, 0640);
return ['ok' => true, 'path' => $target];
}
2026-02-23 12:58:19 +01:00
public function delete(string $path): void
{
$path = trim($path);
if ($path === '') {
return;
}
2026-02-23 12:58:19 +01:00
if (!$this->isInTmpDir($path)) {
return;
}
if (is_file($path)) {
@unlink($path);
}
}
2026-02-23 12:58:19 +01:00
public function cleanupExpired(): void
{
2026-02-23 12:58:19 +01:00
$dir = $this->tmpDir();
if (!is_dir($dir)) {
return;
}
$files = glob($dir . '/*');
if (!is_array($files)) {
return;
}
$threshold = time() - self::MAX_FILE_AGE_SECONDS;
foreach ($files as $file) {
if (!is_file($file)) {
continue;
}
$modifiedAt = @filemtime($file);
if ($modifiedAt !== false && $modifiedAt < $threshold) {
@unlink($file);
}
}
}
2026-02-23 12:58:19 +01:00
public function storageBase(): string
{
if (defined('APP_STORAGE_PATH') && APP_STORAGE_PATH) {
return rtrim((string) APP_STORAGE_PATH, '/');
}
return rtrim(dirname(__DIR__, 3) . '/storage', '/');
}
2026-02-23 12:58:19 +01:00
public function tmpDir(): string
{
2026-02-23 12:58:19 +01:00
return $this->storageBase() . self::TMP_SUBDIR;
}
2026-02-23 12:58:19 +01:00
private function isAllowedExtension(string $name): bool
{
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
return in_array($extension, ['csv', 'txt'], true);
}
2026-03-06 00:44:52 +01:00
// Resolves both paths via realpath to prevent path traversal attacks (e.g. "../../../etc/passwd").
2026-02-23 12:58:19 +01:00
private function isInTmpDir(string $path): bool
{
2026-02-23 12:58:19 +01:00
$tmpDir = $this->tmpDir();
$realTmpDir = realpath($tmpDir);
$realPath = realpath($path);
if ($realTmpDir === false || $realPath === false) {
return false;
}
return str_starts_with($realPath, $realTmpDir . DIRECTORY_SEPARATOR);
}
}