Skip to content

Instantly share code, notes, and snippets.

@ogrosko
Created January 3, 2018 15:05
Show Gist options
  • Save ogrosko/adf526811b0477ed5f9008510c1033ed to your computer and use it in GitHub Desktop.
Save ogrosko/adf526811b0477ed5f9008510c1033ed to your computer and use it in GitHub Desktop.
Flux Drag&Drop accept allowedContentTypes
<?php
namespace BinaryBay\BbBoilerplate\Utility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Extbase\Service\FlexFormService;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use FluidTYPO3\Flux\Service\FluxService;
use TYPO3\CMS\Core\Utility\MathUtility;
/**
* Class ContentProtector
* @package BinaryBay\BbBoilerplate\Utility
*/
class ContentProtector
{
/**
* Table name
*/
const TABLE_NAME = 'tt_content';
/**
* Fluid content CType
*/
const FLUID_CONTENT_CTYPE = 'fluidcontent_content';
/**
* @var ObjectManager
*/
protected $objectManager = null;
/**
* @var DataHandler
*/
protected $dataHandler = null;
/**
* @var FlashMessageService
*/
protected $flashMessageService = null;
/**
* @var FlexFormService
*/
protected $flexFormService = null;
/**
* @var FluxService
*/
protected $fluxService = null;
/**
* ContentProtector constructor.
* @param DataHandler $dataHandler
*/
public function __construct(DataHandler $dataHandler)
{
$this->dataHandler = $dataHandler;
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$this->flashMessageService = $this->objectManager->get(FlashMessageService::class);
$this->flexFormService = $this->objectManager->get(FlexFormService::class);
$this->flexFormService = $this->objectManager->get(FluxService::class);
}
public function checkContentElements()
{
foreach ($this->dataHandler->datamap[self::TABLE_NAME] as $id => $incomingData) {
if (!$id) {
continue;
}
if (MathUtility::canBeInterpretedAsInteger($id)) {
$incomingData = array_merge(BackendUtility::getRecord(self::TABLE_NAME, $id), $incomingData);
}
if ($this->isElementDenied($incomingData)) {
$this->flashMessageService->getMessageQueueByIdentifier()->enqueue(new FlashMessage(
"Placing element tt_content:{$id} is not allowed to create/move/copy here.",
'Element not allowed',
FlashMessage::ERROR,
false
));
unset($this->dataHandler->cmdmap[self::TABLE_NAME][$id]);
unset($this->dataHandler->datamap[self::TABLE_NAME][$id]);
}
}
}
/**
* @param array $record
* @return bool
*/
protected function isElementDenied($record)
{
$colPos = (int)$record['colPos'];
if ($colPos > \FluidTYPO3\Flux\Service\ContentService::COLPOS_FLUXCONTENT) {
return true;
} else {
if (\intval($record['pid']) < 0) {
$record['pid'] = BackendUtility::getRecord(
self::TABLE_NAME,
abs($record['pid']),
'pid'
)['pid'];
}
$page = BackendUtility::getRecord('pages', $record['pid']);
$provider = $this->flexFormService->resolvePrimaryConfigurationProvider('pages', null, $page);
foreach ($provider->getGrid($page)->getRows() as $row) {
foreach ($row->getColumns() as $column) {
if ($column->getColumnPosition() === $colPos) {
if ($this->isRecordFluidContent($record)) {
$blackWhiteList = $this->getWhiteBlackList($column->getVariable('Fluidcontent'));
} else {
$blackWhiteList = $this->getWhiteBlackList($column->getVariables());
}
}
}
}
}
list($whiteList, $blackList) = $blackWhiteList;
if ($this->isRecordFluidContent($record)) {
if (!empty($blackList) and \in_array($record['tx_fed_fcefile'], $blackList)) {
return true;
}
if (!empty($whiteList) and !\in_array($record['tx_fed_fcefile'], $whiteList)) {
return true;
}
} else {
if (!empty($blackList) and \in_array($record['CType'], $blackList)) {
return true;
}
if (!empty($whiteList) and !\in_array($record['CType'], $whiteList)) {
return true;
}
}
return false;
}
/**
* @param array $variables
* @return array
*/
protected function getWhiteBlackList($variables)
{
$whiteList = $blackList = [];
if (isset($variables['allowedContentTypes'])) {
$whiteList = GeneralUtility::trimExplode(',', $variables['allowedContentTypes']);
}
if (isset($variables['deniedContentTypes'])) {
$blackList = GeneralUtility::trimExplode(',', $variables['deniedContentTypes']);
}
return [$whiteList, $blackList];
}
/**
* @param array $record
* @return bool
*/
protected function isRecordFluidContent($record)
{
return $record['CType'] === self::FLUID_CONTENT_CTYPE;
}
/**
* @param string $flexFormXml
* @return array
*/
protected function getFlexFormConfiguration($flexFormXml)
{
return $this->flexFormService->convertFlexFormContentToArray($flexFormXml);
}
}
<?php
if (!defined('TYPO3_MODE')) {
die('Access denied.');
}
/**
* TCEMain hook
*/
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][$extKey] =
\BinaryBay\BbBoilerplate\Hooks\TCEmainHook::class;
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][$extKey] =
\BinaryBay\BbBoilerplate\Hooks\TCEmainHook::class;
<?php
namespace BinaryBay\BbBoilerplate\Hooks;
/**
* Class TCEmainHook
* @package BinaryBay\BbBoilerplate\Hooks
*/
class TCEmainHook
{
/**
* @param \TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler
* @return void
*/
public function processDatamap_beforeStart(\TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler)
{
if (!empty($dataHandler->datamap[\BinaryBay\BbBoilerplate\Utility\ContentProtector::TABLE_NAME])) {
$contentProtector = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\BinaryBay\BbBoilerplate\Utility\ContentProtector::class,
$dataHandler
);
$contentProtector->checkContentElements();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment