This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/handle_svg/main.php

127 lines
3.8 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
2018-02-20 21:35:43 +00:00
use enshrined\svgSanitize\Sanitizer;
class SVGFileHandler extends DataHandlerExtension
{
protected array $SUPPORTED_MIME = [MimeType::SVG];
2020-02-23 18:37:22 +00:00
2020-02-04 00:46:36 +00:00
/** @var SVGFileHandlerTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function onPageRequest(PageRequestEvent $event): void
{
2020-02-23 18:37:22 +00:00
global $page;
2024-02-11 11:34:09 +00:00
if ($event->page_matches("get_svg/{id}")) {
$id = $event->get_iarg('id');
$image = Image::by_id_ex($id);
2020-02-23 18:37:22 +00:00
$hash = $image->hash;
2020-06-14 16:05:55 +00:00
$page->set_mime(MimeType::SVG);
2020-02-23 18:37:22 +00:00
$page->set_mode(PageMode::DATA);
2020-02-23 18:37:22 +00:00
$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(true);
$dirtySVG = \Safe\file_get_contents(warehouse_path(Image::IMAGE_DIR, $hash));
2020-02-23 18:37:22 +00:00
$cleanSVG = $sanitizer->sanitize($dirtySVG);
$page->set_data($cleanSVG);
}
}
public function onDataUpload(DataUploadEvent $event): void
{
global $config;
if ($this->supported_mime($event->mime)) {
// If the SVG handler intends to handle this file,
// then sanitise it before touching it
$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(true);
$dirtySVG = \Safe\file_get_contents($event->tmpname);
2024-01-20 20:48:47 +00:00
$cleanSVG = false_throws($sanitizer->sanitize($dirtySVG));
$event->hash = md5($cleanSVG);
2024-01-20 20:48:47 +00:00
$new_tmpname = shm_tempnam("svg");
file_put_contents($new_tmpname, $cleanSVG);
$event->set_tmpname($new_tmpname);
parent::onDataUpload($event);
}
}
2020-02-23 18:37:22 +00:00
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
$event->image->lossless = true;
$event->image->video = false;
$event->image->audio = false;
$event->image->image = true;
$msp = new MiniSVGParser($event->image->get_image_filename());
2020-02-23 18:37:22 +00:00
$event->image->width = $msp->width;
$event->image->height = $msp->height;
}
protected function create_thumb(Image $image): bool
{
try {
2020-02-01 18:51:57 +00:00
// Normally we require imagemagick, but for unit tests we can use a no-op engine
2020-02-01 21:20:32 +00:00
if (defined('UNITTEST')) {
create_image_thumb($image);
2020-02-01 21:20:32 +00:00
} else {
create_image_thumb($image, MediaEngine::IMAGICK);
2020-02-01 21:20:32 +00:00
}
return true;
} catch (MediaException $e) {
log_warning("handle_svg", "Could not generate thumbnail. " . $e->getMessage());
copy("ext/handle_svg/thumb.jpg", $image->get_thumb_filename());
return false;
}
}
protected function check_contents(string $tmpname): bool
{
2023-11-11 21:49:12 +00:00
if (MimeType::get_for_file($tmpname) !== MimeType::SVG) {
return false;
}
$msp = new MiniSVGParser($tmpname);
return bool_escape($msp->valid);
}
}
class MiniSVGParser
{
public bool $valid = false;
2023-11-11 21:49:12 +00:00
public int $width = 0;
public int $height = 0;
private int $xml_depth = 0;
public function __construct(string $file)
{
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, [$this, "startElement"], [$this, "endElement"]);
$this->valid = bool_escape(xml_parse($xml_parser, \Safe\file_get_contents($file), true));
xml_parser_free($xml_parser);
}
/**
* @param array<string, mixed> $attrs
*/
public function startElement(mixed $parser, string $name, array $attrs): void
{
if ($name == "SVG" && $this->xml_depth == 0) {
$this->width = int_escape($attrs["WIDTH"]);
$this->height = int_escape($attrs["HEIGHT"]);
}
$this->xml_depth++;
}
public function endElement(mixed $parser, string $name): void
{
$this->xml_depth--;
}
}