2007-12-11 18:40:44 +00:00
|
|
|
<?php
|
|
|
|
|
2018-02-20 21:35:43 +00:00
|
|
|
use enshrined\svgSanitize\Sanitizer;
|
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
class SVGFileHandler extends DataHandlerExtension
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-25 20:17:13 +00:00
|
|
|
|
|
|
|
public function onMediaCheckProperties(MediaCheckPropertiesEvent $event)
|
|
|
|
{
|
|
|
|
switch ($event->ext) {
|
|
|
|
case "svg":
|
|
|
|
$event->lossless = true;
|
|
|
|
$event->video = false;
|
|
|
|
$event->audio = false;
|
|
|
|
|
|
|
|
$msp = new MiniSVGParser($event->file_name);
|
|
|
|
$event->width = $msp->width;
|
|
|
|
$event->height = $msp->height;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event)
|
|
|
|
{
|
|
|
|
if ($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
|
|
|
|
$hash = $event->hash;
|
|
|
|
|
|
|
|
$sanitizer = new Sanitizer();
|
|
|
|
$sanitizer->removeRemoteReferences(true);
|
|
|
|
$dirtySVG = file_get_contents($event->tmpname);
|
|
|
|
$cleanSVG = $sanitizer->sanitize($dirtySVG);
|
2019-06-15 16:18:52 +00:00
|
|
|
file_put_contents(warehouse_path(Image::IMAGE_DIR, $hash), $cleanSVG);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
2019-06-15 16:18:52 +00:00
|
|
|
$image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $hash), $event->metadata);
|
2019-05-28 16:59:38 +00:00
|
|
|
if (is_null($image)) {
|
|
|
|
throw new UploadException("SVG handler failed to create image object from data");
|
|
|
|
}
|
|
|
|
$iae = new ImageAdditionEvent($image);
|
|
|
|
send_event($iae);
|
|
|
|
$event->image_id = $iae->image->id;
|
2019-06-20 00:40:25 +00:00
|
|
|
$event->merged = $iae->merged;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 17:34:53 +00:00
|
|
|
protected function create_thumb(string $hash, string $type): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-18 18:45:59 +00:00
|
|
|
try {
|
2019-06-24 15:05:16 +00:00
|
|
|
create_image_thumb($hash, $type, MediaEngine::IMAGICK);
|
2019-06-18 18:45:59 +00:00
|
|
|
return true;
|
2019-06-24 15:05:16 +00:00
|
|
|
} catch (MediaException $e) {
|
2019-06-18 18:45:59 +00:00
|
|
|
log_warning("handle_svg", "Could not generate thumbnail. " . $e->getMessage());
|
2019-06-15 16:18:52 +00:00
|
|
|
copy("ext/handle_svg/thumb.jpg", warehouse_path(Image::THUMBNAIL_DIR, $hash));
|
2019-06-18 18:45:59 +00:00
|
|
|
return false;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event)
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
if ($this->supported_ext($event->image->ext)) {
|
|
|
|
$this->theme->display_image($page, $event->image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
if ($event->page_matches("get_svg")) {
|
|
|
|
$id = int_escape($event->get_arg(0));
|
|
|
|
$image = Image::by_id($id);
|
|
|
|
$hash = $image->hash;
|
|
|
|
|
|
|
|
$page->set_type("image/svg+xml");
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$sanitizer = new Sanitizer();
|
|
|
|
$sanitizer->removeRemoteReferences(true);
|
2019-06-15 16:18:52 +00:00
|
|
|
$dirtySVG = file_get_contents(warehouse_path(Image::IMAGE_DIR, $hash));
|
2019-05-28 16:59:38 +00:00
|
|
|
$cleanSVG = $sanitizer->sanitize($dirtySVG);
|
|
|
|
$page->set_data($cleanSVG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
protected function supported_ext(string $ext): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$exts = ["svg"];
|
|
|
|
return in_array(strtolower($ext), $exts);
|
|
|
|
}
|
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
protected function create_image_from_data(string $filename, array $metadata): Image
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$image = new Image();
|
|
|
|
|
|
|
|
$image->filesize = $metadata['size'];
|
|
|
|
$image->hash = $metadata['hash'];
|
|
|
|
$image->filename = $metadata['filename'];
|
|
|
|
$image->ext = $metadata['extension'];
|
|
|
|
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
|
|
|
|
$image->source = $metadata['source'];
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
protected function check_contents(string $file): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$msp = new MiniSVGParser($file);
|
|
|
|
return bool_escape($msp->valid);
|
|
|
|
}
|
2007-12-11 18:40:44 +00:00
|
|
|
}
|
2008-04-11 06:56:31 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class MiniSVGParser
|
|
|
|
{
|
|
|
|
/** @var bool */
|
|
|
|
public $valid=false;
|
|
|
|
/** @var int */
|
|
|
|
public $width=0;
|
|
|
|
/** @var int */
|
|
|
|
public $height=0;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $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, file_get_contents($file), true));
|
|
|
|
xml_parser_free($xml_parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function startElement($parser, $name, $attrs)
|
|
|
|
{
|
|
|
|
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($parser, $name)
|
|
|
|
{
|
|
|
|
$this->xml_depth--;
|
|
|
|
}
|
2008-04-11 06:56:31 +00:00
|
|
|
}
|