Completed
Push — master ( a088ad...55fcd8 )
by
unknown
23:17 queued 05:43
created

TypoScriptFrontendController::contentStrReplace()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A TypoScriptFrontendController::newCObj() 0 4 1
1
<?php
2
namespace TYPO3\CMS\Frontend\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Log\LoggerAwareInterface;
20
use Psr\Log\LoggerAwareTrait;
21
use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
22
use TYPO3\CMS\Core\Cache\CacheManager;
23
use TYPO3\CMS\Core\Charset\CharsetConverter;
24
use TYPO3\CMS\Core\Charset\UnknownCharsetException;
25
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
26
use TYPO3\CMS\Core\Configuration\Loader\PageTsConfigLoader;
27
use TYPO3\CMS\Core\Configuration\Parser\PageTsConfigParser;
28
use TYPO3\CMS\Core\Context\Context;
29
use TYPO3\CMS\Core\Context\DateTimeAspect;
30
use TYPO3\CMS\Core\Context\LanguageAspect;
31
use TYPO3\CMS\Core\Context\LanguageAspectFactory;
32
use TYPO3\CMS\Core\Context\TypoScriptAspect;
33
use TYPO3\CMS\Core\Context\UserAspect;
34
use TYPO3\CMS\Core\Context\VisibilityAspect;
35
use TYPO3\CMS\Core\Context\WorkspaceAspect;
36
use TYPO3\CMS\Core\Core\Environment;
37
use TYPO3\CMS\Core\Database\Connection;
38
use TYPO3\CMS\Core\Database\ConnectionPool;
39
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
40
use TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction;
41
use TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction;
42
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
43
use TYPO3\CMS\Core\Error\Http\PageNotFoundException;
44
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
45
use TYPO3\CMS\Core\Error\Http\ShortcutTargetPageNotFoundException;
46
use TYPO3\CMS\Core\Exception\Page\RootLineException;
47
use TYPO3\CMS\Core\Http\ImmediateResponseException;
48
use TYPO3\CMS\Core\Http\ServerRequestFactory;
49
use TYPO3\CMS\Core\Localization\LanguageService;
50
use TYPO3\CMS\Core\Localization\Locales;
51
use TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException;
52
use TYPO3\CMS\Core\Locking\LockFactory;
53
use TYPO3\CMS\Core\Locking\LockingStrategyInterface;
54
use TYPO3\CMS\Core\Page\AssetCollector;
55
use TYPO3\CMS\Core\Page\PageRenderer;
56
use TYPO3\CMS\Core\PageTitle\PageTitleProviderManager;
57
use TYPO3\CMS\Core\Resource\StorageRepository;
58
use TYPO3\CMS\Core\Routing\PageArguments;
59
use TYPO3\CMS\Core\Site\Entity\Site;
60
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
61
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
62
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
63
use TYPO3\CMS\Core\Type\Bitmask\Permission;
64
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
65
use TYPO3\CMS\Core\TypoScript\TemplateService;
66
use TYPO3\CMS\Core\Utility\ArrayUtility;
67
use TYPO3\CMS\Core\Utility\GeneralUtility;
68
use TYPO3\CMS\Core\Utility\HttpUtility;
69
use TYPO3\CMS\Core\Utility\MathUtility;
70
use TYPO3\CMS\Core\Utility\PathUtility;
71
use TYPO3\CMS\Core\Utility\RootlineUtility;
72
use TYPO3\CMS\Frontend\Aspect\PreviewAspect;
73
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
74
use TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
75
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
76
use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
77
use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
78
use TYPO3\CMS\Frontend\Resource\FilePathSanitizer;
79
80
/**
81
 * Class for the built TypoScript based frontend. Instantiated in
82
 * \TYPO3\CMS\Frontend\Http\RequestHandler as the global object TSFE.
83
 *
84
 * Main frontend class, instantiated in \TYPO3\CMS\Frontend\Http\RequestHandler
85
 * as the global object TSFE.
86
 *
87
 * This class has a lot of functions and internal variable which are used from
88
 * \TYPO3\CMS\Frontend\Http\RequestHandler
89
 *
90
 * The class is instantiated as $GLOBALS['TSFE'] in \TYPO3\CMS\Frontend\Http\RequestHandler.
91
 *
92
 * The use of this class should be inspired by the order of function calls as
93
 * found in \TYPO3\CMS\Frontend\Http\RequestHandler.
94
 */
95
class TypoScriptFrontendController implements LoggerAwareInterface
96
{
97
    use LoggerAwareTrait;
98
    use PublicPropertyDeprecationTrait;
99
100
    /**
101
     * @var string[]
102
     */
103
    private $deprecatedPublicProperties = [
104
        'imagesOnPage' => 'Using TSFE->imagesOnPage is deprecated and will no longer work with TYPO3 v11.0. Use AssetCollector()->getMedia() instead.',
105
        'lastImageInfo' => 'Using TSFE->lastImageInfo is deprecated and will no longer work with TYPO3 v11.0.'
106
    ];
107
108
    /**
109
     * The page id (int)
110
     * @var string
111
     */
112
    public $id = '';
113
114
    /**
115
     * The type (read-only)
116
     * @var int
117
     */
118
    public $type = '';
119
120
    /**
121
     * @var Site
122
     */
123
    protected $site;
124
125
    /**
126
     * @var SiteLanguage
127
     */
128
    protected $language;
129
130
    /**
131
     * The submitted cHash
132
     * @var string
133
     * @internal
134
     * @deprecated will be removed in TYPO3 v11.0. don't use it anymore, as this is now within the PageArguments property.
135
     */
136
    protected $cHash = '';
137
138
    /**
139
     * @var PageArguments
140
     * @internal
141
     */
142
    protected $pageArguments;
143
144
    /**
145
     * Page will not be cached. Write only TRUE. Never clear value (some other
146
     * code might have reasons to set it TRUE).
147
     * @var bool
148
     */
149
    public $no_cache = false;
150
151
    /**
152
     * The rootLine (all the way to tree root, not only the current site!)
153
     * @var array
154
     */
155
    public $rootLine = [];
156
157
    /**
158
     * The pagerecord
159
     * @var array
160
     */
161
    public $page = [];
162
163
    /**
164
     * This will normally point to the same value as id, but can be changed to
165
     * point to another page from which content will then be displayed instead.
166
     * @var int
167
     */
168
    public $contentPid = 0;
169
170
    /**
171
     * Gets set when we are processing a page of type mounpoint with enabled overlay in getPageAndRootline()
172
     * Used later in checkPageForMountpointRedirect() to determine the final target URL where the user
173
     * should be redirected to.
174
     *
175
     * @var array|null
176
     */
177
    protected $originalMountPointPage;
178
179
    /**
180
     * Gets set when we are processing a page of type shortcut in the early stages
181
     * of the request when we do not know about languages yet, used later in the request
182
     * to determine the correct shortcut in case a translation changes the shortcut
183
     * target
184
     * @var array|null
185
     * @see checkTranslatedShortcut()
186
     */
187
    protected $originalShortcutPage;
188
189
    /**
190
     * sys_page-object, pagefunctions
191
     *
192
     * @var PageRepository
193
     */
194
    public $sys_page = '';
195
196
    /**
197
     * Is set to 1 if a pageNotFound handler could have been called.
198
     * @var int
199
     * @internal
200
     */
201
    public $pageNotFound = 0;
202
203
    /**
204
     * Domain start page
205
     * @var int
206
     * @internal
207
     * @deprecated will be removed in TYPO3 v11.0. don't use it anymore, as this is now within the Site. see $this->site->getRootPageId()
208
     */
209
    protected $domainStartPage = 0;
210
211
    /**
212
     * Array containing a history of why a requested page was not accessible.
213
     * @var array
214
     */
215
    protected $pageAccessFailureHistory = [];
216
217
    /**
218
     * @var string
219
     * @internal
220
     */
221
    public $MP = '';
222
223
    /**
224
     * The frontend user
225
     *
226
     * @var FrontendUserAuthentication
227
     */
228
    public $fe_user = '';
229
230
    /**
231
     * Shows whether logins are allowed in branch
232
     * @var bool
233
     */
234
    protected $loginAllowedInBranch = true;
235
236
    /**
237
     * Shows specific mode (all or groups)
238
     * @var string
239
     * @internal
240
     */
241
    protected $loginAllowedInBranch_mode = '';
242
243
    /**
244
     * Flag indication that preview is active. This is based on the login of a
245
     * backend user and whether the backend user has read access to the current
246
     * page.
247
     * @var int
248
     * @internal
249
     * @deprecated will be removed in TYPO3 v11.0. don't use it anymore, as this is now within PreviewAspect
250
     */
251
    protected $fePreview = 0;
252
253
    /**
254
     * Value that contains the simulated usergroup if any
255
     * @var int
256
     * @internal only to be used in AdminPanel, and within TYPO3 Core
257
     */
258
    public $simUserGroup = 0;
259
260
    /**
261
     * "CONFIG" object from TypoScript. Array generated based on the TypoScript
262
     * configuration of the current page. Saved with the cached pages.
263
     * @var array
264
     */
265
    public $config = [];
266
267
    /**
268
     * The TypoScript template object. Used to parse the TypoScript template
269
     *
270
     * @var TemplateService
271
     */
272
    public $tmpl;
273
274
    /**
275
     * Is set to the time-to-live time of cached pages. Default is 60*60*24, which is 24 hours.
276
     *
277
     * @var int
278
     * @internal
279
     */
280
    protected $cacheTimeOutDefault = 86400;
281
282
    /**
283
     * Set internally if cached content is fetched from the database.
284
     *
285
     * @var bool
286
     * @internal
287
     */
288
    protected $cacheContentFlag = false;
289
290
    /**
291
     * Set to the expire time of cached content
292
     * @var int
293
     * @internal
294
     */
295
    protected $cacheExpires = 0;
296
297
    /**
298
     * Set if cache headers allowing caching are sent.
299
     * @var bool
300
     * @internal
301
     */
302
    protected $isClientCachable = false;
303
304
    /**
305
     * Used by template fetching system. This array is an identification of
306
     * the template. If $this->all is empty it's because the template-data is not
307
     * cached, which it must be.
308
     * @var array
309
     */
310
    public $all = [];
311
312
    /**
313
     * Toplevel - objArrayName, eg 'page'
314
     * @var string
315
     */
316
    public $sPre = '';
317
318
    /**
319
     * TypoScript configuration of the page-object pointed to by sPre.
320
     * $this->tmpl->setup[$this->sPre.'.']
321
     * @var array
322
     */
323
    public $pSetup = '';
324
325
    /**
326
     * This hash is unique to the template, the $this->id and $this->type vars and
327
     * the list of groups. Used to get and later store the cached data
328
     * @var string
329
     * @internal
330
     */
331
    public $newHash = '';
332
333
    /**
334
     * This flag is set before the page is generated IF $this->no_cache is set. If this
335
     * flag is set after the page content was generated, $this->no_cache is forced to be set.
336
     * This is done in order to make sure that PHP code from Plugins / USER scripts does not falsely
337
     * clear the no_cache flag.
338
     * @var bool
339
     * @internal
340
     */
341
    protected $no_cacheBeforePageGen = false;
342
343
    /**
344
     * Passed to TypoScript template class and tells it to force template rendering
345
     * @var bool
346
     * @deprecated
347
     */
348
    private $forceTemplateParsing = false;
349
350
    /**
351
     * The array which cHash_calc is based on, see PageArgumentValidator class.
352
     * @var array
353
     * @internal
354
     * @deprecated will be removed in TYPO3 v11.0. don't use it anymore, see getRelevantParametersForCachingFromPageArguments()
355
     */
356
    protected $cHash_array = [];
357
358
    /**
359
     * May be set to the pagesTSconfig
360
     * @var array
361
     * @internal
362
     */
363
    protected $pagesTSconfig = '';
364
365
    /**
366
     * Eg. insert JS-functions in this array ($additionalHeaderData) to include them
367
     * once. Use associative keys.
368
     *
369
     * Keys in use:
370
     *
371
     * used to accumulate additional HTML-code for the header-section,
372
     * <head>...</head>. Insert either associative keys (like
373
     * additionalHeaderData['myStyleSheet'], see reserved keys above) or num-keys
374
     * (like additionalHeaderData[] = '...')
375
     *
376
     * @var array
377
     */
378
    public $additionalHeaderData = [];
379
380
    /**
381
     * Used to accumulate additional HTML-code for the footer-section of the template
382
     * @var array
383
     */
384
    public $additionalFooterData = [];
385
386
    /**
387
     * Used to accumulate additional JavaScript-code. Works like
388
     * additionalHeaderData. Reserved keys at 'openPic' and 'mouseOver'
389
     *
390
     * @var array
391
     */
392
    public $additionalJavaScript = [];
393
394
    /**
395
     * Used to accumulate additional Style code. Works like additionalHeaderData.
396
     *
397
     * @var array
398
     */
399
    public $additionalCSS = [];
400
401
    /**
402
     * @var  string
403
     */
404
    public $JSCode;
405
406
    /**
407
     * @var string
408
     */
409
    public $inlineJS;
410
411
    /**
412
     * Used to accumulate DHTML-layers.
413
     * @var string
414
     * @deprecated since TYPO3 v10.2, will be removed in TYPO3 v11, use custom USER_INT objects instead.
415
     */
416
    public $divSection = '';
417
418
    /**
419
     * Default internal target
420
     * @var string
421
     */
422
    public $intTarget = '';
423
424
    /**
425
     * Default external target
426
     * @var string
427
     */
428
    public $extTarget = '';
429
430
    /**
431
     * Default file link target
432
     * @var string
433
     */
434
    public $fileTarget = '';
435
436
    /**
437
     * If set, typolink() function encrypts email addresses.
438
     * @var string|int
439
     */
440
    public $spamProtectEmailAddresses = 0;
441
442
    /**
443
     * Absolute Reference prefix
444
     * @var string
445
     */
446
    public $absRefPrefix = '';
447
448
    /**
449
     * <A>-tag parameters
450
     * @var string
451
     */
452
    public $ATagParams = '';
453
454
    /**
455
     * Search word regex, calculated if there has been search-words send. This is
456
     * used to mark up the found search words on a page when jumped to from a link
457
     * in a search-result.
458
     * @var string
459
     * @internal
460
     */
461
    public $sWordRegEx = '';
462
463
    /**
464
     * Is set to the incoming array sword_list in case of a page-view jumped to from
465
     * a search-result.
466
     * @var string
467
     * @internal
468
     */
469
    public $sWordList = '';
470
471
    /**
472
     * A string prepared for insertion in all links on the page as url-parameters.
473
     * Based on configuration in TypoScript where you defined which GET_VARS you
474
     * would like to pass on.
475
     * @var string
476
     */
477
    public $linkVars = '';
478
479
    /**
480
     * If set, edit icons are rendered aside content records. Must be set only if
481
     * the ->beUserLogin flag is set and set_no_cache() must be called as well.
482
     * @var string
483
     */
484
    public $displayEditIcons = '';
485
486
    /**
487
     * If set, edit icons are rendered aside individual fields of content. Must be
488
     * set only if the ->beUserLogin flag is set and set_no_cache() must be called as
489
     * well.
490
     * @var string
491
     */
492
    public $displayFieldEditIcons = '';
493
494
    /**
495
     * Is set to the iso code of the current language
496
     * @var string
497
     * @deprecated will be removed in TYPO3 v11.0. don't use it anymore, as this is now within SiteLanguage->getTwoLetterIsoCode()
498
     */
499
    protected $sys_language_isocode = '';
500
501
    /**
502
     * 'Global' Storage for various applications. Keys should be 'tx_'.extKey for
503
     * extensions.
504
     * @var array
505
     */
506
    public $applicationData = [];
507
508
    /**
509
     * @var array
510
     */
511
    public $register = [];
512
513
    /**
514
     * Stack used for storing array and retrieving register arrays (see
515
     * LOAD_REGISTER and RESTORE_REGISTER)
516
     * @var array
517
     */
518
    public $registerStack = [];
519
520
    /**
521
     * Checking that the function is not called eternally. This is done by
522
     * interrupting at a depth of 50
523
     * @var int
524
     */
525
    public $cObjectDepthCounter = 50;
526
527
    /**
528
     * Used by RecordContentObject and ContentContentObject to ensure the a records is NOT
529
     * rendered twice through it!
530
     * @var array
531
     */
532
    public $recordRegister = [];
533
534
    /**
535
     * This is set to the [table]:[uid] of the latest record rendered. Note that
536
     * class ContentObjectRenderer has an equal value, but that is pointing to the
537
     * record delivered in the $data-array of the ContentObjectRenderer instance, if
538
     * the cObjects CONTENT or RECORD created that instance
539
     * @var string
540
     */
541
    public $currentRecord = '';
542
543
    /**
544
     * Used by class \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject
545
     * to keep track of access-keys.
546
     * @var array
547
     */
548
    public $accessKey = [];
549
550
    /**
551
     * Numerical array where image filenames are added if they are referenced in the
552
     * rendered document. This includes only TYPO3 generated/inserted images.
553
     * @var array
554
     * @deprecated
555
     */
556
    private $imagesOnPage = [];
557
558
    /**
559
     * Is set in ContentObjectRenderer->cImage() function to the info-array of the
560
     * most recent rendered image. The information is used in ImageTextContentObject
561
     * @var array
562
     * @deprecated
563
     */
564
    private $lastImageInfo = [];
565
566
    /**
567
     * Used to generate page-unique keys. Point is that uniqid() functions is very
568
     * slow, so a unikey key is made based on this, see function uniqueHash()
569
     * @var int
570
     * @internal
571
     */
572
    protected $uniqueCounter = 0;
573
574
    /**
575
     * @var string
576
     * @internal
577
     */
578
    protected $uniqueString = '';
579
580
    /**
581
     * This value will be used as the title for the page in the indexer (if
582
     * indexing happens)
583
     * @var string
584
     */
585
    public $indexedDocTitle = '';
586
587
    /**
588
     * The base URL set for the page header.
589
     * @var string
590
     */
591
    public $baseUrl = '';
592
593
    /**
594
     * Page content render object
595
     *
596
     * @var ContentObjectRenderer
597
     */
598
    public $cObj = '';
599
600
    /**
601
     * All page content is accumulated in this variable. See RequestHandler
602
     * @var string
603
     */
604
    public $content = '';
605
606
    /**
607
     * Output charset of the websites content. This is the charset found in the
608
     * header, meta tag etc. If different than utf-8 a conversion
609
     * happens before output to browser. Defaults to utf-8.
610
     * @var string
611
     */
612
    public $metaCharset = 'utf-8';
613
614
    /**
615
     * Internal calculations for labels
616
     *
617
     * @var LanguageService
618
     */
619
    protected $languageService;
620
621
    /**
622
     * @var LockingStrategyInterface[][]
623
     */
624
    protected $locks = [];
625
626
    /**
627
     * @var PageRenderer
628
     */
629
    protected $pageRenderer;
630
631
    /**
632
     * The page cache object, use this to save pages to the cache and to
633
     * retrieve them again
634
     *
635
     * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
636
     */
637
    protected $pageCache;
638
639
    /**
640
     * @var array
641
     */
642
    protected $pageCacheTags = [];
643
644
    /**
645
     * Content type HTTP header being sent in the request.
646
     * @todo Ticket: #63642 Should be refactored to a request/response model later
647
     * @internal Should only be used by TYPO3 core for now
648
     *
649
     * @var string
650
     */
651
    protected $contentType = 'text/html';
652
653
    /**
654
     * Doctype to use
655
     *
656
     * @var string
657
     */
658
    public $xhtmlDoctype = '';
659
660
    /**
661
     * @var int
662
     */
663
    public $xhtmlVersion;
664
665
    /**
666
     * Originally requested id from the initial $_GET variable
667
     *
668
     * @var int
669
     */
670
    protected $requestedId;
671
672
    /**
673
     * The context for keeping the current state, mostly related to current page information,
674
     * backend user / frontend user access, workspaceId
675
     *
676
     * @var Context
677
     */
678
    protected $context;
679
680
    /**
681
     * Since TYPO3 v10.0, TSFE is composed out of
682
     *  - Context
683
     *  - Site
684
     *  - SiteLanguage
685
     *  - PageArguments (containing ID, Type, cHash and MP arguments)
686
     *
687
     * With TYPO3 v11, they will become mandatory and the method arguments will become strongly typed.
688
     * For TYPO3 v10 this is built in a way to ensure maximum compatibility.
689
     *
690
     * Also sets a unique string (->uniqueString) for this script instance; A md5 hash of the microtime()
691
     *
692
     * @param Context|array|null $context the Context object to work on, previously defined to set TYPO3_CONF_VARS
693
     * @param mixed|SiteInterface $siteOrId The resolved site to work on, previously this was the value of GeneralUtility::_GP('id')
694
     * @param SiteLanguage|int|string $siteLanguageOrType The resolved language to work on, previously the value of GeneralUtility::_GP('type')
695
     * @param bool|string|PageArguments|null $pageArguments The PageArguments object containing ID, type and GET parameters, previously unused or the value of GeneralUtility::_GP('no_cache')
696
     * @param string|FrontendUserAuthentication|null $cHashOrFrontendUser FrontendUserAuthentication object, previously the value of GeneralUtility::_GP('cHash'), use the PageArguments object instead, will be removed in TYPO3 v11.0
697
     * @param string|null $_2 previously was used to define the jumpURL, use the PageArguments object instead, will be removed in TYPO3 v11.0
698
     * @param string|null $MP The value of GeneralUtility::_GP('MP'), use the PageArguments object instead, will be removed in TYPO3 v11.0
699
     */
700
    public function __construct($context = null, $siteOrId = null, $siteLanguageOrType = null, $pageArguments = null, $cHashOrFrontendUser = null, $_2 = null, $MP = null)
701
    {
702
        $this->initializeContextWithGlobalFallback($context);
703
704
        // Fetch the request for fetching data (site/language/pageArguments) for compatibility reasons, not needed
705
        // in TYPO3 v11.0 anymore.
706
        /** @var ServerRequestInterface $request */
707
        $request = $GLOBALS['TYPO3_REQUEST'] ?? ServerRequestFactory::fromGlobals();
708
709
        $this->initializeSiteWithCompatibility($siteOrId, $request);
710
        $this->initializeSiteLanguageWithCompatibility($siteLanguageOrType, $request);
711
        $pageArguments = $this->buildPageArgumentsWithFallback($pageArguments, $request);
712
        $pageArguments = $this->initializeFrontendUserOrUpdateCHashArgument($cHashOrFrontendUser, $pageArguments);
713
        $pageArguments = $this->initializeLegacyMountPointArgument($MP, $pageArguments);
714
715
        $this->setPageArguments($pageArguments);
716
717
        $this->uniqueString = md5(microtime());
718
        $this->initPageRenderer();
719
        $this->initCaches();
720
        // Initialize LLL behaviour
721
        $this->setOutputLanguage();
722
    }
723
724
    /**
725
     * Various initialize methods used for fallback, which can be simplified in TYPO3 v11.0
726
     */
727
728
    /**
729
     * Used to set $this->context. The first argument was $GLOBALS[TYPO3_CONF_VARS] (array) until TYPO3 v8,
730
     * so no type hint possible.
731
     *
732
     * @param Context|array|null $context
733
     */
734
    private function initializeContextWithGlobalFallback($context): void
735
    {
736
        if ($context instanceof Context) {
737
            $this->context = $context;
738
        } else {
739
            // Use the global context for now
740
            trigger_error('TypoScriptFrontendController requires a context object as first constructor argument in TYPO3 v11.0, now falling back to the global Context. This fallback layer will be removed in TYPO3 v11.0', E_USER_DEPRECATED);
741
            $this->context = GeneralUtility::makeInstance(Context::class);
742
        }
743
        if (!$this->context->hasAspect('frontend.preview')) {
744
            $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class));
745
        }
746
    }
747
748
    /**
749
     * Second argument of the constructor. Until TYPO3 v10, this was the Page ID (int/string) but since TYPO3 v10.0
750
     * this can also be a SiteInterface object, which will be mandatory in TYPO3 v11.0. If no Site object is given,
751
     * this is fetched from the given request object.
752
     *
753
     * @param SiteInterface|int|string $siteOrId
754
     * @param ServerRequestInterface $request
755
     */
756
    private function initializeSiteWithCompatibility($siteOrId, ServerRequestInterface $request): void
757
    {
758
        if ($siteOrId instanceof SiteInterface) {
759
            $this->site = $siteOrId;
0 ignored issues
show
Documentation Bug introduced by
$siteOrId is of type TYPO3\CMS\Core\Site\Entity\SiteInterface, but the property $site was declared to be of type TYPO3\CMS\Core\Site\Entity\Site. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
760
        } else {
761
            trigger_error('TypoScriptFrontendController should evaluate the parameter "id" by the PageArguments object, not by a separate constructor argument. This functionality will be removed in TYPO3 v11.0', E_USER_DEPRECATED);
762
            $this->id = $siteOrId;
763
            if ($request->getAttribute('site') instanceof SiteInterface) {
764
                $this->site = $request->getAttribute('site');
765
            } else {
766
                throw new \InvalidArgumentException('TypoScriptFrontendController must be constructed with a valid Site object or a resolved site in the current request as fallback. None given.', 1561583122);
767
            }
768
        }
769
    }
770
771
    /**
772
     * Until TYPO3 v10.0, the third argument of the constructor was given from GET/POST "type" to define the page type
773
     * Since TYPO3 v10.0, this argument is requested to be of type SiteLanguage, which will be mandatory in TYPO3 v11.0.
774
     * If no SiteLanguage object is given, this is fetched from the given request object.
775
     *
776
     * @param SiteLanguage|int|string $siteLanguageOrType
777
     * @param ServerRequestInterface $request
778
     */
779
    private function initializeSiteLanguageWithCompatibility($siteLanguageOrType, ServerRequestInterface $request): void
780
    {
781
        if ($siteLanguageOrType instanceof SiteLanguage) {
782
            $this->language = $siteLanguageOrType;
783
        } else {
784
            trigger_error('TypoScriptFrontendController should evaluate the parameter "type" by the PageArguments object, not by a separate constructor argument. This functionality will be removed in TYPO3 v11.0', E_USER_DEPRECATED);
785
            $this->type = $siteLanguageOrType;
0 ignored issues
show
Documentation Bug introduced by
It seems like $siteLanguageOrType can also be of type string. However, the property $type is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
786
            if ($request->getAttribute('language') instanceof SiteLanguage) {
787
                $this->language = $request->getAttribute('language');
788
            } else {
789
                throw new \InvalidArgumentException('TypoScriptFrontendController must be constructed with a valid SiteLanguage object or a resolved site in the current request as fallback. None given.', 1561583127);
790
            }
791
        }
792
    }
793
794
    /**
795
     * Since TYPO3 v10.0, the fourth constructor argument should be of type PageArguments. However, until TYPO3 v8,
796
     * this was the GET/POST parameter "no_cache". If no PageArguments object is given, the given request is checked
797
     * for the PageArguments.
798
     *
799
     * @param bool|string|PageArguments|null $pageArguments
800
     * @param ServerRequestInterface $request
801
     * @return PageArguments
802
     */
803
    private function buildPageArgumentsWithFallback($pageArguments, ServerRequestInterface $request): PageArguments
804
    {
805
        if ($pageArguments instanceof PageArguments) {
806
            return $pageArguments;
807
        }
808
        if ($request->getAttribute('routing') instanceof PageArguments) {
809
            return $request->getAttribute('routing');
810
        }
811
        trigger_error('TypoScriptFrontendController must be constructed with a valid PageArguments object or a resolved page argument in the current request as fallback. None given.', E_USER_DEPRECATED);
812
        $queryParams = $request->getQueryParams();
813
        $pageId = $this->id ?: ($queryParams['id'] ?? $request->getParsedBody()['id'] ?? 0);
814
        $pageType = $this->type ?: ($queryParams['type'] ?? $request->getParsedBody()['type'] ?? 0);
815
        return new PageArguments((int)$pageId, (string)$pageType, [], $queryParams);
816
    }
817
818
    /**
819
     * Since TYPO3 v10.0, the fifth constructor argument is expected to to be of Type FrontendUserAuthentication.
820
     * However, up until TYPO3 v9.5 this argument was used to define the "cHash" GET/POST parameter. In order to
821
     * ensure maximum compatibility, a deprecation is triggered if an old argument is still used, and PageArguments
822
     * are updated accordingly, and returned.
823
     *
824
     * @param string|FrontendUserAuthentication|null $cHashOrFrontendUser
825
     * @param PageArguments $pageArguments
826
     * @return PageArguments
827
     */
828
    private function initializeFrontendUserOrUpdateCHashArgument($cHashOrFrontendUser, PageArguments $pageArguments): PageArguments
829
    {
830
        if ($cHashOrFrontendUser === null) {
831
            return $pageArguments;
832
        }
833
        if ($cHashOrFrontendUser instanceof FrontendUserAuthentication) {
834
            $this->fe_user = $cHashOrFrontendUser;
835
            return $pageArguments;
836
        }
837
        trigger_error('TypoScriptFrontendController should evaluate the parameter "cHash" by the PageArguments object, not by a separate constructor argument. This functionality will be removed in TYPO3 v11.0', E_USER_DEPRECATED);
838
        return new PageArguments(
839
            $pageArguments->getPageId(),
840
            $pageArguments->getPageType(),
841
            $pageArguments->getRouteArguments(),
842
            array_replace_recursive($pageArguments->getStaticArguments(), ['cHash' => $cHashOrFrontendUser]),
843
            $pageArguments->getDynamicArguments()
844
        );
845
    }
846
847
    /**
848
     * Since TYPO3 v10.0 the seventh constructor argument is not needed anymore, as all data is already provided by
849
     * the given PageArguments object. However, if a specific MP parameter is given anyways, the PageArguments object
850
     * is updated and returned.
851
     *
852
     * @param string|null $MP
853
     * @param PageArguments $pageArguments
854
     * @return PageArguments
855
     */
856
    private function initializeLegacyMountPointArgument(?string $MP, PageArguments $pageArguments): PageArguments
857
    {
858
        if ($MP === null) {
859
            return $pageArguments;
860
        }
861
        trigger_error('TypoScriptFrontendController should evaluate the MountPoint Parameter "MP" by the PageArguments object, not by a separate constructor argument. This functionality will be removed in TYPO3 v11.0', E_USER_DEPRECATED);
862
        if (!$GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids']) {
863
            return $pageArguments;
864
        }
865
        return new PageArguments(
866
            $pageArguments->getPageId(),
867
            $pageArguments->getPageType(),
868
            $pageArguments->getRouteArguments(),
869
            array_replace_recursive($pageArguments->getStaticArguments(), ['MP' => $MP]),
870
            $pageArguments->getDynamicArguments()
871
        );
872
    }
873
874
    /**
875
     * Initializes the page renderer object
876
     */
877
    protected function initPageRenderer()
878
    {
879
        if ($this->pageRenderer !== null) {
880
            return;
881
        }
882
        $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
883
        $this->pageRenderer->setTemplateFile('EXT:frontend/Resources/Private/Templates/MainPage.html');
884
        // As initPageRenderer could be called in constructor and for USER_INTs, this information is only set
885
        // once - in order to not override any previous settings of PageRenderer.
886
        if ($this->language instanceof SiteLanguage) {
0 ignored issues
show
introduced by
$this->language is always a sub-type of TYPO3\CMS\Core\Site\Entity\SiteLanguage.
Loading history...
887
            $this->pageRenderer->setLanguage($this->language->getTypo3Language());
888
        }
889
    }
890
891
    /**
892
     * @param string $contentType
893
     * @internal Should only be used by TYPO3 core for now
894
     */
895
    public function setContentType($contentType)
896
    {
897
        $this->contentType = $contentType;
898
    }
899
900
    /********************************************
901
     *
902
     * Initializing, resolving page id
903
     *
904
     ********************************************/
905
    /**
906
     * Initializes the caching system.
907
     */
908
    protected function initCaches()
909
    {
910
        $this->pageCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('pages');
911
    }
912
913
    /**
914
     * Initializes the front-end user groups.
915
     * Sets frontend.user aspect based on front-end user status.
916
     */
917
    public function initUserGroups()
918
    {
919
        $userGroups = [0];
920
        // This affects the hidden-flag selecting the fe_groups for the user!
921
        $this->fe_user->showHiddenRecords = $this->context->getPropertyFromAspect('visibility', 'includeHiddenContent', false);
922
        // no matter if we have an active user we try to fetch matching groups which can be set without an user (simulation for instance!)
923
        $this->fe_user->fetchGroupData();
924
        $isUserAndGroupSet = is_array($this->fe_user->user) && !empty($this->fe_user->groupData['uid']);
925
        if ($isUserAndGroupSet) {
926
            // group -2 is not an existing group, but denotes a 'default' group when a user IS logged in.
927
            // This is used to let elements be shown for all logged in users!
928
            $userGroups[] = -2;
929
            $groupsFromUserRecord = $this->fe_user->groupData['uid'];
930
        } else {
931
            // group -1 is not an existing group, but denotes a 'default' group when not logged in.
932
            // This is used to let elements be hidden, when a user is logged in!
933
            $userGroups[] = -1;
934
            if ($this->loginAllowedInBranch) {
935
                // For cases where logins are not banned from a branch usergroups can be set based on IP masks so we should add the usergroups uids.
936
                $groupsFromUserRecord = $this->fe_user->groupData['uid'];
937
            } else {
938
                // Set to blank since we will NOT risk any groups being set when no logins are allowed!
939
                $groupsFromUserRecord = [];
940
            }
941
        }
942
        // Clean up.
943
        // Make unique and sort the groups
944
        $groupsFromUserRecord = array_unique($groupsFromUserRecord);
945
        if (!empty($groupsFromUserRecord) && !$this->loginAllowedInBranch_mode) {
946
            sort($groupsFromUserRecord);
947
            $userGroups = array_merge($userGroups, array_map('intval', $groupsFromUserRecord));
948
        }
949
950
        $this->context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $this->fe_user ?: null, $userGroups));
951
952
        // For every 60 seconds the is_online timestamp for a logged-in user is updated
953
        if ($isUserAndGroupSet) {
954
            $this->fe_user->updateOnlineTimestamp();
955
        }
956
957
        $this->logger->debug('Valid usergroups for TSFE: ' . implode(',', $userGroups));
958
    }
959
960
    /**
961
     * Checking if a user is logged in or a group constellation different from "0,-1"
962
     *
963
     * @return bool TRUE if either a login user is found (array fe_user->user) OR if the gr_list is set to something else than '0,-1' (could be done even without a user being logged in!)
964
     */
965
    public function isUserOrGroupSet()
966
    {
967
        /** @var UserAspect $userAspect */
968
        $userAspect = $this->context->getAspect('frontend.user');
969
        return $userAspect->isUserOrGroupSet();
970
    }
971
972
    /**
973
     * Clears the preview-flags, sets sim_exec_time to current time.
974
     * Hidden pages must be hidden as default, $GLOBALS['SIM_EXEC_TIME'] is set to $GLOBALS['EXEC_TIME']
975
     * in bootstrap initializeGlobalTimeVariables(). Alter it by adding or subtracting seconds.
976
     */
977
    public function clear_preview()
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::clear_preview" is not in camel caps format
Loading history...
978
    {
979
        if ($this->context->getPropertyFromAspect('frontend.preview', 'isPreview')
980
            || $GLOBALS['EXEC_TIME'] !== $GLOBALS['SIM_EXEC_TIME']
981
            || $this->context->getPropertyFromAspect('visibility', 'includeHiddenPages', false)
982
            || $this->context->getPropertyFromAspect('visibility', 'includeHiddenContent', false)
983
        ) {
984
            $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME'];
985
            $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME'];
986
            $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class));
987
            $this->context->setAspect('date', GeneralUtility::makeInstance(DateTimeAspect::class, new \DateTimeImmutable('@' . $GLOBALS['SIM_EXEC_TIME'])));
988
            $this->context->setAspect('visibility', GeneralUtility::makeInstance(VisibilityAspect::class));
989
        }
990
    }
991
992
    /**
993
     * Checks if a backend user is logged in
994
     *
995
     * @return bool whether a backend user is logged in
996
     */
997
    public function isBackendUserLoggedIn()
998
    {
999
        return (bool)$this->context->getPropertyFromAspect('backend.user', 'isLoggedIn', false);
1000
    }
1001
1002
    /**
1003
     * Determines the id and evaluates any preview settings
1004
     * Basically this function is about determining whether a backend user is logged in,
1005
     * if he has read access to the page and if he's previewing the page.
1006
     * That all determines which id to show and how to initialize the id.
1007
     */
1008
    public function determineId()
1009
    {
1010
        // Call pre processing function for id determination
1011
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PreProcessing'] ?? [] as $functionReference) {
1012
            $parameters = ['parentObject' => $this];
1013
            GeneralUtility::callUserFunction($functionReference, $parameters, $this);
1014
        }
1015
        // If there is a Backend login we are going to check for any preview settings
1016
        $originalFrontendUserGroups = $this->applyPreviewSettings($this->getBackendUser());
1017
        // If the front-end is showing a preview, caching MUST be disabled.
1018
        $isPreview = $this->context->getPropertyFromAspect('frontend.preview', 'isPreview');
1019
        if ($isPreview) {
1020
            $this->disableCache();
1021
        }
1022
        // Now, get the id, validate access etc:
1023
        $this->fetch_the_id();
1024
        // Check if backend user has read access to this page. If not, recalculate the id.
1025
        if ($this->isBackendUserLoggedIn() && $isPreview && !$this->getBackendUser()->doesUserHaveAccess($this->page, Permission::PAGE_SHOW)) {
1026
            // Resetting
1027
            $this->clear_preview();
1028
            $this->fe_user->user[$this->fe_user->usergroup_column] = $originalFrontendUserGroups;
1029
            // Fetching the id again, now with the preview settings reset.
1030
            $this->fetch_the_id();
1031
        }
1032
        // Checks if user logins are blocked for a certain branch and if so, will unset user login and re-fetch ID.
1033
        $this->loginAllowedInBranch = $this->checkIfLoginAllowedInBranch();
1034
        // Logins are not allowed, but there is a login, so will we run this.
1035
        if (!$this->loginAllowedInBranch && $this->isUserOrGroupSet()) {
1036
            if ($this->loginAllowedInBranch_mode === 'all') {
1037
                // Clear out user and group:
1038
                $this->fe_user->hideActiveLogin();
1039
                $userGroups = [0, -1];
1040
            } else {
1041
                $userGroups = [0, -2];
1042
            }
1043
            $this->context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $this->fe_user ?: null, $userGroups));
1044
            // Fetching the id again, now with the preview settings reset.
1045
            $this->fetch_the_id();
1046
        }
1047
        // Final cleaning.
1048
        // Make sure it's an integer
1049
        $this->id = ($this->contentPid = (int)$this->id);
1050
        // Make sure it's an integer
1051
        $this->type = (int)$this->type;
1052
        // Call post processing function for id determination:
1053
        $_params = ['pObj' => &$this];
1054
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc'] ?? [] as $_funcRef) {
1055
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
1056
        }
1057
    }
1058
1059
    /**
1060
     * Evaluates admin panel or workspace settings to see if
1061
     * visibility settings like
1062
     * - Preview Aspect: isPreview
1063
     * - Visibility Aspect: includeHiddenPages
1064
     * - Visibility Aspect: includeHiddenContent
1065
     * - $simUserGroup
1066
     * should be applied to the current object.
1067
     *
1068
     * @param FrontendBackendUserAuthentication $backendUser
1069
     * @return string|null null if no changes to the current frontend usergroups have been made, otherwise the original list of frontend usergroups
1070
     * @internal
1071
     */
1072
    protected function applyPreviewSettings($backendUser = null)
1073
    {
1074
        if (!$backendUser) {
1075
            return null;
1076
        }
1077
        $originalFrontendUserGroup = null;
1078
        if ($this->fe_user->user) {
1079
            $originalFrontendUserGroup = $this->context->getPropertyFromAspect('frontend.user', 'groupIds');
1080
        }
1081
1082
        // The preview flag is set if the current page turns out to be hidden
1083
        if ($this->id && $this->determineIdIsHiddenPage()) {
1084
            $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class, true));
1085
            /** @var VisibilityAspect $aspect */
1086
            $aspect = $this->context->getAspect('visibility');
1087
            $newAspect = GeneralUtility::makeInstance(VisibilityAspect::class, true, $aspect->includeHiddenContent(), $aspect->includeDeletedRecords());
1088
            $this->context->setAspect('visibility', $newAspect);
1089
        }
1090
        // The preview flag will be set if an offline workspace will be previewed
1091
        if ($this->whichWorkspace() > 0) {
1092
            $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class, true));
1093
        }
1094
        return $this->context->getPropertyFromAspect('frontend.preview', 'preview', false) ? $originalFrontendUserGroup : null;
1095
    }
1096
1097
    /**
1098
     * Checks if the page is hidden in the active workspace.
1099
     * If it is hidden, preview flags will be set.
1100
     *
1101
     * @return bool
1102
     */
1103
    protected function determineIdIsHiddenPage()
1104
    {
1105
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1106
            ->getQueryBuilderForTable('pages');
1107
        $queryBuilder
1108
            ->getRestrictions()
1109
            ->removeAll()
1110
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1111
1112
        $queryBuilder
1113
            ->select('uid', 'hidden', 'starttime', 'endtime')
1114
            ->from('pages')
1115
            ->where(
1116
                $queryBuilder->expr()->gte('pid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
1117
            )
1118
            ->setMaxResults(1);
1119
1120
        // $this->id always points to the ID of the default language page, so we check
1121
        // the current site language to determine if we need to fetch a translation but consider fallbacks
1122
        if ($this->language->getLanguageId() > 0) {
1123
            $languagesToCheck = array_merge([$this->language->getLanguageId()], $this->language->getFallbackLanguageIds());
1124
            // Check for the language and all its fallbacks
1125
            $constraint = $queryBuilder->expr()->andX(
1126
                $queryBuilder->expr()->eq('l10n_parent', $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)),
1127
                $queryBuilder->expr()->in('sys_language_uid', $queryBuilder->createNamedParameter(array_filter($languagesToCheck), Connection::PARAM_INT_ARRAY))
1128
            );
1129
            // If the fallback language Ids also contains the default language, this needs to be considered
1130
            if (in_array(0, $languagesToCheck, true)) {
1131
                $constraint = $queryBuilder->expr()->orX(
1132
                    $constraint,
1133
                    // Ensure to also fetch the default record
1134
                    $queryBuilder->expr()->andX(
1135
                        $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)),
1136
                        $queryBuilder->expr()->in('sys_language_uid', 0)
1137
                    )
1138
                );
1139
            }
1140
            // Ensure that the translated records are shown first (maxResults is set to 1)
1141
            $queryBuilder->orderBy('sys_language_uid', 'DESC');
1142
        } else {
1143
            $constraint = $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT));
1144
        }
1145
        $queryBuilder->andWhere($constraint);
1146
1147
        $page = $queryBuilder->execute()->fetch();
1148
1149
        if ($this->whichWorkspace() > 0) {
1150
            // Fetch overlay of page if in workspace and check if it is hidden
1151
            $customContext = clone $this->context;
1152
            $customContext->setAspect('workspace', GeneralUtility::makeInstance(WorkspaceAspect::class, $this->whichWorkspace()));
1153
            $customContext->setAspect('visibility', GeneralUtility::makeInstance(VisibilityAspect::class));
1154
            $pageSelectObject = GeneralUtility::makeInstance(PageRepository::class, $customContext);
1155
            $targetPage = $pageSelectObject->getWorkspaceVersionOfRecord($this->whichWorkspace(), 'pages', $page['uid']);
1156
            $result = $targetPage === -1 || $targetPage === -2;
1157
        } else {
1158
            $result = is_array($page) && ($page['hidden'] || $page['starttime'] > $GLOBALS['SIM_EXEC_TIME'] || $page['endtime'] != 0 && $page['endtime'] <= $GLOBALS['SIM_EXEC_TIME']);
1159
        }
1160
        return $result;
1161
    }
1162
1163
    /**
1164
     * Resolves the page id and sets up several related properties.
1165
     *
1166
     * If $this->id is not set at all or is not a plain integer, the method
1167
     * does it's best to set the value to an integer. Resolving is based on
1168
     * this options:
1169
     *
1170
     * - Splitting $this->id if it contains an additional type parameter.
1171
     * - Finding the domain record start page
1172
     * - First visible page
1173
     * - Relocating the id below the domain record if outside
1174
     *
1175
     * The following properties may be set up or updated:
1176
     *
1177
     * - id
1178
     * - requestedId
1179
     * - type
1180
     * - sys_page
1181
     * - sys_page->where_groupAccess
1182
     * - sys_page->where_hid_del
1183
     * - Context: FrontendUser Aspect
1184
     * - no_cache
1185
     * - register['SYS_LASTCHANGED']
1186
     * - pageNotFound
1187
     *
1188
     * Via getPageAndRootlineWithDomain()
1189
     *
1190
     * - rootLine
1191
     * - page
1192
     * - MP
1193
     * - originalShortcutPage
1194
     * - originalMountPointPage
1195
     * - pageAccessFailureHistory['direct_access']
1196
     * - pageNotFound
1197
     *
1198
     * @todo:
1199
     *
1200
     * On the first impression the method does to much. This is increased by
1201
     * the fact, that is is called repeated times by the method determineId.
1202
     * The reasons are manifold.
1203
     *
1204
     * 1.) The first part, the creation of sys_page and the type
1205
     * resolution don't need to be repeated. They could be separated to be
1206
     * called only once.
1207
     *
1208
     * 2.) The user group setup could be done once on a higher level.
1209
     *
1210
     * 3.) The workflow of the resolution could be elaborated to be less
1211
     * tangled. Maybe the check of the page id to be below the domain via the
1212
     * root line doesn't need to be done each time, but for the final result
1213
     * only.
1214
     *
1215
     * 4.) The root line does not need to be directly addressed by this class.
1216
     * A root line is always related to one page. The rootline could be handled
1217
     * indirectly by page objects. Page objects still don't exist.
1218
     *
1219
     * @throws ServiceUnavailableException
1220
     * @internal
1221
     */
1222
    public function fetch_the_id()
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::fetch_the_id" is not in camel caps format
Loading history...
1223
    {
1224
        $timeTracker = $this->getTimeTracker();
1225
        $timeTracker->push('fetch_the_id initialize/');
1226
        // Set the valid usergroups for FE
1227
        $this->initUserGroups();
1228
        // Initialize the PageRepository has to be done after the frontend usergroups are initialized / resolved, as
1229
        // frontend group aspect is modified before
1230
        $this->sys_page = GeneralUtility::makeInstance(PageRepository::class, $this->context);
1231
        // The id and type is set to the integer-value - just to be sure...
1232
        $this->id = (int)$this->id;
1233
        $this->type = (int)$this->type;
1234
        $timeTracker->pull();
1235
        // We find the first page belonging to the current domain
1236
        $timeTracker->push('fetch_the_id domain/');
1237
        if (!$this->id) {
1238
            // If the id was not previously set, set it to the root page id of the site.
1239
            $this->id = $this->site->getRootPageId();
1240
        }
1241
        $timeTracker->pull();
1242
        $timeTracker->push('fetch_the_id rootLine/');
1243
        // We store the originally requested id
1244
        $this->requestedId = $this->id;
1245
        try {
1246
            $this->getPageAndRootlineWithDomain($this->site->getRootPageId());
1247
        } catch (ShortcutTargetPageNotFoundException $e) {
1248
            $this->pageNotFound = 1;
1249
        }
1250
        $timeTracker->pull();
1251
        if ($this->pageNotFound) {
1252
            switch ($this->pageNotFound) {
1253
                case 1:
1254
                    $response = GeneralUtility::makeInstance(ErrorController::class)->accessDeniedAction(
1255
                        $GLOBALS['TYPO3_REQUEST'],
1256
                        'ID was not an accessible page',
1257
                        $this->getPageAccessFailureReasons(PageAccessFailureReasons::ACCESS_DENIED_PAGE_NOT_RESOLVED)
1258
                    );
1259
                    break;
1260
                case 2:
1261
                    $response = GeneralUtility::makeInstance(ErrorController::class)->accessDeniedAction(
1262
                        $GLOBALS['TYPO3_REQUEST'],
1263
                        'Subsection was found and not accessible',
1264
                        $this->getPageAccessFailureReasons(PageAccessFailureReasons::ACCESS_DENIED_SUBSECTION_NOT_RESOLVED)
1265
                    );
1266
                    break;
1267
                case 3:
1268
                    $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
1269
                        $GLOBALS['TYPO3_REQUEST'],
1270
                        'ID was outside the domain',
1271
                        $this->getPageAccessFailureReasons(PageAccessFailureReasons::ACCESS_DENIED_HOST_PAGE_MISMATCH)
1272
                    );
1273
                    break;
1274
                default:
1275
                    $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
1276
                        $GLOBALS['TYPO3_REQUEST'],
1277
                        'Unspecified error',
1278
                        $this->getPageAccessFailureReasons()
1279
                    );
1280
            }
1281
            throw new ImmediateResponseException($response, 1533931329);
1282
        }
1283
1284
        $this->setRegisterValueForSysLastChanged($this->page);
1285
1286
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['fetchPageId-PostProcessing'] ?? [] as $functionReference) {
1287
            $parameters = ['parentObject' => $this];
1288
            GeneralUtility::callUserFunction($functionReference, $parameters, $this);
1289
        }
1290
    }
1291
1292
    /**
1293
     * Loads the page and root line records based on $this->id
1294
     *
1295
     * A final page and the matching root line are determined and loaded by
1296
     * the algorithm defined by this method.
1297
     *
1298
     * First it loads the initial page from the page repository for $this->id.
1299
     * If that can't be loaded directly, it gets the root line for $this->id.
1300
     * It walks up the root line towards the root page until the page
1301
     * repository can deliver a page record. (The loading restrictions of
1302
     * the root line records are more liberal than that of the page record.)
1303
     *
1304
     * Now the page type is evaluated and handled if necessary. If the page is
1305
     * a short cut, it is replaced by the target page. If the page is a mount
1306
     * point in overlay mode, the page is replaced by the mounted page.
1307
     *
1308
     * After this potential replacements are done, the root line is loaded
1309
     * (again) for this page record. It walks up the root line up to
1310
     * the first viewable record.
1311
     *
1312
     * (While upon the first accessibility check of the root line it was done
1313
     * by loading page by page from the page repository, this time the method
1314
     * checkRootlineForIncludeSection() is used to find the most distant
1315
     * accessible page within the root line.)
1316
     *
1317
     * Having found the final page id, the page record and the root line are
1318
     * loaded for last time by this method.
1319
     *
1320
     * Exceptions may be thrown for DOKTYPE_SPACER and not loadable page records
1321
     * or root lines.
1322
     *
1323
     * May set or update this properties:
1324
     *
1325
     * @see TypoScriptFrontendController::$id
1326
     * @see TypoScriptFrontendController::$MP
1327
     * @see TypoScriptFrontendController::$page
1328
     * @see TypoScriptFrontendController::$pageNotFound
1329
     * @see TypoScriptFrontendController::$pageAccessFailureHistory
1330
     * @see TypoScriptFrontendController::$originalMountPointPage
1331
     * @see TypoScriptFrontendController::$originalShortcutPage
1332
     *
1333
     * @throws ServiceUnavailableException
1334
     * @throws PageNotFoundException
1335
     */
1336
    protected function getPageAndRootline()
1337
    {
1338
        $requestedPageRowWithoutGroupCheck = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $requestedPageRowWithoutGroupCheck is dead and can be removed.
Loading history...
1339
        $this->resolveTranslatedPageId();
1340
        if (empty($this->page)) {
1341
            // If no page, we try to find the page before in the rootLine.
1342
            // Page is 'not found' in case the id itself was not an accessible page. code 1
1343
            $this->pageNotFound = 1;
1344
            try {
1345
                $requestedPageRowWithoutGroupCheck = $this->sys_page->getPage($this->id, true);
1346
                if (!empty($requestedPageRowWithoutGroupCheck)) {
1347
                    $this->pageAccessFailureHistory['direct_access'][] = $requestedPageRowWithoutGroupCheck;
1348
                }
1349
                $this->rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $this->id, $this->MP, $this->context)->get();
1350
                if (!empty($this->rootLine)) {
1351
                    $c = count($this->rootLine) - 1;
1352
                    while ($c > 0) {
1353
                        // Add to page access failure history:
1354
                        $this->pageAccessFailureHistory['direct_access'][] = $this->rootLine[$c];
1355
                        // Decrease to next page in rootline and check the access to that, if OK, set as page record and ID value.
1356
                        $c--;
1357
                        $this->id = $this->rootLine[$c]['uid'];
1358
                        $this->page = $this->sys_page->getPage($this->id);
1359
                        if (!empty($this->page)) {
1360
                            break;
1361
                        }
1362
                    }
1363
                }
1364
            } catch (RootLineException $e) {
1365
                $this->rootLine = [];
1366
            }
1367
            // If still no page...
1368
            if (empty($requestedPageRowWithoutGroupCheck) && empty($this->page)) {
1369
                $message = 'The requested page does not exist!';
1370
                $this->logger->error($message);
1371
                try {
1372
                    $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
1373
                        $GLOBALS['TYPO3_REQUEST'],
1374
                        $message,
1375
                        $this->getPageAccessFailureReasons(PageAccessFailureReasons::PAGE_NOT_FOUND)
1376
                    );
1377
                    throw new ImmediateResponseException($response, 1533931330);
1378
                } catch (PageNotFoundException $e) {
1379
                    throw new PageNotFoundException($message, 1301648780);
1380
                }
1381
            }
1382
        }
1383
        // Spacer is not accessible in frontend
1384
        if ($this->page['doktype'] == PageRepository::DOKTYPE_SPACER) {
1385
            $message = 'The requested page does not exist!';
1386
            $this->logger->error($message);
1387
            try {
1388
                $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
1389
                    $GLOBALS['TYPO3_REQUEST'],
1390
                    $message,
1391
                    $this->getPageAccessFailureReasons(PageAccessFailureReasons::ACCESS_DENIED_INVALID_PAGETYPE)
1392
                );
1393
                throw new ImmediateResponseException($response, 1533931343);
1394
            } catch (PageNotFoundException $e) {
1395
                throw new PageNotFoundException($message, 1301648781);
1396
            }
1397
        }
1398
        // Is the ID a link to another page??
1399
        if ($this->page['doktype'] == PageRepository::DOKTYPE_SHORTCUT) {
1400
            // We need to clear MP if the page is a shortcut. Reason is if the short cut goes to another page, then we LEAVE the rootline which the MP expects.
1401
            $this->MP = '';
1402
            // saving the page so that we can check later - when we know
1403
            // about languages - whether we took the correct shortcut or
1404
            // whether a translation of the page overwrites the shortcut
1405
            // target and we need to follow the new target
1406
            $this->originalShortcutPage = $this->page;
1407
            $this->page = $this->sys_page->getPageShortcut($this->page['shortcut'], $this->page['shortcut_mode'], $this->page['uid']);
1408
            $this->id = $this->page['uid'];
1409
        }
1410
        // If the page is a mountpoint which should be overlaid with the contents of the mounted page,
1411
        // it must never be accessible directly, but only in the mountpoint context. Therefore we change
1412
        // the current ID and the user is redirected by checkPageForMountpointRedirect().
1413
        if ($this->page['doktype'] == PageRepository::DOKTYPE_MOUNTPOINT && $this->page['mount_pid_ol']) {
1414
            $this->originalMountPointPage = $this->page;
1415
            $this->page = $this->sys_page->getPage($this->page['mount_pid']);
1416
            if (empty($this->page)) {
1417
                $message = 'This page (ID ' . $this->originalMountPointPage['uid'] . ') is of type "Mount point" and '
1418
                    . 'mounts a page which is not accessible (ID ' . $this->originalMountPointPage['mount_pid'] . ').';
1419
                throw new PageNotFoundException($message, 1402043263);
1420
            }
1421
            // If the current page is a shortcut, the MP parameter will be replaced
1422
            if ($this->MP === '' || !empty($this->originalShortcutPage)) {
1423
                $this->MP = $this->page['uid'] . '-' . $this->originalMountPointPage['uid'];
1424
            } else {
1425
                $this->MP .= ',' . $this->page['uid'] . '-' . $this->originalMountPointPage['uid'];
1426
            }
1427
            $this->id = $this->page['uid'];
1428
        }
1429
        // Gets the rootLine
1430
        try {
1431
            $this->rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $this->id, $this->MP, $this->context)->get();
1432
        } catch (RootLineException $e) {
1433
            $this->rootLine = [];
1434
        }
1435
        // If not rootline we're off...
1436
        if (empty($this->rootLine)) {
1437
            $message = 'The requested page didn\'t have a proper connection to the tree-root!';
1438
            $this->logger->error($message);
1439
            try {
1440
                $response = GeneralUtility::makeInstance(ErrorController::class)->unavailableAction(
1441
                    $GLOBALS['TYPO3_REQUEST'],
1442
                    $message,
1443
                    $this->getPageAccessFailureReasons(PageAccessFailureReasons::ROOTLINE_BROKEN)
1444
                );
1445
                throw new ImmediateResponseException($response, 1533931350);
1446
            } catch (ServiceUnavailableException $e) {
1447
                throw new ServiceUnavailableException($message, 1301648167);
1448
            }
1449
        }
1450
        // Checking for include section regarding the hidden/starttime/endtime/fe_user (that is access control of a whole subbranch!)
1451
        if ($this->checkRootlineForIncludeSection()) {
1452
            if (empty($this->rootLine)) {
1453
                $message = 'The requested page was not accessible!';
1454
                try {
1455
                    $response = GeneralUtility::makeInstance(ErrorController::class)->unavailableAction(
1456
                        $GLOBALS['TYPO3_REQUEST'],
1457
                        $message,
1458
                        $this->getPageAccessFailureReasons(PageAccessFailureReasons::ACCESS_DENIED_GENERAL)
1459
                    );
1460
                    throw new ImmediateResponseException($response, 1533931351);
1461
                } catch (ServiceUnavailableException $e) {
1462
                    $this->logger->warning($message);
1463
                    throw new ServiceUnavailableException($message, 1301648234);
1464
                }
1465
            } else {
1466
                $el = reset($this->rootLine);
1467
                $this->id = $el['uid'];
1468
                $this->page = $this->sys_page->getPage($this->id);
1469
                try {
1470
                    $this->rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $this->id, $this->MP, $this->context)->get();
1471
                } catch (RootLineException $e) {
1472
                    $this->rootLine = [];
1473
                }
1474
            }
1475
        }
1476
    }
1477
1478
    /**
1479
     * If $this->id contains a translated page record, this needs to be resolved to the default language
1480
     * in order for all rootline functionality and access restrictions to be in place further on.
1481
     *
1482
     * Additionally, if a translated page is found, LanguageAspect is set as well.
1483
     */
1484
    protected function resolveTranslatedPageId()
1485
    {
1486
        $this->page = $this->sys_page->getPage($this->id);
0 ignored issues
show
Bug introduced by
$this->id of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Core\Domain\Re...geRepository::getPage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1486
        $this->page = $this->sys_page->getPage(/** @scrutinizer ignore-type */ $this->id);
Loading history...
1487
        // Accessed a default language page record, nothing to resolve
1488
        if (empty($this->page) || (int)$this->page[$GLOBALS['TCA']['pages']['ctrl']['languageField']] === 0) {
1489
            return;
1490
        }
1491
        $languageId = (int)$this->page[$GLOBALS['TCA']['pages']['ctrl']['languageField']];
1492
        $this->page = $this->sys_page->getPage($this->page[$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField']]);
1493
        $this->context->setAspect('language', GeneralUtility::makeInstance(LanguageAspect::class, $languageId));
1494
        $this->id = $this->page['uid'];
1495
    }
1496
1497
    /**
1498
     * Checks if visibility of the page is blocked upwards in the root line.
1499
     *
1500
     * If any page in the root line is blocking visibility, true is returned.
1501
     *
1502
     * All pages from the blocking page downwards are removed from the root
1503
     * line, so that the remaining pages can be used to relocate the page up
1504
     * to lowest visible page.
1505
     *
1506
     * The blocking feature of a page must be turned on by setting the page
1507
     * record field 'extendToSubpages' to 1 in case of hidden, starttime,
1508
     * endtime or fe_group restrictions.
1509
     *
1510
     * Additionally this method checks for backend user sections in root line
1511
     * and if found evaluates if a backend user is logged in and has access.
1512
     *
1513
     * Recyclers are also checked and trigger page not found if found in root
1514
     * line.
1515
     *
1516
     * @todo Find a better name, i.e. checkVisibilityByRootLine
1517
     * @todo Invert boolean return value. Return true if visible.
1518
     *
1519
     * @return bool
1520
     */
1521
    protected function checkRootlineForIncludeSection(): bool
1522
    {
1523
        $c = count($this->rootLine);
1524
        $removeTheRestFlag = false;
1525
        for ($a = 0; $a < $c; $a++) {
1526
            if (!$this->checkPagerecordForIncludeSection($this->rootLine[$a])) {
1527
                // Add to page access failure history and mark the page as not found
1528
                // Keep the rootline however to trigger an access denied error instead of a service unavailable error
1529
                $this->pageAccessFailureHistory['sub_section'][] = $this->rootLine[$a];
1530
                $this->pageNotFound = 2;
1531
            }
1532
1533
            if ((int)$this->rootLine[$a]['doktype'] === PageRepository::DOKTYPE_BE_USER_SECTION) {
1534
                // If there is a backend user logged in, check if they have read access to the page:
1535
                if ($this->isBackendUserLoggedIn()) {
1536
                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1537
                        ->getQueryBuilderForTable('pages');
1538
1539
                    $queryBuilder
1540
                        ->getRestrictions()
1541
                        ->removeAll();
1542
1543
                    $row = $queryBuilder
1544
                        ->select('uid')
1545
                        ->from('pages')
1546
                        ->where(
1547
                            $queryBuilder->expr()->eq(
1548
                                'uid',
1549
                                $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
1550
                            ),
1551
                            $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)
1552
                        )
1553
                        ->execute()
1554
                        ->fetch();
1555
1556
                    // versionOL()?
1557
                    if (!$row) {
1558
                        // If there was no page selected, the user apparently did not have read access to the current PAGE (not position in rootline) and we set the remove-flag...
1559
                        $removeTheRestFlag = true;
1560
                    }
1561
                } else {
1562
                    // Don't go here, if there is no backend user logged in.
1563
                    $removeTheRestFlag = true;
1564
                }
1565
            } elseif ((int)$this->rootLine[$a]['doktype'] === PageRepository::DOKTYPE_RECYCLER) {
1566
                // page is in a recycler
1567
                $removeTheRestFlag = true;
1568
            }
1569
            if ($removeTheRestFlag) {
1570
                // Page is 'not found' in case a subsection was found and not accessible, code 2
1571
                $this->pageNotFound = 2;
1572
                unset($this->rootLine[$a]);
1573
            }
1574
        }
1575
        return $removeTheRestFlag;
1576
    }
1577
1578
    /**
1579
     * Checks page record for enableFields
1580
     * Returns TRUE if enableFields does not disable the page record.
1581
     * Takes notice of the includeHiddenPages visibility aspect flag and uses SIM_ACCESS_TIME for start/endtime evaluation
1582
     *
1583
     * @param array $row The page record to evaluate (needs fields: hidden, starttime, endtime, fe_group)
1584
     * @param bool $bypassGroupCheck Bypass group-check
1585
     * @return bool TRUE, if record is viewable.
1586
     * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::getTreeList()
1587
     * @see checkPagerecordForIncludeSection()
1588
     */
1589
    public function checkEnableFields($row, $bypassGroupCheck = false)
1590
    {
1591
        $_params = ['pObj' => $this, 'row' => &$row, 'bypassGroupCheck' => &$bypassGroupCheck];
1592
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_checkEnableFields'] ?? [] as $_funcRef) {
1593
            // Call hooks: If one returns FALSE, method execution is aborted with result "This record is not available"
1594
            $return = GeneralUtility::callUserFunction($_funcRef, $_params, $this);
1595
            if ($return === false) {
1596
                return false;
1597
            }
1598
        }
1599
        if ((!$row['hidden'] || $this->context->getPropertyFromAspect('visibility', 'includeHiddenPages', false))
1600
            && $row['starttime'] <= $GLOBALS['SIM_ACCESS_TIME']
1601
            && ($row['endtime'] == 0 || $row['endtime'] > $GLOBALS['SIM_ACCESS_TIME'])
1602
            && ($bypassGroupCheck || $this->checkPageGroupAccess($row))) {
1603
            return true;
1604
        }
1605
        return false;
1606
    }
1607
1608
    /**
1609
     * Check group access against a page record
1610
     *
1611
     * @param array $row The page record to evaluate (needs field: fe_group)
1612
     * @return bool TRUE, if group access is granted.
1613
     * @internal
1614
     */
1615
    public function checkPageGroupAccess($row)
1616
    {
1617
        /** @var UserAspect $userAspect */
1618
        $userAspect = $this->context->getAspect('frontend.user');
1619
        $pageGroupList = explode(',', $row['fe_group'] ?: 0);
1620
        return count(array_intersect($userAspect->getGroupIds(), $pageGroupList)) > 0;
1621
    }
1622
1623
    /**
1624
     * Checks if the current page of the root line is visible.
1625
     *
1626
     * If the field extendToSubpages is 0, access is granted,
1627
     * else the fields hidden, starttime, endtime, fe_group are evaluated.
1628
     *
1629
     * @todo Find a better name, i.e. isVisibleRecord()
1630
     *
1631
     * @param array $row The page record
1632
     * @return bool true if visible
1633
     * @internal
1634
     * @see checkEnableFields()
1635
     * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::getTreeList()
1636
     * @see checkRootlineForIncludeSection()
1637
     */
1638
    public function checkPagerecordForIncludeSection(array $row): bool
1639
    {
1640
        return !$row['extendToSubpages'] || $this->checkEnableFields($row);
1641
    }
1642
1643
    /**
1644
     * Checks if logins are allowed in the current branch of the page tree. Traverses the full root line and returns TRUE if logins are OK, otherwise FALSE (and then the login user must be unset!)
1645
     *
1646
     * @return bool returns TRUE if logins are OK, otherwise FALSE (and then the login user must be unset!)
1647
     */
1648
    public function checkIfLoginAllowedInBranch()
1649
    {
1650
        // Initialize:
1651
        $c = count($this->rootLine);
1652
        $loginAllowed = true;
1653
        // Traverse root line from root and outwards:
1654
        for ($a = 0; $a < $c; $a++) {
1655
            // If a value is set for login state:
1656
            if ($this->rootLine[$a]['fe_login_mode'] > 0) {
1657
                // Determine state from value:
1658
                if ((int)$this->rootLine[$a]['fe_login_mode'] === 1) {
1659
                    $loginAllowed = false;
1660
                    $this->loginAllowedInBranch_mode = 'all';
1661
                } elseif ((int)$this->rootLine[$a]['fe_login_mode'] === 3) {
1662
                    $loginAllowed = false;
1663
                    $this->loginAllowedInBranch_mode = 'groups';
1664
                } else {
1665
                    $loginAllowed = true;
1666
                }
1667
            }
1668
        }
1669
        return $loginAllowed;
1670
    }
1671
1672
    /**
1673
     * Analysing $this->pageAccessFailureHistory into a summary array telling which features disabled display and on which pages and conditions. That data can be used inside a page-not-found handler
1674
     *
1675
     * @param string $failureReasonCode the error code to be attached (optional), see PageAccessFailureReasons list for details
1676
     * @return array Summary of why page access was not allowed.
1677
     */
1678
    public function getPageAccessFailureReasons(string $failureReasonCode = null)
1679
    {
1680
        $output = [];
1681
        if ($failureReasonCode) {
1682
            $output['code'] = $failureReasonCode;
1683
        }
1684
        $combinedRecords = array_merge(is_array($this->pageAccessFailureHistory['direct_access']) ? $this->pageAccessFailureHistory['direct_access'] : [['fe_group' => 0]], is_array($this->pageAccessFailureHistory['sub_section']) ? $this->pageAccessFailureHistory['sub_section'] : []);
1685
        if (!empty($combinedRecords)) {
1686
            foreach ($combinedRecords as $k => $pagerec) {
1687
                // If $k=0 then it is the very first page the original ID was pointing at and that will get a full check of course
1688
                // If $k>0 it is parent pages being tested. They are only significant for the access to the first page IF they had the extendToSubpages flag set, hence checked only then!
1689
                if (!$k || $pagerec['extendToSubpages']) {
1690
                    if ($pagerec['hidden']) {
1691
                        $output['hidden'][$pagerec['uid']] = true;
1692
                    }
1693
                    if ($pagerec['starttime'] > $GLOBALS['SIM_ACCESS_TIME']) {
1694
                        $output['starttime'][$pagerec['uid']] = $pagerec['starttime'];
1695
                    }
1696
                    if ($pagerec['endtime'] != 0 && $pagerec['endtime'] <= $GLOBALS['SIM_ACCESS_TIME']) {
1697
                        $output['endtime'][$pagerec['uid']] = $pagerec['endtime'];
1698
                    }
1699
                    if (!$this->checkPageGroupAccess($pagerec)) {
1700
                        $output['fe_group'][$pagerec['uid']] = $pagerec['fe_group'];
1701
                    }
1702
                }
1703
            }
1704
        }
1705
        return $output;
1706
    }
1707
1708
    /**
1709
     * Gets ->page and ->rootline information based on ->id. ->id may change during this operation.
1710
     * If not inside a site, then default to first page in site.
1711
     *
1712
     * @param int $rootPageId Page uid of the page where the found site is located
1713
     * @internal
1714
     */
1715
    public function getPageAndRootlineWithDomain($rootPageId)
1716
    {
1717
        $this->getPageAndRootline();
1718
        // Checks if the $domain-startpage is in the rootLine. This is necessary so that references to page-id's via ?id=123 from other sites are not possible.
1719
        if (is_array($this->rootLine) && $this->rootLine !== []) {
1720
            $idFound = false;
1721
            foreach ($this->rootLine as $key => $val) {
1722
                if ($val['uid'] == $rootPageId) {
1723
                    $idFound = true;
1724
                    break;
1725
                }
1726
            }
1727
            if (!$idFound) {
1728
                // Page is 'not found' in case the id was outside the domain, code 3
1729
                $this->pageNotFound = 3;
1730
                $this->id = $rootPageId;
1731
                // re-get the page and rootline if the id was not found.
1732
                $this->getPageAndRootline();
1733
            }
1734
        }
1735
    }
1736
1737
    /********************************************
1738
     *
1739
     * Template and caching related functions.
1740
     *
1741
     *******************************************/
1742
1743
    /**
1744
     * Will disable caching if the cHash value was not set when having dynamic arguments in GET query parameters.
1745
     * This function should be called to check the _existence_ of "&cHash" whenever a plugin generating cacheable output is using extra GET variables. If there _is_ a cHash value the validation of it automatically takes place in makeCacheHash() (see above)
1746
     *
1747
     * @deprecated since TYPO3 v10.2, will be removed in TYPO3 v11. The PSR-15 middleware PageArgumentValidator is already taking care of this.
1748
     */
1749
    public function reqCHash()
1750
    {
1751
        trigger_error('TypoScriptFrontendController->reqCHash() is not needed anymore, as all functionality is handled via the PSR-15 PageArgumentValidator middleware already.', E_USER_DEPRECATED);
1752
        if (!empty($this->pageArguments->getArguments()['cHash']) || empty($this->pageArguments->getDynamicArguments())) {
1753
            return;
1754
        }
1755
        $queryParams = $this->pageArguments->getDynamicArguments();
1756
        $queryParams['id'] = $this->pageArguments->getPageId();
1757
        $argumentsThatWouldRequireCacheHash = GeneralUtility::makeInstance(CacheHashCalculator::class)
1758
                ->getRelevantParameters(HttpUtility::buildQueryString($queryParams));
1759
        if (empty($argumentsThatWouldRequireCacheHash)) {
1760
            return;
1761
        }
1762
        if ($GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError']) {
1763
            $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
1764
                $GLOBALS['TYPO3_REQUEST'],
1765
                'Request parameters could not be validated (&cHash empty)',
1766
                ['code' => PageAccessFailureReasons::CACHEHASH_EMPTY]
1767
            );
1768
            throw new ImmediateResponseException($response, 1533931354);
1769
        }
1770
        $this->disableCache();
1771
        $this->getTimeTracker()->setTSlogMessage('TSFE->reqCHash(): No &cHash parameter was sent for GET vars though required so caching is disabled', 2);
1772
    }
1773
1774
    protected function setPageArguments(PageArguments $pageArguments): void
1775
    {
1776
        $this->pageArguments = $pageArguments;
1777
        $this->id = $pageArguments->getPageId();
1778
        $this->type = $pageArguments->getPageType() ?: 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pageArguments->getPageType() ?: 0 can also be of type string. However, the property $type is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
1779
        if ($GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids']) {
1780
            $this->MP = (string)($pageArguments->getArguments()['MP'] ?? '');
1781
        }
1782
    }
1783
1784
    /**
1785
     * Fetches the arguments that are relevant for creating the hash base from the given PageArguments object.
1786
     * Excluded parameters are not taken into account when calculating the hash base.
1787
     *
1788
     * @param PageArguments $pageArguments
1789
     * @return array
1790
     */
1791
    protected function getRelevantParametersForCachingFromPageArguments(PageArguments $pageArguments): array
1792
    {
1793
        $queryParams = $pageArguments->getDynamicArguments();
1794
        if (!empty($queryParams) && $pageArguments->getArguments()['cHash'] ?? false) {
1795
            $queryParams['id'] = $pageArguments->getPageId();
1796
            return GeneralUtility::makeInstance(CacheHashCalculator::class)
1797
                ->getRelevantParameters(HttpUtility::buildQueryString($queryParams));
1798
        }
1799
        return [];
1800
    }
1801
1802
    /**
1803
     * See if page is in cache and get it if so
1804
     * Stores the page content in $this->content if something is found.
1805
     *
1806
     * @throws \InvalidArgumentException
1807
     * @throws \RuntimeException
1808
     */
1809
    public function getFromCache()
1810
    {
1811
        // clearing the content-variable, which will hold the pagecontent
1812
        $this->content = '';
1813
        // Unsetting the lowlevel config
1814
        $this->config = [];
1815
        $this->cacheContentFlag = false;
1816
1817
        if ($this->no_cache) {
1818
            return;
1819
        }
1820
1821
        if (!$this->tmpl instanceof TemplateService) {
0 ignored issues
show
introduced by
$this->tmpl is always a sub-type of TYPO3\CMS\Core\TypoScript\TemplateService.
Loading history...
1822
            $this->tmpl = GeneralUtility::makeInstance(TemplateService::class, $this->context, null, $this);
1823
        }
1824
1825
        $pageSectionCacheContent = $this->tmpl->getCurrentPageData((int)$this->id, (string)$this->MP);
1826
        if (!is_array($pageSectionCacheContent)) {
0 ignored issues
show
introduced by
The condition is_array($pageSectionCacheContent) is always true.
Loading history...
1827
            // Nothing in the cache, we acquire an "exclusive lock" for the key now.
1828
            // We use the Registry to store this lock centrally,
1829
            // but we protect the access again with a global exclusive lock to avoid race conditions
1830
1831
            $this->acquireLock('pagesection', $this->id . '::' . $this->MP);
1832
            //
1833
            // from this point on we're the only one working on that page ($key)
1834
            //
1835
1836
            // query the cache again to see if the page data are there meanwhile
1837
            $pageSectionCacheContent = $this->tmpl->getCurrentPageData((int)$this->id, (string)$this->MP);
1838
            if (is_array($pageSectionCacheContent)) {
1839
                // we have the content, nice that some other process did the work for us already
1840
                $this->releaseLock('pagesection');
1841
            }
1842
            // We keep the lock set, because we are the ones generating the page now and filling the cache.
1843
            // This indicates that we have to release the lock later in releaseLocks()
1844
        }
1845
1846
        if (is_array($pageSectionCacheContent)) {
0 ignored issues
show
introduced by
The condition is_array($pageSectionCacheContent) is always true.
Loading history...
1847
            // BE CAREFUL to change the content of the cc-array. This array is serialized and an md5-hash based on this is used for caching the page.
1848
            // If this hash is not the same in here in this section and after page-generation, then the page will not be properly cached!
1849
            // This array is an identification of the template. If $this->all is empty it's because the template-data is not cached, which it must be.
1850
            $pageSectionCacheContent = $this->tmpl->matching($pageSectionCacheContent);
1851
            ksort($pageSectionCacheContent);
1852
            $this->all = $pageSectionCacheContent;
1853
        }
1854
1855
        // Look for page in cache only if a shift-reload is not sent to the server.
1856
        $lockHash = $this->getLockHash();
1857
        if (!$this->headerNoCache() && $this->all) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->all of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1858
            // we got page section information (TypoScript), so lets see if there is also a cached version
1859
            // of this page in the pages cache.
1860
            $this->newHash = $this->getHash();
1861
            $this->getTimeTracker()->push('Cache Row');
1862
            $row = $this->getFromCache_queryRow();
1863
            if (!is_array($row)) {
0 ignored issues
show
introduced by
The condition is_array($row) is always true.
Loading history...
1864
                // nothing in the cache, we acquire an exclusive lock now
1865
                $this->acquireLock('pages', $lockHash);
1866
                //
1867
                // from this point on we're the only one working on that page ($lockHash)
1868
                //
1869
1870
                // query the cache again to see if the data are there meanwhile
1871
                $row = $this->getFromCache_queryRow();
1872
                if (is_array($row)) {
1873
                    // we have the content, nice that some other process did the work for us
1874
                    $this->releaseLock('pages');
1875
                }
1876
                // We keep the lock set, because we are the ones generating the page now and filling the cache.
1877
                // This indicates that we have to release the lock later in releaseLocks()
1878
            }
1879
            if (is_array($row)) {
0 ignored issues
show
introduced by
The condition is_array($row) is always true.
Loading history...
1880
                $this->populatePageDataFromCache($row);
1881
            }
1882
            $this->getTimeTracker()->pull();
1883
        } else {
1884
            // the user forced rebuilding the page cache or there was no pagesection information
1885
            // get a lock for the page content so other processes will not interrupt the regeneration
1886
            $this->acquireLock('pages', $lockHash);
1887
        }
1888
    }
1889
1890
    /**
1891
     * Returning the cached version of page with hash = newHash
1892
     *
1893
     * @return array Cached row, if any. Otherwise void.
1894
     */
1895
    public function getFromCache_queryRow()
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::getFromCache_queryRow" is not in camel caps format
Loading history...
1896
    {
1897
        $this->getTimeTracker()->push('Cache Query');
1898
        $row = $this->pageCache->get($this->newHash);
1899
        $this->getTimeTracker()->pull();
1900
        return $row;
1901
    }
1902
1903
    /**
1904
     * This method properly sets the values given from the pages cache into the corresponding
1905
     * TSFE variables. The counterpart is setPageCacheContent() where all relevant information is fetched.
1906
     * This also contains all data that could be cached, even for pages that are partially cached, as they
1907
     * have non-cacheable content still to be rendered.
1908
     *
1909
     * @see getFromCache()
1910
     * @see setPageCacheContent()
1911
     * @param array $cachedData
1912
     */
1913
    protected function populatePageDataFromCache(array $cachedData): void
1914
    {
1915
        // Call hook when a page is retrieved from cache
1916
        $_params = ['pObj' => &$this, 'cache_pages_row' => &$cachedData];
1917
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['pageLoadedFromCache'] ?? [] as $_funcRef) {
1918
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
1919
        }
1920
        // Fetches the lowlevel config stored with the cached data
1921
        $this->config = $cachedData['cache_data'];
1922
        // Getting the content
1923
        $this->content = $cachedData['content'];
1924
        // Setting flag, so we know, that some cached content has been loaded
1925
        $this->cacheContentFlag = true;
1926
        $this->cacheExpires = $cachedData['expires'];
1927
        // Restore the current tags as they can be retrieved by getPageCacheTags()
1928
        $this->pageCacheTags = $cachedData['cacheTags'] ?? [];
1929
1930
        // Restore page title information, this is needed to generate the page title for
1931
        // partially cached pages.
1932
        $this->page['title'] = $cachedData['pageTitleInfo']['title'];
1933
        $this->indexedDocTitle = $cachedData['pageTitleInfo']['indexedDocTitle'];
1934
1935
        if (isset($this->config['config']['debug'])) {
1936
            $debugCacheTime = (bool)$this->config['config']['debug'];
1937
        } else {
1938
            $debugCacheTime = !empty($GLOBALS['TYPO3_CONF_VARS']['FE']['debug']);
1939
        }
1940
        if ($debugCacheTime) {
1941
            $dateFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'];
1942
            $timeFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
1943
            $this->content .= LF . '<!-- Cached page generated ' . date($dateFormat . ' ' . $timeFormat, $cachedData['tstamp']) . '. Expires ' . date($dateFormat . ' ' . $timeFormat, $cachedData['expires']) . ' -->';
1944
        }
1945
    }
1946
1947
    /**
1948
     * Detecting if shift-reload has been clicked
1949
     * Will not be called if re-generation of page happens by other reasons (for instance that the page is not in cache yet!)
1950
     * Also, a backend user MUST be logged in for the shift-reload to be detected due to DoS-attack-security reasons.
1951
     *
1952
     * @return bool If shift-reload in client browser has been clicked, disable getting cached page (and regenerate it).
1953
     */
1954
    public function headerNoCache()
1955
    {
1956
        $disableAcquireCacheData = false;
1957
        if ($this->isBackendUserLoggedIn()) {
1958
            if (strtolower($_SERVER['HTTP_CACHE_CONTROL']) === 'no-cache' || strtolower($_SERVER['HTTP_PRAGMA']) === 'no-cache') {
1959
                $disableAcquireCacheData = true;
1960
            }
1961
        }
1962
        // Call hook for possible by-pass of requiring of page cache (for recaching purpose)
1963
        $_params = ['pObj' => &$this, 'disableAcquireCacheData' => &$disableAcquireCacheData];
1964
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['headerNoCache'] ?? [] as $_funcRef) {
1965
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
1966
        }
1967
        return $disableAcquireCacheData;
1968
    }
1969
1970
    /**
1971
     * Calculates the cache-hash
1972
     * This hash is unique to the template, the variables ->id, ->type, list of fe user groups, ->MP (Mount Points) and cHash array
1973
     * Used to get and later store the cached data.
1974
     *
1975
     * @return string MD5 hash of serialized hash base from createHashBase()
1976
     * @see getFromCache()
1977
     * @see getLockHash()
1978
     */
1979
    protected function getHash()
1980
    {
1981
        return md5($this->createHashBase(false));
1982
    }
1983
1984
    /**
1985
     * Calculates the lock-hash
1986
     * This hash is unique to the above hash, except that it doesn't contain the template information in $this->all.
1987
     *
1988
     * @return string MD5 hash
1989
     * @see getFromCache()
1990
     * @see getHash()
1991
     */
1992
    protected function getLockHash()
1993
    {
1994
        $lockHash = $this->createHashBase(true);
1995
        return md5($lockHash);
1996
    }
1997
1998
    /**
1999
     * Calculates the cache-hash (or the lock-hash)
2000
     * This hash is unique to the template,
2001
     * the variables ->id, ->type, list of frontend user groups,
2002
     * ->MP (Mount Points) and cHash array
2003
     * Used to get and later store the cached data.
2004
     *
2005
     * @param bool $createLockHashBase Whether to create the lock hash, which doesn't contain the "this->all" (the template information)
2006
     * @return string the serialized hash base
2007
     */
2008
    protected function createHashBase($createLockHashBase = false)
2009
    {
2010
        // Fetch the list of user groups
2011
        /** @var UserAspect $userAspect */
2012
        $userAspect = $this->context->getAspect('frontend.user');
2013
        $hashParameters = [
2014
            'id' => (int)$this->id,
2015
            'type' => (int)$this->type,
2016
            'groupIds' => (string)implode(',', $userAspect->getGroupIds()),
2017
            'MP' => (string)$this->MP,
2018
            'site' => $this->site->getIdentifier(),
2019
            // Ensure the language base is used for the hash base calculation as well, otherwise TypoScript and page-related rendering
2020
            // is not cached properly as we don't have any language-specific conditions anymore
2021
            'siteBase' => (string)$this->language->getBase(),
2022
            // additional variation trigger for static routes
2023
            'staticRouteArguments' => $this->pageArguments->getStaticArguments(),
2024
            // dynamic route arguments (if route was resolved)
2025
            'dynamicArguments' => $this->getRelevantParametersForCachingFromPageArguments($this->pageArguments),
2026
        ];
2027
        // Include the template information if we shouldn't create a lock hash
2028
        if (!$createLockHashBase) {
2029
            $hashParameters['all'] = $this->all;
2030
        }
2031
        // Call hook to influence the hash calculation
2032
        $_params = [
2033
            'hashParameters' => &$hashParameters,
2034
            'createLockHashBase' => $createLockHashBase
2035
        ];
2036
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['createHashBase'] ?? [] as $_funcRef) {
2037
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
2038
        }
2039
        return serialize($hashParameters);
2040
    }
2041
2042
    /**
2043
     * Checks if config-array exists already but if not, gets it
2044
     *
2045
     * @throws ServiceUnavailableException
2046
     */
2047
    public function getConfigArray()
2048
    {
2049
        if (!$this->tmpl instanceof TemplateService) {
0 ignored issues
show
introduced by
$this->tmpl is always a sub-type of TYPO3\CMS\Core\TypoScript\TemplateService.
Loading history...
2050
            $this->tmpl = GeneralUtility::makeInstance(TemplateService::class, $this->context, null, $this);
2051
        }
2052
2053
        // If config is not set by the cache (which would be a major mistake somewhere) OR if INTincScripts-include-scripts have been registered, then we must parse the template in order to get it
2054
        if (empty($this->config) || $this->isINTincScript() || $this->context->getPropertyFromAspect('typoscript', 'forcedTemplateParsing')) {
2055
            $timeTracker = $this->getTimeTracker();
2056
            $timeTracker->push('Parse template');
2057
            // Start parsing the TS template. Might return cached version.
2058
            $this->tmpl->start($this->rootLine);
2059
            $timeTracker->pull();
2060
            // At this point we have a valid pagesection_cache (generated in $this->tmpl->start()),
2061
            // so let all other processes proceed now. (They are blocked at the pagessection_lock in getFromCache())
2062
            $this->releaseLock('pagesection');
2063
            if ($this->tmpl->loaded) {
2064
                $timeTracker->push('Setting the config-array');
2065
                // toplevel - objArrayName
2066
                $this->sPre = $this->tmpl->setup['types.'][$this->type];
2067
                $this->pSetup = $this->tmpl->setup[$this->sPre . '.'];
2068
                if (!is_array($this->pSetup)) {
2069
                    $message = 'The page is not configured! [type=' . $this->type . '][' . $this->sPre . '].';
2070
                    $this->logger->alert($message);
2071
                    try {
2072
                        $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
2073
                            $GLOBALS['TYPO3_REQUEST'],
2074
                            $message,
2075
                            ['code' => PageAccessFailureReasons::RENDERING_INSTRUCTIONS_NOT_CONFIGURED]
2076
                        );
2077
                        throw new ImmediateResponseException($response, 1533931374);
2078
                    } catch (PageNotFoundException $e) {
2079
                        $explanation = 'This means that there is no TypoScript object of type PAGE with typeNum=' . $this->type . ' configured.';
2080
                        throw new ServiceUnavailableException($message . ' ' . $explanation, 1294587217);
2081
                    }
2082
                } else {
2083
                    if (!isset($this->config['config'])) {
2084
                        $this->config['config'] = [];
2085
                    }
2086
                    // Filling the config-array, first with the main "config." part
2087
                    if (is_array($this->tmpl->setup['config.'])) {
2088
                        ArrayUtility::mergeRecursiveWithOverrule($this->tmpl->setup['config.'], $this->config['config']);
2089
                        $this->config['config'] = $this->tmpl->setup['config.'];
2090
                    }
2091
                    // override it with the page/type-specific "config."
2092
                    if (is_array($this->pSetup['config.'])) {
2093
                        ArrayUtility::mergeRecursiveWithOverrule($this->config['config'], $this->pSetup['config.']);
2094
                    }
2095
                    // Set default values for removeDefaultJS and inlineStyle2TempFile so CSS and JS are externalized if compatversion is higher than 4.0
2096
                    if (!isset($this->config['config']['removeDefaultJS'])) {
2097
                        $this->config['config']['removeDefaultJS'] = 'external';
2098
                    }
2099
                    if (!isset($this->config['config']['inlineStyle2TempFile'])) {
2100
                        $this->config['config']['inlineStyle2TempFile'] = 1;
2101
                    }
2102
2103
                    if (!isset($this->config['config']['compressJs'])) {
2104
                        $this->config['config']['compressJs'] = 0;
2105
                    }
2106
                    // Setting default cache_timeout
2107
                    if (isset($this->config['config']['cache_period'])) {
2108
                        $this->set_cache_timeout_default((int)$this->config['config']['cache_period']);
2109
                    }
2110
2111
                    // Processing for the config_array:
2112
                    $this->config['rootLine'] = $this->tmpl->rootLine;
2113
                    // Class for render Header and Footer parts
2114
                    if ($this->pSetup['pageHeaderFooterTemplateFile']) {
2115
                        try {
2116
                            $file = GeneralUtility::makeInstance(FilePathSanitizer::class)
2117
                                ->sanitize((string)$this->pSetup['pageHeaderFooterTemplateFile']);
2118
                            $this->pageRenderer->setTemplateFile($file);
2119
                        } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
2120
                            // do nothing
2121
                        }
2122
                    }
2123
                }
2124
                $timeTracker->pull();
2125
            } else {
2126
                $message = 'No TypoScript template found!';
2127
                $this->logger->alert($message);
2128
                try {
2129
                    $response = GeneralUtility::makeInstance(ErrorController::class)->unavailableAction(
2130
                        $GLOBALS['TYPO3_REQUEST'],
2131
                        $message,
2132
                        ['code' => PageAccessFailureReasons::RENDERING_INSTRUCTIONS_NOT_FOUND]
2133
                    );
2134
                    throw new ImmediateResponseException($response, 1533931380);
2135
                } catch (ServiceUnavailableException $e) {
2136
                    throw new ServiceUnavailableException($message, 1294587218);
2137
                }
2138
            }
2139
        }
2140
2141
        // No cache
2142
        // Set $this->no_cache TRUE if the config.no_cache value is set!
2143
        if ($this->config['config']['no_cache']) {
2144
            $this->set_no_cache('config.no_cache is set', true);
2145
        }
2146
2147
        // Auto-configure settings when a site is configured
2148
        $this->config['config']['absRefPrefix'] = $this->config['config']['absRefPrefix'] ?? 'auto';
2149
2150
        // Hook for postProcessing the configuration array
2151
        $params = ['config' => &$this->config['config']];
2152
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['configArrayPostProc'] ?? [] as $funcRef) {
2153
            GeneralUtility::callUserFunction($funcRef, $params, $this);
2154
        }
2155
    }
2156
2157
    /********************************************
2158
     *
2159
     * Further initialization and data processing
2160
     *
2161
     *******************************************/
2162
2163
    /**
2164
     * Setting the language key that will be used by the current page.
2165
     * In this function it should be checked, 1) that this language exists, 2) that a page_overlay_record exists, .. and if not the default language, 0 (zero), should be set.
2166
     *
2167
     * @internal
2168
     */
2169
    public function settingLanguage()
2170
    {
2171
        $_params = [];
2172
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['settingLanguage_preProcess'] ?? [] as $_funcRef) {
2173
            $ref = $this; // introduced for phpstan to not lose type information when passing $this into callUserFunction
2174
            GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
2175
        }
2176
2177
        // Rendering charset of HTML page.
2178
        if (isset($this->config['config']['metaCharset']) && $this->config['config']['metaCharset'] !== 'utf-8') {
2179
            $this->metaCharset = $this->config['config']['metaCharset'];
2180
        }
2181
2182
        // Get values from site language
2183
        $languageAspect = LanguageAspectFactory::createFromSiteLanguage($this->language);
2184
2185
        $languageId = $languageAspect->getId();
2186
        $languageContentId = $languageAspect->getContentId();
2187
2188
        // If sys_language_uid is set to another language than default:
2189
        if ($languageAspect->getId() > 0) {
2190
            // check whether a shortcut is overwritten by a translated page
2191
            // we can only do this now, as this is the place where we get
2192
            // to know about translations
2193
            $this->checkTranslatedShortcut($languageAspect->getId());
2194
            // Request the overlay record for the sys_language_uid:
2195
            $olRec = $this->sys_page->getPageOverlay($this->id, $languageAspect->getId());
2196
            if (empty($olRec)) {
2197
                // If requested translation is not available:
2198
                if (GeneralUtility::hideIfNotTranslated($this->page['l18n_cfg'])) {
2199
                    $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
2200
                        $GLOBALS['TYPO3_REQUEST'],
2201
                        'Page is not available in the requested language.',
2202
                        ['code' => PageAccessFailureReasons::LANGUAGE_NOT_AVAILABLE]
2203
                    );
2204
                    throw new ImmediateResponseException($response, 1533931388);
2205
                }
2206
                switch ((string)$languageAspect->getLegacyLanguageMode()) {
2207
                    case 'strict':
2208
                        $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
2209
                            $GLOBALS['TYPO3_REQUEST'],
2210
                            'Page is not available in the requested language (strict).',
2211
                            ['code' => PageAccessFailureReasons::LANGUAGE_NOT_AVAILABLE_STRICT_MODE]
2212
                        );
2213
                        throw new ImmediateResponseException($response, 1533931395);
2214
                        break;
2215
                    case 'fallback':
2216
                    case 'content_fallback':
2217
                        // Setting content uid (but leaving the sys_language_uid) when a content_fallback
2218
                        // value was found.
2219
                        foreach ($languageAspect->getFallbackChain() ?? [] as $orderValue) {
2220
                            if ($orderValue === '0' || $orderValue === 0 || $orderValue === '') {
2221
                                $languageContentId = 0;
2222
                                break;
2223
                            }
2224
                            if (MathUtility::canBeInterpretedAsInteger($orderValue) && !empty($this->sys_page->getPageOverlay($this->id, (int)$orderValue))) {
2225
                                $languageContentId = (int)$orderValue;
2226
                                break;
2227
                            }
2228
                            if ($orderValue === 'pageNotFound') {
2229
                                // The existing fallbacks have not been found, but instead of continuing
2230
                                // page rendering with default language, a "page not found" message should be shown
2231
                                // instead.
2232
                                $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
2233
                                    $GLOBALS['TYPO3_REQUEST'],
2234
                                    'Page is not available in the requested language (fallbacks did not apply).',
2235
                                    ['code' => PageAccessFailureReasons::LANGUAGE_AND_FALLBACKS_NOT_AVAILABLE]
2236
                                );
2237
                                throw new ImmediateResponseException($response, 1533931402);
2238
                            }
2239
                        }
2240
                        break;
2241
                    case 'ignore':
2242
                        $languageContentId = $languageAspect->getId();
2243
                        break;
2244
                    default:
2245
                        // Default is that everything defaults to the default language...
2246
                        $languageId = ($languageContentId = 0);
2247
                }
2248
            }
2249
2250
            // Define the language aspect again now
2251
            $languageAspect = GeneralUtility::makeInstance(
2252
                LanguageAspect::class,
2253
                $languageId,
2254
                $languageContentId,
2255
                $languageAspect->getOverlayType(),
2256
                $languageAspect->getFallbackChain()
2257
            );
2258
2259
            // Setting sys_language if an overlay record was found (which it is only if a language is used)
2260
            // We'll do this every time since the language aspect might have changed now
2261
            // Doing this ensures that page properties like the page title are returned in the correct language
2262
            $this->page = $this->sys_page->getPageOverlay($this->page, $languageAspect->getContentId());
2263
2264
            // Update SYS_LASTCHANGED for localized page record
2265
            $this->setRegisterValueForSysLastChanged($this->page);
2266
        }
2267
2268
        // Set the language aspect
2269
        $this->context->setAspect('language', $languageAspect);
2270
2271
        // Setting sys_language_uid inside sys-page by creating a new page repository
2272
        $this->sys_page = GeneralUtility::makeInstance(PageRepository::class, $this->context);
2273
        // If default language is not available:
2274
        if ((!$languageAspect->getContentId() || !$languageAspect->getId())
2275
            && GeneralUtility::hideIfDefaultLanguage($this->page['l18n_cfg'] ?? 0)
2276
        ) {
2277
            $message = 'Page is not available in default language.';
2278
            $this->logger->error($message);
2279
            $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
2280
                $GLOBALS['TYPO3_REQUEST'],
2281
                $message,
2282
                ['code' => PageAccessFailureReasons::LANGUAGE_DEFAULT_NOT_AVAILABLE]
2283
            );
2284
            throw new ImmediateResponseException($response, 1533931423);
2285
        }
2286
2287
        if ($languageAspect->getId() > 0) {
2288
            $this->updateRootLinesWithTranslations();
2289
        }
2290
2291
        $_params = [];
2292
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['settingLanguage_postProcess'] ?? [] as $_funcRef) {
2293
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
2294
        }
2295
    }
2296
2297
    /**
2298
     * Updating content of the two rootLines IF the language key is set!
2299
     */
2300
    protected function updateRootLinesWithTranslations()
2301
    {
2302
        try {
2303
            $this->rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $this->id, $this->MP, $this->context)->get();
2304
        } catch (RootLineException $e) {
2305
            $this->rootLine = [];
2306
        }
2307
        $this->tmpl->updateRootlineData($this->rootLine);
2308
    }
2309
2310
    /**
2311
     * Setting locale for frontend rendering
2312
     * @deprecated will be removed in TYPO3 v11.0. Use Locales::setSystemLocaleFromSiteLanguage() instead.
2313
     */
2314
    public function settingLocale()
2315
    {
2316
        trigger_error('TSFE->settingLocale() will be removed in TYPO3 v11.0. Use Locales::setSystemLocaleFromSiteLanguage() instead, as this functionality is independent of TSFE.', E_USER_DEPRECATED);
2317
        if ($this->language->getLocale() && !Locales::setSystemLocaleFromSiteLanguage($this->language)) {
2318
            $this->getTimeTracker()->setTSlogMessage('Locale "' . htmlspecialchars($this->language->getLocale()) . '" not found.', 3);
2319
        }
2320
    }
2321
2322
    /**
2323
     * Checks whether a translated shortcut page has a different shortcut
2324
     * target than the original language page.
2325
     * If that is the case, things get corrected to follow that alternative
2326
     * shortcut
2327
     * @param int $languageId
2328
     */
2329
    protected function checkTranslatedShortcut(int $languageId)
2330
    {
2331
        if (!is_null($this->originalShortcutPage)) {
2332
            $originalShortcutPageOverlay = $this->sys_page->getPageOverlay($this->originalShortcutPage['uid'], $languageId);
2333
            if (!empty($originalShortcutPageOverlay['shortcut']) && $originalShortcutPageOverlay['shortcut'] != $this->id) {
2334
                // the translation of the original shortcut page has a different shortcut target!
2335
                // set the correct page and id
2336
                $shortcut = $this->sys_page->getPageShortcut($originalShortcutPageOverlay['shortcut'], $originalShortcutPageOverlay['shortcut_mode'], $originalShortcutPageOverlay['uid']);
2337
                $this->id = ($this->contentPid = $shortcut['uid']);
2338
                $this->page = $this->sys_page->getPage($this->id);
2339
                // Fix various effects on things like menus f.e.
2340
                $this->fetch_the_id();
2341
                $this->tmpl->rootLine = array_reverse($this->rootLine);
2342
            }
2343
        }
2344
    }
2345
2346
    /**
2347
     * Calculates and sets the internal linkVars based upon the current request parameters
2348
     * and the setting "config.linkVars".
2349
     *
2350
     * @param array $queryParams $_GET (usually called with a PSR-7 $request->getQueryParams())
2351
     */
2352
    public function calculateLinkVars(array $queryParams)
2353
    {
2354
        $this->linkVars = '';
2355
        if (empty($this->config['config']['linkVars'])) {
2356
            return;
2357
        }
2358
2359
        $linkVars = $this->splitLinkVarsString((string)$this->config['config']['linkVars']);
2360
2361
        if (empty($linkVars)) {
2362
            return;
2363
        }
2364
        foreach ($linkVars as $linkVar) {
2365
            $test = $value = '';
2366
            if (preg_match('/^(.*)\\((.+)\\)$/', $linkVar, $match)) {
2367
                $linkVar = trim($match[1]);
2368
                $test = trim($match[2]);
2369
            }
2370
2371
            $keys = explode('|', $linkVar);
2372
            $numberOfLevels = count($keys);
2373
            $rootKey = trim($keys[0]);
2374
            if (!isset($queryParams[$rootKey])) {
2375
                continue;
2376
            }
2377
            $value = $queryParams[$rootKey];
2378
            for ($i = 1; $i < $numberOfLevels; $i++) {
2379
                $currentKey = trim($keys[$i]);
2380
                if (isset($value[$currentKey])) {
2381
                    $value = $value[$currentKey];
2382
                } else {
2383
                    $value = false;
2384
                    break;
2385
                }
2386
            }
2387
            if ($value !== false) {
2388
                $parameterName = $keys[0];
2389
                for ($i = 1; $i < $numberOfLevels; $i++) {
2390
                    $parameterName .= '[' . $keys[$i] . ']';
2391
                }
2392
                if (!is_array($value)) {
2393
                    $temp = rawurlencode($value);
2394
                    if ($test !== '' && !$this->isAllowedLinkVarValue($temp, $test)) {
2395
                        // Error: This value was not allowed for this key
2396
                        continue;
2397
                    }
2398
                    $value = '&' . $parameterName . '=' . $temp;
2399
                } else {
2400
                    if ($test !== '' && $test !== 'array') {
2401
                        // Error: This key must not be an array!
2402
                        continue;
2403
                    }
2404
                    $value = HttpUtility::buildQueryString([$parameterName => $value], '&');
2405
                }
2406
                $this->linkVars .= $value;
2407
            }
2408
        }
2409
    }
2410
2411
    /**
2412
     * Split the link vars string by "," but not if the "," is inside of braces
2413
     *
2414
     * @param string $string
2415
     *
2416
     * @return array
2417
     */
2418
    protected function splitLinkVarsString(string $string): array
2419
    {
2420
        $tempCommaReplacementString = '###KASPER###';
2421
2422
        // replace every "," wrapped in "()" by a "unique" string
2423
        $string = preg_replace_callback('/\((?>[^()]|(?R))*\)/', function ($result) use ($tempCommaReplacementString) {
2424
            return str_replace(',', $tempCommaReplacementString, $result[0]);
2425
        }, $string);
2426
2427
        $string = GeneralUtility::trimExplode(',', $string);
2428
2429
        // replace all "unique" strings back to ","
2430
        return str_replace($tempCommaReplacementString, ',', $string);
2431
    }
2432
2433
    /**
2434
     * Checks if the value defined in "config.linkVars" contains an allowed value.
2435
     * Otherwise, return FALSE which means the value will not be added to any links.
2436
     *
2437
     * @param string $haystack The string in which to find $needle
2438
     * @param string $needle The string to find in $haystack
2439
     * @return bool Returns TRUE if $needle matches or is found in $haystack
2440
     */
2441
    protected function isAllowedLinkVarValue(string $haystack, string $needle): bool
2442
    {
2443
        $isAllowed = false;
2444
        // Integer
2445
        if ($needle === 'int' || $needle === 'integer') {
2446
            if (MathUtility::canBeInterpretedAsInteger($haystack)) {
2447
                $isAllowed = true;
2448
            }
2449
        } elseif (preg_match('/^\\/.+\\/[imsxeADSUXu]*$/', $needle)) {
2450
            // Regular expression, only "//" is allowed as delimiter
2451
            if (@preg_match($needle, $haystack)) {
2452
                $isAllowed = true;
2453
            }
2454
        } elseif (strpos($needle, '-') !== false) {
2455
            // Range
2456
            if (MathUtility::canBeInterpretedAsInteger($haystack)) {
2457
                $range = explode('-', $needle);
2458
                if ($range[0] <= $haystack && $range[1] >= $haystack) {
2459
                    $isAllowed = true;
2460
                }
2461
            }
2462
        } elseif (strpos($needle, '|') !== false) {
2463
            // List
2464
            // Trim the input
2465
            $haystack = str_replace(' ', '', $haystack);
2466
            if (strpos('|' . $needle . '|', '|' . $haystack . '|') !== false) {
2467
                $isAllowed = true;
2468
            }
2469
        } elseif ((string)$needle === (string)$haystack) {
2470
            // String comparison
2471
            $isAllowed = true;
2472
        }
2473
        return $isAllowed;
2474
    }
2475
2476
    /**
2477
     * Returns URI of target page, if the current page is an overlaid mountpoint.
2478
     *
2479
     * If the current page is of type mountpoint and should be overlaid with the contents of the mountpoint page
2480
     * and is accessed directly, the user will be redirected to the mountpoint context.
2481
     * @internal
2482
     * @param ServerRequestInterface $request
2483
     * @return string|null
2484
     */
2485
    public function getRedirectUriForMountPoint(ServerRequestInterface $request): ?string
2486
    {
2487
        if (!empty($this->originalMountPointPage) && (int)$this->originalMountPointPage['doktype'] === PageRepository::DOKTYPE_MOUNTPOINT) {
2488
            return $this->getUriToCurrentPageForRedirect($request);
2489
        }
2490
2491
        return null;
2492
    }
2493
2494
    /**
2495
     * Returns URI of target page, if the current page is a Shortcut.
2496
     *
2497
     * If the current page is of type shortcut and accessed directly via its URL,
2498
     * the user will be redirected to shortcut target.
2499
     *
2500
     * @internal
2501
     * @param ServerRequestInterface $request
2502
     * @return string|null
2503
     */
2504
    public function getRedirectUriForShortcut(ServerRequestInterface $request): ?string
2505
    {
2506
        if (!empty($this->originalShortcutPage) && $this->originalShortcutPage['doktype'] == PageRepository::DOKTYPE_SHORTCUT) {
2507
            return $this->getUriToCurrentPageForRedirect($request);
2508
        }
2509
2510
        return null;
2511
    }
2512
2513
    /**
2514
     * Instantiate \TYPO3\CMS\Frontend\ContentObject to generate the correct target URL
2515
     *
2516
     * @param ServerRequestInterface $request
2517
     * @return string
2518
     */
2519
    protected function getUriToCurrentPageForRedirect(ServerRequestInterface $request): string
2520
    {
2521
        $this->calculateLinkVars($request->getQueryParams());
2522
        $parameter = $this->page['uid'];
2523
        if ($this->type && MathUtility::canBeInterpretedAsInteger($this->type)) {
2524
            $parameter .= ',' . $this->type;
2525
        }
2526
        return GeneralUtility::makeInstance(ContentObjectRenderer::class, $this)->typoLink_URL([
2527
            'parameter' => $parameter,
2528
            'addQueryString' => true,
2529
            'addQueryString.' => ['exclude' => 'id'],
2530
            // ensure absolute URL is generated when having a valid Site
2531
            'forceAbsoluteUrl' => $GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface
2532
                && $GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site
2533
        ]);
2534
    }
2535
2536
    /********************************************
2537
     *
2538
     * Page generation; cache handling
2539
     *
2540
     *******************************************/
2541
    /**
2542
     * Returns TRUE if the page should be generated.
2543
     * That is if no URL handler is active and the cacheContentFlag is not set.
2544
     *
2545
     * @return bool
2546
     */
2547
    public function isGeneratePage()
2548
    {
2549
        return !$this->cacheContentFlag;
2550
    }
2551
2552
    /**
2553
     * Set cache content to $this->content
2554
     */
2555
    protected function realPageCacheContent()
2556
    {
2557
        // seconds until a cached page is too old
2558
        $cacheTimeout = $this->get_cache_timeout();
2559
        $timeOutTime = $GLOBALS['EXEC_TIME'] + $cacheTimeout;
2560
        $usePageCache = true;
2561
        // Hook for deciding whether page cache should be written to the cache backend or not
2562
        // NOTE: as hooks are called in a loop, the last hook will have the final word (however each
2563
        // hook receives the current status of the $usePageCache flag)
2564
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['usePageCache'] ?? [] as $className) {
2565
            $usePageCache = GeneralUtility::makeInstance($className)->usePageCache($this, $usePageCache);
2566
        }
2567
        // Write the page to cache, if necessary
2568
        if ($usePageCache) {
2569
            $this->setPageCacheContent($this->content, $this->config, $timeOutTime);
2570
        }
2571
        // Hook for cache post processing (eg. writing static files!)
2572
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['insertPageIncache'] ?? [] as $className) {
2573
            GeneralUtility::makeInstance($className)->insertPageIncache($this, $timeOutTime);
2574
        }
2575
    }
2576
2577
    /**
2578
     * Sets cache content; Inserts the content string into the cache_pages cache.
2579
     *
2580
     * @param string $content The content to store in the HTML field of the cache table
2581
     * @param mixed $data The additional cache_data array, fx. $this->config
2582
     * @param int $expirationTstamp Expiration timestamp
2583
     * @see realPageCacheContent()
2584
     */
2585
    protected function setPageCacheContent($content, $data, $expirationTstamp)
2586
    {
2587
        $cacheData = [
2588
            'identifier' => $this->newHash,
2589
            'page_id' => $this->id,
2590
            'content' => $content,
2591
            'cache_data' => $data,
2592
            'expires' => $expirationTstamp,
2593
            'tstamp' => $GLOBALS['EXEC_TIME'],
2594
            'pageTitleInfo' => [
2595
                'title' => $this->page['title'],
2596
                'indexedDocTitle' => $this->indexedDocTitle
2597
            ]
2598
        ];
2599
        $this->cacheExpires = $expirationTstamp;
2600
        $this->pageCacheTags[] = 'pageId_' . $cacheData['page_id'];
2601
        // Respect the page cache when content of pid is shown
2602
        if ($this->id !== $this->contentPid) {
0 ignored issues
show
introduced by
The condition $this->id !== $this->contentPid is always true.
Loading history...
2603
            $this->pageCacheTags[] = 'pageId_' . $this->contentPid;
2604
        }
2605
        if (!empty($this->page['cache_tags'])) {
2606
            $tags = GeneralUtility::trimExplode(',', $this->page['cache_tags'], true);
2607
            $this->pageCacheTags = array_merge($this->pageCacheTags, $tags);
2608
        }
2609
        // Add the cache themselves as well, because they are fetched by getPageCacheTags()
2610
        $cacheData['cacheTags'] = $this->pageCacheTags;
2611
        $this->pageCache->set($this->newHash, $cacheData, $this->pageCacheTags, $expirationTstamp - $GLOBALS['EXEC_TIME']);
2612
    }
2613
2614
    /**
2615
     * Clears cache content (for $this->newHash)
2616
     */
2617
    public function clearPageCacheContent()
2618
    {
2619
        $this->pageCache->remove($this->newHash);
2620
    }
2621
2622
    /**
2623
     * Sets sys last changed
2624
     * Setting the SYS_LASTCHANGED value in the pagerecord: This value will thus be set to the highest tstamp of records rendered on the page. This includes all records with no regard to hidden records, userprotection and so on.
2625
     *
2626
     * @see ContentObjectRenderer::lastChanged()
2627
     */
2628
    protected function setSysLastChanged()
2629
    {
2630
        // We only update the info if browsing the live workspace
2631
        if ($this->page['SYS_LASTCHANGED'] < (int)$this->register['SYS_LASTCHANGED'] && !$this->doWorkspacePreview()) {
2632
            $connection = GeneralUtility::makeInstance(ConnectionPool::class)
2633
                ->getConnectionForTable('pages');
2634
            $pageId = $this->page['_PAGES_OVERLAY_UID'] ?? $this->id;
2635
            $connection->update(
2636
                'pages',
2637
                [
2638
                    'SYS_LASTCHANGED' => (int)$this->register['SYS_LASTCHANGED']
2639
                ],
2640
                [
2641
                    'uid' => (int)$pageId
2642
                ]
2643
            );
2644
        }
2645
    }
2646
2647
    /**
2648
     * Set the SYS_LASTCHANGED register value, is also called when a translated page is in use,
2649
     * so the register reflects the state of the translated page, not the page in the default language.
2650
     *
2651
     * @param array $page
2652
     * @internal
2653
     */
2654
    protected function setRegisterValueForSysLastChanged(array $page): void
2655
    {
2656
        $this->register['SYS_LASTCHANGED'] = (int)$page['tstamp'];
2657
        if ($this->register['SYS_LASTCHANGED'] < (int)$page['SYS_LASTCHANGED']) {
2658
            $this->register['SYS_LASTCHANGED'] = (int)$page['SYS_LASTCHANGED'];
2659
        }
2660
    }
2661
2662
    /**
2663
     * Release pending locks
2664
     *
2665
     * @internal
2666
     */
2667
    public function releaseLocks()
2668
    {
2669
        $this->releaseLock('pagesection');
2670
        $this->releaseLock('pages');
2671
    }
2672
2673
    /**
2674
     * Adds tags to this page's cache entry, you can then f.e. remove cache
2675
     * entries by tag
2676
     *
2677
     * @param array $tags An array of tag
2678
     */
2679
    public function addCacheTags(array $tags)
2680
    {
2681
        $this->pageCacheTags = array_merge($this->pageCacheTags, $tags);
2682
    }
2683
2684
    /**
2685
     * @return array
2686
     */
2687
    public function getPageCacheTags(): array
2688
    {
2689
        return $this->pageCacheTags;
2690
    }
2691
2692
    /********************************************
2693
     *
2694
     * Page generation; rendering and inclusion
2695
     *
2696
     *******************************************/
2697
    /**
2698
     * Does some processing BEFORE the page content is generated / built.
2699
     */
2700
    public function generatePage_preProcessing()
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::generatePage_preProcessing" is not in camel caps format
Loading history...
2701
    {
2702
        // Same codeline as in getFromCache(). But $this->all has been changed by
2703
        // \TYPO3\CMS\Core\TypoScript\TemplateService::start() in the meantime, so this must be called again!
2704
        $this->newHash = $this->getHash();
2705
2706
        // Used as a safety check in case a PHP script is falsely disabling $this->no_cache during page generation.
2707
        $this->no_cacheBeforePageGen = $this->no_cache;
2708
    }
2709
2710
    /**
2711
     * Check the value of "content_from_pid" of the current page record, and see if the current request
2712
     * should actually show content from another page.
2713
     *
2714
     * By using $TSFE->getPageAndRootline() on the cloned object, all rootline restrictions (extendToSubPages)
2715
     * are evaluated as well.
2716
     *
2717
     * @return int the current page ID or another one if resolved properly - usually set to $this->contentPid
2718
     */
2719
    protected function resolveContentPid(): int
2720
    {
2721
        if (!isset($this->page['content_from_pid']) || empty($this->page['content_from_pid'])) {
2722
            return (int)$this->id;
2723
        }
2724
        // make REAL copy of TSFE object - not reference!
2725
        $temp_copy_TSFE = clone $this;
2726
        // Set ->id to the content_from_pid value - we are going to evaluate this pid as was it a given id for a page-display!
2727
        $temp_copy_TSFE->id = $this->page['content_from_pid'];
2728
        $temp_copy_TSFE->MP = '';
2729
        $temp_copy_TSFE->getPageAndRootline();
2730
        return (int)$temp_copy_TSFE->id;
2731
    }
2732
    /**
2733
     * Sets up TypoScript "config." options and set properties in $TSFE.
2734
     *
2735
     * @param ServerRequestInterface $request
2736
     */
2737
    public function preparePageContentGeneration(ServerRequestInterface $request)
2738
    {
2739
        $this->getTimeTracker()->push('Prepare page content generation');
2740
        $this->contentPid = $this->resolveContentPid();
2741
        // Global vars...
2742
        $this->indexedDocTitle = $this->page['title'] ?? null;
2743
        // Base url:
2744
        if (isset($this->config['config']['baseURL'])) {
2745
            $this->baseUrl = $this->config['config']['baseURL'];
2746
        }
2747
        // Internal and External target defaults
2748
        $this->intTarget = (string)($this->config['config']['intTarget'] ?? '');
2749
        $this->extTarget = (string)($this->config['config']['extTarget'] ?? '');
2750
        $this->fileTarget = (string)($this->config['config']['fileTarget'] ?? '');
2751
        $this->spamProtectEmailAddresses = $this->config['config']['spamProtectEmailAddresses'] ?? 0;
2752
        if ($this->spamProtectEmailAddresses !== 'ascii') {
2753
            $this->spamProtectEmailAddresses = MathUtility::forceIntegerInRange($this->spamProtectEmailAddresses, -10, 10, 0);
2754
        }
2755
        // calculate the absolute path prefix
2756
        if (!empty($this->config['config']['absRefPrefix'])) {
2757
            $absRefPrefix = trim($this->config['config']['absRefPrefix']);
2758
            if ($absRefPrefix === 'auto') {
2759
                $this->absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
2760
            } else {
2761
                $this->absRefPrefix = $absRefPrefix;
2762
            }
2763
        } else {
2764
            $this->absRefPrefix = '';
2765
        }
2766
        $this->ATagParams = trim($this->config['config']['ATagParams'] ?? '') ? ' ' . trim($this->config['config']['ATagParams']) : '';
2767
        $this->initializeSearchWordData($request->getParsedBody()['sword_list'] ?? $request->getQueryParams()['sword_list'] ?? null);
2768
        // linkVars
2769
        $this->calculateLinkVars($request->getQueryParams());
2770
        // Setting XHTML-doctype from doctype
2771
        if (!isset($this->config['config']['xhtmlDoctype']) || !$this->config['config']['xhtmlDoctype']) {
2772
            $this->config['config']['xhtmlDoctype'] = $this->config['config']['doctype'] ?? '';
2773
        }
2774
        if ($this->config['config']['xhtmlDoctype']) {
2775
            $this->xhtmlDoctype = $this->config['config']['xhtmlDoctype'];
2776
            // Checking XHTML-docytpe
2777
            switch ((string)$this->config['config']['xhtmlDoctype']) {
2778
                case 'xhtml_trans':
2779
                case 'xhtml_strict':
2780
                    $this->xhtmlVersion = 100;
2781
                    break;
2782
                case 'xhtml_basic':
2783
                    $this->xhtmlVersion = 105;
2784
                    break;
2785
                case 'xhtml_11':
2786
                case 'xhtml+rdfa_10':
2787
                    $this->xhtmlVersion = 110;
2788
                    break;
2789
                default:
2790
                    $this->pageRenderer->setRenderXhtml(false);
2791
                    $this->xhtmlDoctype = '';
2792
                    $this->xhtmlVersion = 0;
2793
            }
2794
        } else {
2795
            $this->pageRenderer->setRenderXhtml(false);
2796
        }
2797
2798
        // Global content object
2799
        $this->newCObj();
2800
        $this->getTimeTracker()->pull();
2801
    }
2802
2803
    /**
2804
     * Fills the sWordList property and builds the regular expression in TSFE that can be used to split
2805
     * strings by the submitted search words.
2806
     *
2807
     * @param mixed $searchWords - usually an array, but we can't be sure (yet)
2808
     * @see sWordList
2809
     * @see sWordRegEx
2810
     */
2811
    protected function initializeSearchWordData($searchWords)
2812
    {
2813
        $this->sWordRegEx = '';
2814
        $this->sWordList = $searchWords ?? '';
2815
        if (is_array($this->sWordList)) {
2816
            $space = !empty($this->config['config']['sword_standAlone'] ?? null) ? '[[:space:]]' : '';
2817
            $regexpParts = [];
2818
            foreach ($this->sWordList as $val) {
2819
                if (trim($val) !== '') {
2820
                    $regexpParts[] = $space . preg_quote($val, '/') . $space;
2821
                }
2822
            }
2823
            $this->sWordRegEx = implode('|', $regexpParts);
2824
        }
2825
    }
2826
2827
    /**
2828
     * Does processing of the content after the page content was generated.
2829
     *
2830
     * This includes caching the page, indexing the page (if configured) and setting sysLastChanged
2831
     */
2832
    public function generatePage_postProcessing()
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::generatePage_postProcessing" is not in camel caps format
Loading history...
2833
    {
2834
        $this->setAbsRefPrefix();
2835
        // This is to ensure, that the page is NOT cached if the no_cache parameter was set before the page was generated. This is a safety precaution, as it could have been unset by some script.
2836
        if ($this->no_cacheBeforePageGen) {
2837
            $this->set_no_cache('no_cache has been set before the page was generated - safety check', true);
2838
        }
2839
        // Hook for post-processing of page content cached/non-cached:
2840
        $_params = ['pObj' => &$this];
2841
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all'] ?? [] as $_funcRef) {
2842
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
2843
        }
2844
        // Processing if caching is enabled:
2845
        if (!$this->no_cache) {
2846
            // Hook for post-processing of page content before being cached:
2847
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-cached'] ?? [] as $_funcRef) {
2848
                GeneralUtility::callUserFunction($_funcRef, $_params, $this);
2849
            }
2850
        }
2851
        // Convert char-set for output: (should be BEFORE indexing of the content (changed 22/4 2005)),
2852
        // because otherwise indexed search might convert from the wrong charset!
2853
        // One thing is that the charset mentioned in the HTML header would be wrong since the output charset (metaCharset)
2854
        // has not been converted to from utf-8. And indexed search will internally convert from metaCharset
2855
        // to utf-8 so the content MUST be in metaCharset already!
2856
        $this->content = $this->convOutputCharset($this->content);
2857
        // Hook for indexing pages
2858
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['pageIndexing'] ?? [] as $className) {
2859
            GeneralUtility::makeInstance($className)->hook_indexContent($this);
2860
        }
2861
        // Storing for cache:
2862
        if (!$this->no_cache) {
2863
            $this->realPageCacheContent();
2864
        }
2865
        // Sets sys-last-change:
2866
        $this->setSysLastChanged();
2867
    }
2868
2869
    /**
2870
     * Generate the page title, can be called multiple times,
2871
     * as PageTitleProvider might have been modified by an uncached plugin etc.
2872
     *
2873
     * @return string the generated page title
2874
     */
2875
    public function generatePageTitle(): string
2876
    {
2877
        $pageTitleSeparator = '';
2878
2879
        // Check for a custom pageTitleSeparator, and perform stdWrap on it
2880
        if (isset($this->config['config']['pageTitleSeparator']) && $this->config['config']['pageTitleSeparator'] !== '') {
2881
            $pageTitleSeparator = $this->config['config']['pageTitleSeparator'];
2882
2883
            if (isset($this->config['config']['pageTitleSeparator.']) && is_array($this->config['config']['pageTitleSeparator.'])) {
2884
                $pageTitleSeparator = $this->cObj->stdWrap($pageTitleSeparator, $this->config['config']['pageTitleSeparator.']);
2885
            } else {
2886
                $pageTitleSeparator .= ' ';
2887
            }
2888
        }
2889
2890
        $titleProvider = GeneralUtility::makeInstance(PageTitleProviderManager::class);
2891
        $pageTitle = $titleProvider->getTitle();
2892
2893
        if ($pageTitle !== '') {
2894
            $this->indexedDocTitle = $pageTitle;
2895
        }
2896
2897
        $titleTagContent = $this->printTitle(
2898
            $pageTitle,
2899
            (bool)($this->config['config']['noPageTitle'] ?? false),
2900
            (bool)($this->config['config']['pageTitleFirst'] ?? false),
2901
            $pageTitleSeparator
2902
        );
2903
        // stdWrap around the title tag
2904
        if (isset($this->config['config']['pageTitle.']) && is_array($this->config['config']['pageTitle.'])) {
2905
            $titleTagContent = $this->cObj->stdWrap($titleTagContent, $this->config['config']['pageTitle.']);
2906
        }
2907
2908
        // config.noPageTitle = 2 - means do not render the page title
2909
        if (isset($this->config['config']['noPageTitle']) && (int)$this->config['config']['noPageTitle'] === 2) {
2910
            $titleTagContent = '';
2911
        }
2912
        if ($titleTagContent !== '') {
2913
            $this->pageRenderer->setTitle($titleTagContent);
2914
        }
2915
        return (string)$titleTagContent;
2916
    }
2917
2918
    /**
2919
     * Compiles the content for the page <title> tag.
2920
     *
2921
     * @param string $pageTitle The input title string, typically the "title" field of a page's record.
2922
     * @param bool $noTitle If set, then only the site title is outputted (from $this->setup['sitetitle'])
2923
     * @param bool $showTitleFirst If set, then "sitetitle" and $title is swapped
2924
     * @param string $pageTitleSeparator an alternative to the ": " as the separator between site title and page title
2925
     * @return string The page title on the form "[sitetitle]: [input-title]". Not htmlspecialchar()'ed.
2926
     * @see generatePageTitle()
2927
     */
2928
    protected function printTitle(string $pageTitle, bool $noTitle = false, bool $showTitleFirst = false, string $pageTitleSeparator = ''): string
2929
    {
2930
        $websiteTitle = $this->getWebsiteTitle();
2931
        $pageTitle = $noTitle ? '' : $pageTitle;
2932
        if ($showTitleFirst) {
2933
            $temp = $websiteTitle;
2934
            $websiteTitle = $pageTitle;
2935
            $pageTitle = $temp;
2936
        }
2937
        // only show a separator if there are both site title and page title
2938
        if ($pageTitle === '' || $websiteTitle === '') {
2939
            $pageTitleSeparator = '';
2940
        } elseif (empty($pageTitleSeparator)) {
2941
            // use the default separator if non given
2942
            $pageTitleSeparator = ': ';
2943
        }
2944
        return $websiteTitle . $pageTitleSeparator . $pageTitle;
2945
    }
2946
2947
    /**
2948
     * @return string
2949
     */
2950
    protected function getWebsiteTitle(): string
2951
    {
2952
        if ($this->language instanceof SiteLanguage
2953
            && trim($this->language->getWebsiteTitle()) !== ''
2954
        ) {
2955
            return trim($this->language->getWebsiteTitle());
2956
        }
2957
        if ($this->site instanceof SiteInterface
2958
            && trim($this->site->getConfiguration()['websiteTitle']) !== ''
2959
        ) {
2960
            return trim($this->site->getConfiguration()['websiteTitle']);
2961
        }
2962
        if (!empty($this->tmpl->setup['sitetitle'])) {
2963
            // @deprecated since TYPO3 v10.2 and will be removed in TYPO3 v11.0
2964
            return trim($this->tmpl->setup['sitetitle']);
2965
        }
2966
2967
        return '';
2968
    }
2969
2970
    /**
2971
     * Processes the INTinclude-scripts
2972
     */
2973
    public function INTincScript()
2974
    {
2975
        $this->additionalHeaderData = is_array($this->config['INTincScript_ext']['additionalHeaderData'] ?? false)
2976
            ? $this->config['INTincScript_ext']['additionalHeaderData']
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
2977
            : [];
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
2978
        $this->additionalFooterData = is_array($this->config['INTincScript_ext']['additionalFooterData'] ?? false)
2979
            ? $this->config['INTincScript_ext']['additionalFooterData']
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
2980
            : [];
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
2981
        $this->additionalJavaScript = $this->config['INTincScript_ext']['additionalJavaScript'] ?? null;
2982
        $this->additionalCSS = $this->config['INTincScript_ext']['additionalCSS'] ?? null;
2983
        if (empty($this->config['INTincScript_ext']['pageRenderer'])) {
2984
            $this->initPageRenderer();
2985
        } else {
2986
            /** @var PageRenderer $pageRenderer */
2987
            $pageRenderer = unserialize($this->config['INTincScript_ext']['pageRenderer']);
2988
            $this->pageRenderer = $pageRenderer;
2989
            GeneralUtility::setSingletonInstance(PageRenderer::class, $pageRenderer);
2990
        }
2991
        if (!empty($this->config['INTincScript_ext']['assetCollector'])) {
2992
            /** @var AssetCollector $assetCollector */
2993
            $assetCollector = unserialize($this->config['INTincScript_ext']['assetCollector'], ['allowed_classes' => [AssetCollector::class]]);
2994
            GeneralUtility::setSingletonInstance(AssetCollector::class, $assetCollector);
2995
        }
2996
2997
        $this->recursivelyReplaceIntPlaceholdersInContent();
2998
        $this->getTimeTracker()->push('Substitute header section');
2999
        $this->INTincScript_loadJSCode();
3000
        $this->generatePageTitle();
3001
3002
        $this->content = str_replace(
3003
            [
3004
                '<!--HD_' . $this->config['INTincScript_ext']['divKey'] . '-->',
3005
                '<!--FD_' . $this->config['INTincScript_ext']['divKey'] . '-->',
3006
            ],
3007
            [
3008
                $this->convOutputCharset(implode(LF, $this->additionalHeaderData)),
3009
                $this->convOutputCharset(implode(LF, $this->additionalFooterData)),
3010
            ],
3011
            $this->pageRenderer->renderJavaScriptAndCssForProcessingOfUncachedContentObjects($this->content, $this->config['INTincScript_ext']['divKey'])
3012
        );
3013
        // Replace again, because header and footer data and page renderer replacements may introduce additional placeholders (see #44825)
3014
        $this->recursivelyReplaceIntPlaceholdersInContent();
3015
        $this->setAbsRefPrefix();
3016
        $this->getTimeTracker()->pull();
3017
    }
3018
3019
    /**
3020
     * Replaces INT placeholders (COA_INT and USER_INT) in $this->content
3021
     * In case the replacement adds additional placeholders, it loops
3022
     * until no new placeholders are found any more.
3023
     */
3024
    protected function recursivelyReplaceIntPlaceholdersInContent()
3025
    {
3026
        do {
3027
            $nonCacheableData = $this->config['INTincScript'];
3028
            $this->processNonCacheableContentPartsAndSubstituteContentMarkers($nonCacheableData);
3029
            // Check if there were new items added to INTincScript during the previous execution:
3030
            // array_diff_assoc throws notices if values are arrays but not strings. We suppress this here.
3031
            $nonCacheableData = @array_diff_assoc($this->config['INTincScript'], $nonCacheableData);
3032
            $reprocess = count($nonCacheableData) > 0;
3033
        } while ($reprocess);
3034
    }
3035
3036
    /**
3037
     * Processes the INTinclude-scripts and substitute in content.
3038
     *
3039
     * Takes $this->content, and splits the content by <!--INT_SCRIPT.12345 --> and then puts the content
3040
     * back together.
3041
     *
3042
     * @param array $nonCacheableData $GLOBALS['TSFE']->config['INTincScript'] or part of it
3043
     * @see INTincScript()
3044
     */
3045
    protected function processNonCacheableContentPartsAndSubstituteContentMarkers(array $nonCacheableData)
3046
    {
3047
        $timeTracker = $this->getTimeTracker();
3048
        $timeTracker->push('Split content');
3049
        // Splits content with the key.
3050
        $contentSplitByUncacheableMarkers = explode('<!--INT_SCRIPT.', $this->content);
3051
        $this->content = '';
3052
        $timeTracker->setTSlogMessage('Parts: ' . count($contentSplitByUncacheableMarkers));
3053
        $timeTracker->pull();
3054
        foreach ($contentSplitByUncacheableMarkers as $counter => $contentPart) {
3055
            // If the split had a comment-end after 32 characters it's probably a split-string
3056
            if (substr($contentPart, 32, 3) === '-->') {
3057
                $nonCacheableKey = 'INT_SCRIPT.' . substr($contentPart, 0, 32);
3058
                if (is_array($nonCacheableData[$nonCacheableKey])) {
3059
                    $label = 'Include ' . $nonCacheableData[$nonCacheableKey]['type'];
3060
                    $timeTracker->push($label);
3061
                    $nonCacheableContent = '';
3062
                    $contentObjectRendererForNonCacheable = unserialize($nonCacheableData[$nonCacheableKey]['cObj']);
3063
                    /* @var ContentObjectRenderer $contentObjectRendererForNonCacheable */
3064
                    switch ($nonCacheableData[$nonCacheableKey]['type']) {
3065
                        case 'COA':
3066
                            $nonCacheableContent = $contentObjectRendererForNonCacheable->cObjGetSingle('COA', $nonCacheableData[$nonCacheableKey]['conf']);
3067
                            break;
3068
                        case 'FUNC':
3069
                            $nonCacheableContent = $contentObjectRendererForNonCacheable->cObjGetSingle('USER', $nonCacheableData[$nonCacheableKey]['conf']);
3070
                            break;
3071
                        case 'POSTUSERFUNC':
3072
                            $nonCacheableContent = $contentObjectRendererForNonCacheable->callUserFunction($nonCacheableData[$nonCacheableKey]['postUserFunc'], $nonCacheableData[$nonCacheableKey]['conf'], $nonCacheableData[$nonCacheableKey]['content']);
3073
                            break;
3074
                    }
3075
                    $this->content .= $this->convOutputCharset($nonCacheableContent);
3076
                    $this->content .= substr($contentPart, 35);
3077
                    $timeTracker->pull($nonCacheableContent);
3078
                } else {
3079
                    $this->content .= substr($contentPart, 35);
3080
                }
3081
            } elseif ($counter) {
3082
                // If it's not the first entry (which would be "0" of the array keys), then re-add the INT_SCRIPT part
3083
                $this->content .= '<!--INT_SCRIPT.' . $contentPart;
3084
            } else {
3085
                $this->content .= $contentPart;
3086
            }
3087
        }
3088
    }
3089
3090
    /**
3091
     * Loads the JavaScript/CSS code for INTincScript, if there are non-cacheable content objects
3092
     * it prepares the placeholders, otherwise populates options directly.
3093
     *
3094
     * @internal this method should be renamed as it does not only handle JS, but all additional header data
3095
     */
3096
    public function INTincScript_loadJSCode()
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::INTincScript_loadJSCode" is not in camel caps format
Loading history...
3097
    {
3098
        // Prepare code and placeholders for additional header and footer files (and make sure that this isn't called twice)
3099
        if ($this->isINTincScript() && !isset($this->config['INTincScript_ext'])) {
3100
            // Storing the JSCode vars...
3101
            $this->additionalHeaderData['JSCode'] = $this->JSCode;
3102
            $this->config['INTincScript_ext']['divKey'] = $this->uniqueHash();
3103
            // Storing the header-data array
3104
            $this->config['INTincScript_ext']['additionalHeaderData'] = $this->additionalHeaderData;
3105
            // Storing the footer-data array
3106
            $this->config['INTincScript_ext']['additionalFooterData'] = $this->additionalFooterData;
3107
            // Storing the JS-data array
3108
            $this->config['INTincScript_ext']['additionalJavaScript'] = $this->additionalJavaScript;
3109
            // Storing the Style-data array
3110
            $this->config['INTincScript_ext']['additionalCSS'] = $this->additionalCSS;
3111
            // Clearing the array
3112
            $this->additionalHeaderData = ['<!--HD_' . $this->config['INTincScript_ext']['divKey'] . '-->'];
3113
            // Clearing the array
3114
            $this->additionalFooterData = ['<!--FD_' . $this->config['INTincScript_ext']['divKey'] . '-->'];
3115
        } else {
3116
            // Add javascript in a "regular" fashion
3117
            $jsCode = trim($this->JSCode);
3118
            $additionalJavaScript = is_array($this->additionalJavaScript)
0 ignored issues
show
introduced by
The condition is_array($this->additionalJavaScript) is always true.
Loading history...
3119
                ? implode(LF, $this->additionalJavaScript)
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "?"; newline found
Loading history...
3120
                : $this->additionalJavaScript;
0 ignored issues
show
Coding Style introduced by
Expected 1 space before ":"; newline found
Loading history...
3121
            $additionalJavaScript = trim($additionalJavaScript);
3122
            if ($jsCode !== '' || $additionalJavaScript !== '') {
3123
                $doctype = $this->config['config']['doctype'] ?? 'html5';
3124
                $scriptAttribute = $doctype === 'html5' ? '' : ' type="text/javascript"';
3125
3126
                $this->additionalHeaderData['JSCode'] = '
3127
<script' . $scriptAttribute . '>
3128
	/*<![CDATA[*/
3129
<!--
3130
' . $additionalJavaScript . '
3131
' . $jsCode . '
3132
// -->
3133
	/*]]>*/
3134
</script>';
3135
            }
3136
            // Add CSS
3137
            $additionalCss = is_array($this->additionalCSS) ? implode(LF, $this->additionalCSS) : $this->additionalCSS;
0 ignored issues
show
introduced by
The condition is_array($this->additionalCSS) is always true.
Loading history...
3138
            $additionalCss = trim($additionalCss);
3139
            if ($additionalCss !== '') {
3140
                $this->additionalHeaderData['_CSS'] = '
3141
<style type="text/css">
3142
' . $additionalCss . '
3143
</style>';
3144
            }
3145
        }
3146
    }
3147
3148
    /**
3149
     * Determines if there are any INTincScripts to include = "non-cacheable" parts
3150
     *
3151
     * @return bool Returns TRUE if scripts are found
3152
     */
3153
    public function isINTincScript()
3154
    {
3155
        return !empty($this->config['INTincScript']) && is_array($this->config['INTincScript']);
3156
    }
3157
3158
    /********************************************
3159
     *
3160
     * Finished off; outputting, storing session data, statistics...
3161
     *
3162
     *******************************************/
3163
    /**
3164
     * Determines if content should be outputted.
3165
     * Outputting content is done only if no URL handler is active and no hook disables the output.
3166
     *
3167
     * @return bool Returns TRUE if no redirect URL is set and no hook disables the output.
3168
     */
3169
    public function isOutputting()
3170
    {
3171
        // Initialize by status if there is a Redirect URL
3172
        $enableOutput = true;
3173
        // Call hook for possible disabling of output:
3174
        $_params = ['pObj' => &$this, 'enableOutput' => &$enableOutput];
3175
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['isOutputting'] ?? [] as $_funcRef) {
3176
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
3177
        }
3178
        return $enableOutput;
3179
    }
3180
3181
    /**
3182
     * Process the output before it's actually outputted.
3183
     *
3184
     * This includes substituting the "username" comment.
3185
     * Works on $this->content.
3186
     */
3187
    public function processContentForOutput()
3188
    {
3189
        // Make substitution of eg. username/uid in content only if cache-headers for client/proxy caching is NOT sent!
3190
        if (!$this->isClientCachable) {
3191
            // Substitute various tokens in content. This should happen only if the content is not cached by proxies or client browsers.
3192
            $search = [];
3193
            $replace = [];
3194
            // Hook for supplying custom search/replace data
3195
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['tslib_fe-contentStrReplace'] ?? [] as $_funcRef) {
3196
                $_params = [
3197
                    'search' => &$search,
3198
                    'replace' => &$replace
3199
                ];
3200
                GeneralUtility::callUserFunction($_funcRef, $_params, $this);
3201
            }
3202
            if (!empty($search)) {
3203
                $this->content = str_replace($search, $replace, $this->content);
3204
            }
3205
        }
3206
        // Hook for post-processing of page content before output:
3207
        $_params = ['pObj' => &$this];
3208
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-output'] ?? [] as $_funcRef) {
3209
            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
3210
        }
3211
    }
3212
3213
    /**
3214
     * Add HTTP headers to the response object.
3215
     *
3216
     * @param ResponseInterface $response
3217
     * @return ResponseInterface
3218
     */
3219
    public function applyHttpHeadersToResponse(ResponseInterface $response): ResponseInterface
3220
    {
3221
        // Set header for charset-encoding unless disabled
3222
        if (empty($this->config['config']['disableCharsetHeader'])) {
3223
            $response = $response->withHeader('Content-Type', $this->contentType . '; charset=' . trim($this->metaCharset));
3224
        }
3225
        // Set header for content language unless disabled
3226
        $contentLanguage = $this->language->getTwoLetterIsoCode();
3227
        if (empty($this->config['config']['disableLanguageHeader']) && !empty($contentLanguage)) {
3228
            $response = $response->withHeader('Content-Language', trim($contentLanguage));
3229
        }
3230
        // Set cache related headers to client (used to enable proxy / client caching!)
3231
        if (!empty($this->config['config']['sendCacheHeaders'])) {
3232
            $headers = $this->getCacheHeaders();
3233
            foreach ($headers as $header => $value) {
3234
                $response = $response->withHeader($header, $value);
3235
            }
3236
        }
3237
        // Set additional headers if any have been configured via TypoScript
3238
        $additionalHeaders = $this->getAdditionalHeaders();
3239
        foreach ($additionalHeaders as $headerConfig) {
3240
            [$header, $value] = GeneralUtility::trimExplode(':', $headerConfig['header'], false, 2);
3241
            if ($headerConfig['statusCode']) {
3242
                $response = $response->withStatus((int)$headerConfig['statusCode']);
3243
            }
3244
            if ($headerConfig['replace']) {
3245
                $response = $response->withHeader($header, $value);
3246
            } else {
3247
                $response = $response->withAddedHeader($header, $value);
3248
            }
3249
        }
3250
        return $response;
3251
    }
3252
3253
    /**
3254
     * Get cache headers good for client/reverse proxy caching.
3255
     *
3256
     * @return array
3257
     */
3258
    protected function getCacheHeaders(): array
3259
    {
3260
        // Getting status whether we can send cache control headers for proxy caching:
3261
        $doCache = $this->isStaticCacheble();
3262
        // This variable will be TRUE unless cache headers are configured to be sent ONLY if a branch does not allow logins and logins turns out to be allowed anyway...
3263
        $loginsDeniedCfg = empty($this->config['config']['sendCacheHeaders_onlyWhenLoginDeniedInBranch']) || empty($this->loginAllowedInBranch);
3264
        // Finally, when backend users are logged in, do not send cache headers at all (Admin Panel might be displayed for instance).
3265
        $this->isClientCachable = $doCache && !$this->isBackendUserLoggedIn() && !$this->doWorkspacePreview() && $loginsDeniedCfg;
3266
        if ($this->isClientCachable) {
3267
            $headers = [
3268
                'Expires' => gmdate('D, d M Y H:i:s T', $this->cacheExpires),
3269
                'ETag' => '"' . md5($this->content) . '"',
3270
                'Cache-Control' => 'max-age=' . ($this->cacheExpires - $GLOBALS['EXEC_TIME']),
3271
                // no-cache
3272
                'Pragma' => 'public'
3273
            ];
3274
        } else {
3275
            // "no-store" is used to ensure that the client HAS to ask the server every time, and is not allowed to store anything at all
3276
            $headers = [
3277
                'Cache-Control' => 'private, no-store'
3278
            ];
3279
            // Now, if a backend user is logged in, tell him in the Admin Panel log what the caching status would have been:
3280
            if ($this->isBackendUserLoggedIn()) {
3281
                if ($doCache) {
3282
                    $this->getTimeTracker()->setTSlogMessage('Cache-headers with max-age "' . ($this->cacheExpires - $GLOBALS['EXEC_TIME']) . '" would have been sent');
3283
                } else {
3284
                    $reasonMsg = [];
3285
                    if ($this->no_cache) {
3286
                        $reasonMsg[] = 'Caching disabled (no_cache).';
3287
                    }
3288
                    if ($this->isINTincScript()) {
3289
                        $reasonMsg[] = '*_INT object(s) on page.';
3290
                    }
3291
                    if (is_array($this->fe_user->user)) {
3292
                        $reasonMsg[] = 'Frontend user logged in.';
3293
                    }
3294
                    $this->getTimeTracker()->setTSlogMessage('Cache-headers would disable proxy caching! Reason(s): "' . implode(' ', $reasonMsg) . '"', 1);
3295
                }
3296
            }
3297
        }
3298
        return $headers;
3299
    }
3300
3301
    /**
3302
     * Reporting status whether we can send cache control headers for proxy caching or publishing to static files
3303
     *
3304
     * Rules are:
3305
     * no_cache cannot be set: If it is, the page might contain dynamic content and should never be cached.
3306
     * There can be no USER_INT objects on the page ("isINTincScript()") because they implicitly indicate dynamic content
3307
     * There can be no logged in user because user sessions are based on a cookie and thereby does not offer client caching a chance to know if the user is logged in. Actually, there will be a reverse problem here; If a page will somehow change when a user is logged in he may not see it correctly if the non-login version sent a cache-header! So do NOT use cache headers in page sections where user logins change the page content. (unless using such as realurl to apply a prefix in case of login sections)
3308
     *
3309
     * @return bool
3310
     */
3311
    public function isStaticCacheble()
3312
    {
3313
        return !$this->no_cache && !$this->isINTincScript() && !$this->isUserOrGroupSet();
3314
    }
3315
3316
    /********************************************
3317
     *
3318
     * Various internal API functions
3319
     *
3320
     *******************************************/
3321
3322
    /**
3323
     * Creates an instance of ContentObjectRenderer in $this->cObj
3324
     * This instance is used to start the rendering of the TypoScript template structure
3325
     *
3326
     * @see RequestHandler
3327
     */
3328
    public function newCObj()
3329
    {
3330
        $this->cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class, $this);
3331
        $this->cObj->start($this->page, 'pages');
3332
    }
3333
3334
    /**
3335
     * Converts relative paths in the HTML source to absolute paths for fileadmin/, typo3conf/ext/ and media/ folders.
3336
     *
3337
     * @internal
3338
     * @see \TYPO3\CMS\Frontend\Http\RequestHandler
3339
     * @see INTincScript()
3340
     */
3341
    public function setAbsRefPrefix()
3342
    {
3343
        if (!$this->absRefPrefix) {
3344
            return;
3345
        }
3346
        $search = [
3347
            '"typo3temp/',
3348
            '"' . PathUtility::stripPathSitePrefix(Environment::getExtensionsPath()) . '/',
3349
            '"' . PathUtility::stripPathSitePrefix(Environment::getBackendPath()) . '/ext/',
3350
            '"' . PathUtility::stripPathSitePrefix(Environment::getFrameworkBasePath()) . '/',
3351
        ];
3352
        $replace = [
3353
            '"' . $this->absRefPrefix . 'typo3temp/',
3354
            '"' . $this->absRefPrefix . PathUtility::stripPathSitePrefix(Environment::getExtensionsPath()) . '/',
3355
            '"' . $this->absRefPrefix . PathUtility::stripPathSitePrefix(Environment::getBackendPath()) . '/ext/',
3356
            '"' . $this->absRefPrefix . PathUtility::stripPathSitePrefix(Environment::getFrameworkBasePath()) . '/',
3357
        ];
3358
        /** @var StorageRepository $storageRepository */
3359
        $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
3360
        $storages = $storageRepository->findAll();
3361
        foreach ($storages as $storage) {
3362
            if ($storage->getDriverType() === 'Local' && $storage->isPublic() && $storage->isOnline()) {
3363
                $folder = $storage->getPublicUrl($storage->getRootLevelFolder(), true);
3364
                $search[] = '"' . $folder;
3365
                $replace[] = '"' . $this->absRefPrefix . $folder;
3366
            }
3367
        }
3368
        // Process additional directories
3369
        $directories = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['FE']['additionalAbsRefPrefixDirectories'], true);
3370
        foreach ($directories as $directory) {
3371
            $search[] = '"' . $directory;
3372
            $replace[] = '"' . $this->absRefPrefix . $directory;
3373
        }
3374
        $this->content = str_replace(
3375
            $search,
3376
            $replace,
3377
            $this->content
3378
        );
3379
    }
3380
3381
    /**
3382
     * Prefixing the input URL with ->baseUrl If ->baseUrl is set and the input url is not absolute in some way.
3383
     * Designed as a wrapper functions for use with all frontend links that are processed by JavaScript (for "realurl" compatibility!). So each time a URL goes into window.open, window.location.href or otherwise, wrap it with this function!
3384
     *
3385
     * @param string $url Input URL, relative or absolute
3386
     * @return string Processed input value.
3387
     */
3388
    public function baseUrlWrap($url)
3389
    {
3390
        if ($this->baseUrl) {
3391
            $urlParts = parse_url($url);
3392
            if (empty($urlParts['scheme']) && $url[0] !== '/') {
3393
                $url = $this->baseUrl . $url;
3394
            }
3395
        }
3396
        return $url;
3397
    }
3398
3399
    /**
3400
     * Logs access to deprecated TypoScript objects and properties.
3401
     *
3402
     * Dumps message to the TypoScript message log (admin panel) and the TYPO3 deprecation log.
3403
     *
3404
     * @param string $typoScriptProperty Deprecated object or property
3405
     * @param string $explanation Message or additional information
3406
     */
3407
    public function logDeprecatedTyposcript($typoScriptProperty, $explanation = '')
3408
    {
3409
        $explanationText = $explanation !== '' ? ' - ' . $explanation : '';
3410
        $this->getTimeTracker()->setTSlogMessage($typoScriptProperty . ' is deprecated.' . $explanationText, 2);
3411
        trigger_error('TypoScript property ' . $typoScriptProperty . ' is deprecated' . $explanationText, E_USER_DEPRECATED);
3412
    }
3413
3414
    /********************************************
3415
     * PUBLIC ACCESSIBLE WORKSPACES FUNCTIONS
3416
     *******************************************/
3417
3418
    /**
3419
     * Returns TRUE if workspace preview is enabled
3420
     *
3421
     * @return bool Returns TRUE if workspace preview is enabled
3422
     */
3423
    public function doWorkspacePreview()
3424
    {
3425
        return $this->context->getPropertyFromAspect('workspace', 'isOffline', false);
3426
    }
3427
3428
    /**
3429
     * Returns the uid of the current workspace
3430
     *
3431
     * @return int returns workspace integer for which workspace is being preview. 0 if none (= live workspace).
3432
     */
3433
    public function whichWorkspace(): int
3434
    {
3435
        return $this->context->getPropertyFromAspect('workspace', 'id', 0);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->context->g...t('workspace', 'id', 0) could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
3436
    }
3437
3438
    /********************************************
3439
     *
3440
     * Various external API functions - for use in plugins etc.
3441
     *
3442
     *******************************************/
3443
3444
    /**
3445
     * Returns the pages TSconfig array based on the current ->rootLine
3446
     *
3447
     * @return array
3448
     */
3449
    public function getPagesTSconfig()
3450
    {
3451
        if (!is_array($this->pagesTSconfig)) {
0 ignored issues
show
introduced by
The condition is_array($this->pagesTSconfig) is always true.
Loading history...
3452
            $contentHashCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('hash');
3453
            $loader = GeneralUtility::makeInstance(PageTsConfigLoader::class);
3454
            $tsConfigString = $loader->load(array_reverse($this->rootLine));
3455
            $parser = GeneralUtility::makeInstance(
3456
                PageTsConfigParser::class,
3457
                GeneralUtility::makeInstance(TypoScriptParser::class),
3458
                $contentHashCache
3459
            );
3460
            $this->pagesTSconfig = $parser->parse(
3461
                $tsConfigString,
3462
                GeneralUtility::makeInstance(ConditionMatcher::class, $this->context, $this->id, $this->rootLine)
3463
            );
3464
        }
3465
        return $this->pagesTSconfig;
3466
    }
3467
3468
    /**
3469
     * Sets JavaScript code in the additionalJavaScript array
3470
     *
3471
     * @param string $key is the key in the array, for num-key let the value be empty. Note reserved key: 'openPic'
3472
     * @param string $content is the content if you want any
3473
     * @see ContentObjectRenderer::imageLinkWrap()
3474
     */
3475
    public function setJS($key, $content = '')
3476
    {
3477
        if ($key === 'openPic') {
3478
            $this->additionalJavaScript[$key] = '	function openPic(url, winName, winParams) {
3479
                var theWindow = window.open(url, winName, winParams);
3480
                if (theWindow)	{theWindow.focus();}
3481
            }';
3482
        } elseif ($key) {
3483
            $this->additionalJavaScript[$key] = $content;
3484
        }
3485
    }
3486
3487
    /**
3488
     * Returns a unique md5 hash.
3489
     * There is no special magic in this, the only point is that you don't have to call md5(uniqid()) which is slow and by this you are sure to get a unique string each time in a little faster way.
3490
     *
3491
     * @param string $str Some string to include in what is hashed. Not significant at all.
3492
     * @return string MD5 hash of ->uniqueString, input string and uniqueCounter
3493
     */
3494
    public function uniqueHash($str = '')
3495
    {
3496
        return md5($this->uniqueString . '_' . $str . $this->uniqueCounter++);
3497
    }
3498
3499
    /**
3500
     * Sets the cache-flag to 1. Could be called from user-included php-files in order to ensure that a page is not cached.
3501
     *
3502
     * @param string $reason An optional reason to be written to the log.
3503
     * @param bool $internal Whether the call is done from core itself (should only be used by core).
3504
     */
3505
    public function set_no_cache($reason = '', $internal = false)
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::set_no_cache" is not in camel caps format
Loading history...
3506
    {
3507
        if ($reason !== '') {
3508
            $warning = '$TSFE->set_no_cache() was triggered. Reason: ' . $reason . '.';
3509
        } else {
3510
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
3511
            // This is a hack to work around ___FILE___ resolving symbolic links
3512
            $realWebPath = PathUtility::dirname(realpath(Environment::getBackendPath())) . '/';
3513
            $file = $trace[0]['file'];
3514
            if (strpos($file, $realWebPath) === 0) {
3515
                $file = str_replace($realWebPath, '', $file);
3516
            } else {
3517
                $file = str_replace(Environment::getPublicPath() . '/', '', $file);
3518
            }
3519
            $line = $trace[0]['line'];
3520
            $trigger = $file . ' on line ' . $line;
3521
            $warning = '$GLOBALS[\'TSFE\']->set_no_cache() was triggered by ' . $trigger . '.';
3522
        }
3523
        if (!$internal && $GLOBALS['TYPO3_CONF_VARS']['FE']['disableNoCacheParameter']) {
3524
            $warning .= ' However, $TYPO3_CONF_VARS[\'FE\'][\'disableNoCacheParameter\'] is set, so it will be ignored!';
3525
            $this->getTimeTracker()->setTSlogMessage($warning, 2);
3526
        } else {
3527
            $warning .= ' Caching is disabled!';
3528
            $this->disableCache();
3529
        }
3530
        if ($internal && $this->isBackendUserLoggedIn()) {
3531
            $this->logger->notice($warning);
3532
        } else {
3533
            $this->logger->warning($warning);
3534
        }
3535
    }
3536
3537
    /**
3538
     * Disables caching of the current page.
3539
     *
3540
     * @internal
3541
     */
3542
    protected function disableCache()
3543
    {
3544
        $this->no_cache = true;
3545
    }
3546
3547
    /**
3548
     * Sets the cache-timeout in seconds
3549
     *
3550
     * @param int $seconds Cache-timeout in seconds
3551
     */
3552
    public function set_cache_timeout_default($seconds)
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::set_cache_timeout_default" is not in camel caps format
Loading history...
3553
    {
3554
        $seconds = (int)$seconds;
3555
        if ($seconds > 0) {
3556
            $this->cacheTimeOutDefault = $seconds;
3557
        }
3558
    }
3559
3560
    /**
3561
     * Get the cache timeout for the current page.
3562
     *
3563
     * @return int The cache timeout for the current page.
3564
     */
3565
    public function get_cache_timeout()
0 ignored issues
show
Coding Style introduced by
Method name "TypoScriptFrontendController::get_cache_timeout" is not in camel caps format
Loading history...
3566
    {
3567
        /** @var \TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend $runtimeCache */
3568
        $runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
3569
        $cachedCacheLifetimeIdentifier = 'core-tslib_fe-get_cache_timeout';
3570
        $cachedCacheLifetime = $runtimeCache->get($cachedCacheLifetimeIdentifier);
3571
        if ($cachedCacheLifetime === false) {
3572
            if ($this->page['cache_timeout']) {
3573
                // Cache period was set for the page:
3574
                $cacheTimeout = $this->page['cache_timeout'];
3575
            } else {
3576
                // Cache period was set via TypoScript "config.cache_period",
3577
                // otherwise it's the default of 24 hours
3578
                $cacheTimeout = $this->cacheTimeOutDefault;
3579
            }
3580
            if (!empty($this->config['config']['cache_clearAtMidnight'])) {
3581
                $timeOutTime = $GLOBALS['EXEC_TIME'] + $cacheTimeout;
3582
                $midnightTime = mktime(0, 0, 0, date('m', $timeOutTime), date('d', $timeOutTime), date('Y', $timeOutTime));
0 ignored issues
show
Bug introduced by
date('Y', $timeOutTime) of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3582
                $midnightTime = mktime(0, 0, 0, date('m', $timeOutTime), date('d', $timeOutTime), /** @scrutinizer ignore-type */ date('Y', $timeOutTime));
Loading history...
Bug introduced by
date('d', $timeOutTime) of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3582
                $midnightTime = mktime(0, 0, 0, date('m', $timeOutTime), /** @scrutinizer ignore-type */ date('d', $timeOutTime), date('Y', $timeOutTime));
Loading history...
Bug introduced by
date('m', $timeOutTime) of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3582
                $midnightTime = mktime(0, 0, 0, /** @scrutinizer ignore-type */ date('m', $timeOutTime), date('d', $timeOutTime), date('Y', $timeOutTime));
Loading history...
3583
                // If the midnight time of the expire-day is greater than the current time,
3584
                // we may set the timeOutTime to the new midnighttime.
3585
                if ($midnightTime > $GLOBALS['EXEC_TIME']) {
3586
                    $cacheTimeout = $midnightTime - $GLOBALS['EXEC_TIME'];
3587
                }
3588
            }
3589
3590
            // Calculate the timeout time for records on the page and adjust cache timeout if necessary
3591
            $cacheTimeout = min($this->calculatePageCacheTimeout(), $cacheTimeout);
3592
3593
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['get_cache_timeout'] ?? [] as $_funcRef) {
3594
                $params = ['cacheTimeout' => $cacheTimeout];
3595
                $cacheTimeout = GeneralUtility::callUserFunction($_funcRef, $params, $this);
3596
            }
3597
            $runtimeCache->set($cachedCacheLifetimeIdentifier, $cacheTimeout);
3598
            $cachedCacheLifetime = $cacheTimeout;
3599
        }
3600
        return $cachedCacheLifetime;
3601
    }
3602
3603
    /*********************************************
3604
     *
3605
     * Localization and character set conversion
3606
     *
3607
     *********************************************/
3608
    /**
3609
     * Split Label function for front-end applications.
3610
     *
3611
     * @param string $input Key string. Accepts the "LLL:" prefix.
3612
     * @return string Label value, if any.
3613
     */
3614
    public function sL($input)
3615
    {
3616
        return $this->languageService->sL($input);
3617
    }
3618
3619
    /**
3620
     * Sets all internal measures what language the page should be rendered.
3621
     * This is not for records, but rather the HTML / charset and the locallang labels
3622
     */
3623
    protected function setOutputLanguage()
3624
    {
3625
        $this->languageService = LanguageService::createFromSiteLanguage($this->language);
3626
        // Always disable debugging for TSFE
3627
        $this->languageService->debugKey = false;
3628
    }
3629
3630
    /**
3631
     * Converts input string from utf-8 to metaCharset IF the two charsets are different.
3632
     *
3633
     * @param string $content Content to be converted.
3634
     * @return string Converted content string.
3635
     * @throws \RuntimeException if an invalid charset was configured
3636
     */
3637
    public function convOutputCharset($content)
3638
    {
3639
        if ($this->metaCharset !== 'utf-8') {
3640
            /** @var CharsetConverter $charsetConverter */
3641
            $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
3642
            try {
3643
                $content = $charsetConverter->conv($content, 'utf-8', $this->metaCharset);
3644
            } catch (UnknownCharsetException $e) {
3645
                throw new \RuntimeException('Invalid config.metaCharset: ' . $e->getMessage(), 1508916185);
3646
            }
3647
        }
3648
        return $content;
3649
    }
3650
3651
    /**
3652
     * Calculates page cache timeout according to the records with starttime/endtime on the page.
3653
     *
3654
     * @return int Page cache timeout or PHP_INT_MAX if cannot be determined
3655
     */
3656
    protected function calculatePageCacheTimeout()
3657
    {
3658
        $result = PHP_INT_MAX;
3659
        // Get the configuration
3660
        $tablesToConsider = $this->getCurrentPageCacheConfiguration();
3661
        // Get the time, rounded to the minute (do not pollute MySQL cache!)
3662
        // It is ok that we do not take seconds into account here because this
3663
        // value will be subtracted later. So we never get the time "before"
3664
        // the cache change.
3665
        $now = $GLOBALS['ACCESS_TIME'];
3666
        // Find timeout by checking every table
3667
        foreach ($tablesToConsider as $tableDef) {
3668
            $result = min($result, $this->getFirstTimeValueForRecord($tableDef, $now));
3669
        }
3670
        // We return + 1 second just to ensure that cache is definitely regenerated
3671
        return $result === PHP_INT_MAX ? PHP_INT_MAX : $result - $now + 1;
3672
    }
3673
3674
    /**
3675
     * Obtains a list of table/pid pairs to consider for page caching.
3676
     *
3677
     * TS configuration looks like this:
3678
     *
3679
     * The cache lifetime of all pages takes starttime and endtime of news records of page 14 into account:
3680
     * config.cache.all = tt_news:14
3681
     *
3682
     * The cache.lifetime of the current page allows to take records (e.g. fe_users) into account:
3683
     * config.cache.all = fe_users:current
3684
     *
3685
     * The cache lifetime of page 42 takes starttime and endtime of news records of page 15 and addresses of page 16 into account:
3686
     * config.cache.42 = tt_news:15,tt_address:16
3687
     *
3688
     * @return array Array of 'tablename:pid' pairs. There is at least a current page id in the array
3689
     * @see TypoScriptFrontendController::calculatePageCacheTimeout()
3690
     */
3691
    protected function getCurrentPageCacheConfiguration()
3692
    {
3693
        $result = ['tt_content:' . $this->id];
3694
        if (isset($this->config['config']['cache.'][$this->id])) {
3695
            $result = array_merge($result, GeneralUtility::trimExplode(',', str_replace(':current', ':' . $this->id, $this->config['config']['cache.'][$this->id])));
3696
        }
3697
        if (isset($this->config['config']['cache.']['all'])) {
3698
            $result = array_merge($result, GeneralUtility::trimExplode(',', str_replace(':current', ':' . $this->id, $this->config['config']['cache.']['all'])));
3699
        }
3700
        return array_unique($result);
3701
    }
3702
3703
    /**
3704
     * Find the minimum starttime or endtime value in the table and pid that is greater than the current time.
3705
     *
3706
     * @param string $tableDef Table definition (format tablename:pid)
3707
     * @param int $now "Now" time value
3708
     * @throws \InvalidArgumentException
3709
     * @return int Value of the next start/stop time or PHP_INT_MAX if not found
3710
     * @see TypoScriptFrontendController::calculatePageCacheTimeout()
3711
     */
3712
    protected function getFirstTimeValueForRecord($tableDef, $now)
3713
    {
3714
        $now = (int)$now;
3715
        $result = PHP_INT_MAX;
3716
        [$tableName, $pid] = GeneralUtility::trimExplode(':', $tableDef);
3717
        if (empty($tableName) || empty($pid)) {
3718
            throw new \InvalidArgumentException('Unexpected value for parameter $tableDef. Expected <tablename>:<pid>, got \'' . htmlspecialchars($tableDef) . '\'.', 1307190365);
3719
        }
3720
3721
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
3722
            ->getQueryBuilderForTable($tableName);
3723
        $queryBuilder->getRestrictions()
3724
            ->removeByType(StartTimeRestriction::class)
3725
            ->removeByType(EndTimeRestriction::class);
3726
        $timeFields = [];
3727
        $timeConditions = $queryBuilder->expr()->orX();
3728
        foreach (['starttime', 'endtime'] as $field) {
3729
            if (isset($GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'][$field])) {
3730
                $timeFields[$field] = $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'][$field];
3731
                $queryBuilder->addSelectLiteral(
3732
                    'MIN('
3733
                        . 'CASE WHEN '
3734
                        . $queryBuilder->expr()->lte(
3735
                            $timeFields[$field],
3736
                            $queryBuilder->createNamedParameter($now, \PDO::PARAM_INT)
3737
                        )
3738
                        . ' THEN NULL ELSE ' . $queryBuilder->quoteIdentifier($timeFields[$field]) . ' END'
3739
                        . ') AS ' . $queryBuilder->quoteIdentifier($timeFields[$field])
3740
                );
3741
                $timeConditions->add(
3742
                    $queryBuilder->expr()->gt(
3743
                        $timeFields[$field],
3744
                        $queryBuilder->createNamedParameter($now, \PDO::PARAM_INT)
3745
                    )
3746
                );
3747
            }
3748
        }
3749
3750
        // if starttime or endtime are defined, evaluate them
3751
        if (!empty($timeFields)) {
3752
            // find the timestamp, when the current page's content changes the next time
3753
            $row = $queryBuilder
3754
                ->from($tableName)
3755
                ->where(
3756
                    $queryBuilder->expr()->eq(
3757
                        'pid',
3758
                        $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
3759
                    ),
3760
                    $timeConditions
3761
                )
3762
                ->execute()
3763
                ->fetch();
3764
3765
            if ($row) {
3766
                foreach ($timeFields as $timeField => $_) {
3767
                    // if a MIN value is found, take it into account for the
3768
                    // cache lifetime we have to filter out start/endtimes < $now,
3769
                    // as the SQL query also returns rows with starttime < $now
3770
                    // and endtime > $now (and using a starttime from the past
3771
                    // would be wrong)
3772
                    if ($row[$timeField] !== null && (int)$row[$timeField] > $now) {
3773
                        $result = min($result, (int)$row[$timeField]);
3774
                    }
3775
                }
3776
            }
3777
        }
3778
3779
        return $result;
3780
    }
3781
3782
    /**
3783
     * Fetches the originally requested id, falls back to $this->id
3784
     *
3785
     * @return int the originally requested page uid
3786
     * @see fetch_the_id()
3787
     */
3788
    public function getRequestedId()
3789
    {
3790
        return $this->requestedId ?: $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->requestedId ?: $this->id also could return the type string which is incompatible with the documented return type integer.
Loading history...
3791
    }
3792
3793
    /**
3794
     * Acquire a page specific lock
3795
     *
3796
     *
3797
     * The schematics here is:
3798
     * - First acquire an access lock. This is using the type of the requested lock as key.
3799
     *   Since the number of types is rather limited we can use the type as key as it will only
3800
     *   eat up a limited number of lock resources on the system (files, semaphores)
3801
     * - Second, we acquire the actual lock (named page lock). We can be sure we are the only process at this
3802
     *   very moment, hence we either get the lock for the given key or we get an error as we request a non-blocking mode.
3803
     *
3804
     * Interleaving two locks is extremely important, because the actual page lock uses a hash value as key (see callers
3805
     * of this function). If we would simply employ a normal blocking lock, we would get a potentially unlimited
3806
     * (number of pages at least) number of different locks. Depending on the available locking methods on the system
3807
     * we might run out of available resources. (e.g. maximum limit of semaphores is a system setting and applies
3808
     * to the whole system)
3809
     * We therefore must make sure that page locks are destroyed again if they are not used anymore, such that
3810
     * we never use more locking resources than parallel requests to different pages (hashes).
3811
     * In order to ensure this, we need to guarantee that no other process is waiting on a page lock when
3812
     * the process currently having the lock on the page lock is about to release the lock again.
3813
     * This can only be achieved by using a non-blocking mode, such that a process is never put into wait state
3814
     * by the kernel, but only checks the availability of the lock. The access lock is our guard to be sure
3815
     * that no two processes are at the same time releasing/destroying a page lock, whilst the other one tries to
3816
     * get a lock for this page lock.
3817
     * The only drawback of this implementation is that we basically have to poll the availability of the page lock.
3818
     *
3819
     * Note that the access lock resources are NEVER deleted/destroyed, otherwise the whole thing would be broken.
3820
     *
3821
     * @param string $type
3822
     * @param string $key
3823
     * @throws \InvalidArgumentException
3824
     * @throws \RuntimeException
3825
     * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
3826
     */
3827
    protected function acquireLock($type, $key)
3828
    {
3829
        $lockFactory = GeneralUtility::makeInstance(LockFactory::class);
3830
        $this->locks[$type]['accessLock'] = $lockFactory->createLocker($type);
3831
3832
        $this->locks[$type]['pageLock'] = $lockFactory->createLocker(
3833
            $key,
3834
            LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK
3835
        );
3836
3837
        do {
3838
            if (!$this->locks[$type]['accessLock']->acquire()) {
3839
                throw new \RuntimeException('Could not acquire access lock for "' . $type . '"".', 1294586098);
3840
            }
3841
3842
            try {
3843
                $locked = $this->locks[$type]['pageLock']->acquire(
3844
                    LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK
3845
                );
3846
            } catch (LockAcquireWouldBlockException $e) {
3847
                // somebody else has the lock, we keep waiting
3848
3849
                // first release the access lock
3850
                $this->locks[$type]['accessLock']->release();
3851
                // now lets make a short break (100ms) until we try again, since
3852
                // the page generation by the lock owner will take a while anyways
3853
                usleep(100000);
3854
                continue;
3855
            }
3856
            $this->locks[$type]['accessLock']->release();
3857
            if ($locked) {
3858
                break;
3859
            }
3860
            throw new \RuntimeException('Could not acquire page lock for ' . $key . '.', 1460975877);
3861
        } while (true);
3862
    }
3863
3864
    /**
3865
     * Release a page specific lock
3866
     *
3867
     * @param string $type
3868
     * @throws \InvalidArgumentException
3869
     * @throws \RuntimeException
3870
     * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
3871
     */
3872
    protected function releaseLock($type)
3873
    {
3874
        if ($this->locks[$type]['accessLock']) {
3875
            if (!$this->locks[$type]['accessLock']->acquire()) {
3876
                throw new \RuntimeException('Could not acquire access lock for "' . $type . '"".', 1460975902);
3877
            }
3878
3879
            $this->locks[$type]['pageLock']->release();
3880
            $this->locks[$type]['pageLock']->destroy();
3881
            $this->locks[$type]['pageLock'] = null;
3882
3883
            $this->locks[$type]['accessLock']->release();
3884
            $this->locks[$type]['accessLock'] = null;
3885
        }
3886
    }
3887
3888
    /**
3889
     * Send additional headers from config.additionalHeaders
3890
     */
3891
    protected function getAdditionalHeaders(): array
3892
    {
3893
        if (!isset($this->config['config']['additionalHeaders.'])) {
3894
            return [];
3895
        }
3896
        $additionalHeaders = [];
3897
        ksort($this->config['config']['additionalHeaders.']);
3898
        foreach ($this->config['config']['additionalHeaders.'] as $options) {
3899
            if (!is_array($options)) {
3900
                continue;
3901
            }
3902
            $header = trim($options['header'] ?? '');
3903
            if ($header === '') {
3904
                continue;
3905
            }
3906
            $additionalHeaders[] = [
3907
                'header' => $header,
3908
                // "replace existing headers" is turned on by default, unless turned off
3909
                'replace' => ($options['replace'] ?? '') !== '0',
3910
                'statusCode' => (int)($options['httpResponseCode'] ?? 0) ?: null
3911
            ];
3912
        }
3913
        return $additionalHeaders;
3914
    }
3915
3916
    /**
3917
     * Returns the current BE user.
3918
     *
3919
     * @return \TYPO3\CMS\Backend\FrontendBackendUserAuthentication
3920
     */
3921
    protected function getBackendUser()
3922
    {
3923
        return $GLOBALS['BE_USER'];
3924
    }
3925
3926
    /**
3927
     * @return TimeTracker
3928
     */
3929
    protected function getTimeTracker()
3930
    {
3931
        return GeneralUtility::makeInstance(TimeTracker::class);
3932
    }
3933
3934
    /**
3935
     * Return the global instance of this class.
3936
     *
3937
     * Intended to be used as prototype factory for this class, see Services.yaml.
3938
     * This is required as long as TypoScriptFrontendController needs request
3939
     * dependent constructor parameters. Once that has been refactored this
3940
     * factory will be removed.
3941
     *
3942
     * @return TypoScriptFrontendController
3943
     * @internal
3944
     */
3945
    public static function getGlobalInstance(): ?self
3946
    {
3947
        if ($GLOBALS['TSFE'] instanceof self) {
3948
            return $GLOBALS['TSFE'];
3949
        }
3950
3951
        if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE)) {
3952
            // Return null for now (together with shared: false in Services.yaml) as TSFE might not be available in backend context
3953
            // That's not an error then
3954
            return null;
3955
        }
3956
3957
        throw new \LogicException('TypoScriptFrontendController was tried to be injected before initial creation', 1538370377);
3958
    }
3959
3960
    public function getLanguage(): SiteLanguage
3961
    {
3962
        return $this->language;
3963
    }
3964
3965
    public function getSite(): Site
3966
    {
3967
        return $this->site;
3968
    }
3969
3970
    public function getContext(): Context
3971
    {
3972
        return $this->context;
3973
    }
3974
3975
    public function getPageArguments(): PageArguments
3976
    {
3977
        return $this->pageArguments;
3978
    }
3979
3980
    /**
3981
     * Deprecation messages for TYPO3 10 - public properties of TSFE which have been (re)moved
3982
     */
3983
3984
    /**
3985
     * Checks if the property of the given name is set.
3986
     *
3987
     * Unmarked protected properties must return false as usual.
3988
     * Marked properties are evaluated by isset().
3989
     *
3990
     * This method is not called for public properties.
3991
     *
3992
     * @param string $propertyName
3993
     * @return bool
3994
     */
3995
    public function __isset(string $propertyName)
3996
    {
3997
        switch ($propertyName) {
3998
            case 'domainStartPage':
3999
                trigger_error('Property $TSFE->domainStartPage is not in use anymore as this information is now stored within the Site object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4000
                return  true;
0 ignored issues
show
Coding Style introduced by
Language constructs must be followed by a single space; expected 1 space but found 2
Loading history...
4001
            case 'cHash':
4002
                trigger_error('Property $TSFE->cHash is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4003
                return isset($this->pageArguments->getArguments()['cHash']);
4004
            case 'cHash_array':
4005
                trigger_error('Property $TSFE->cHash_array is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4006
                $value = $this->getRelevantParametersForCachingFromPageArguments($this->pageArguments);
4007
                return !empty($value);
4008
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
4009
            case 'sys_language_isocode':
4010
                trigger_error('Property $TSFE->sys_language_isocode is not in use anymore as this information is now stored within the SiteLanguage object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4011
                return isset($this->$propertyName);
4012
            case 'divSection':
4013
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4014
                return isset($this->$propertyName);
4015
            case 'fePreview':
4016
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4017
                return $this->context->hasAspect('frontend.preview');
4018
            case 'forceTemplateParsing':
4019
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4020
                return $this->context->hasAspect('typoscript') && $this->context->getPropertyFromAspect('typoscript', 'forcedTemplateParsing');
4021
        }
4022
        return false;
4023
    }
4024
4025
    /**
4026
     * Gets the value of the property of the given name if tagged.
4027
     *
4028
     * The evaluation is done in the assumption that this method is never
4029
     * reached for a public property.
4030
     *
4031
     * @param string $propertyName
4032
     * @return mixed
4033
     */
4034
    public function __get(string $propertyName)
4035
    {
4036
        switch ($propertyName) {
4037
            case 'domainStartPage':
4038
                trigger_error('Property $TSFE->domainStartPage is not in use anymore as this information is now stored within the Site object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4039
                return $this->site->getRootPageId();
4040
            case 'cHash':
4041
                trigger_error('Property $TSFE->cHash is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4042
                return $this->pageArguments->getArguments()['cHash'] ?? false;
4043
            case 'cHash_array':
4044
                trigger_error('Property $TSFE->cHash_array is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4045
                return $this->getRelevantParametersForCachingFromPageArguments($this->pageArguments);
4046
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
4047
            case 'sys_language_isocode':
4048
                trigger_error('Property $TSFE->sys_language_isocode is not in use anymore as this information is now stored within the SiteLanguage object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4049
                return $this->sys_language_isocode ?? $this->language->getTwoLetterIsoCode();
4050
            case 'divSection':
4051
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4052
                break;
4053
            case 'fePreview':
4054
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4055
                if ($this->context->hasAspect('frontend.preview')) {
4056
                    return $this->context->getPropertyFromAspect('frontend.preview', 'isPreview');
4057
                }
4058
                break;
4059
            case 'forceTemplateParsing':
4060
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4061
                if ($this->context->hasAspect('typoscript')) {
4062
                    return $this->context->getPropertyFromAspect('typoscript', 'forcedTemplateParsing');
4063
                }
4064
                break;
4065
        }
4066
        return $this->$propertyName;
4067
    }
4068
4069
    /**
4070
     * Sets the property of the given name if tagged.
4071
     *
4072
     * Additionally it's allowed to set unknown properties.
4073
     *
4074
     * The evaluation is done in the assumption that this method is never
4075
     * reached for a public property.
4076
     *
4077
     * @param string $propertyName
4078
     * @param mixed $propertyValue
4079
     */
4080
    public function __set(string $propertyName, $propertyValue)
4081
    {
4082
        switch ($propertyName) {
4083
            case 'domainStartPage':
4084
                trigger_error('Property $TSFE->domainStartPage is not in use anymore as this information is now stored within the Site object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4085
                break;
4086
            case 'cHash':
4087
                trigger_error('Property $TSFE->cHash is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4088
                break;
4089
            case 'cHash_array':
4090
                trigger_error('Property $TSFE->cHash_array is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4091
                break;
4092
            case 'sys_language_isocode':
4093
                trigger_error('Property $TSFE->sys_language_isocode is not in use anymore as this information is now stored within the SiteLanguage object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4094
                break;
4095
            case 'divSection':
4096
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4097
                break;
4098
            case 'fePreview':
4099
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4100
                $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class, (bool)$propertyValue));
4101
                break;
4102
            case 'forceTemplateParsing':
4103
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4104
                $this->context->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, (bool)$propertyValue));
4105
                break;
4106
        }
4107
        $this->$propertyName = $propertyValue;
4108
    }
4109
4110
    /**
4111
     * Unsets the property of the given name if tagged.
4112
     *
4113
     * @param string $propertyName
4114
     */
4115
    public function __unset(string $propertyName)
4116
    {
4117
        switch ($propertyName) {
4118
            case 'domainStartPage':
4119
                trigger_error('Property $TSFE->domainStartPage is not in use anymore as this information is now stored within the Site object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4120
                break;
4121
            case 'cHash':
4122
                trigger_error('Property $TSFE->cHash is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4123
                break;
4124
            case 'cHash_array':
4125
                trigger_error('Property $TSFE->cHash_array is not in use anymore as this information is now stored within the PageArguments object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4126
                break;
4127
            case 'sys_language_isocode':
4128
                trigger_error('Property $TSFE->sys_language_isocode is not in use anymore as this information is now stored within the SiteLanguage object. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4129
                break;
4130
            case 'divSection':
4131
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4132
                break;
4133
            case 'fePreview':
4134
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4135
                $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class, false));
4136
                break;
4137
            case 'forceTemplateParsing':
4138
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4139
                $this->context->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, false));
4140
                break;
4141
        }
4142
        unset($this->$propertyName);
4143
    }
4144
}
4145