2007-12-11 18:40:44 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2010-01-05 10:32:39 +00:00
|
|
|
* Name: Handle SVG
|
2007-12-11 18:40:44 +00:00
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
2012-02-08 02:52:11 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2017-03-18 00:13:16 +00:00
|
|
|
* Description: Handle static SVG files. (No thumbnail is generated for SVG files)
|
2007-12-11 18:40:44 +00:00
|
|
|
*/
|
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class SVGFileHandler extends Extension {
|
2012-02-08 11:45:35 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event) {
|
|
|
|
if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
|
2007-12-11 18:40:44 +00:00
|
|
|
$hash = $event->hash;
|
2016-07-30 21:11:49 +00:00
|
|
|
move_upload_to_archive($event);
|
2007-12-11 18:40:44 +00:00
|
|
|
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
2010-02-02 01:04:38 +00:00
|
|
|
$image = $this->create_image_from_data(warehouse_path("images", $hash), $event->metadata);
|
2007-12-11 18:40:44 +00:00
|
|
|
if(is_null($image)) {
|
2009-01-04 14:01:59 +00:00
|
|
|
throw new UploadException("SVG handler failed to create image object from data");
|
2007-12-11 18:40:44 +00:00
|
|
|
}
|
2012-08-18 18:45:39 +00:00
|
|
|
$iae = new ImageAdditionEvent($image);
|
2009-07-16 19:20:53 +00:00
|
|
|
send_event($iae);
|
|
|
|
$event->image_id = $iae->image->id;
|
2007-12-11 18:40:44 +00:00
|
|
|
}
|
2012-02-08 11:45:35 +00:00
|
|
|
}
|
2007-12-11 18:40:44 +00:00
|
|
|
|
2012-02-08 11:45:35 +00:00
|
|
|
public function onThumbnailGeneration(ThumbnailGenerationEvent $event) {
|
|
|
|
if($this->supported_ext($event->type)) {
|
2007-12-11 18:40:44 +00:00
|
|
|
$hash = $event->hash;
|
2007-12-11 22:47:55 +00:00
|
|
|
|
2012-02-08 11:45:35 +00:00
|
|
|
copy("ext/handle_svg/thumb.jpg", warehouse_path("thumbs", $hash));
|
2007-12-11 18:40:44 +00:00
|
|
|
}
|
2012-02-08 11:45:35 +00:00
|
|
|
}
|
2007-12-11 18:40:44 +00:00
|
|
|
|
2012-02-08 11:45:35 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event) {
|
|
|
|
global $page;
|
|
|
|
if($this->supported_ext($event->image->ext)) {
|
2009-07-16 19:20:53 +00:00
|
|
|
$this->theme->display_image($page, $event->image);
|
2007-12-11 18:40:44 +00:00
|
|
|
}
|
2012-02-08 11:45:35 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2012-02-08 11:45:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2015-09-12 10:43:28 +00:00
|
|
|
global $page;
|
2012-02-08 11:45:35 +00:00
|
|
|
if($event->page_matches("get_svg")) {
|
2007-12-11 18:40:44 +00:00
|
|
|
$id = int_escape($event->get_arg(0));
|
2009-05-11 14:04:33 +00:00
|
|
|
$image = Image::by_id($id);
|
2007-12-11 18:40:44 +00:00
|
|
|
$hash = $image->hash;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2009-07-16 19:20:53 +00:00
|
|
|
$page->set_type("image/svg+xml");
|
|
|
|
$page->set_mode("data");
|
2010-02-02 01:04:38 +00:00
|
|
|
$page->set_data(file_get_contents(warehouse_path("images", $hash)));
|
2007-12-11 18:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
2015-09-27 11:38:48 +00:00
|
|
|
* @param string $ext
|
2014-04-27 19:33:57 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2008-04-08 15:41:11 +00:00
|
|
|
private function supported_ext($ext) {
|
|
|
|
$exts = array("svg");
|
2009-06-05 19:53:00 +00:00
|
|
|
return in_array(strtolower($ext), $exts);
|
2008-04-08 15:41:11 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
2015-09-27 11:38:48 +00:00
|
|
|
* @param string $filename
|
|
|
|
* @param mixed[] $metadata
|
2014-04-27 19:33:57 +00:00
|
|
|
* @return Image
|
|
|
|
*/
|
2007-12-11 18:40:44 +00:00
|
|
|
private function create_image_from_data($filename, $metadata) {
|
|
|
|
$image = new Image();
|
|
|
|
|
2008-04-11 06:56:31 +00:00
|
|
|
$msp = new MiniSVGParser($filename);
|
|
|
|
$image->width = $msp->width;
|
|
|
|
$image->height = $msp->height;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-12-11 18:40:44 +00:00
|
|
|
$image->filesize = $metadata['size'];
|
|
|
|
$image->hash = $metadata['hash'];
|
|
|
|
$image->filename = $metadata['filename'];
|
|
|
|
$image->ext = $metadata['extension'];
|
2017-05-12 07:57:50 +00:00
|
|
|
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
|
2007-12-11 18:40:44 +00:00
|
|
|
$image->source = $metadata['source'];
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
2015-09-27 11:38:48 +00:00
|
|
|
* @param string $file
|
2014-04-27 19:33:57 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2007-12-11 18:40:44 +00:00
|
|
|
private function check_contents($file) {
|
2008-04-11 06:56:31 +00:00
|
|
|
if(!file_exists($file)) return false;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-04-11 06:56:31 +00:00
|
|
|
$msp = new MiniSVGParser($file);
|
2014-04-27 19:33:57 +00:00
|
|
|
return bool_escape($msp->valid);
|
2007-12-11 18:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-11 06:56:31 +00:00
|
|
|
|
|
|
|
class MiniSVGParser {
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var bool */
|
|
|
|
public $valid=false;
|
|
|
|
/** @var int */
|
|
|
|
public $width=0;
|
|
|
|
/** @var int */
|
|
|
|
public $height=0;
|
2008-04-11 06:56:31 +00:00
|
|
|
|
2016-09-03 08:39:40 +00:00
|
|
|
/** @var int */
|
|
|
|
private $xml_depth=0;
|
|
|
|
|
2016-06-19 16:41:40 +00:00
|
|
|
/** @param string $file */
|
2014-03-22 09:00:59 +00:00
|
|
|
function __construct($file) {
|
2008-04-11 06:56:31 +00:00
|
|
|
$xml_parser = xml_parser_create();
|
|
|
|
xml_set_element_handler($xml_parser, array($this, "startElement"), array($this, "endElement"));
|
2014-04-27 19:33:57 +00:00
|
|
|
$this->valid = bool_escape(xml_parse($xml_parser, file_get_contents($file), true));
|
2008-04-11 06:56:31 +00:00
|
|
|
xml_parser_free($xml_parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
function startElement($parser, $name, $attrs) {
|
2016-09-03 08:39:40 +00:00
|
|
|
if($name == "SVG" && $this->xml_depth == 0) {
|
2008-04-11 06:56:31 +00:00
|
|
|
$this->width = int_escape($attrs["WIDTH"]);
|
|
|
|
$this->height = int_escape($attrs["HEIGHT"]);
|
|
|
|
}
|
2016-09-03 08:39:40 +00:00
|
|
|
$this->xml_depth++;
|
2008-04-11 06:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function endElement($parser, $name) {
|
2016-09-03 08:39:40 +00:00
|
|
|
$this->xml_depth--;
|
2008-04-11 06:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-26 02:54:51 +00:00
|
|
|
|