Completed
Push — master ( 7a82fc...a55b2b )
by
unknown
14:42
created

processContentForOutput()   B

Complexity

Conditions 8
Paths 40

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

3612
                $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

3612
                $midnightTime = mktime(0, 0, 0, /** @scrutinizer ignore-type */ date('m', $timeOutTime), date('d', $timeOutTime), date('Y', $timeOutTime));
Loading history...
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

3612
                $midnightTime = mktime(0, 0, 0, date('m', $timeOutTime), date('d', $timeOutTime), /** @scrutinizer ignore-type */ date('Y', $timeOutTime));
Loading history...
3613
                // If the midnight time of the expire-day is greater than the current time,
3614
                // we may set the timeOutTime to the new midnighttime.
3615
                if ($midnightTime > $GLOBALS['EXEC_TIME']) {
3616
                    $cacheTimeout = $midnightTime - $GLOBALS['EXEC_TIME'];
3617
                }
3618
            }
3619
3620
            // Calculate the timeout time for records on the page and adjust cache timeout if necessary
3621
            $cacheTimeout = min($this->calculatePageCacheTimeout(), $cacheTimeout);
3622
3623
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['get_cache_timeout'] ?? [] as $_funcRef) {
3624
                $params = ['cacheTimeout' => $cacheTimeout];
3625
                $cacheTimeout = GeneralUtility::callUserFunction($_funcRef, $params, $this);
3626
            }
3627
            $runtimeCache->set($cachedCacheLifetimeIdentifier, $cacheTimeout);
3628
            $cachedCacheLifetime = $cacheTimeout;
3629
        }
3630
        return $cachedCacheLifetime;
3631
    }
3632
3633
    /*********************************************
3634
     *
3635
     * Localization and character set conversion
3636
     *
3637
     *********************************************/
3638
    /**
3639
     * Split Label function for front-end applications.
3640
     *
3641
     * @param string $input Key string. Accepts the "LLL:" prefix.
3642
     * @return string Label value, if any.
3643
     */
3644
    public function sL($input)
3645
    {
3646
        return $this->languageService->sL($input);
3647
    }
3648
3649
    /**
3650
     * Sets all internal measures what language the page should be rendered.
3651
     * This is not for records, but rather the HTML / charset and the locallang labels
3652
     */
3653
    protected function setOutputLanguage()
3654
    {
3655
        $this->languageService = LanguageService::createFromSiteLanguage($this->language);
3656
        // Always disable debugging for TSFE
3657
        $this->languageService->debugKey = false;
3658
    }
3659
3660
    /**
3661
     * Converts input string from utf-8 to metaCharset IF the two charsets are different.
3662
     *
3663
     * @param string $content Content to be converted.
3664
     * @return string Converted content string.
3665
     * @throws \RuntimeException if an invalid charset was configured
3666
     */
3667
    public function convOutputCharset($content)
3668
    {
3669
        if ($this->metaCharset !== 'utf-8') {
3670
            /** @var CharsetConverter $charsetConverter */
3671
            $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
3672
            try {
3673
                $content = $charsetConverter->conv($content, 'utf-8', $this->metaCharset);
3674
            } catch (UnknownCharsetException $e) {
3675
                throw new \RuntimeException('Invalid config.metaCharset: ' . $e->getMessage(), 1508916185);
3676
            }
3677
        }
3678
        return $content;
3679
    }
3680
3681
    /**
3682
     * Calculates page cache timeout according to the records with starttime/endtime on the page.
3683
     *
3684
     * @return int Page cache timeout or PHP_INT_MAX if cannot be determined
3685
     */
3686
    protected function calculatePageCacheTimeout()
3687
    {
3688
        $result = PHP_INT_MAX;
3689
        // Get the configuration
3690
        $tablesToConsider = $this->getCurrentPageCacheConfiguration();
3691
        // Get the time, rounded to the minute (do not pollute MySQL cache!)
3692
        // It is ok that we do not take seconds into account here because this
3693
        // value will be subtracted later. So we never get the time "before"
3694
        // the cache change.
3695
        $now = $GLOBALS['ACCESS_TIME'];
3696
        // Find timeout by checking every table
3697
        foreach ($tablesToConsider as $tableDef) {
3698
            $result = min($result, $this->getFirstTimeValueForRecord($tableDef, $now));
3699
        }
3700
        // We return + 1 second just to ensure that cache is definitely regenerated
3701
        return $result === PHP_INT_MAX ? PHP_INT_MAX : $result - $now + 1;
3702
    }
3703
3704
    /**
3705
     * Obtains a list of table/pid pairs to consider for page caching.
3706
     *
3707
     * TS configuration looks like this:
3708
     *
3709
     * The cache lifetime of all pages takes starttime and endtime of news records of page 14 into account:
3710
     * config.cache.all = tt_news:14
3711
     *
3712
     * The cache.lifetime of the current page allows to take records (e.g. fe_users) into account:
3713
     * config.cache.all = fe_users:current
3714
     *
3715
     * The cache lifetime of page 42 takes starttime and endtime of news records of page 15 and addresses of page 16 into account:
3716
     * config.cache.42 = tt_news:15,tt_address:16
3717
     *
3718
     * @return array Array of 'tablename:pid' pairs. There is at least a current page id in the array
3719
     * @see TypoScriptFrontendController::calculatePageCacheTimeout()
3720
     */
3721
    protected function getCurrentPageCacheConfiguration()
3722
    {
3723
        $result = ['tt_content:' . $this->id];
3724
        if (isset($this->config['config']['cache.'][$this->id])) {
3725
            $result = array_merge($result, GeneralUtility::trimExplode(',', str_replace(':current', ':' . $this->id, $this->config['config']['cache.'][$this->id])));
3726
        }
3727
        if (isset($this->config['config']['cache.']['all'])) {
3728
            $result = array_merge($result, GeneralUtility::trimExplode(',', str_replace(':current', ':' . $this->id, $this->config['config']['cache.']['all'])));
3729
        }
3730
        return array_unique($result);
3731
    }
3732
3733
    /**
3734
     * Find the minimum starttime or endtime value in the table and pid that is greater than the current time.
3735
     *
3736
     * @param string $tableDef Table definition (format tablename:pid)
3737
     * @param int $now "Now" time value
3738
     * @throws \InvalidArgumentException
3739
     * @return int Value of the next start/stop time or PHP_INT_MAX if not found
3740
     * @see TypoScriptFrontendController::calculatePageCacheTimeout()
3741
     */
3742
    protected function getFirstTimeValueForRecord($tableDef, $now)
3743
    {
3744
        $now = (int)$now;
3745
        $result = PHP_INT_MAX;
3746
        [$tableName, $pid] = GeneralUtility::trimExplode(':', $tableDef);
3747
        if (empty($tableName) || empty($pid)) {
3748
            throw new \InvalidArgumentException('Unexpected value for parameter $tableDef. Expected <tablename>:<pid>, got \'' . htmlspecialchars($tableDef) . '\'.', 1307190365);
3749
        }
3750
3751
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
3752
            ->getQueryBuilderForTable($tableName);
3753
        $queryBuilder->getRestrictions()
3754
            ->removeByType(StartTimeRestriction::class)
3755
            ->removeByType(EndTimeRestriction::class);
3756
        $timeFields = [];
3757
        $timeConditions = $queryBuilder->expr()->orX();
3758
        foreach (['starttime', 'endtime'] as $field) {
3759
            if (isset($GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'][$field])) {
3760
                $timeFields[$field] = $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'][$field];
3761
                $queryBuilder->addSelectLiteral(
3762
                    'MIN('
3763
                        . 'CASE WHEN '
3764
                        . $queryBuilder->expr()->lte(
3765
                            $timeFields[$field],
3766
                            $queryBuilder->createNamedParameter($now, \PDO::PARAM_INT)
3767
                        )
3768
                        . ' THEN NULL ELSE ' . $queryBuilder->quoteIdentifier($timeFields[$field]) . ' END'
3769
                        . ') AS ' . $queryBuilder->quoteIdentifier($timeFields[$field])
3770
                );
3771
                $timeConditions->add(
3772
                    $queryBuilder->expr()->gt(
3773
                        $timeFields[$field],
3774
                        $queryBuilder->createNamedParameter($now, \PDO::PARAM_INT)
3775
                    )
3776
                );
3777
            }
3778
        }
3779
3780
        // if starttime or endtime are defined, evaluate them
3781
        if (!empty($timeFields)) {
3782
            // find the timestamp, when the current page's content changes the next time
3783
            $row = $queryBuilder
3784
                ->from($tableName)
3785
                ->where(
3786
                    $queryBuilder->expr()->eq(
3787
                        'pid',
3788
                        $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
3789
                    ),
3790
                    $timeConditions
3791
                )
3792
                ->execute()
3793
                ->fetch();
3794
3795
            if ($row) {
3796
                foreach ($timeFields as $timeField => $_) {
3797
                    // if a MIN value is found, take it into account for the
3798
                    // cache lifetime we have to filter out start/endtimes < $now,
3799
                    // as the SQL query also returns rows with starttime < $now
3800
                    // and endtime > $now (and using a starttime from the past
3801
                    // would be wrong)
3802
                    if ($row[$timeField] !== null && (int)$row[$timeField] > $now) {
3803
                        $result = min($result, (int)$row[$timeField]);
3804
                    }
3805
                }
3806
            }
3807
        }
3808
3809
        return $result;
3810
    }
3811
3812
    /**
3813
     * Fetches the originally requested id, falls back to $this->id
3814
     *
3815
     * @return int the originally requested page uid
3816
     * @see fetch_the_id()
3817
     */
3818
    public function getRequestedId()
3819
    {
3820
        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...
3821
    }
3822
3823
    /**
3824
     * Acquire a page specific lock
3825
     *
3826
     *
3827
     * The schematics here is:
3828
     * - First acquire an access lock. This is using the type of the requested lock as key.
3829
     *   Since the number of types is rather limited we can use the type as key as it will only
3830
     *   eat up a limited number of lock resources on the system (files, semaphores)
3831
     * - Second, we acquire the actual lock (named page lock). We can be sure we are the only process at this
3832
     *   very moment, hence we either get the lock for the given key or we get an error as we request a non-blocking mode.
3833
     *
3834
     * Interleaving two locks is extremely important, because the actual page lock uses a hash value as key (see callers
3835
     * of this function). If we would simply employ a normal blocking lock, we would get a potentially unlimited
3836
     * (number of pages at least) number of different locks. Depending on the available locking methods on the system
3837
     * we might run out of available resources. (e.g. maximum limit of semaphores is a system setting and applies
3838
     * to the whole system)
3839
     * We therefore must make sure that page locks are destroyed again if they are not used anymore, such that
3840
     * we never use more locking resources than parallel requests to different pages (hashes).
3841
     * In order to ensure this, we need to guarantee that no other process is waiting on a page lock when
3842
     * the process currently having the lock on the page lock is about to release the lock again.
3843
     * This can only be achieved by using a non-blocking mode, such that a process is never put into wait state
3844
     * by the kernel, but only checks the availability of the lock. The access lock is our guard to be sure
3845
     * that no two processes are at the same time releasing/destroying a page lock, whilst the other one tries to
3846
     * get a lock for this page lock.
3847
     * The only drawback of this implementation is that we basically have to poll the availability of the page lock.
3848
     *
3849
     * Note that the access lock resources are NEVER deleted/destroyed, otherwise the whole thing would be broken.
3850
     *
3851
     * @param string $type
3852
     * @param string $key
3853
     * @throws \InvalidArgumentException
3854
     * @throws \RuntimeException
3855
     * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
3856
     */
3857
    protected function acquireLock($type, $key)
3858
    {
3859
        $lockFactory = GeneralUtility::makeInstance(LockFactory::class);
3860
        $this->locks[$type]['accessLock'] = $lockFactory->createLocker($type);
3861
3862
        $this->locks[$type]['pageLock'] = $lockFactory->createLocker(
3863
            $key,
3864
            LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK
3865
        );
3866
3867
        do {
3868
            if (!$this->locks[$type]['accessLock']->acquire()) {
3869
                throw new \RuntimeException('Could not acquire access lock for "' . $type . '"".', 1294586098);
3870
            }
3871
3872
            try {
3873
                $locked = $this->locks[$type]['pageLock']->acquire(
3874
                    LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE | LockingStrategyInterface::LOCK_CAPABILITY_NOBLOCK
3875
                );
3876
            } catch (LockAcquireWouldBlockException $e) {
3877
                // somebody else has the lock, we keep waiting
3878
3879
                // first release the access lock
3880
                $this->locks[$type]['accessLock']->release();
3881
                // now lets make a short break (100ms) until we try again, since
3882
                // the page generation by the lock owner will take a while anyways
3883
                usleep(100000);
3884
                continue;
3885
            }
3886
            $this->locks[$type]['accessLock']->release();
3887
            if ($locked) {
3888
                break;
3889
            }
3890
            throw new \RuntimeException('Could not acquire page lock for ' . $key . '.', 1460975877);
3891
        } while (true);
3892
    }
3893
3894
    /**
3895
     * Release a page specific lock
3896
     *
3897
     * @param string $type
3898
     * @throws \InvalidArgumentException
3899
     * @throws \RuntimeException
3900
     * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
3901
     */
3902
    protected function releaseLock($type)
3903
    {
3904
        if ($this->locks[$type]['accessLock']) {
3905
            if (!$this->locks[$type]['accessLock']->acquire()) {
3906
                throw new \RuntimeException('Could not acquire access lock for "' . $type . '"".', 1460975902);
3907
            }
3908
3909
            $this->locks[$type]['pageLock']->release();
3910
            $this->locks[$type]['pageLock']->destroy();
3911
            $this->locks[$type]['pageLock'] = null;
3912
3913
            $this->locks[$type]['accessLock']->release();
3914
            $this->locks[$type]['accessLock'] = null;
3915
        }
3916
    }
3917
3918
    /**
3919
     * Send additional headers from config.additionalHeaders
3920
     */
3921
    protected function getAdditionalHeaders(): array
3922
    {
3923
        if (!isset($this->config['config']['additionalHeaders.'])) {
3924
            return [];
3925
        }
3926
        $additionalHeaders = [];
3927
        ksort($this->config['config']['additionalHeaders.']);
3928
        foreach ($this->config['config']['additionalHeaders.'] as $options) {
3929
            if (!is_array($options)) {
3930
                continue;
3931
            }
3932
            $header = trim($options['header'] ?? '');
3933
            if ($header === '') {
3934
                continue;
3935
            }
3936
            $additionalHeaders[] = [
3937
                'header' => $header,
3938
                // "replace existing headers" is turned on by default, unless turned off
3939
                'replace' => ($options['replace'] ?? '') !== '0',
3940
                'statusCode' => (int)($options['httpResponseCode'] ?? 0) ?: null
3941
            ];
3942
        }
3943
        return $additionalHeaders;
3944
    }
3945
3946
    /**
3947
     * Returns the current BE user.
3948
     *
3949
     * @return \TYPO3\CMS\Backend\FrontendBackendUserAuthentication
3950
     */
3951
    protected function getBackendUser()
3952
    {
3953
        return $GLOBALS['BE_USER'];
3954
    }
3955
3956
    /**
3957
     * @return TimeTracker
3958
     */
3959
    protected function getTimeTracker()
3960
    {
3961
        return GeneralUtility::makeInstance(TimeTracker::class);
3962
    }
3963
3964
    /**
3965
     * Return the global instance of this class.
3966
     *
3967
     * Intended to be used as prototype factory for this class, see Services.yaml.
3968
     * This is required as long as TypoScriptFrontendController needs request
3969
     * dependent constructor parameters. Once that has been refactored this
3970
     * factory will be removed.
3971
     *
3972
     * @return TypoScriptFrontendController
3973
     * @internal
3974
     */
3975
    public static function getGlobalInstance(): ?self
3976
    {
3977
        if ($GLOBALS['TSFE'] instanceof self) {
3978
            return $GLOBALS['TSFE'];
3979
        }
3980
3981
        if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE)) {
3982
            // Return null for now (together with shared: false in Services.yaml) as TSFE might not be available in backend context
3983
            // That's not an error then
3984
            return null;
3985
        }
3986
3987
        throw new \LogicException('TypoScriptFrontendController was tried to be injected before initial creation', 1538370377);
3988
    }
3989
3990
    public function getLanguage(): SiteLanguage
3991
    {
3992
        return $this->language;
3993
    }
3994
3995
    public function getSite(): Site
3996
    {
3997
        return $this->site;
3998
    }
3999
4000
    public function getContext(): Context
4001
    {
4002
        return $this->context;
4003
    }
4004
4005
    public function getPageArguments(): PageArguments
4006
    {
4007
        return $this->pageArguments;
4008
    }
4009
4010
    /**
4011
     * Deprecation messages for TYPO3 10 - public properties of TSFE which have been (re)moved
4012
     */
4013
4014
    /**
4015
     * Checks if the property of the given name is set.
4016
     *
4017
     * Unmarked protected properties must return false as usual.
4018
     * Marked properties are evaluated by isset().
4019
     *
4020
     * This method is not called for public properties.
4021
     *
4022
     * @param string $propertyName
4023
     * @return bool
4024
     */
4025
    public function __isset(string $propertyName)
4026
    {
4027
        switch ($propertyName) {
4028
            case 'domainStartPage':
4029
                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);
4030
                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...
4031
            case 'cHash':
4032
                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);
4033
                return isset($this->pageArguments->getArguments()['cHash']);
4034
            case 'cHash_array':
4035
                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);
4036
                $value = $this->getRelevantParametersForCachingFromPageArguments($this->pageArguments);
4037
                return !empty($value);
4038
            case 'sys_language_isocode':
4039
                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);
4040
                return isset($this->$propertyName);
4041
            case 'divSection':
4042
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4043
                return isset($this->$propertyName);
4044
            case 'fePreview':
4045
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4046
                return $this->context->hasAspect('frontend.preview');
4047
            case 'forceTemplateParsing':
4048
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4049
                return $this->context->hasAspect('typoscript') && $this->context->getPropertyFromAspect('typoscript', 'forcedTemplateParsing');
4050
        }
4051
        return false;
4052
    }
4053
4054
    /**
4055
     * Gets the value of the property of the given name if tagged.
4056
     *
4057
     * The evaluation is done in the assumption that this method is never
4058
     * reached for a public property.
4059
     *
4060
     * @param string $propertyName
4061
     * @return mixed
4062
     */
4063
    public function __get(string $propertyName)
4064
    {
4065
        switch ($propertyName) {
4066
            case 'domainStartPage':
4067
                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);
4068
                return $this->site->getRootPageId();
4069
            case 'cHash':
4070
                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);
4071
                return $this->pageArguments->getArguments()['cHash'] ?? false;
4072
            case 'cHash_array':
4073
                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);
4074
                return $this->getRelevantParametersForCachingFromPageArguments($this->pageArguments);
4075
            case 'sys_language_isocode':
4076
                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);
4077
                return $this->sys_language_isocode ?? $this->language->getTwoLetterIsoCode();
4078
            case 'divSection':
4079
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4080
                break;
4081
            case 'fePreview':
4082
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4083
                if ($this->context->hasAspect('frontend.preview')) {
4084
                    return $this->context->getPropertyFromAspect('frontend.preview', 'isPreview');
4085
                }
4086
                break;
4087
            case 'forceTemplateParsing':
4088
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4089
                if ($this->context->hasAspect('typoscript')) {
4090
                    return $this->context->getPropertyFromAspect('typoscript', 'forcedTemplateParsing');
4091
                }
4092
                break;
4093
        }
4094
        return $this->$propertyName;
4095
    }
4096
4097
    /**
4098
     * Sets the property of the given name if tagged.
4099
     *
4100
     * Additionally it's allowed to set unknown properties.
4101
     *
4102
     * The evaluation is done in the assumption that this method is never
4103
     * reached for a public property.
4104
     *
4105
     * @param string $propertyName
4106
     * @param mixed $propertyValue
4107
     */
4108
    public function __set(string $propertyName, $propertyValue)
4109
    {
4110
        switch ($propertyName) {
4111
            case 'domainStartPage':
4112
                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);
4113
                break;
4114
            case 'cHash':
4115
                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);
4116
                break;
4117
            case 'cHash_array':
4118
                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);
4119
                break;
4120
            case 'sys_language_isocode':
4121
                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);
4122
                break;
4123
            case 'divSection':
4124
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4125
                break;
4126
            case 'fePreview':
4127
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4128
                $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class, (bool)$propertyValue));
4129
                break;
4130
            case 'forceTemplateParsing':
4131
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4132
                $this->context->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, (bool)$propertyValue));
4133
                break;
4134
        }
4135
        $this->$propertyName = $propertyValue;
4136
    }
4137
4138
    /**
4139
     * Unsets the property of the given name if tagged.
4140
     *
4141
     * @param string $propertyName
4142
     */
4143
    public function __unset(string $propertyName)
4144
    {
4145
        switch ($propertyName) {
4146
            case 'domainStartPage':
4147
                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);
4148
                break;
4149
            case 'cHash':
4150
                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);
4151
                break;
4152
            case 'cHash_array':
4153
                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);
4154
                break;
4155
            case 'sys_language_isocode':
4156
                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);
4157
                break;
4158
            case 'divSection':
4159
                trigger_error('Property $TSFE->divSection is not in use anymore. Will be removed in TYPO3 v11.0.', E_USER_DEPRECATED);
4160
                break;
4161
            case 'fePreview':
4162
                trigger_error('Property $TSFE->fePreview is not in use anymore as this information is now stored within the FrontendPreview aspect.', E_USER_DEPRECATED);
4163
                $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class, false));
4164
                break;
4165
            case 'forceTemplateParsing':
4166
                trigger_error('Property $TSFE->forceTemplateParsing is not in use anymore as this information is now stored within the TypoScript aspect.', E_USER_DEPRECATED);
4167
                $this->context->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, false));
4168
                break;
4169
        }
4170
        unset($this->$propertyName);
4171
    }
4172
}
4173