external_media/Classes/IFrameRenderer.php

134 lines
4.1 KiB
PHP

<?php
namespace Codappix\ExternalTypo3Multimedia;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
/**
* Provides common API to generate iFrames for external multimedia.
*/
class IFrameRenderer
{
/**
* Generates pairs of key/value; not yet html-escaped
*/
public function collectAttributes(
string $width,
string $height,
array $options
): array {
$attributes = [];
$attributes['allowfullscreen'] = true;
if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
$attributes = array_merge($attributes, $options['additionalAttributes']);
}
if (isset($options['data']) && is_array($options['data'])) {
array_walk(
$options['data'],
function (&$value, $key) use (&$attributes) {
$attributes['data-' . $key] = $value;
}
);
}
if ($width !== '' && $width !== 0) {
$attributes['width'] = htmlspecialchars($width);
}
if ($height !== '' && $height !== 0) {
$attributes['height'] = htmlspecialchars($height);
}
if (
isset($GLOBALS['TSFE'])
&& is_object($GLOBALS['TSFE'])
&& (
isset($GLOBALS['TSFE']->config['config']['doctype'])
&& $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5'
)
) {
$attributes['frameborder'] = 0;
}
$allowedAttributes = [
'class',
'dir',
'id',
'lang',
'style',
'title',
'accesskey',
'tabindex',
'onclick',
'allow',
];
foreach ($allowedAttributes as $key) {
if (!empty($options[$key])) {
$attributes[$key] = $options[$key];
}
}
$attributes = array_map('htmlspecialchars', $attributes);
return $attributes;
}
public function collectOptions(
array $options,
FileInterface $file
): array {
// Check for an autoplay option at the file reference itself, if not overridden yet.
if (!isset($options['autoplay']) && $file instanceof FileReference) {
$autoplay = $file->getProperty('autoplay');
if ($autoplay !== null) {
$options['autoplay'] = $autoplay;
}
}
if (!isset($options['allow'])) {
$options['allow'] = 'fullscreen';
if (!empty($options['autoplay'])) {
$options['allow'] = 'autoplay; fullscreen';
}
}
return $options;
}
public function getOriginalFile(FileInterface $file): File
{
$orgFile = $file;
if ($orgFile instanceof FileReference) {
$orgFile = $orgFile->getOriginalFile();
}
return $orgFile;
}
public function getOnlineMediaHelper(FileInterface $file): OnlineMediaHelperInterface
{
return OnlineMediaHelperRegistry::getInstance()
->getOnlineMediaHelper($this->getOriginalFile($file));
}
}