<?php
namespace ModulesAbstract\ElectronicInvoice; class TemporaryFilePickerSecure { protected $basePath = null; protected $baseName = 'file.tmp'; protected $usedFilenames = []; public function __construct($basePath, $baseName) { $this->basePath = $basePath; $this->baseName = preg_replace("/[^a-z0-9-_\.]/i", "", $baseName); } public function __destruct() { $this->cleanup(); } public function initialize() { return $this; } public function cleanup() { foreach ($this->usedFilenames as $f) { if (is_file($f)) { \Engine\File::delete($f); } } return $this; } public function getAvailableFile() { $f = $this->basePath . sprintf("%s-%s-%03d.%s", \Engine\File::getName($this->baseName), uniqid(), \Engine\RandomGenerator::randomStringFromAlphabet(3, '1234567890'), \Engine\File::getExtension($this->baseName)); if (!is_file($f)) { \Engine\File::create($f, ""); } $this->usedFilenames[]= $f; return $f; } } 