Skip to content

Commit

Permalink
[TASK] Use ServerRequestInterface in File/CreateFolderController
Browse files Browse the repository at this point in the history
Use ServerRequestInterface object introduced earlier throughout the
controller instead accessing the global variables directly.

Visibility of properties and methods became more restrictive and will
report external usage by throwing deprecated errors.

Change-Id: Icf13b2b74507652541245453fd45558e7a24ea13
Resolves: #84289
Releases: master
Reviewed-on: https://review.typo3.org/56168
Reviewed-by: Łukasz Uznański <l.uznanski@macopedia.pl>
Tested-by: Łukasz Uznański <l.uznanski@macopedia.pl>
Reviewed-by: Mathias Brodala <mbrodala@pagemachine.de>
Tested-by: Mathias Brodala <mbrodala@pagemachine.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
maddy2101 authored and lolli42 committed Mar 15, 2018
1 parent 72805b4 commit 607cd36
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 34 deletions.
@@ -1,4 +1,5 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Backend\Controller\File;

/*
Expand All @@ -16,10 +17,13 @@

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
use TYPO3\CMS\Core\Resource\ResourceFactory;
Expand All @@ -34,29 +38,44 @@
*/
class CreateFolderController
{
use PublicPropertyDeprecationTrait;

/**
* @var array
*/
protected $deprecatedPublicProperties = [
'number' => 'Using $number of class CreateFolderController from outside is discouraged, as this variable is only used for internal storage.',
'folderNumber' => 'Using $folderNumber of class CreateFolderController from outside is discouraged, as this variable is only used for internal storage.',
'target' => 'Using $target of class CreateFolderController from outside is discouraged, as this variable is only used for internal storage.',
'content' => 'Using $content of class CreateFolderController from outside is discouraged, as this variable is only used for internal storage.',
'returnUrl' => 'Using $content of class CreateFolderController from outside is discouraged, as this variable is only used for internal storage.',
'title' => 'Using $content of class CreateFolderController from outside is discouraged, as this variable is only used for internal storage.',
];

/**
* @var int
*/
public $folderNumber = 10;
protected $folderNumber = 10;

/**
* Name of the filemount
*
* @var string
* @deprecated since v9, will be removed in v10, unused
*/
public $title;
protected $title;

/**
* @var int
*/
public $number;
protected $number;

/**
* Set with the target path inputted in &target
*
* @var string
*/
public $target;
protected $target;

/**
* The folder object which is the target directory
Expand All @@ -70,7 +89,7 @@ class CreateFolderController
*
* @var string
*/
public $returnUrl;
protected $returnUrl;

/**
* @var array
Expand All @@ -81,8 +100,9 @@ class CreateFolderController
* Accumulating content
*
* @var string
* @deprecated since v9, will be removed in v10, unused
*/
public $content;
protected $content;

/**
* ModuleTemplate object
Expand All @@ -98,19 +118,36 @@ public function __construct()
{
$this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
$GLOBALS['SOBE'] = $this;
$this->init();
// @deprecated since v9, will be moved out of __construct() in v10
$this->init($GLOBALS['TYPO3_REQUEST']);
}

/**
* Processes the request, currently everything is handled and put together via "main()"
*
* @param ServerRequestInterface $request the current request
* @return ResponseInterface the response with the content
*/
public function mainAction(ServerRequestInterface $request): ResponseInterface
{
$this->main();
return new HtmlResponse($this->moduleTemplate->renderContent());
}

/**
* @param ServerRequestInterface|null $request
*
* @throws InsufficientFolderAccessPermissionsException
* @throws \RuntimeException
*/
protected function init()
protected function init(ServerRequestInterface $request): void
{
// Initialize GPvars:
$this->number = GeneralUtility::_GP('number');
$this->target = ($combinedIdentifier = GeneralUtility::_GP('target'));
$this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
$parsedBody = $request->getParsedBody();
$queryParams = $request->getQueryParams();

$this->number = $parsedBody['number'] ?? $queryParams['number'] ?? 0;
$this->target = ($combinedIdentifier = $parsedBody['target'] ?? $queryParams['target'] ?? '');
$this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
// create the folder object
if ($combinedIdentifier) {
$this->folderObject = ResourceFactory::getInstance()
Expand All @@ -132,8 +169,7 @@ protected function init()
$pathInfo = [
'combined_identifier' => $this->folderObject->getCombinedIdentifier(),
];
/** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);

$this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
$this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
Expand Down Expand Up @@ -178,12 +214,18 @@ function backToList() {
*/
public function main()
{
// Foreign class call? Method will be protected in v10, giving core freedom to move stuff around
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
if (end($backtrace)['class'] !== __CLASS__) {
// @deprecated since TYPO3 v9, this method will be set to protected in v10
trigger_error('Method main() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
}

$lang = $this->getLanguageService();
$assigns = [];
$assigns['target'] = $this->target;
if ($this->folderObject->checkActionPermission('add')) {
/** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
$assigns['cshFileNewFolder'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfolder');
// Making the selector box for the number of concurrent folder-creations
Expand All @@ -206,8 +248,7 @@ public function main()
}

if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
/** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$assigns['moduleUrlOnlineMedia'] = (string)$uriBuilder->buildUriFromRoute('online_media');
$assigns['cshFileNewMedia'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newMedia');
// Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
Expand Down Expand Up @@ -257,28 +298,15 @@ public function main()
'EXT:backend/Resources/Private/Templates/File/CreateFolder.html'
));
$view->assignMultiple($assigns);
$this->content = $view->render();
$this->moduleTemplate->setContent($this->content);
}

/**
* Processes the request, currently everything is handled and put together via "main()"
*
* @param ServerRequestInterface $request the current request
* @return ResponseInterface the response with the content
*/
public function mainAction(ServerRequestInterface $request): ResponseInterface
{
$this->main();
return new HtmlResponse($this->moduleTemplate->renderContent());
$this->moduleTemplate->setContent($view->render());
}

/**
* Returns LanguageService
*
* @return \TYPO3\CMS\Core\Localization\LanguageService
* @return LanguageService
*/
protected function getLanguageService()
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
Expand Down
@@ -0,0 +1,50 @@
.. include:: ../../Includes.txt

===============================================================================
Deprecation: #84289 - Use ServerRequestInterface in File/CreateFolderController
===============================================================================

See :issue:`84289`

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

A series of class properties has been set to protected.
They will throw deprecation warnings if called public from outside:

* :php:`$number`
* :php:`$folderNumber`
* :php:`$target`
* [not scanned] :php:`$title`
* [not scanned] :php:`$returnUrl`
* :php:`$content`

All methods not used as entry points by :php:`TYPO3\CMS\Backend\Http\RouteDispatcher` will be
removed or set to protected in v10 and throw deprecation warnings if used from a third party:

* [not scanned] :php:`main()`

Impact
======

Calling one of the above methods or accessing one of the above properties on an instance of
:php:`CreateFolderController` will throw a deprecation warning in v9 and a PHP fatal in v10.


Affected Installations
======================

The extension scanner will find most usages, but may also find some false positives. The most
common property and method names like :php:`$title` are not registered and will not be found
if an extension uses that on an instance of :php:`CreateFolderController`. In general all extensions
that set properties or call methods except :php:`mainAction()` are affected.


Migration
=========

In general, extensions should not instantiate and re-use controllers of the core. Existing
usages should be rewritten to be free of calls like these.


.. index:: Backend, PHP-API, PartiallyScanned, ext:backend
Expand Up @@ -341,4 +341,24 @@
'Deprecation-83904-ArrayHandlingInAbstractTreeView.rst',
],
],
'TYPO3\CMS\Backend\Controller\File\CreateFolderController->number' => [
'restFiles' => [
'Deprecation-84289-UseServerRequestInterfaceInFileCreateFolderController.rst',
],
],
'TYPO3\CMS\Backend\Controller\File\CreateFolderController->folderNumber' => [
'restFiles' => [
'Deprecation-84289-UseServerRequestInterfaceInFileCreateFolderController.rst',
],
],
'TYPO3\CMS\Backend\Controller\File\CreateFolderController->target' => [
'restFiles' => [
'Deprecation-84289-UseServerRequestInterfaceInFileCreateFolderController.rst',
],
],
'TYPO3\CMS\Backend\Controller\File\CreateFolderController->content' => [
'restFiles' => [
'Deprecation-84289-UseServerRequestInterfaceInFileCreateFolderController.rst',
],
],
];

0 comments on commit 607cd36

Please sign in to comment.