. */ declare(strict_types=1); namespace App\Services\Attachments; use Rhukster\DomSanitizer\DOMSanitizer; class SVGSanitizer { /** * Sanitizes the given SVG string by removing any potentially harmful content (like inline scripts). * @param string $input * @return string */ public function sanitizeString(string $input): string { return (new DOMSanitizer(DOMSanitizer::SVG))->sanitize($input); } /** * Sanitizes the given SVG file by removing any potentially harmful content (like inline scripts). * The sanitized content is written back to the file. * @param string $filepath */ public function sanitizeFile(string $filepath): void { //Open the file and read the content $content = file_get_contents($filepath); if ($content === false) { throw new \RuntimeException('Could not read file: ' . $filepath); } //Sanitize the content $sanitizedContent = $this->sanitizeString($content); //Write the sanitized content back to the file file_put_contents($filepath, $sanitizedContent); } }