93c82ba002fd89cbfa6ec92290663a5477b1ff76
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Page / PageRepository.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Page;
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 TYPO3\CMS\Core\Cache\CacheManager;
18 use TYPO3\CMS\Core\Database\Connection;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Database\Query\QueryHelper;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
22 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
23 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendWorkspaceRestriction;
24 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
25 use TYPO3\CMS\Core\Resource\FileRepository;
26 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Core\Utility\HttpUtility;
29 use TYPO3\CMS\Core\Utility\RootlineUtility;
30 use TYPO3\CMS\Core\Versioning\VersionState;
31
32 /**
33 * Page functions, a lot of sql/pages-related functions
34 *
35 * Mainly used in the frontend but also in some cases in the backend. It's
36 * important to set the right $where_hid_del in the object so that the
37 * functions operate properly
38 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::fetch_the_id()
39 */
40 class PageRepository
41 {
42 /**
43 * @var array
44 */
45 public $urltypes = ['', 'http://', 'ftp://', 'mailto:', 'https://'];
46
47 /**
48 * This is not the final clauses. There will normally be conditions for the
49 * hidden, starttime and endtime fields as well. You MUST initialize the object
50 * by the init() function
51 *
52 * @var string
53 */
54 public $where_hid_del = ' AND pages.deleted=0';
55
56 /**
57 * Clause for fe_group access
58 *
59 * @var string
60 */
61 public $where_groupAccess = '';
62
63 /**
64 * @var int
65 */
66 public $sys_language_uid = 0;
67
68 /**
69 * If TRUE, versioning preview of other record versions is allowed. THIS MUST
70 * ONLY BE SET IF the page is not cached and truly previewed by a backend
71 * user!!!
72 *
73 * @var bool
74 */
75 public $versioningPreview = false;
76
77 /**
78 * Workspace ID for preview
79 *
80 * @var int
81 */
82 public $versioningWorkspaceId = 0;
83
84 /**
85 * @var array
86 */
87 public $workspaceCache = [];
88
89 /**
90 * Error string set by getRootLine()
91 *
92 * @var string
93 */
94 public $error_getRootLine = '';
95
96 /**
97 * Error uid set by getRootLine()
98 *
99 * @var int
100 */
101 public $error_getRootLine_failPid = 0;
102
103 /**
104 * @var array
105 */
106 protected $cache_getPage = [];
107
108 /**
109 * @var array
110 */
111 protected $cache_getPage_noCheck = [];
112
113 /**
114 * @var array
115 */
116 protected $cache_getPageIdFromAlias = [];
117
118 /**
119 * @var array
120 */
121 protected $cache_getMountPointInfo = [];
122
123 /**
124 * @var array
125 */
126 protected $tableNamesAllowedOnRootLevel = [
127 'sys_file_metadata',
128 'sys_category',
129 ];
130
131 /**
132 * Computed properties that are added to database rows.
133 *
134 * @var array
135 */
136 protected $computedPropertyNames = [
137 '_LOCALIZED_UID',
138 '_MP_PARAM',
139 '_ORIG_uid',
140 '_ORIG_pid',
141 '_PAGES_OVERLAY',
142 '_PAGES_OVERLAY_UID',
143 '_PAGES_OVERLAY_LANGUAGE',
144 ];
145
146 /**
147 * Named constants for "magic numbers" of the field doktype
148 */
149 const DOKTYPE_DEFAULT = 1;
150 const DOKTYPE_LINK = 3;
151 const DOKTYPE_SHORTCUT = 4;
152 const DOKTYPE_BE_USER_SECTION = 6;
153 const DOKTYPE_MOUNTPOINT = 7;
154 const DOKTYPE_SPACER = 199;
155 const DOKTYPE_SYSFOLDER = 254;
156 const DOKTYPE_RECYCLER = 255;
157
158 /**
159 * Named constants for "magic numbers" of the field shortcut_mode
160 */
161 const SHORTCUT_MODE_NONE = 0;
162 const SHORTCUT_MODE_FIRST_SUBPAGE = 1;
163 const SHORTCUT_MODE_RANDOM_SUBPAGE = 2;
164 const SHORTCUT_MODE_PARENT_PAGE = 3;
165
166 /**
167 * init() MUST be run directly after creating a new template-object
168 * This sets the internal variable $this->where_hid_del to the correct where
169 * clause for page records taking deleted/hidden/starttime/endtime/t3ver_state
170 * into account
171 *
172 * @param bool $show_hidden If $show_hidden is TRUE, the hidden-field is ignored!! Normally this should be FALSE. Is used for previewing.
173 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::fetch_the_id(), \TYPO3\CMS\Tstemplate\Controller\TemplateAnalyzerModuleFunctionController::initialize_editor()
174 */
175 public function init($show_hidden)
176 {
177 $this->where_groupAccess = '';
178
179 if ($this->versioningPreview) {
180 // For version previewing, make sure that enable-fields are not
181 // de-selecting hidden pages - we need versionOL() to unset them only
182 // if the overlay record instructs us to.
183 // Clear where_hid_del and restrict to live and current workspaces
184 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
185 ->getQueryBuilderForTable('pages')
186 ->expr();
187 $this->where_hid_del = ' AND ' . $expressionBuilder->andX(
188 $expressionBuilder->eq('pages.deleted', 0),
189 $expressionBuilder->orX(
190 $expressionBuilder->eq('pages.t3ver_wsid', 0),
191 $expressionBuilder->eq('pages.t3ver_wsid', (int)$this->versioningWorkspaceId)
192 )
193 );
194 } else {
195 // add starttime / endtime, and check for hidden/deleted
196 // Filter out new/deleted place-holder pages in case we are NOT in a
197 // versioning preview (that means we are online!)
198 $this->where_hid_del = $this->enableFields('pages', $show_hidden, ['fe_group' => true], true);
199 }
200 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['init'])) {
201 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['init'] as $classRef) {
202 $hookObject = GeneralUtility::makeInstance($classRef);
203 if (!$hookObject instanceof PageRepositoryInitHookInterface) {
204 throw new \UnexpectedValueException($hookObject . ' must implement interface ' . PageRepositoryInitHookInterface::class, 1379579812);
205 }
206 $hookObject->init_postProcess($this);
207 }
208 }
209 }
210
211 /**************************
212 *
213 * Selecting page records
214 *
215 **************************/
216
217 /**
218 * Returns the $row for the page with uid = $uid (observing ->where_hid_del)
219 * Any pages_language_overlay will be applied before the result is returned.
220 * If no page is found an empty array is returned.
221 *
222 * @param int $uid The page id to look up.
223 * @param bool $disableGroupAccessCheck If set, the check for group access is disabled. VERY rarely used
224 * @throws \UnexpectedValueException
225 * @return array The page row with overlaid localized fields. Empty it no page.
226 * @see getPage_noCheck()
227 */
228 public function getPage($uid, $disableGroupAccessCheck = false)
229 {
230 // Hook to manipulate the page uid for special overlay handling
231 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'])) {
232 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'] as $classRef) {
233 $hookObject = GeneralUtility::getUserObj($classRef);
234 if (!$hookObject instanceof PageRepositoryGetPageHookInterface) {
235 throw new \UnexpectedValueException($classRef . ' must implement interface ' . PageRepositoryGetPageHookInterface::class, 1251476766);
236 }
237 $hookObject->getPage_preProcess($uid, $disableGroupAccessCheck, $this);
238 }
239 }
240 $cacheKey = md5(
241 implode(
242 '-',
243 [
244 ($disableGroupAccessCheck ? '' : $this->where_groupAccess),
245 $this->where_hid_del,
246 $this->sys_language_uid
247 ]
248 )
249 );
250 if (is_array($this->cache_getPage[$uid][$cacheKey])) {
251 return $this->cache_getPage[$uid][$cacheKey];
252 }
253 $result = [];
254 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
255 $queryBuilder->getRestrictions()->removeAll();
256 $queryBuilder->select('*')
257 ->from('pages')
258 ->where(
259 $queryBuilder->expr()->eq('uid', (int)$uid),
260 QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del)
261 );
262
263 if (!$disableGroupAccessCheck) {
264 $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess));
265 }
266
267 $row = $queryBuilder->execute()->fetch();
268 if ($row) {
269 $this->versionOL('pages', $row);
270 if (is_array($row)) {
271 $result = $this->getPageOverlay($row);
272 }
273 }
274 $this->cache_getPage[$uid][$cacheKey] = $result;
275 return $result;
276 }
277
278 /**
279 * Return the $row for the page with uid = $uid WITHOUT checking for
280 * ->where_hid_del (start- and endtime or hidden). Only "deleted" is checked!
281 *
282 * @param int $uid The page id to look up
283 * @return array The page row with overlaid localized fields. Empty array if no page.
284 * @see getPage()
285 */
286 public function getPage_noCheck($uid)
287 {
288 if ($this->cache_getPage_noCheck[$uid]) {
289 return $this->cache_getPage_noCheck[$uid];
290 }
291
292 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
293 $queryBuilder->getRestrictions()
294 ->removeAll()
295 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
296 $row = $queryBuilder->select('*')
297 ->from('pages')
298 ->where($queryBuilder->expr()->eq('uid', (int)$uid))
299 ->execute()
300 ->fetch();
301
302 $result = [];
303 if ($row) {
304 $this->versionOL('pages', $row);
305 if (is_array($row)) {
306 $result = $this->getPageOverlay($row);
307 }
308 }
309 $this->cache_getPage_noCheck[$uid] = $result;
310 return $result;
311 }
312
313 /**
314 * Returns the $row of the first web-page in the tree (for the default menu...)
315 *
316 * @param int $uid The page id for which to fetch first subpages (PID)
317 * @return mixed If found: The page record (with overlaid localized fields, if any). If NOT found: blank value (not array!)
318 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::fetch_the_id()
319 */
320 public function getFirstWebPage($uid)
321 {
322 $output = '';
323 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
324 $queryBuilder->getRestrictions()->removeAll();
325 $row = $queryBuilder->select('*')
326 ->from('pages')
327 ->where(
328 $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)),
329 QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del),
330 QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess)
331 )
332 ->orderBy('sorting')
333 ->setMaxResults(1)
334 ->execute()
335 ->fetch();
336
337 if ($row) {
338 $this->versionOL('pages', $row);
339 if (is_array($row)) {
340 $output = $this->getPageOverlay($row);
341 }
342 }
343 return $output;
344 }
345
346 /**
347 * Returns a pagerow for the page with alias $alias
348 *
349 * @param string $alias The alias to look up the page uid for.
350 * @return int Returns page uid (int) if found, otherwise 0 (zero)
351 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::checkAndSetAlias(), ContentObjectRenderer::typoLink()
352 */
353 public function getPageIdFromAlias($alias)
354 {
355 $alias = strtolower($alias);
356 if ($this->cache_getPageIdFromAlias[$alias]) {
357 return $this->cache_getPageIdFromAlias[$alias];
358 }
359 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
360 $queryBuilder->getRestrictions()
361 ->removeAll()
362 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
363
364 $row = $queryBuilder->select('uid')
365 ->from('pages')
366 ->where(
367 $queryBuilder->expr()->eq('alias', $queryBuilder->createNamedParameter($alias, \PDO::PARAM_STR)),
368 // "AND pid>=0" because of versioning (means that aliases sent MUST be online!)
369 $queryBuilder->expr()->gte('pid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
370 )
371 ->setMaxResults(1)
372 ->execute()
373 ->fetch();
374
375 if ($row) {
376 $this->cache_getPageIdFromAlias[$alias] = $row['uid'];
377 return $row['uid'];
378 }
379 $this->cache_getPageIdFromAlias[$alias] = 0;
380 return 0;
381 }
382
383 /**
384 * Returns the relevant page overlay record fields
385 *
386 * @param mixed $pageInput If $pageInput is an integer, it's the pid of the pageOverlay record and thus the page overlay record is returned. If $pageInput is an array, it's a page-record and based on this page record the language record is found and OVERLAID before the page record is returned.
387 * @param int $lUid Language UID if you want to set an alternative value to $this->sys_language_uid which is default. Should be >=0
388 * @throws \UnexpectedValueException
389 * @return array Page row which is overlaid with language_overlay record (or the overlay record alone)
390 */
391 public function getPageOverlay($pageInput, $lUid = -1)
392 {
393 $rows = $this->getPagesOverlay([$pageInput], $lUid);
394 // Always an array in return
395 return isset($rows[0]) ? $rows[0] : [];
396 }
397
398 /**
399 * Returns the relevant page overlay record fields
400 *
401 * @param array $pagesInput Array of integers or array of arrays. If each value is an integer, it's the pids of the pageOverlay records and thus the page overlay records are returned. If each value is an array, it's page-records and based on this page records the language records are found and OVERLAID before the page records are returned.
402 * @param int $lUid Language UID if you want to set an alternative value to $this->sys_language_uid which is default. Should be >=0
403 * @throws \UnexpectedValueException
404 * @return array Page rows which are overlaid with language_overlay record.
405 * If the input was an array of integers, missing records are not
406 * included. If the input were page rows, untranslated pages
407 * are returned.
408 */
409 public function getPagesOverlay(array $pagesInput, $lUid = -1)
410 {
411 if (empty($pagesInput)) {
412 return [];
413 }
414 // Initialize:
415 if ($lUid < 0) {
416 $lUid = $this->sys_language_uid;
417 }
418 $row = null;
419 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay'])) {
420 foreach ($pagesInput as &$origPage) {
421 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay'] as $classRef) {
422 $hookObject = GeneralUtility::getUserObj($classRef);
423 if (!$hookObject instanceof PageRepositoryGetPageOverlayHookInterface) {
424 throw new \UnexpectedValueException($classRef . ' must implement interface ' . PageRepositoryGetPageOverlayHookInterface::class, 1269878881);
425 }
426 $hookObject->getPageOverlay_preProcess($origPage, $lUid, $this);
427 }
428 }
429 unset($origPage);
430 }
431 // If language UID is different from zero, do overlay:
432 if ($lUid) {
433 $page_ids = [];
434
435 $origPage = reset($pagesInput);
436 foreach ($pagesInput as $origPage) {
437 if (is_array($origPage)) {
438 // Was the whole record
439 $page_ids[] = $origPage['uid'];
440 } else {
441 // Was the id
442 $page_ids[] = $origPage;
443 }
444 }
445 // NOTE regarding the query restrictions
446 // Currently the showHiddenRecords of TSFE set will allow
447 // pages_language_overlay records to be selected as they are
448 // child-records of a page.
449 // However you may argue that the showHiddenField flag should
450 // determine this. But that's not how it's done right now.
451 // Selecting overlay record:
452 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
453 ->getQueryBuilderForTable('pages_language_overlay');
454 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
455 $result = $queryBuilder->select('*')
456 ->from('pages_language_overlay')
457 ->where(
458 $queryBuilder->expr()->in(
459 'pid',
460 $queryBuilder->createNamedParameter($page_ids, Connection::PARAM_INT_ARRAY)
461 ),
462 $queryBuilder->expr()->eq(
463 'sys_language_uid',
464 $queryBuilder->createNamedParameter($lUid, \PDO::PARAM_INT)
465 )
466 )
467 ->execute();
468
469 $overlays = [];
470 while ($row = $result->fetch()) {
471 $this->versionOL('pages_language_overlay', $row);
472 if (is_array($row)) {
473 $row['_PAGES_OVERLAY'] = true;
474 $row['_PAGES_OVERLAY_UID'] = $row['uid'];
475 $row['_PAGES_OVERLAY_LANGUAGE'] = $lUid;
476 $origUid = $row['pid'];
477 // Unset vital fields that are NOT allowed to be overlaid:
478 unset($row['uid']);
479 unset($row['pid']);
480 $overlays[$origUid] = $row;
481 }
482 }
483 }
484 // Create output:
485 $pagesOutput = [];
486 foreach ($pagesInput as $key => $origPage) {
487 if (is_array($origPage)) {
488 $pagesOutput[$key] = $origPage;
489 if (isset($overlays[$origPage['uid']])) {
490 // Overwrite the original field with the overlay
491 foreach ($overlays[$origPage['uid']] as $fieldName => $fieldValue) {
492 if ($fieldName !== 'uid' && $fieldName !== 'pid') {
493 $pagesOutput[$key][$fieldName] = $fieldValue;
494 }
495 }
496 }
497 } else {
498 if (isset($overlays[$origPage])) {
499 $pagesOutput[$key] = $overlays[$origPage];
500 }
501 }
502 }
503 return $pagesOutput;
504 }
505
506 /**
507 * Creates language-overlay for records in general (where translation is found
508 * in records from the same table)
509 *
510 * @param string $table Table name
511 * @param array $row Record to overlay. Must contain uid, pid and $table]['ctrl']['languageField']
512 * @param int $sys_language_content Pointer to the sys_language uid for content on the site.
513 * @param string $OLmode Overlay mode. If "hideNonTranslated" then records without translation will not be returned un-translated but unset (and return value is FALSE)
514 * @throws \UnexpectedValueException
515 * @return mixed Returns the input record, possibly overlaid with a translation. But if $OLmode is "hideNonTranslated" then it will return FALSE if no translation is found.
516 */
517 public function getRecordOverlay($table, $row, $sys_language_content, $OLmode = '')
518 {
519 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'])) {
520 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'] as $classRef) {
521 $hookObject = GeneralUtility::getUserObj($classRef);
522 if (!$hookObject instanceof PageRepositoryGetRecordOverlayHookInterface) {
523 throw new \UnexpectedValueException($classRef . ' must implement interface ' . PageRepositoryGetRecordOverlayHookInterface::class, 1269881658);
524 }
525 $hookObject->getRecordOverlay_preProcess($table, $row, $sys_language_content, $OLmode, $this);
526 }
527 }
528 if ($row['uid'] > 0 && ($row['pid'] > 0 || in_array($table, $this->tableNamesAllowedOnRootLevel, true))) {
529 if ($GLOBALS['TCA'][$table] && $GLOBALS['TCA'][$table]['ctrl']['languageField'] && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']) {
530 // Return record for ALL languages untouched
531 // TODO: Fix call stack to prevent this situation in the first place
532 if ($table !== 'pages_language_overlay' && (int)$row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] !== -1) {
533 // Will not be able to work with other tables (Just didn't implement it yet;
534 // Requires a scan over all tables [ctrl] part for first FIND the table that
535 // carries localization information for this table (which could even be more
536 // than a single table) and then use that. Could be implemented, but obviously
537 // takes a little more....) Will try to overlay a record only if the
538 // sys_language_content value is larger than zero.
539 if ($sys_language_content > 0) {
540 // Must be default language, otherwise no overlaying
541 if ((int)$row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] === 0) {
542 // Select overlay record:
543 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
544 ->getQueryBuilderForTable($table);
545 $queryBuilder->setRestrictions(
546 GeneralUtility::makeInstance(FrontendRestrictionContainer::class)
547 );
548 $olrow = $queryBuilder->select('*')
549 ->from($table)
550 ->where(
551 $queryBuilder->expr()->eq(
552 'pid',
553 $queryBuilder->createNamedParameter($row['pid'], \PDO::PARAM_INT)
554 ),
555 $queryBuilder->expr()->eq(
556 $GLOBALS['TCA'][$table]['ctrl']['languageField'],
557 $queryBuilder->createNamedParameter($sys_language_content, \PDO::PARAM_INT)
558 ),
559 $queryBuilder->expr()->eq(
560 $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
561 $queryBuilder->createNamedParameter($row['uid'], \PDO::PARAM_INT)
562 )
563 )
564 ->setMaxResults(1)
565 ->execute()
566 ->fetch();
567
568 $this->versionOL($table, $olrow);
569 // Merge record content by traversing all fields:
570 if (is_array($olrow)) {
571 if (isset($olrow['_ORIG_uid'])) {
572 $row['_ORIG_uid'] = $olrow['_ORIG_uid'];
573 }
574 if (isset($olrow['_ORIG_pid'])) {
575 $row['_ORIG_pid'] = $olrow['_ORIG_pid'];
576 }
577 foreach ($row as $fN => $fV) {
578 if ($fN !== 'uid' && $fN !== 'pid' && isset($olrow[$fN])) {
579 $row[$fN] = $olrow[$fN];
580 } elseif ($fN === 'uid') {
581 $row['_LOCALIZED_UID'] = $olrow['uid'];
582 }
583 }
584 } elseif ($OLmode === 'hideNonTranslated' && (int)$row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] === 0) {
585 // Unset, if non-translated records should be hidden. ONLY done if the source
586 // record really is default language and not [All] in which case it is allowed.
587 unset($row);
588 }
589 } elseif ($sys_language_content != $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']]) {
590 unset($row);
591 }
592 } else {
593 // When default language is displayed, we never want to return a record carrying
594 // another language!
595 if ($row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0) {
596 unset($row);
597 }
598 }
599 }
600 }
601 }
602 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'])) {
603 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'] as $classRef) {
604 $hookObject = GeneralUtility::getUserObj($classRef);
605 if (!$hookObject instanceof PageRepositoryGetRecordOverlayHookInterface) {
606 throw new \UnexpectedValueException($classRef . ' must implement interface ' . PageRepositoryGetRecordOverlayHookInterface::class, 1269881659);
607 }
608 $hookObject->getRecordOverlay_postProcess($table, $row, $sys_language_content, $OLmode, $this);
609 }
610 }
611 return $row;
612 }
613
614 /************************************************
615 *
616 * Page related: Menu, Domain record, Root line
617 *
618 ************************************************/
619
620 /**
621 * Returns an array with page rows for subpages of a certain page ID. This is used for menus in the frontend.
622 * If there are mount points in overlay mode the _MP_PARAM field is set to the correct MPvar.
623 *
624 * If the $pageId being input does in itself require MPvars to define a correct
625 * rootline these must be handled externally to this function.
626 *
627 * @param int|int[] $pageId The page id (or array of page ids) for which to fetch subpages (PID)
628 * @param string $fields List of fields to select. Default is "*" = all
629 * @param string $sortField The field to sort by. Default is "sorting
630 * @param string $additionalWhereClause Optional additional where clauses. Like "AND title like '%blabla%'" for instance.
631 * @param bool $checkShortcuts Check if shortcuts exist, checks by default
632 * @return array Array with key/value pairs; keys are page-uid numbers. values are the corresponding page records (with overlaid localized fields, if any)
633 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::getPageShortcut(), \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject::makeMenu()
634 * @see \TYPO3\CMS\WizardCrpages\Controller\CreatePagesWizardModuleFunctionController, \TYPO3\CMS\WizardSortpages\View\SortPagesWizardModuleFunction
635 */
636 public function getMenu($pageId, $fields = '*', $sortField = 'sorting', $additionalWhereClause = '', $checkShortcuts = true)
637 {
638 return $this->getSubpagesForPages((array)$pageId, $fields, $sortField, $additionalWhereClause, $checkShortcuts);
639 }
640
641 /**
642 * Returns an array with page-rows for pages with uid in $pageIds.
643 *
644 * This is used for menus. If there are mount points in overlay mode
645 * the _MP_PARAM field is set to the correct MPvar.
646 *
647 * @param int[] $pageIds Array of page ids to fetch
648 * @param string $fields List of fields to select. Default is "*" = all
649 * @param string $sortField The field to sort by. Default is "sorting"
650 * @param string $additionalWhereClause Optional additional where clauses. Like "AND title like '%blabla%'" for instance.
651 * @param bool $checkShortcuts Check if shortcuts exist, checks by default
652 * @return array Array with key/value pairs; keys are page-uid numbers. values are the corresponding page records (with overlaid localized fields, if any)
653 */
654 public function getMenuForPages(array $pageIds, $fields = '*', $sortField = 'sorting', $additionalWhereClause = '', $checkShortcuts = true)
655 {
656 return $this->getSubpagesForPages($pageIds, $fields, $sortField, $additionalWhereClause, $checkShortcuts, false);
657 }
658
659 /**
660 * Internal method used by getMenu() and getMenuForPages()
661 * Returns an array with page rows for subpages with pid is in $pageIds or uid is in $pageIds, depending on $parentPages
662 * This is used for menus. If there are mount points in overlay mode
663 * the _MP_PARAM field is set to the correct MPvar.
664 *
665 * If the $pageIds being input does in itself require MPvars to define a correct
666 * rootline these must be handled externally to this function.
667 *
668 * @param int[] $pageIds The page id (or array of page ids) for which to fetch subpages (PID)
669 * @param string $fields List of fields to select. Default is "*" = all
670 * @param string $sortField The field to sort by. Default is "sorting
671 * @param string $additionalWhereClause Optional additional where clauses. Like "AND title like '%blabla%'" for instance.
672 * @param bool $checkShortcuts Check if shortcuts exist, checks by default
673 * @param bool $parentPages Whether the uid list is meant as list of parent pages or the page itself TRUE means id list is checked against pid field
674 * @return array Array with key/value pairs; keys are page-uid numbers. values are the corresponding page records (with overlaid localized fields, if any)
675 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::getPageShortcut(), \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject::makeMenu()
676 * @see \TYPO3\CMS\WizardCrpages\Controller\CreatePagesWizardModuleFunctionController, \TYPO3\CMS\WizardSortpages\View\SortPagesWizardModuleFunction
677 */
678 protected function getSubpagesForPages(array $pageIds, $fields = '*', $sortField = 'sorting', $additionalWhereClause = '', $checkShortcuts = true, $parentPages = true)
679 {
680 $pages = [];
681 $relationField = $parentPages ? 'pid' : 'uid';
682 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
683 $queryBuilder->getRestrictions()->removeAll();
684
685 $res = $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true))
686 ->from('pages')
687 ->where(
688 $queryBuilder->expr()->in(
689 $relationField,
690 $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
691 ),
692 QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del),
693 QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess),
694 QueryHelper::stripLogicalOperatorPrefix($additionalWhereClause)
695 );
696
697 if (!empty($sortField)) {
698 $orderBy = QueryHelper::parseOrderBy($sortField);
699 foreach ($orderBy as $order) {
700 $res->orderBy(...$order);
701 }
702 }
703 $result = $res->execute();
704
705 while ($page = $result->fetch()) {
706 $originalUid = $page['uid'];
707
708 // Versioning Preview Overlay
709 $this->versionOL('pages', $page, true);
710 // Skip if page got disabled due to version overlay
711 // (might be delete or move placeholder)
712 if (empty($page)) {
713 continue;
714 }
715
716 // Add a mount point parameter if needed
717 $page = $this->addMountPointParameterToPage((array)$page);
718
719 // If shortcut, look up if the target exists and is currently visible
720 if ($checkShortcuts) {
721 $page = $this->checkValidShortcutOfPage((array)$page, $additionalWhereClause);
722 }
723
724 // If the page still is there, we add it to the output
725 if (!empty($page)) {
726 $pages[$originalUid] = $page;
727 }
728 }
729
730 // Finally load language overlays
731 return $this->getPagesOverlay($pages);
732 }
733
734 /**
735 * Add the mount point parameter to the page if needed
736 *
737 * @param array $page The page to check
738 * @return array
739 */
740 protected function addMountPointParameterToPage(array $page)
741 {
742 if (empty($page)) {
743 return [];
744 }
745
746 // $page MUST have "uid", "pid", "doktype", "mount_pid", "mount_pid_ol" fields in it
747 $mountPointInfo = $this->getMountPointInfo($page['uid'], $page);
748
749 // There is a valid mount point.
750 if (is_array($mountPointInfo) && $mountPointInfo['overlay']) {
751
752 // Using "getPage" is OK since we need the check for enableFields AND for type 2
753 // of mount pids we DO require a doktype < 200!
754 $mountPointPage = $this->getPage($mountPointInfo['mount_pid']);
755
756 if (!empty($mountPointPage)) {
757 $page = $mountPointPage;
758 $page['_MP_PARAM'] = $mountPointInfo['MPvar'];
759 } else {
760 $page = [];
761 }
762 }
763 return $page;
764 }
765
766 /**
767 * If shortcut, look up if the target exists and is currently visible
768 *
769 * @param array $page The page to check
770 * @param string $additionalWhereClause Optional additional where clauses. Like "AND title like '%blabla%'" for instance.
771 * @return array
772 */
773 protected function checkValidShortcutOfPage(array $page, $additionalWhereClause)
774 {
775 if (empty($page)) {
776 return [];
777 }
778
779 $dokType = (int)$page['doktype'];
780 $shortcutMode = (int)$page['shortcut_mode'];
781
782 if ($dokType === self::DOKTYPE_SHORTCUT && ($page['shortcut'] || $shortcutMode)) {
783 if ($shortcutMode === self::SHORTCUT_MODE_NONE) {
784 // No shortcut_mode set, so target is directly set in $page['shortcut']
785 $searchField = 'uid';
786 $searchUid = (int)$page['shortcut'];
787 } elseif ($shortcutMode === self::SHORTCUT_MODE_FIRST_SUBPAGE || $shortcutMode === self::SHORTCUT_MODE_RANDOM_SUBPAGE) {
788 // Check subpages - first subpage or random subpage
789 $searchField = 'pid';
790 // If a shortcut mode is set and no valid page is given to select subpags
791 // from use the actual page.
792 $searchUid = (int)$page['shortcut'] ?: $page['uid'];
793 } elseif ($shortcutMode === self::SHORTCUT_MODE_PARENT_PAGE) {
794 // Shortcut to parent page
795 $searchField = 'uid';
796 $searchUid = $page['pid'];
797 } else {
798 $searchField = '';
799 $searchUid = 0;
800 }
801
802 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
803 $queryBuilder->getRestrictions()->removeAll();
804 $count = $queryBuilder->count('uid')
805 ->from('pages')
806 ->where(
807 $queryBuilder->expr()->eq(
808 $searchField,
809 $queryBuilder->createNamedParameter($searchUid, \PDO::PARAM_INT)
810 ),
811 QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del),
812 QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess),
813 QueryHelper::stripLogicalOperatorPrefix($additionalWhereClause)
814 )
815 ->execute()
816 ->fetchColumn();
817
818 if (!$count) {
819 $page = [];
820 }
821 } elseif ($dokType === self::DOKTYPE_SHORTCUT) {
822 // Neither shortcut target nor mode is set. Remove the page from the menu.
823 $page = [];
824 }
825 return $page;
826 }
827 /**
828 * Will find the page carrying the domain record matching the input domain.
829 * Might exit after sending a redirect-header IF a found domain record
830 * instructs to do so.
831 *
832 * @param string $domain Domain name to search for. Eg. "www.typo3.com". Typical the HTTP_HOST value.
833 * @param string $path Path for the current script in domain. Eg. "/somedir/subdir". Typ. supplied by \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('SCRIPT_NAME')
834 * @param string $request_uri Request URI: Used to get parameters from if they should be appended. Typ. supplied by \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')
835 * @return mixed If found, returns integer with page UID where found. Otherwise blank. Might exit if location-header is sent, see description.
836 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::findDomainRecord()
837 */
838 public function getDomainStartPage($domain, $path = '', $request_uri = '')
839 {
840 $domain = explode(':', $domain);
841 $domain = strtolower(preg_replace('/\\.$/', '', $domain[0]));
842 // Removing extra trailing slashes
843 $path = trim(preg_replace('/\\/[^\\/]*$/', '', $path));
844 // Appending to domain string
845 $domain .= $path;
846 $domain = preg_replace('/\\/*$/', '', $domain);
847 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
848 $queryBuilder->getRestrictions()->removeAll();
849 $row = $queryBuilder
850 ->select(
851 'pages.uid',
852 'sys_domain.redirectTo',
853 'sys_domain.redirectHttpStatusCode',
854 'sys_domain.prepend_params'
855 )
856 ->from('pages')
857 ->from('sys_domain')
858 ->where(
859 $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('sys_domain.pid')),
860 $queryBuilder->expr()->eq(
861 'sys_domain.hidden',
862 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
863 ),
864 $queryBuilder->expr()->orX(
865 $queryBuilder->expr()->eq(
866 'sys_domain.domainName',
867 $queryBuilder->createNamedParameter($domain, \PDO::PARAM_STR)
868 ),
869 $queryBuilder->expr()->eq(
870 'sys_domain.domainName',
871 $queryBuilder->createNamedParameter($domain . '/', \PDO::PARAM_STR)
872 )
873 ),
874 QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del),
875 QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess)
876 )
877 ->setMaxResults(1)
878 ->execute()
879 ->fetch();
880
881 if (!$row) {
882 return '';
883 }
884
885 if ($row['redirectTo']) {
886 $redirectUrl = $row['redirectTo'];
887 if ($row['prepend_params']) {
888 $redirectUrl = rtrim($redirectUrl, '/');
889 $prependStr = ltrim(substr($request_uri, strlen($path)), '/');
890 $redirectUrl .= '/' . $prependStr;
891 }
892 $statusCode = (int)$row['redirectHttpStatusCode'];
893 if ($statusCode && defined(HttpUtility::class . '::HTTP_STATUS_' . $statusCode)) {
894 HttpUtility::redirect($redirectUrl, constant(HttpUtility::class . '::HTTP_STATUS_' . $statusCode));
895 } else {
896 HttpUtility::redirect($redirectUrl, HttpUtility::HTTP_STATUS_301);
897 }
898 die;
899 } else {
900 return $row['uid'];
901 }
902 }
903
904 /**
905 * Returns array with fields of the pages from here ($uid) and back to the root
906 *
907 * NOTICE: This function only takes deleted pages into account! So hidden,
908 * starttime and endtime restricted pages are included no matter what.
909 *
910 * Further: If any "recycler" page is found (doktype=255) then it will also block
911 * for the rootline)
912 *
913 * If you want more fields in the rootline records than default such can be added
914 * by listing them in $GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields']
915 *
916 * @param int $uid The page uid for which to seek back to the page tree root.
917 * @param string $MP Commalist of MountPoint parameters, eg. "1-2,3-4" etc. Normally this value comes from the GET var, MP
918 * @param bool $ignoreMPerrors If set, some errors related to Mount Points in root line are ignored.
919 * @throws \Exception
920 * @throws \RuntimeException
921 * @return array Array with page records from the root line as values. The array is ordered with the outer records first and root record in the bottom. The keys are numeric but in reverse order. So if you traverse/sort the array by the numeric keys order you will get the order from root and out. If an error is found (like eternal looping or invalid mountpoint) it will return an empty array.
922 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::getPageAndRootline()
923 */
924 public function getRootLine($uid, $MP = '', $ignoreMPerrors = false)
925 {
926 $rootline = GeneralUtility::makeInstance(RootlineUtility::class, $uid, $MP, $this);
927 try {
928 return $rootline->get();
929 } catch (\RuntimeException $ex) {
930 if ($ignoreMPerrors) {
931 $this->error_getRootLine = $ex->getMessage();
932 if (substr($this->error_getRootLine, -7) === 'uid -1.') {
933 $this->error_getRootLine_failPid = -1;
934 }
935 return [];
936 /** @see \TYPO3\CMS\Core\Utility\RootlineUtility::getRecordArray */
937 } elseif ($ex->getCode() === 1343589451) {
938 return [];
939 }
940 throw $ex;
941 }
942 }
943
944 /**
945 * Creates a "path" string for the input root line array titles.
946 * Used for writing statistics.
947 *
948 * @param array $rl A rootline array!
949 * @param int $len The max length of each title from the rootline.
950 * @return string The path in the form "/page title/This is another pageti.../Another page
951 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::getConfigArray()
952 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
953 */
954 public function getPathFromRootline($rl, $len = 20)
955 {
956 GeneralUtility::logDeprecatedFunction();
957 $path = '';
958 if (is_array($rl)) {
959 $c = count($rl);
960 for ($a = 0; $a < $c; $a++) {
961 if ($rl[$a]['uid']) {
962 $path .= '/' . GeneralUtility::fixed_lgd_cs(strip_tags($rl[$a]['title']), $len);
963 }
964 }
965 }
966 return $path;
967 }
968
969 /**
970 * Returns the URL type for the input page row IF the doktype is set to 3.
971 *
972 * @param array $pagerow The page row to return URL type for
973 * @return string|bool The URL from based on the data from "urltype" and "url". False if not found.
974 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::initializeRedirectUrlHandlers()
975 */
976 public function getExtURL($pagerow)
977 {
978 if ((int)$pagerow['doktype'] === self::DOKTYPE_LINK) {
979 $redirectTo = $this->urltypes[$pagerow['urltype']] . $pagerow['url'];
980 // If relative path, prefix Site URL:
981 $uI = parse_url($redirectTo);
982 // Relative path assumed now.
983 if (!$uI['scheme'] && $redirectTo[0] !== '/') {
984 $redirectTo = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . $redirectTo;
985 }
986 return $redirectTo;
987 }
988 return false;
989 }
990
991 /**
992 * Returns a MountPoint array for the specified page
993 *
994 * Does a recursive search if the mounted page should be a mount page
995 * itself.
996 *
997 * Note:
998 *
999 * Recursive mount points are not supported by all parts of the core.
1000 * The usage is discouraged. They may be removed from this method.
1001 *
1002 * @see: https://decisions.typo3.org/t/supporting-or-prohibiting-recursive-mount-points/165/3
1003 *
1004 * An array will be returned if mount pages are enabled, the correct
1005 * doktype (7) is set for page and there IS a mount_pid with a valid
1006 * record.
1007 *
1008 * The optional page record must contain at least uid, pid, doktype,
1009 * mount_pid,mount_pid_ol. If it is not supplied it will be looked up by
1010 * the system at additional costs for the lookup.
1011 *
1012 * Returns FALSE if no mount point was found, "-1" if there should have been
1013 * one, but no connection to it, otherwise an array with information
1014 * about mount pid and modes.
1015 *
1016 * @param int $pageId Page id to do the lookup for.
1017 * @param array|bool $pageRec Optional page record for the given page.
1018 * @param array $prevMountPids Internal register to prevent lookup cycles.
1019 * @param int $firstPageUid The first page id.
1020 * @return mixed Mount point array or failure flags (-1, false).
1021 * @see \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject
1022 */
1023 public function getMountPointInfo($pageId, $pageRec = false, $prevMountPids = [], $firstPageUid = 0)
1024 {
1025 $result = false;
1026 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids']) {
1027 if (isset($this->cache_getMountPointInfo[$pageId])) {
1028 return $this->cache_getMountPointInfo[$pageId];
1029 }
1030 // Get pageRec if not supplied:
1031 if (!is_array($pageRec)) {
1032 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
1033 $queryBuilder->getRestrictions()
1034 ->removeAll()
1035 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1036
1037 $pageRec = $queryBuilder->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state')
1038 ->from('pages')
1039 ->where(
1040 $queryBuilder->expr()->eq(
1041 'uid',
1042 $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
1043 ),
1044 $queryBuilder->expr()->neq(
1045 'doktype',
1046 $queryBuilder->createNamedParameter(255, \PDO::PARAM_INT)
1047 )
1048 )
1049 ->execute()
1050 ->fetch();
1051
1052 // Only look for version overlay if page record is not supplied; This assumes
1053 // that the input record is overlaid with preview version, if any!
1054 $this->versionOL('pages', $pageRec);
1055 }
1056 // Set first Page uid:
1057 if (!$firstPageUid) {
1058 $firstPageUid = $pageRec['uid'];
1059 }
1060 // Look for mount pid value plus other required circumstances:
1061 $mount_pid = (int)$pageRec['mount_pid'];
1062 if (is_array($pageRec) && (int)$pageRec['doktype'] === self::DOKTYPE_MOUNTPOINT && $mount_pid > 0 && !in_array($mount_pid, $prevMountPids, true)) {
1063 // Get the mount point record (to verify its general existence):
1064 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
1065 $queryBuilder->getRestrictions()
1066 ->removeAll()
1067 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1068
1069 $mountRec = $queryBuilder->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state')
1070 ->from('pages')
1071 ->where(
1072 $queryBuilder->expr()->eq(
1073 'uid',
1074 $queryBuilder->createNamedParameter($mount_pid, \PDO::PARAM_INT)
1075 ),
1076 $queryBuilder->expr()->neq(
1077 'doktype',
1078 $queryBuilder->createNamedParameter(255, \PDO::PARAM_INT)
1079 )
1080 )
1081 ->execute()
1082 ->fetch();
1083
1084 $this->versionOL('pages', $mountRec);
1085 if (is_array($mountRec)) {
1086 // Look for recursive mount point:
1087 $prevMountPids[] = $mount_pid;
1088 $recursiveMountPid = $this->getMountPointInfo($mount_pid, $mountRec, $prevMountPids, $firstPageUid);
1089 // Return mount point information:
1090 $result = $recursiveMountPid ?: [
1091 'mount_pid' => $mount_pid,
1092 'overlay' => $pageRec['mount_pid_ol'],
1093 'MPvar' => $mount_pid . '-' . $firstPageUid,
1094 'mount_point_rec' => $pageRec,
1095 'mount_pid_rec' => $mountRec
1096 ];
1097 } else {
1098 // Means, there SHOULD have been a mount point, but there was none!
1099 $result = -1;
1100 }
1101 }
1102 }
1103 $this->cache_getMountPointInfo[$pageId] = $result;
1104 return $result;
1105 }
1106
1107 /********************************
1108 *
1109 * Selecting records in general
1110 *
1111 ********************************/
1112
1113 /**
1114 * Checks if a record exists and is accessible.
1115 * The row is returned if everything's OK.
1116 *
1117 * @param string $table The table name to search
1118 * @param int $uid The uid to look up in $table
1119 * @param bool|int $checkPage If checkPage is set, it's also required that the page on which the record resides is accessible
1120 * @return array|int Returns array (the record) if OK, otherwise blank/0 (zero)
1121 */
1122 public function checkRecord($table, $uid, $checkPage = 0)
1123 {
1124 $uid = (int)$uid;
1125 if (is_array($GLOBALS['TCA'][$table]) && $uid > 0) {
1126 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1127 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1128 $row = $queryBuilder->select('*')
1129 ->from($table)
1130 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
1131 ->execute()
1132 ->fetch();
1133
1134 if ($row) {
1135 $this->versionOL($table, $row);
1136 if (is_array($row)) {
1137 if ($checkPage) {
1138 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1139 ->getQueryBuilderForTable('pages');
1140 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1141 $numRows = (int)$queryBuilder->count('*')
1142 ->from('pages')
1143 ->where(
1144 $queryBuilder->expr()->eq(
1145 'uid',
1146 $queryBuilder->createNamedParameter($row['pid'], \PDO::PARAM_INT)
1147 )
1148 )
1149 ->execute()
1150 ->fetchColumn();
1151 if ($numRows > 0) {
1152 return $row;
1153 } else {
1154 return 0;
1155 }
1156 } else {
1157 return $row;
1158 }
1159 }
1160 }
1161 }
1162 return 0;
1163 }
1164
1165 /**
1166 * Returns record no matter what - except if record is deleted
1167 *
1168 * @param string $table The table name to search
1169 * @param int $uid The uid to look up in $table
1170 * @param string $fields The fields to select, default is "*
1171 * @param bool $noWSOL If set, no version overlay is applied
1172 * @return mixed Returns array (the record) if found, otherwise blank/0 (zero)
1173 * @see getPage_noCheck()
1174 */
1175 public function getRawRecord($table, $uid, $fields = '*', $noWSOL = false)
1176 {
1177 $uid = (int)$uid;
1178 if (isset($GLOBALS['TCA'][$table]) && is_array($GLOBALS['TCA'][$table]) && $uid > 0) {
1179 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1180 $queryBuilder->getRestrictions()
1181 ->removeAll()
1182 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1183 $row = $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true))
1184 ->from($table)
1185 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
1186 ->execute()
1187 ->fetch();
1188
1189 if ($row) {
1190 if (!$noWSOL) {
1191 $this->versionOL($table, $row);
1192 }
1193 if (is_array($row)) {
1194 return $row;
1195 }
1196 }
1197 }
1198 return 0;
1199 }
1200
1201 /**
1202 * Selects records based on matching a field (ei. other than UID) with a value
1203 *
1204 * @param string $theTable The table name to search, eg. "pages" or "tt_content
1205 * @param string $theField The fieldname to match, eg. "uid" or "alias
1206 * @param string $theValue The value that fieldname must match, eg. "123" or "frontpage
1207 * @param string $whereClause Optional additional WHERE clauses put in the end of the query. DO NOT PUT IN GROUP BY, ORDER BY or LIMIT!
1208 * @param string $groupBy Optional GROUP BY field(s). If none, supply blank string.
1209 * @param string $orderBy Optional ORDER BY field(s). If none, supply blank string.
1210 * @param string $limit Optional LIMIT value ([begin,]max). If none, supply blank string.
1211 * @return mixed Returns array (the record) if found, otherwise nothing (void)
1212 */
1213 public function getRecordsByField($theTable, $theField, $theValue, $whereClause = '', $groupBy = '', $orderBy = '', $limit = '')
1214 {
1215 if (is_array($GLOBALS['TCA'][$theTable])) {
1216 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($theTable);
1217 $queryBuilder->getRestrictions()
1218 ->removeAll()
1219 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1220
1221 $queryBuilder->select('*')
1222 ->from($theTable)
1223 ->where($queryBuilder->expr()->eq($theField, $queryBuilder->createNamedParameter($theValue)));
1224
1225 if ($whereClause !== '') {
1226 $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($whereClause));
1227 }
1228
1229 if ($groupBy !== '') {
1230 $queryBuilder->groupBy(QueryHelper::parseGroupBy($groupBy));
1231 }
1232
1233 if ($orderBy !== '') {
1234 foreach (QueryHelper::parseOrderBy($orderBy) as $orderPair) {
1235 list($fieldName, $order) = $orderPair;
1236 $queryBuilder->addOrderBy($fieldName, $order);
1237 }
1238 }
1239
1240 if ($limit !== '') {
1241 if (strpos($limit, ',')) {
1242 $limitOffsetAndMax = GeneralUtility::intExplode(',', $limit);
1243 $queryBuilder->setFirstResult((int)$limitOffsetAndMax[0]);
1244 $queryBuilder->setMaxResults((int)$limitOffsetAndMax[1]);
1245 } else {
1246 $queryBuilder->setMaxResults((int)$limit);
1247 }
1248 }
1249
1250 $rows = $queryBuilder->execute()->fetchAll();
1251
1252 if (!empty($rows)) {
1253 return $rows;
1254 }
1255 }
1256 return null;
1257 }
1258
1259 /********************************
1260 *
1261 * Caching and standard clauses
1262 *
1263 ********************************/
1264
1265 /**
1266 * Returns data stored for the hash string in the cache "cache_hash"
1267 * Can be used to retrieved a cached value, array or object
1268 * Can be used from your frontend plugins if you like. It is also used to
1269 * store the parsed TypoScript template structures. You can call it directly
1270 * like PageRepository::getHash()
1271 *
1272 * @param string $hash The hash-string which was used to store the data value
1273 * @return mixed The "data" from the cache
1274 * @see tslib_TStemplate::start(), storeHash()
1275 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, please use the Cache Manager directly to fetch cache entries
1276 */
1277 public static function getHash($hash)
1278 {
1279 GeneralUtility::logDeprecatedFunction();
1280 $hashContent = null;
1281 /** @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $contentHashCache */
1282 $contentHashCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_hash');
1283 $cacheEntry = $contentHashCache->get($hash);
1284 if ($cacheEntry) {
1285 $hashContent = $cacheEntry;
1286 }
1287 return $hashContent;
1288 }
1289
1290 /**
1291 * Stores $data in the 'cache_hash' cache with the hash key, $hash
1292 * and visual/symbolic identification, $ident
1293 *
1294 * Can be used from your frontend plugins if you like. You can call it
1295 * directly like PageRepository::storeHash()
1296 *
1297 * @param string $hash 32 bit hash string (eg. a md5 hash of a serialized array identifying the data being stored)
1298 * @param mixed $data The data to store
1299 * @param string $ident Is just a textual identification in order to inform about the content!
1300 * @param int $lifetime The lifetime for the cache entry in seconds
1301 * @see tslib_TStemplate::start(), getHash()
1302 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, please use the Cache Manager directly to store cache entries
1303 */
1304 public static function storeHash($hash, $data, $ident, $lifetime = 0)
1305 {
1306 GeneralUtility::logDeprecatedFunction();
1307 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_hash')->set($hash, $data, ['ident_' . $ident], (int)$lifetime);
1308 }
1309
1310 /**
1311 * Returns the "AND NOT deleted" clause for the tablename given IF
1312 * $GLOBALS['TCA'] configuration points to such a field.
1313 *
1314 * @param string $table Tablename
1315 * @return string
1316 * @see enableFields()
1317 */
1318 public function deleteClause($table)
1319 {
1320 return $GLOBALS['TCA'][$table]['ctrl']['delete'] ? ' AND ' . $table . '.' . $GLOBALS['TCA'][$table]['ctrl']['delete'] . '=0' : '';
1321 }
1322
1323 /**
1324 * Returns a part of a WHERE clause which will filter out records with start/end
1325 * times or hidden/fe_groups fields set to values that should de-select them
1326 * according to the current time, preview settings or user login. Definitely a
1327 * frontend function.
1328 *
1329 * Is using the $GLOBALS['TCA'] arrays "ctrl" part where the key "enablefields"
1330 * determines for each table which of these features applies to that table.
1331 *
1332 * @param string $table Table name found in the $GLOBALS['TCA'] array
1333 * @param int $show_hidden If $show_hidden is set (0/1), any hidden-fields in records are ignored. NOTICE: If you call this function, consider what to do with the show_hidden parameter. Maybe it should be set? See ContentObjectRenderer->enableFields where it's implemented correctly.
1334 * @param array $ignore_array Array you can pass where keys can be "disabled", "starttime", "endtime", "fe_group" (keys from "enablefields" in TCA) and if set they will make sure that part of the clause is not added. Thus disables the specific part of the clause. For previewing etc.
1335 * @param bool $noVersionPreview If set, enableFields will be applied regardless of any versioning preview settings which might otherwise disable enableFields
1336 * @throws \InvalidArgumentException
1337 * @return string The clause starting like " AND ...=... AND ...=...
1338 * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::enableFields(), deleteClause()
1339 */
1340 public function enableFields($table, $show_hidden = -1, $ignore_array = [], $noVersionPreview = false)
1341 {
1342 if ($show_hidden === -1 && is_object($this->getTypoScriptFrontendController())) {
1343 // If show_hidden was not set from outside and if TSFE is an object, set it
1344 // based on showHiddenPage and showHiddenRecords from TSFE
1345 $show_hidden = $table === 'pages' || $table === 'pages_language_overlay'
1346 ? $this->getTypoScriptFrontendController()->showHiddenPage
1347 : $this->getTypoScriptFrontendController()->showHiddenRecords;
1348 }
1349 if ($show_hidden === -1) {
1350 $show_hidden = 0;
1351 }
1352 // If show_hidden was not changed during the previous evaluation, do it here.
1353 $ctrl = $GLOBALS['TCA'][$table]['ctrl'];
1354 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1355 ->getQueryBuilderForTable($table)
1356 ->expr();
1357 $constraints = [];
1358 if (is_array($ctrl)) {
1359 // Delete field check:
1360 if ($ctrl['delete']) {
1361 $constraints[] = $expressionBuilder->eq($table . '.' . $ctrl['delete'], 0);
1362 }
1363 if ($ctrl['versioningWS']) {
1364 if (!$this->versioningPreview) {
1365 // Filter out placeholder records (new/moved/deleted items)
1366 // in case we are NOT in a versioning preview (that means we are online!)
1367 $constraints[] = $expressionBuilder->lte(
1368 $table . '.t3ver_state',
1369 new VersionState(VersionState::DEFAULT_STATE)
1370 );
1371 } elseif ($table !== 'pages') {
1372 // show only records of live and of the current workspace
1373 // in case we are in a versioning preview
1374 $constraints[] = $expressionBuilder->orX(
1375 $expressionBuilder->eq($table . '.t3ver_wsid', 0),
1376 $expressionBuilder->eq($table . '.t3ver_wsid', (int)$this->versioningWorkspaceId)
1377 );
1378 }
1379
1380 // Filter out versioned records
1381 if (!$noVersionPreview && empty($ignore_array['pid'])) {
1382 $constraints[] = $expressionBuilder->neq($table . '.pid', -1);
1383 }
1384 }
1385
1386 // Enable fields:
1387 if (is_array($ctrl['enablecolumns'])) {
1388 // In case of versioning-preview, enableFields are ignored (checked in
1389 // versionOL())
1390 if (!$this->versioningPreview || !$ctrl['versioningWS'] || $noVersionPreview) {
1391 if ($ctrl['enablecolumns']['disabled'] && !$show_hidden && !$ignore_array['disabled']) {
1392 $field = $table . '.' . $ctrl['enablecolumns']['disabled'];
1393 $constraints[] = $expressionBuilder->eq($field, 0);
1394 }
1395 if ($ctrl['enablecolumns']['starttime'] && !$ignore_array['starttime']) {
1396 $field = $table . '.' . $ctrl['enablecolumns']['starttime'];
1397 $constraints[] = $expressionBuilder->lte($field, (int)$GLOBALS['SIM_ACCESS_TIME']);
1398 }
1399 if ($ctrl['enablecolumns']['endtime'] && !$ignore_array['endtime']) {
1400 $field = $table . '.' . $ctrl['enablecolumns']['endtime'];
1401 $constraints[] = $expressionBuilder->orX(
1402 $expressionBuilder->eq($field, 0),
1403 $expressionBuilder->gt($field, (int)$GLOBALS['SIM_ACCESS_TIME'])
1404 );
1405 }
1406 if ($ctrl['enablecolumns']['fe_group'] && !$ignore_array['fe_group']) {
1407 $field = $table . '.' . $ctrl['enablecolumns']['fe_group'];
1408 $constraints[] = QueryHelper::stripLogicalOperatorPrefix(
1409 $this->getMultipleGroupsWhereClause($field, $table)
1410 );
1411 }
1412 // Call hook functions for additional enableColumns
1413 // It is used by the extension ingmar_accessctrl which enables assigning more
1414 // than one usergroup to content and page records
1415 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['addEnableColumns'])) {
1416 $_params = [
1417 'table' => $table,
1418 'show_hidden' => $show_hidden,
1419 'ignore_array' => $ignore_array,
1420 'ctrl' => $ctrl
1421 ];
1422 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['addEnableColumns'] as $_funcRef) {
1423 $constraints[] = QueryHelper::stripLogicalOperatorPrefix(
1424 GeneralUtility::callUserFunction($_funcRef, $_params, $this)
1425 );
1426 }
1427 }
1428 }
1429 }
1430 } else {
1431 throw new \InvalidArgumentException('There is no entry in the $TCA array for the table "' . $table . '". This means that the function enableFields() is ' . 'called with an invalid table name as argument.', 1283790586);
1432 }
1433
1434 return empty($constraints) ? '' : ' AND ' . $expressionBuilder->andX(...$constraints);
1435 }
1436
1437 /**
1438 * Creating where-clause for checking group access to elements in enableFields
1439 * function
1440 *
1441 * @param string $field Field with group list
1442 * @param string $table Table name
1443 * @return string AND sql-clause
1444 * @see enableFields()
1445 */
1446 public function getMultipleGroupsWhereClause($field, $table)
1447 {
1448 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1449 ->getQueryBuilderForTable($table)
1450 ->expr();
1451 $memberGroups = GeneralUtility::intExplode(',', $this->getTypoScriptFrontendController()->gr_list);
1452 $orChecks = [];
1453 // If the field is empty, then OK
1454 $orChecks[] = $expressionBuilder->eq($field, $expressionBuilder->literal(''));
1455 // If the field is NULL, then OK
1456 $orChecks[] = $expressionBuilder->isNull($field);
1457 // If the field contains zero, then OK
1458 $orChecks[] = $expressionBuilder->eq($field, $expressionBuilder->literal('0'));
1459 foreach ($memberGroups as $value) {
1460 $orChecks[] = $expressionBuilder->inSet($field, $expressionBuilder->literal($value));
1461 }
1462
1463 return' AND (' . $expressionBuilder->orX(...$orChecks) . ')';
1464 }
1465
1466 /**********************
1467 *
1468 * Versioning Preview
1469 *
1470 **********************/
1471
1472 /**
1473 * Finding online PID for offline version record
1474 *
1475 * ONLY active when backend user is previewing records. MUST NEVER affect a site
1476 * served which is not previewed by backend users!!!
1477 *
1478 * Will look if the "pid" value of the input record is -1 (it is an offline
1479 * version) and if the table supports versioning; if so, it will translate the -1
1480 * PID into the PID of the original record.
1481 *
1482 * Used whenever you are tracking something back, like making the root line.
1483 *
1484 * Principle; Record offline! => Find online?
1485 *
1486 * @param string $table Table name
1487 * @param array $rr Record array passed by reference. As minimum, "pid" and "uid" fields must exist! "t3ver_oid" and "t3ver_wsid" is nice and will save you a DB query.
1488 * @see BackendUtility::fixVersioningPid(), versionOL(), getRootLine()
1489 */
1490 public function fixVersioningPid($table, &$rr)
1491 {
1492 if ($this->versioningPreview && is_array($rr) && (int)$rr['pid'] === -1 && $GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
1493 $oid = 0;
1494 $wsid = 0;
1495 // Check values for t3ver_oid and t3ver_wsid:
1496 if (isset($rr['t3ver_oid']) && isset($rr['t3ver_wsid'])) {
1497 // If "t3ver_oid" is already a field, just set this:
1498 $oid = $rr['t3ver_oid'];
1499 $wsid = $rr['t3ver_wsid'];
1500 } else {
1501 // Otherwise we have to expect "uid" to be in the record and look up based
1502 // on this:
1503 $newPidRec = $this->getRawRecord($table, $rr['uid'], 't3ver_oid,t3ver_wsid', true);
1504 if (is_array($newPidRec)) {
1505 $oid = $newPidRec['t3ver_oid'];
1506 $wsid = $newPidRec['t3ver_wsid'];
1507 }
1508 }
1509 // If workspace ids matches and ID of current online version is found, look up
1510 // the PID value of that:
1511 if ($oid && ((int)$this->versioningWorkspaceId === 0 && $this->checkWorkspaceAccess($wsid) || (int)$wsid === (int)$this->versioningWorkspaceId)) {
1512 $oidRec = $this->getRawRecord($table, $oid, 'pid', true);
1513 if (is_array($oidRec)) {
1514 // SWAP uid as well? Well no, because when fixing a versioning PID happens it is
1515 // assumed that this is a "branch" type page and therefore the uid should be
1516 // kept (like in versionOL()). However if the page is NOT a branch version it
1517 // should not happen - but then again, direct access to that uid should not
1518 // happen!
1519 $rr['_ORIG_pid'] = $rr['pid'];
1520 $rr['pid'] = $oidRec['pid'];
1521 }
1522 }
1523 }
1524 // Changing PID in case of moving pointer:
1525 if ($movePlhRec = $this->getMovePlaceholder($table, $rr['uid'], 'pid')) {
1526 $rr['pid'] = $movePlhRec['pid'];
1527 }
1528 }
1529
1530 /**
1531 * Versioning Preview Overlay
1532 *
1533 * ONLY active when backend user is previewing records. MUST NEVER affect a site
1534 * served which is not previewed by backend users!!!
1535 *
1536 * Generally ALWAYS used when records are selected based on uid or pid. If
1537 * records are selected on other fields than uid or pid (eg. "email = ....") then
1538 * usage might produce undesired results and that should be evaluated on
1539 * individual basis.
1540 *
1541 * Principle; Record online! => Find offline?
1542 *
1543 * @param string $table Table name
1544 * @param array $row Record array passed by reference. As minimum, the "uid", "pid" and "t3ver_state" fields must exist! The record MAY be set to FALSE in which case the calling function should act as if the record is forbidden to access!
1545 * @param bool $unsetMovePointers If set, the $row is cleared in case it is a move-pointer. This is only for preview of moved records (to remove the record from the original location so it appears only in the new location)
1546 * @param bool $bypassEnableFieldsCheck Unless this option is TRUE, the $row is unset if enablefields for BOTH the version AND the online record deselects it. This is because when versionOL() is called it is assumed that the online record is already selected with no regards to it's enablefields. However, after looking for a new version the online record enablefields must ALSO be evaluated of course. This is done all by this function!
1547 * @see fixVersioningPid(), BackendUtility::workspaceOL()
1548 */
1549 public function versionOL($table, &$row, $unsetMovePointers = false, $bypassEnableFieldsCheck = false)
1550 {
1551 if ($this->versioningPreview && is_array($row)) {
1552 // will overlay any movePlhOL found with the real record, which in turn
1553 // will be overlaid with its workspace version if any.
1554 $movePldSwap = $this->movePlhOL($table, $row);
1555 // implode(',',array_keys($row)) = Using fields from original record to make
1556 // sure no additional fields are selected. This is best for eg. getPageOverlay()
1557 // Computed properties are excluded since those would lead to SQL errors.
1558 $fieldNames = implode(',', array_keys($this->purgeComputedProperties($row)));
1559 if ($wsAlt = $this->getWorkspaceVersionOfRecord($this->versioningWorkspaceId, $table, $row['uid'], $fieldNames, $bypassEnableFieldsCheck)) {
1560 if (is_array($wsAlt)) {
1561 // Always fix PID (like in fixVersioningPid() above). [This is usually not
1562 // the important factor for versioning OL]
1563 // Keep the old (-1) - indicates it was a version...
1564 $wsAlt['_ORIG_pid'] = $wsAlt['pid'];
1565 // Set in the online versions PID.
1566 $wsAlt['pid'] = $row['pid'];
1567 // For versions of single elements or page+content, preserve online UID and PID
1568 // (this will produce true "overlay" of element _content_, not any references)
1569 // For page+content the "_ORIG_uid" should actually be used as PID for selection.
1570 $wsAlt['_ORIG_uid'] = $wsAlt['uid'];
1571 $wsAlt['uid'] = $row['uid'];
1572 // Translate page alias as well so links are pointing to the _online_ page:
1573 if ($table === 'pages') {
1574 $wsAlt['alias'] = $row['alias'];
1575 }
1576 // Changing input record to the workspace version alternative:
1577 $row = $wsAlt;
1578 // Check if it is deleted/new
1579 $rowVersionState = VersionState::cast($row['t3ver_state']);
1580 if (
1581 $rowVersionState->equals(VersionState::NEW_PLACEHOLDER)
1582 || $rowVersionState->equals(VersionState::DELETE_PLACEHOLDER)
1583 ) {
1584 // Unset record if it turned out to be deleted in workspace
1585 $row = false;
1586 }
1587 // Check if move-pointer in workspace (unless if a move-placeholder is the
1588 // reason why it appears!):
1589 // You have to specifically set $unsetMovePointers in order to clear these
1590 // because it is normally a display issue if it should be shown or not.
1591 if (
1592 ($rowVersionState->equals(VersionState::MOVE_POINTER)
1593 && !$movePldSwap
1594 ) && $unsetMovePointers
1595 ) {
1596 // Unset record if it turned out to be deleted in workspace
1597 $row = false;
1598 }
1599 } else {
1600 // No version found, then check if t3ver_state = VersionState::NEW_PLACEHOLDER
1601 // (online version is dummy-representation)
1602 // Notice, that unless $bypassEnableFieldsCheck is TRUE, the $row is unset if
1603 // enablefields for BOTH the version AND the online record deselects it. See
1604 // note for $bypassEnableFieldsCheck
1605 /** @var \TYPO3\CMS\Core\Versioning\VersionState $versionState */
1606 $versionState = VersionState::cast($row['t3ver_state']);
1607 if ($wsAlt <= -1 || $versionState->indicatesPlaceholder()) {
1608 // Unset record if it turned out to be "hidden"
1609 $row = false;
1610 }
1611 }
1612 }
1613 }
1614 }
1615
1616 /**
1617 * Checks if record is a move-placeholder
1618 * (t3ver_state==VersionState::MOVE_PLACEHOLDER) and if so it will set $row to be
1619 * the pointed-to live record (and return TRUE) Used from versionOL
1620 *
1621 * @param string $table Table name
1622 * @param array $row Row (passed by reference) - only online records...
1623 * @return bool TRUE if overlay is made.
1624 * @see BackendUtility::movePlhOl()
1625 */
1626 public function movePlhOL($table, &$row)
1627 {
1628 if (!empty($GLOBALS['TCA'][$table]['ctrl']['versioningWS'])
1629 && (int)VersionState::cast($row['t3ver_state'])->equals(VersionState::MOVE_PLACEHOLDER)
1630 ) {
1631 // If t3ver_move_id is not found, then find it (but we like best if it is here)
1632 if (!isset($row['t3ver_move_id'])) {
1633 $moveIDRec = $this->getRawRecord($table, $row['uid'], 't3ver_move_id', true);
1634 $moveID = $moveIDRec['t3ver_move_id'];
1635 } else {
1636 $moveID = $row['t3ver_move_id'];
1637 }
1638 // Find pointed-to record.
1639 if ($moveID) {
1640 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1641 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1642 $origRow = $queryBuilder->select(...array_keys($this->purgeComputedProperties($row)))
1643 ->from($table)
1644 ->where(
1645 $queryBuilder->expr()->eq(
1646 'uid',
1647 $queryBuilder->createNamedParameter($moveID, \PDO::PARAM_INT)
1648 )
1649 )
1650 ->setMaxResults(1)
1651 ->execute()
1652 ->fetch();
1653
1654 if ($origRow) {
1655 $row = $origRow;
1656 return true;
1657 }
1658 }
1659 }
1660 return false;
1661 }
1662
1663 /**
1664 * Returns move placeholder of online (live) version
1665 *
1666 * @param string $table Table name
1667 * @param int $uid Record UID of online version
1668 * @param string $fields Field list, default is *
1669 * @return array If found, the record, otherwise nothing.
1670 * @see BackendUtility::getMovePlaceholder()
1671 */
1672 public function getMovePlaceholder($table, $uid, $fields = '*')
1673 {
1674 if ($this->versioningPreview) {
1675 $workspace = (int)$this->versioningWorkspaceId;
1676 if (!empty($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) && $workspace !== 0) {
1677 // Select workspace version of record:
1678 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1679 $queryBuilder->getRestrictions()
1680 ->removeAll()
1681 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1682
1683 $row = $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true))
1684 ->from($table)
1685 ->where(
1686 $queryBuilder->expr()->neq('pid', $queryBuilder->createNamedParameter(-1, \PDO::PARAM_INT)),
1687 $queryBuilder->expr()->eq(
1688 't3ver_state',
1689 $queryBuilder->createNamedParameter(
1690 new VersionState(VersionState::MOVE_PLACEHOLDER),
1691 \PDO::PARAM_INT
1692 )
1693 ),
1694 $queryBuilder->expr()->eq(
1695 't3ver_move_id',
1696 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
1697 ),
1698 $queryBuilder->expr()->eq(
1699 't3ver_wsid',
1700 $queryBuilder->createNamedParameter($workspace, \PDO::PARAM_INT)
1701 )
1702 )
1703 ->setMaxResults(1)
1704 ->execute()
1705 ->fetch();
1706
1707 if (is_array($row)) {
1708 return $row;
1709 }
1710 }
1711 }
1712 return false;
1713 }
1714
1715 /**
1716 * Select the version of a record for a workspace
1717 *
1718 * @param int $workspace Workspace ID
1719 * @param string $table Table name to select from
1720 * @param int $uid Record uid for which to find workspace version.
1721 * @param string $fields Field list to select
1722 * @param bool $bypassEnableFieldsCheck If TRUE, enablefields are not checked for.
1723 * @return mixed If found, return record, otherwise other value: Returns 1 if version was sought for but not found, returns -1/-2 if record (offline/online) existed but had enableFields that would disable it. Returns FALSE if not in workspace or no versioning for record. Notice, that the enablefields of the online record is also tested.
1724 * @see BackendUtility::getWorkspaceVersionOfRecord()
1725 */
1726 public function getWorkspaceVersionOfRecord($workspace, $table, $uid, $fields = '*', $bypassEnableFieldsCheck = false)
1727 {
1728 if ($workspace !== 0 && !empty($GLOBALS['TCA'][$table]['ctrl']['versioningWS'])) {
1729 $workspace = (int)$workspace;
1730 $uid = (int)$uid;
1731 // Select workspace version of record, only testing for deleted.
1732 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1733 $queryBuilder->getRestrictions()
1734 ->removeAll()
1735 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1736
1737 $newrow = $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true))
1738 ->from($table)
1739 ->where(
1740 $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(-1, \PDO::PARAM_INT)),
1741 $queryBuilder->expr()->eq(
1742 't3ver_oid',
1743 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
1744 ),
1745 $queryBuilder->expr()->eq(
1746 't3ver_wsid',
1747 $queryBuilder->createNamedParameter($workspace, \PDO::PARAM_INT)
1748 )
1749 )
1750 ->setMaxResults(1)
1751 ->execute()
1752 ->fetch();
1753
1754 // If version found, check if it could have been selected with enableFields on
1755 // as well:
1756 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1757 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1758 // Remove the frontend workspace restriction because we are testing a version record
1759 $queryBuilder->getRestrictions()->removeByType(FrontendWorkspaceRestriction::class);
1760 $queryBuilder->select('uid')
1761 ->from($table)
1762 ->setMaxResults(1);
1763
1764 if (is_array($newrow)) {
1765 $queryBuilder->where(
1766 $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(-1, \PDO::PARAM_INT)),
1767 $queryBuilder->expr()->eq(
1768 't3ver_oid',
1769 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
1770 ),
1771 $queryBuilder->expr()->eq(
1772 't3ver_wsid',
1773 $queryBuilder->createNamedParameter($workspace, \PDO::PARAM_INT)
1774 )
1775 );
1776 if ($bypassEnableFieldsCheck || $queryBuilder->execute()->fetchColumn()) {
1777 // Return offline version, tested for its enableFields.
1778 return $newrow;
1779 } else {
1780 // Return -1 because offline version was de-selected due to its enableFields.
1781 return -1;
1782 }
1783 } else {
1784 // OK, so no workspace version was found. Then check if online version can be
1785 // selected with full enable fields and if so, return 1:
1786 $queryBuilder->where(
1787 $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT))
1788 );
1789 if ($bypassEnableFieldsCheck || $queryBuilder->execute()->fetchColumn()) {
1790 // Means search was done, but no version found.
1791 return 1;
1792 } else {
1793 // Return -2 because the online record was de-selected due to its enableFields.
1794 return -2;
1795 }
1796 }
1797 }
1798 // No look up in database because versioning not enabled / or workspace not
1799 // offline
1800 return false;
1801 }
1802
1803 /**
1804 * Checks if user has access to workspace.
1805 *
1806 * @param int $wsid Workspace ID
1807 * @return bool true if the backend user has access to a certain workspace
1808 */
1809 public function checkWorkspaceAccess($wsid)
1810 {
1811 if (!$this->getBackendUser() || !ExtensionManagementUtility::isLoaded('workspaces')) {
1812 return false;
1813 }
1814 if (!isset($this->workspaceCache[$wsid])) {
1815 $this->workspaceCache[$wsid] = $this->getBackendUser()->checkWorkspace($wsid);
1816 }
1817 return (string)$this->workspaceCache[$wsid]['_ACCESS'] !== '';
1818 }
1819
1820 /**
1821 * Gets file references for a given record field.
1822 *
1823 * @param string $tableName Name of the table
1824 * @param string $fieldName Name of the field
1825 * @param array $element The parent element referencing to files
1826 * @return array
1827 */
1828 public function getFileReferences($tableName, $fieldName, array $element)
1829 {
1830 /** @var $fileRepository FileRepository */
1831 $fileRepository = GeneralUtility::makeInstance(FileRepository::class);
1832 $currentId = !empty($element['uid']) ? $element['uid'] : 0;
1833
1834 // Fetch the references of the default element
1835 try {
1836 $references = $fileRepository->findByRelation($tableName, $fieldName, $currentId);
1837 } catch (FileDoesNotExistException $e) {
1838 /**
1839 * We just catch the exception here
1840 * Reasoning: There is nothing an editor or even admin could do
1841 */
1842 return [];
1843 } catch (\InvalidArgumentException $e) {
1844 /**
1845 * The storage does not exist anymore
1846 * Log the exception message for admins as they maybe can restore the storage
1847 */
1848 $logMessage = $e->getMessage() . ' (table: "' . $tableName . '", fieldName: "' . $fieldName . '", currentId: ' . $currentId . ')';
1849 GeneralUtility::sysLog($logMessage, 'core', GeneralUtility::SYSLOG_SEVERITY_ERROR);
1850 return [];
1851 }
1852
1853 $localizedId = null;
1854 if (isset($element['_LOCALIZED_UID'])) {
1855 $localizedId = $element['_LOCALIZED_UID'];
1856 } elseif (isset($element['_PAGES_OVERLAY_UID'])) {
1857 $localizedId = $element['_PAGES_OVERLAY_UID'];
1858 }
1859
1860 if ($tableName === 'pages') {
1861 $tableName = 'pages_language_overlay';
1862 }
1863
1864 $isTableLocalizable = (
1865 !empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
1866 && !empty($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
1867 );
1868 if ($isTableLocalizable && $localizedId !== null) {
1869 $localizedReferences = $fileRepository->findByRelation($tableName, $fieldName, $localizedId);
1870 $references = $localizedReferences;
1871 }
1872
1873 return $references;
1874 }
1875
1876 /**
1877 * Purges computed properties from database rows,
1878 * such as _ORIG_uid or _ORIG_pid for instance.
1879 *
1880 * @param array $row
1881 * @return array
1882 */
1883 protected function purgeComputedProperties(array $row)
1884 {
1885 foreach ($this->computedPropertyNames as $computedPropertyName) {
1886 if (array_key_exists($computedPropertyName, $row)) {
1887 unset($row[$computedPropertyName]);
1888 }
1889 }
1890 return $row;
1891 }
1892
1893 /**
1894 * Determine if a field needs an overlay
1895 *
1896 * @param string $table TCA tablename
1897 * @param string $field TCA fieldname
1898 * @param mixed $value Current value of the field
1899 * @return bool Returns TRUE if a given record field needs to be overlaid
1900 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
1901 */
1902 protected function shouldFieldBeOverlaid($table, $field, $value)
1903 {
1904 GeneralUtility::logDeprecatedFunction();
1905 return true;
1906 }
1907
1908 /**
1909 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
1910 */
1911 protected function getTypoScriptFrontendController()
1912 {
1913 return $GLOBALS['TSFE'];
1914 }
1915
1916 /**
1917 * Returns the current BE user.
1918 *
1919 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1920 */
1921 protected function getBackendUser()
1922 {
1923 return $GLOBALS['BE_USER'];
1924 }
1925 }