Skip to content

Commit

Permalink
[!!!][FEATURE] Replace $GLOBALS['PAGES_TYPES'] with shared state object
Browse files Browse the repository at this point in the history
This change introduces a new registry class to be used
automatically instead of accessing $GLOBALS['PAGES_TYPES']
which is not evaluated anymore.

Also, instead of ExtensionManagementUtility::allowTableOnStandardPages()
the flag $TCA[$table][ctrl][security][ignorePageTypeRestriction]
is introduced which allows to avoid this call.

Resolves: #98487
Releases: main
Change-Id: I91bdb321500d133e645ccc5b6c0d159f5dd3ad87
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/75188
Tested-by: core-ci <typo3@b13.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Kevin Appelt <kevin.appelt@icloud.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
  • Loading branch information
bmack authored and ohader committed Oct 2, 2022
1 parent a7dd022 commit e88b9a0
Show file tree
Hide file tree
Showing 62 changed files with 450 additions and 210 deletions.
5 changes: 0 additions & 5 deletions Build/phpstan/phpstan-baseline.neon
Expand Up @@ -385,11 +385,6 @@ parameters:
count: 4
path: ../../typo3/sysext/core/Classes/DataHandling/DataHandler.php

-
message: "#^Method TYPO3\\\\CMS\\\\Core\\\\DataHandling\\\\DataHandler\\:\\:doesPageHaveUnallowedTables\\(\\) should return array\\|bool but returns string\\.$#"
count: 1
path: ../../typo3/sysext/core/Classes/DataHandling/DataHandler.php

-
message: "#^Method TYPO3\\\\CMS\\\\Core\\\\DataHandling\\\\DataHandler\\:\\:getSortNumber\\(\\) should return array\\|bool\\|int\\|null but returns float\\.$#"
count: 1
Expand Down
Expand Up @@ -30,6 +30,7 @@
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\DataHandling\PageDoktypeRegistry;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Imaging\Icon;
Expand Down Expand Up @@ -604,10 +605,8 @@ protected function isTableAllowedOnPage(string $table, array $page): bool
return false;
}
// Checking doktype
$doktype = (int)$page['doktype'];
$allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['allowedTables'] ?? '';
// If all tables or the table is listed as an allowed type, return TRUE
return $rootLevelConstraintMatches && (str_contains($allowedTableList, '*') || GeneralUtility::inList($allowedTableList, $table));
$isAllowed = GeneralUtility::makeInstance(PageDoktypeRegistry::class)->isRecordTypeAllowedForDoktype($table, $page['doktype']);
return $rootLevelConstraintMatches && $isAllowed;
}

/**
Expand Down
Expand Up @@ -26,6 +26,7 @@
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\DataHandling\PageDoktypeRegistry;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
Expand Down Expand Up @@ -178,9 +179,7 @@ protected function getTypeSelectData(int $pageUid): array
$pagesTsConfig = $tsConfig['TCEFORM.']['pages.'] ?? [];

// Find all available doktypes for the current user
$types = $GLOBALS['PAGES_TYPES'];
unset($types['default']);
$types = array_keys($types);
$types = GeneralUtility::makeInstance(PageDoktypeRegistry::class)->getRegisteredDoktypes();
$types[] = PageRepository::DOKTYPE_DEFAULT;
$types[] = PageRepository::DOKTYPE_LINK;
$types[] = PageRepository::DOKTYPE_SHORTCUT;
Expand Down
1 change: 0 additions & 1 deletion typo3/sysext/core/Classes/Core/Bootstrap.php
Expand Up @@ -455,7 +455,6 @@ protected static function setMemoryLimit()
*/
public static function unsetReservedGlobalVariables()
{
unset($GLOBALS['PAGES_TYPES']);
unset($GLOBALS['TCA']);
unset($GLOBALS['TBE_STYLES']);
unset($GLOBALS['BE_USER']);
Expand Down
37 changes: 15 additions & 22 deletions typo3/sysext/core/Classes/DataHandling/DataHandler.php
Expand Up @@ -1369,15 +1369,15 @@ public function checkValue($table, $field, $value, $id, $status, $realPid, $tscP
}
if ($status === 'update') {
// This checks 1) if we should check for disallowed tables and 2) if there are records from disallowed tables on the current page
$onlyAllowedTables = $GLOBALS['PAGES_TYPES'][$value]['onlyAllowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['onlyAllowedTables'];
$onlyAllowedTables = GeneralUtility::makeInstance(PageDoktypeRegistry::class)->doesDoktypeOnlyAllowSpecifiedRecordTypes((int)$value);
if ($onlyAllowedTables) {
// use the real page id (default language)
$recordId = $this->getDefaultLanguagePageId((int)$id);
$theWrongTables = $this->doesPageHaveUnallowedTables($recordId, (int)$value);
if ($theWrongTables) {
if ($theWrongTables !== []) {
if ($this->enableLogging) {
$propArr = $this->getRecordProperties($table, $id);
$this->log($table, (int)$id, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, '"doktype" of page "{title}" could not be changed because the page contains records from disallowed tables; {disallowedTables}', 2, ['title' => $propArr['header'], 'disallowedTables' => $theWrongTables], $propArr['event_pid']);
$this->log($table, (int)$id, SystemLogDatabaseAction::CHECK, 0, SystemLogErrorClassification::USER_ERROR, '"doktype" of page "{title}" could not be changed because the page contains records from disallowed tables; {disallowedTables}', 2, ['title' => $propArr['header'], 'disallowedTables' => implode(', ', $theWrongTables)], $propArr['event_pid']);
}
return $res;
}
Expand Down Expand Up @@ -7120,17 +7120,11 @@ public function isTableAllowedForThisPage($page_uid, $checkTable)
if ($this->admin || BackendUtility::isRootLevelRestrictionIgnored($checkTable)) {
$allowed = true;
}
} else {
// Check non-root-level
$doktype = $this->pageInfo($page_uid, 'doktype');
$allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
$allowedArray = GeneralUtility::trimExplode(',', $allowedTableList, true);
// If all tables or the table is listed as an allowed type, return TRUE
if (str_contains($allowedTableList, '*') || in_array($checkTable, $allowedArray, true)) {
$allowed = true;
}
return $allowed;
}
return $allowed;
// Check non-root-level
$doktype = $this->pageInfo($page_uid, 'doktype');
return GeneralUtility::makeInstance(PageDoktypeRegistry::class)->isRecordTypeAllowedForDoktype($checkTable, (int)$doktype);
}

/**
Expand Down Expand Up @@ -7344,27 +7338,26 @@ public function getExcludeListArray()
*
* @param int|string $page_uid Page ID
* @param int $doktype Page doktype
* @return bool|array Returns a list of the tables that are 'present' on the page but not allowed with the page_uid/doktype
* @return array Returns a list of the tables that are 'present' on the page but not allowed with the page_uid/doktype
* @internal should only be used from within DataHandler
*/
public function doesPageHaveUnallowedTables($page_uid, $doktype)
public function doesPageHaveUnallowedTables($page_uid, int $doktype): array
{
$page_uid = (int)$page_uid;
if (!$page_uid) {
// Not a number. Probably a new page
return false;
return [];
}
$allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
$allowedTables = GeneralUtility::makeInstance(PageDoktypeRegistry::class)->getAllowedTypesForDoktype($doktype);
// If all tables are allowed, return early
if (str_contains($allowedTableList, '*')) {
return false;
if (in_array('*', $allowedTables, true)) {
return [];
}
$allowedArray = GeneralUtility::trimExplode(',', $allowedTableList, true);
$tableList = [];
$allTableNames = $this->compileAdminTables();
foreach ($allTableNames as $table) {
// If the table is not in the allowed list, check if there are records...
if (in_array($table, $allowedArray, true)) {
if (in_array($table, $allowedTables, true)) {
continue;
}
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
Expand All @@ -7382,7 +7375,7 @@ public function doesPageHaveUnallowedTables($page_uid, $doktype)
$tableList[] = $table;
}
}
return implode(',', $tableList);
return $tableList;
}

/*****************************
Expand Down
131 changes: 131 additions & 0 deletions typo3/sysext/core/Classes/DataHandling/PageDoktypeRegistry.php
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\Core\DataHandling;

use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* This object defines the various types of pages (field: doktype) the system
* can handle and what restrictions may apply to them when adding records.
* Here you can define which tables are allowed on a certain pagetype (doktype).
*
* NOTE: The 'default' entry array is the 'base' for all types, and for every type the
* entries simply overrides the entries in the 'default' type!
*
* You can fully use this once TCA is properly loaded (e.g. in ext_tables.php). Please be
* aware that this class might change until TYPO3 v12 LTS, for further optimizations.
*/
class PageDoktypeRegistry implements SingletonInterface
{
protected array $pageTypes = [
PageRepository::DOKTYPE_BE_USER_SECTION => [
'allowedTables' => '*',
],
// Doktype 254 is a 'Folder' - a general purpose storage folder for whatever you like.
// In CMS context it's NOT a viewable page. Can contain any element.
PageRepository::DOKTYPE_SYSFOLDER => [
'allowedTables' => '*',
],
// Doktype 255 is a recycle-bin.
PageRepository::DOKTYPE_RECYCLER => [
'allowedTables' => 'sys_file',
],
PageRepository::DOKTYPE_MOUNTPOINT => [
],
// Even though both options look contradictory, the "allowedTables" key is used for other $pageTypes
// that have no custom definitions. So "allowedTables" works as a fallback for additional page types
// Effectively working like "allowTableOnStandardPages()" as the default entry is a "standard page"
'default' => [
'allowedTables' => 'pages,sys_category,sys_file_reference,sys_file_collection',
'onlyAllowedTables' => false,
],
];

/**
* Adds a specific configuration for a doktype. By default, it is NOT restricted to only allow tables that
* have been explicitly added via addAllowedRecordTypes().
*
* @param int $dokType
* @param array $configuration
*/
public function add(int $dokType, array $configuration): void
{
$this->pageTypes[$dokType] = array_replace(['onlyAllowedTables' => false], $configuration);
}

public function addAllowedRecordTypes(array $recordTypes, ?int $doktype = null): void
{
if ($recordTypes === []) {
return;
}
$doktype ??= 'default';
if (!isset($this->pageTypes[$doktype]['allowedTables'])) {
$this->pageTypes[$doktype]['allowedTables'] = '';
}
$this->pageTypes[$doktype]['allowedTables'] .= ',' . implode(',', $recordTypes);
}

/**
* Check if a record can be added on a page with a given $doktype.
*/
public function isRecordTypeAllowedForDoktype(string $type, ?int $doktype): bool
{
$doktype ??= 'default';
$allowedTableList = $this->pageTypes[$doktype]['allowedTables'] ?? $this->pageTypes['default']['allowedTables'];
return str_contains($allowedTableList, '*') || GeneralUtility::inList($allowedTableList, $type);
}

/**
* @internal
*/
public function getRegisteredDoktypes(): array
{
$items = $this->pageTypes;
unset($items['default']);
return array_keys($items);
}

/**
* Used to find out if a specific doktype is restricted to only allow a certain list of tables.
* This list can be checked against via 'isRecordTypeAllowedForDoktype()'
*/
public function doesDoktypeOnlyAllowSpecifiedRecordTypes(int $doktype = null): bool
{
$doktype = $doktype ?? 'default';
return $this->pageTypes[$doktype]['onlyAllowedTables'] ?? false;
}

/**
* @internal only to be used within TYPO3 Core
*/
public function getAllowedTypesForDoktype(int $doktype): array
{
$allowedTableList = $this->pageTypes[$doktype]['allowedTables'] ?? $this->pageTypes['default']['allowedTables'];
return explode(',', $allowedTableList);
}

/**
* @internal only to be used within TYPO3 Core
*/
public function exportConfiguration(): array
{
return $this->pageTypes;
}
}
22 changes: 20 additions & 2 deletions typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
Expand Up @@ -23,6 +23,7 @@
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Configuration\Event\AfterTcaCompilationEvent;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\DataHandling\PageDoktypeRegistry;
use TYPO3\CMS\Core\Migrations\TcaMigration;
use TYPO3\CMS\Core\Package\Cache\PackageDependentCacheIdentifier;
use TYPO3\CMS\Core\Package\Exception as PackageException;
Expand Down Expand Up @@ -676,11 +677,20 @@ protected static function removeDuplicatesForInsertion(string $insertionList, st
* FOR USE IN ext_tables.php FILES
*
* @param string $table Table name
* @deprecated will be removed in TYPO3 v13.0. Use $GLOBALS['TCA'][$table]['ctrl']['security']['ignorePageTypeRestriction'] instead.
*/
public static function allowTableOnStandardPages(string $table): void
{
$GLOBALS['PAGES_TYPES']['default']['allowedTables'] ??= '';
$GLOBALS['PAGES_TYPES']['default']['allowedTables'] .= ',' . $table;
if ($table === '') {
return;
}
$registry = GeneralUtility::makeInstance(PageDoktypeRegistry::class);
$tables = explode(',', $table);
foreach ($tables as $singleTable) {
if (!$registry->isRecordTypeAllowedForDoktype($singleTable, null)) {
$registry->addAllowedRecordTypes(explode(',', $singleTable));
}
}
}

/**
Expand Down Expand Up @@ -1372,6 +1382,14 @@ public static function loadBaseTca(bool $allowCaching = true, FrontendInterface
} else {
static::buildBaseTcaFromSingleFiles();
}

$allowedRecordTypesForDefault = [];
foreach ($GLOBALS['TCA'] as $table => $tableConfiguration) {
if ($tableConfiguration['ctrl']['security']['ignorePageTypeRestriction'] ?? false) {
$allowedRecordTypesForDefault[] = $table;
}
}
GeneralUtility::makeInstance(PageDoktypeRegistry::class)->addAllowedRecordTypes($allowedRecordTypesForDefault);
}

/**
Expand Down
@@ -0,0 +1,38 @@
.. include:: /Includes.rst.txt

.. _breaking-98487-1664575125:

==================================================
Breaking: #98487 - $GLOBALS['PAGES_TYPES'] removed
==================================================

See :issue:`98487`

Description
===========

The global array :php:`PAGES_TYPES` has been removed in favor of a new registry
class containing the shared state.


Impact
======

Accessing or modifying :php:`$GLOBALS['PAGES_TYPES']` will have no effect anymore.


Affected installations
======================

TYPO3 installations with custom extensions creating custom TCA records or custom
Page Doktypes.


Migration
=========

Use the new PageTypeRegistry class to register custom page types with their
dependency what kind of records should be allowed for creation, or to read the
information what record types are allowed on a specific pages.doktype.

.. index:: PHP-API, FullyScanned, ext:core

0 comments on commit e88b9a0

Please sign in to comment.