Skip to content

Commit

Permalink
[TASK] add svg viewhelper to theme_t3kit and register t3kit fluid nam… (
Browse files Browse the repository at this point in the history
#150)

* [TASK] add svg viewhelper to theme_t3kit and register t3kit fluid namespaces

* [FIX] correct some PHPCGL issues, only allow image/svg-xml
  • Loading branch information
pixelmatseriks authored and MattiasNilsson committed Apr 10, 2017
1 parent 2c208be commit ab51a44
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
110 changes: 110 additions & 0 deletions Classes/ViewHelpers/Render/SvgViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
namespace T3kit\themeT3kit\ViewHelpers\Render;

/***************************************************************
*
* Copyright notice
*
* (c) 2016 Andrii Pozdieiev <[email protected]>, Mosa Al-Husseini <[email protected]>, Pixelant AB
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class SvgViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{

/**
* @var boolean
*/
protected $escapeChildren = false;

/**
* @var boolean
*/
protected $escapeOutput = false;

/**
* Initialize arguments
*
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('class', 'string', 'Specifies an alternate class for the svg', false);
$this->registerArgument('width', 'float', 'Specifies a width for the svg', false);
$this->registerArgument('height', 'float', 'Specifies a height for the svg', false);
}

/**
* Generate a list from the content
*
* @param string $source
*/
public function render($source)
{
$sourceAbs = PATH_site . $source;

if (!file_exists($sourceAbs)) {
return '<!-- unable to open file: ' . $source . ' (missing) -->';
}

$finfo = \mime_content_type($sourceAbs);

if ($finfo !== 'image/svg+xml') {
return '<!-- unable to open file: ' . $source . ' (' . $finfo . ') -->';
}

return $this->getInlineSvg($sourceAbs);
}

/**
* Get xml from SVG
*
* @param string $source
*
* @return string
*/
protected function getInlineSvg($source)
{

if (!file_exists($source)) {
return '';
}

$svgContent = file_get_contents($source);
$svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
// Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
$previousValueOfEntityLoader = libxml_disable_entity_loader(true);
$svgElement = simplexml_load_string($svgContent);
libxml_disable_entity_loader($previousValueOfEntityLoader);

// remove xml version tag
$domXml = dom_import_simplexml($svgElement);
if (isset($this->arguments['class'])) {
$domXml->setAttribute('class', $this->arguments['class']);
}
if (isset($this->arguments['width'])) {
$domXml->setAttribute('width', $this->arguments['width']);
}
if (isset($this->arguments['height'])) {
$domXml->setAttribute('height', $this->arguments['height']);
}
return $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
}
}
2 changes: 2 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']['imageTextLink']
= \T3kit\themeT3kit\Hooks\ImageTextLinkPreviewRenderer::class;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['t3kit'][] = 'T3kit\\themeT3kit\\ViewHelpers';

0 comments on commit ab51a44

Please sign in to comment.