Fixed bug #16147: getRecordOverlay fetches "Versioning Preview Overlay" but does...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_page.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains a class with "Page functions" mainly for the frontend
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 2/2003 by Kasper Skårhøj
32 * XHTML-trans compliant
33 *
34 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 109: class t3lib_pageSelect
42 * 134: function init($show_hidden)
43 *
44 * SECTION: Selecting page records
45 * 184: function getPage($uid, $disableGroupAccessCheck=FALSE)
46 * 200: function getPage_noCheck($uid)
47 * 216: function getFirstWebPage($uid)
48 * 234: function getPageIdFromAlias($alias)
49 * 250: function getPageOverlay($pageInput,$lUid=-1)
50 * 314: function getRecordOverlay($table,$row,$sys_language_content,$OLmode='')
51 *
52 * SECTION: Page related: Menu, Domain record, Root line
53 * 413: function getMenu($uid,$fields='*',$sortField='sorting',$addWhere='',$checkShortcuts=1)
54 * 471: function getDomainStartPage($domain, $path='',$request_uri='')
55 * 519: function getRootLine($uid, $MP='', $ignoreMPerrors=FALSE)
56 * 640: function getPathFromRootline($rl,$len=20)
57 * 661: function getExtURL($pagerow,$disable=0)
58 * 685: function getMountPointInfo($pageId, $pageRec=FALSE, $prevMountPids=array(), $firstPageUid=0)
59 *
60 * SECTION: Selecting records in general
61 * 762: function checkRecord($table,$uid,$checkPage=0)
62 * 797: function getRawRecord($table,$uid,$fields='*',$noWSOL=FALSE)
63 * 823: function getRecordsByField($theTable,$theField,$theValue,$whereClause='',$groupBy='',$orderBy='',$limit='')
64 *
65 * SECTION: Caching and standard clauses
66 * 875: function getHash($hash)
67 * 898: function storeHash($hash,$data,$ident)
68 * 916: function deleteClause($table)
69 * 936: function enableFields($table,$show_hidden=-1,$ignore_array=array(),$noVersionPreview=FALSE)
70 * 1008: function getMultipleGroupsWhereClause($field, $table)
71 *
72 * SECTION: Versioning Preview
73 * 1055: function fixVersioningPid($table,&$rr)
74 * 1096: function versionOL($table,&$row)
75 * 1151: function getWorkspaceVersionOfRecord($workspace, $table, $uid, $fields='*')
76 *
77 * TOTAL FUNCTIONS: 24
78 * (This index is automatically created/updated by the extension "extdeveval")
79 *
80 */
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 /**
100 * Page functions, a lot of sql/pages-related functions
101 * Mainly used in the frontend but also in some cases in the backend.
102 * It's important to set the right $where_hid_del in the object so that the functions operate properly
103 *
104 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
105 * @package TYPO3
106 * @subpackage t3lib
107 * @see tslib_fe::fetch_the_id()
108 */
109 class t3lib_pageSelect {
110 var $urltypes = Array('','http://','ftp://','mailto:','https://');
111 var $where_hid_del = ' AND pages.deleted=0'; // This is not the final clauses. There will normally be conditions for the hidden,starttime and endtime fields as well. You MUST initialize the object by the init() function
112 var $where_groupAccess = ''; // Clause for fe_group access
113 var $sys_language_uid = 0;
114
115 // Versioning preview related:
116 var $versioningPreview = FALSE; // If true, versioning preview of other record versions is allowed. THIS MUST ONLY BE SET IF the page is not cached and truely previewed by a backend user!!!
117 var $versioningWorkspaceId = 0; // Workspace ID for preview
118 var $workspaceCache = array();
119
120
121 // Internal, dynamic:
122 var $error_getRootLine = ''; // Error string set by getRootLine()
123 var $error_getRootLine_failPid = 0; // Error uid set by getRootLine()
124
125 // Internal caching
126 protected $cache_getRootLine = array();
127 protected $cache_getPage = array();
128 protected $cache_getPage_noCheck = array();
129 protected $cache_getPageIdFromAlias = array();
130 protected $cache_getMountPointInfo = array();
131
132 /**
133 * Named constants for "magic numbers" of the field doktype
134 */
135 const DOKTYPE_DEFAULT = 1;
136 const DOKTYPE_ADVANCED = 2; // @deprecated since TYPO3 4.2
137 const DOKTYPE_LINK = 3;
138 const DOKTYPE_SHORTCUT = 4;
139 const DOKTYPE_HIDE_IN_MENU = 5; // @deprecated since TYPO3 4.2
140 const DOKTYPE_BE_USER_SECTION = 6;
141 const DOKTYPE_MOUNTPOINT = 7;
142 const DOKTYPE_SPACER = 199;
143 const DOKTYPE_SYSFOLDER = 254;
144 const DOKTYPE_RECYCLER = 255;
145
146
147 /**
148 * Named constants for "magic numbers" of the field shortcut_mode
149 */
150 const SHORTCUT_MODE_NONE = 0;
151 const SHORTCUT_MODE_FIRST_SUBPAGE = 1;
152 const SHORTCUT_MODE_RANDOM_SUBPAGE = 2;
153 const SHORTCUT_MODE_PARENT_PAGE = 3;
154
155 /**
156 * init() MUST be run directly after creating a new template-object
157 * This sets the internal variable $this->where_hid_del to the correct where clause for page records taking deleted/hidden/starttime/endtime/t3ver_state into account
158 *
159 * @param boolean If $show_hidden is true, the hidden-field is ignored!! Normally this should be false. Is used for previewing.
160 * @return void
161 * @see tslib_fe::fetch_the_id(), tx_tstemplateanalyzer::initialize_editor()
162 */
163 function init($show_hidden) {
164 $this->where_groupAccess = '';
165 $this->where_hid_del = ' AND pages.deleted=0 ';
166 if (!$show_hidden) {
167 $this->where_hid_del.= 'AND pages.hidden=0 ';
168 }
169 $this->where_hid_del .= 'AND pages.starttime<=' . $GLOBALS['SIM_ACCESS_TIME'] . ' AND (pages.endtime=0 OR pages.endtime>' . $GLOBALS['SIM_ACCESS_TIME'] . ') ';
170
171 // Filter out new/deleted place-holder pages in case we are NOT in a versioning preview (that means we are online!)
172 if (!$this->versioningPreview) {
173 $this->where_hid_del .= ' AND NOT pages.t3ver_state>0';
174 } else {
175 // For version previewing, make sure that enable-fields are not de-selecting hidden pages - we need versionOL() to unset them only if the overlay record instructs us to.
176 $this->versioningPreview_where_hid_del = $this->where_hid_del; // Copy where_hid_del to other variable (used in relation to versionOL())
177 $this->where_hid_del = ' AND pages.deleted=0 '; // Clear where_hid_del
178 }
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 /*******************************************
198 *
199 * Selecting page records
200 *
201 ******************************************/
202
203 /**
204 * Returns the $row for the page with uid = $uid (observing ->where_hid_del)
205 * Any pages_language_overlay will be applied before the result is returned.
206 * If no page is found an empty array is returned.
207 *
208 * @param integer The page id to look up.
209 * @param boolean If set, the check for group access is disabled. VERY rarely used
210 * @return array The page row with overlayed localized fields. Empty it no page.
211 * @see getPage_noCheck()
212 */
213 function getPage($uid, $disableGroupAccessCheck=FALSE) {
214 // Hook to manipulate the page uid for special overlay handling
215 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'])) {
216 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'] as $classRef) {
217 $hookObject = t3lib_div::getUserObj($classRef);
218
219 if (!($hookObject instanceof t3lib_pageSelect_getPageHook)) {
220 throw new UnexpectedValueException('$hookObject must implement interface t3lib_pageSelect_getPageHook', 1251476766);
221 }
222
223 $hookObject->getPage_preProcess($uid, $disableGroupAccessCheck, $this);
224 }
225 }
226
227 $accessCheck = $disableGroupAccessCheck ? '' : $this->where_groupAccess;
228 $cacheKey = md5($accessCheck . '-' . $this->where_hid_del . '-' . $this->sys_language_uid);
229
230 if (is_array($this->cache_getPage[$uid][$cacheKey])) {
231 return $this->cache_getPage[$uid][$cacheKey];
232 }
233 $result = array();
234 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid='.intval($uid).$this->where_hid_del.$accessCheck);
235 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
236 $GLOBALS['TYPO3_DB']->sql_free_result($res);
237 if ($row) {
238 $this->versionOL('pages',$row);
239 if (is_array($row)) {
240 $result = $this->getPageOverlay($row);
241 }
242 }
243 $this->cache_getPage[$uid][$cacheKey] = $result;
244 return $result;
245 }
246
247 /**
248 * Return the $row for the page with uid = $uid WITHOUT checking for ->where_hid_del (start- and endtime or hidden). Only "deleted" is checked!
249 *
250 * @param integer The page id to look up
251 * @return array The page row with overlayed localized fields. Empty array if no page.
252 * @see getPage()
253 */
254 function getPage_noCheck($uid) {
255 if ($this->cache_getPage_noCheck[$uid]) {
256 return $this->cache_getPage_noCheck[$uid];
257 }
258
259 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid='.intval($uid).$this->deleteClause('pages'));
260 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
261 $GLOBALS['TYPO3_DB']->sql_free_result($res);
262
263 $result = array();
264 if ($row) {
265 $this->versionOL('pages',$row);
266 if (is_array($row)) {
267 $result = $this->getPageOverlay($row);
268 }
269 }
270 $this->cache_getPage_noCheck[$uid] = $result;
271
272 return $result;
273 }
274
275 /**
276 * Returns the $row of the first web-page in the tree (for the default menu...)
277 *
278 * @param integer The page id for which to fetch first subpages (PID)
279 * @return mixed If found: The page record (with overlayed localized fields, if any). If NOT found: blank value (not array!)
280 * @see tslib_fe::fetch_the_id()
281 */
282 function getFirstWebPage($uid) {
283 $output = '';
284 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'pid='.intval($uid).$this->where_hid_del.$this->where_groupAccess, '', 'sorting', '1');
285 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
286 $GLOBALS['TYPO3_DB']->sql_free_result($res);
287 if ($row) {
288 $this->versionOL('pages',$row);
289 if (is_array($row)) {
290 $output = $this->getPageOverlay($row);
291 }
292 }
293 return $output;
294 }
295
296 /**
297 * Returns a pagerow for the page with alias $alias
298 *
299 * @param string The alias to look up the page uid for.
300 * @return integer Returns page uid (integer) if found, otherwise 0 (zero)
301 * @see tslib_fe::checkAndSetAlias(), tslib_cObj::typoLink()
302 */
303 function getPageIdFromAlias($alias) {
304 $alias = strtolower($alias);
305 if ($this->cache_getPageIdFromAlias[$alias]) {
306 return $this->cache_getPageIdFromAlias[$alias];
307 }
308 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'alias='.$GLOBALS['TYPO3_DB']->fullQuoteStr($alias, 'pages').' AND pid>=0 AND pages.deleted=0'); // "AND pid>=0" because of versioning (means that aliases sent MUST be online!)
309 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
310 $GLOBALS['TYPO3_DB']->sql_free_result($res);
311 if ($row) {
312 $this->cache_getPageIdFromAlias[$alias] = $row['uid'];
313 return $row['uid'];
314 }
315 $this->cache_getPageIdFromAlias[$alias] = 0;
316 return 0;
317 }
318
319 /**
320 * Returns the relevant page overlay record fields
321 *
322 * @param mixed 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 OVERLAYED before the page record is returned.
323 * @param integer Language UID if you want to set an alternative value to $this->sys_language_uid which is default. Should be >=0
324 * @return array Page row which is overlayed with language_overlay record (or the overlay record alone)
325 */
326 function getPageOverlay($pageInput,$lUid=-1) {
327
328 // Initialize:
329 if ($lUid<0) $lUid = $this->sys_language_uid;
330 $row = NULL;
331
332 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay'])) {
333 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay'] as $classRef) {
334 $hookObject = t3lib_div::getUserObj($classRef);
335
336 if (!($hookObject instanceof t3lib_pageSelect_getPageOverlayHook)) {
337 throw new UnexpectedValueException('$hookObject must implement interface t3lib_pageSelect_getPageOverlayHook', 1269878881);
338 }
339
340 $hookObject->getPageOverlay_preProcess($pageInput, $lUid, $this);
341 }
342 }
343
344 // If language UID is different from zero, do overlay:
345 if ($lUid) {
346 $fieldArr = explode(',', $GLOBALS['TYPO3_CONF_VARS']['FE']['pageOverlayFields']);
347 if (is_array($pageInput)) {
348 $page_id = $pageInput['uid']; // Was the whole record
349 $fieldArr = array_intersect($fieldArr,array_keys($pageInput)); // Make sure that only fields which exist in the incoming record are overlaid!
350 } else {
351 $page_id = $pageInput; // Was the id
352 }
353
354 if (count($fieldArr)) {
355 /*
356 NOTE to enabledFields('pages_language_overlay'):
357 Currently the showHiddenRecords of TSFE set will allow pages_language_overlay records to be selected as they are child-records of a page.
358 However you may argue that the showHiddenField flag should determine this. But that's not how it's done right now.
359 */
360
361 // Selecting overlay record:
362 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
363 implode(',',$fieldArr),
364 'pages_language_overlay',
365 'pid='.intval($page_id).'
366 AND sys_language_uid='.intval($lUid).
367 $this->enableFields('pages_language_overlay'),
368 '',
369 '',
370 '1'
371 );
372 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
373 $GLOBALS['TYPO3_DB']->sql_free_result($res);
374 $this->versionOL('pages_language_overlay',$row);
375
376 if (is_array($row)) {
377 $row['_PAGES_OVERLAY'] = TRUE;
378
379 // Unset vital fields that are NOT allowed to be overlaid:
380 unset($row['uid']);
381 unset($row['pid']);
382 }
383 }
384 }
385
386 // Create output:
387 if (is_array($pageInput)) {
388 return is_array($row) ? array_merge($pageInput,$row) : $pageInput; // If the input was an array, simply overlay the newfound array and return...
389 } else {
390 return is_array($row) ? $row : array(); // always an array in return
391 }
392 }
393
394 /**
395 * Creates language-overlay for records in general (where translation is found in records from the same table)
396 *
397 * @param string Table name
398 * @param array Record to overlay. Must containt uid, pid and $table]['ctrl']['languageField']
399 * @param integer Pointer to the sys_language uid for content on the site.
400 * @param string Overlay mode. If "hideNonTranslated" then records without translation will not be returned un-translated but unset (and return value is false)
401 * @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.
402 */
403 function getRecordOverlay($table,$row,$sys_language_content,$OLmode='') {
404 global $TCA;
405
406 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'])) {
407 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'] as $classRef) {
408 $hookObject = t3lib_div::getUserObj($classRef);
409
410 if (!($hookObject instanceof t3lib_pageSelect_getRecordOverlayHook)) {
411 throw new UnexpectedValueException('$hookObject must implement interface t3lib_pageSelect_getRecordOverlayHook', 1269881658);
412 }
413 $hookObject->getRecordOverlay_preProcess($table,$row,$sys_language_content,$OLmode, $this);
414 }
415 }
416
417 if ($row['uid']>0 && $row['pid']>0) {
418 if ($TCA[$table] && $TCA[$table]['ctrl']['languageField'] && $TCA[$table]['ctrl']['transOrigPointerField']) {
419 if (!$TCA[$table]['ctrl']['transOrigPointerTable']) { // Will not be able to work with other tables (Just didn't implement it yet; Requires a scan over all tables [ctrl] part for first FIND the table that carries localization information for this table (which could even be more than a single table) and then use that. Could be implemented, but obviously takes a little more....)
420
421 // Will try to overlay a record only if the sys_language_content value is larger than zero.
422 if ($sys_language_content>0) {
423
424 // Must be default language or [All], otherwise no overlaying:
425 if ($row[$TCA[$table]['ctrl']['languageField']]<=0) {
426
427 // Select overlay record:
428 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
429 '*',
430 $table,
431 'pid='.intval($row['pid']).
432 ' AND '.$TCA[$table]['ctrl']['languageField'].'='.intval($sys_language_content).
433 ' AND '.$TCA[$table]['ctrl']['transOrigPointerField'].'='.intval($row['uid']).
434 $this->enableFields($table),
435 '',
436 '',
437 '1'
438 );
439 $olrow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
440 $GLOBALS['TYPO3_DB']->sql_free_result($res);
441 $this->versionOL($table,$olrow);
442
443 // Merge record content by traversing all fields:
444 if (is_array($olrow)) {
445 if (isset($olrow['_ORIG_uid'])) {
446 $row['_ORIG_uid'] = $olrow['_ORIG_uid'];
447 }
448 if (isset($olrow['_ORIG_pid'])) {
449 $row['_ORIG_pid'] = $olrow['_ORIG_pid'];
450 }
451 foreach($row as $fN => $fV) {
452 if ($fN!='uid' && $fN!='pid' && isset($olrow[$fN])) {
453
454 if ($GLOBALS['TSFE']->TCAcachedExtras[$table]['l10n_mode'][$fN]!='exclude'
455 && ($GLOBALS['TSFE']->TCAcachedExtras[$table]['l10n_mode'][$fN]!='mergeIfNotBlank' || strcmp(trim($olrow[$fN]),''))) {
456 $row[$fN] = $olrow[$fN];
457 }
458 } elseif ($fN=='uid') {
459 $row['_LOCALIZED_UID'] = $olrow['uid'];
460 }
461 }
462 } elseif ($OLmode==='hideNonTranslated' && $row[$TCA[$table]['ctrl']['languageField']]==0) { // Unset, if non-translated records should be hidden. ONLY done if the source record really is default language and not [All] in which case it is allowed.
463 unset($row);
464 }
465
466 // Otherwise, check if sys_language_content is different from the value of the record - that means a japanese site might try to display french content.
467 } elseif ($sys_language_content!=$row[$TCA[$table]['ctrl']['languageField']]) {
468 unset($row);
469 }
470 } else {
471 // When default language is displayed, we never want to return a record carrying another language!
472 if ($row[$TCA[$table]['ctrl']['languageField']]>0) {
473 unset($row);
474 }
475 }
476 }
477 }
478 }
479 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'])) {
480 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'] as $classRef) {
481 $hookObject = t3lib_div::getUserObj($classRef);
482
483 if (!($hookObject instanceof t3lib_pageSelect_getRecordOverlayHook)) {
484 throw new UnexpectedValueException('$hookObject must implement interface t3lib_pageSelect_getRecordOverlayHook', 1269881659);
485 }
486 $hookObject->getRecordOverlay_postProcess($table,$row,$sys_language_content,$OLmode, $this);
487 }
488 }
489 return $row;
490 }
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 /*******************************************
510 *
511 * Page related: Menu, Domain record, Root line
512 *
513 ******************************************/
514
515 /**
516 * Returns an array with pagerows for subpages with pid=$uid (which is pid here!). This is used for menus.
517 * If there are mount points in overlay mode the _MP_PARAM field is set to the corret MPvar.
518 * If the $uid being input does in itself require MPvars to define a correct rootline these must be handled externally to this function.
519 *
520 * @param integer The page id for which to fetch subpages (PID)
521 * @param string List of fields to select. Default is "*" = all
522 * @param string The field to sort by. Default is "sorting"
523 * @param string Optional additional where clauses. Like "AND title like '%blabla%'" for instance.
524 * @param boolean check if shortcuts exist, checks by default
525 * @return array Array with key/value pairs; keys are page-uid numbers. values are the corresponding page records (with overlayed localized fields, if any)
526 * @see tslib_fe::getPageShortcut(), tslib_menu::makeMenu(), tx_wizardcrpages_webfunc_2, tx_wizardsortpages_webfunc_2
527 */
528 function getMenu($uid,$fields='*',$sortField='sorting',$addWhere='',$checkShortcuts=1) {
529
530 $output = Array();
531 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, 'pages', 'pid='.intval($uid).$this->where_hid_del.$this->where_groupAccess.' '.$addWhere, '', $sortField);
532 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
533 $this->versionOL('pages',$row,TRUE);
534 if (is_array($row)) {
535 // Keep mount point:
536 $origUid = $row['uid'];
537 $mount_info = $this->getMountPointInfo($origUid, $row); // $row MUST have "uid", "pid", "doktype", "mount_pid", "mount_pid_ol" fields in it
538 if (is_array($mount_info) && $mount_info['overlay']) { // There is a valid mount point.
539 $mp_row = $this->getPage($mount_info['mount_pid']); // Using "getPage" is OK since we need the check for enableFields AND for type 2 of mount pids we DO require a doktype < 200!
540 if (count($mp_row)) {
541 $row = $mp_row;
542 $row['_MP_PARAM'] = $mount_info['MPvar'];
543 } else unset($row); // If the mount point could not be fetched with respect to enableFields, unset the row so it does not become a part of the menu!
544 }
545
546 // if shortcut, look up if the target exists and is currently visible
547 if ($row['doktype'] == t3lib_pageSelect::DOKTYPE_SHORTCUT && ($row['shortcut'] || $row['shortcut_mode']) && $checkShortcuts) {
548 if ($row['shortcut_mode'] == self::SHORTCUT_MODE_NONE) {
549 // no shortcut_mode set, so target is directly set in $row['shortcut']
550 $searchField = 'uid';
551 $searchUid = intval($row['shortcut']);
552 } elseif ($row['shortcut_mode'] == self::SHORTCUT_MODE_FIRST_SUBPAGE || $row['shortcut_mode'] == self::SHORTCUT_MODE_RANDOM_SUBPAGE) {
553 // check subpages - first subpage or random subpage
554 $searchField = 'pid';
555 // If a shortcut mode is set and no valid page is given to select subpags from use the actual page.
556 $searchUid = intval($row['shortcut'])?intval($row['shortcut']):$row['uid'];
557 } elseif ($row['shortcut_mode'] == self::SHORTCUT_MODE_PARENT_PAGE) {
558 // shortcut to parent page
559 $searchField = 'uid';
560 $searchUid = $row['pid'];
561 }
562 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
563 'uid',
564 'pages',
565 $searchField . '=' . $searchUid .
566 $this->where_hid_del .
567 $this->where_groupAccess .
568 ' ' . $addWhere
569 );
570 if (!$count) {
571 unset($row);
572 }
573 } elseif ($row['doktype'] == t3lib_pageSelect::DOKTYPE_SHORTCUT && $checkShortcuts) {
574 // Neither shortcut target nor mode is set. Remove the page from the menu.
575 unset($row);
576 }
577
578 // Add to output array after overlaying language:
579 if (is_array($row)) {
580 $output[$origUid] = $this->getPageOverlay($row);
581 }
582 }
583 }
584 $GLOBALS['TYPO3_DB']->sql_free_result($res);
585 return $output;
586 }
587
588 /**
589 * Will find the page carrying the domain record matching the input domain.
590 * Might exit after sending a redirect-header IF a found domain record instructs to do so.
591 *
592 * @param string Domain name to search for. Eg. "www.typo3.com". Typical the HTTP_HOST value.
593 * @param string Path for the current script in domain. Eg. "/somedir/subdir". Typ. supplied by t3lib_div::getIndpEnv('SCRIPT_NAME')
594 * @param string Request URI: Used to get parameters from if they should be appended. Typ. supplied by t3lib_div::getIndpEnv('REQUEST_URI')
595 * @return mixed If found, returns integer with page UID where found. Otherwise blank. Might exit if location-header is sent, see description.
596 * @see tslib_fe::findDomainRecord()
597 */
598 function getDomainStartPage($domain, $path='',$request_uri='') {
599 $domain = explode(':',$domain);
600 $domain = strtolower(preg_replace('/\.$/','',$domain[0]));
601 // Removing extra trailing slashes
602 $path = trim(preg_replace('/\/[^\/]*$/','',$path));
603 // Appending to domain string
604 $domain.= $path;
605 $domain = preg_replace('/\/*$/','',$domain);
606
607 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
608 'pages.uid,sys_domain.redirectTo,sys_domain.redirectHttpStatusCode,sys_domain.prepend_params',
609 'pages,sys_domain',
610 'pages.uid=sys_domain.pid
611 AND sys_domain.hidden=0
612 AND (sys_domain.domainName='.$GLOBALS['TYPO3_DB']->fullQuoteStr($domain, 'sys_domain').' OR sys_domain.domainName='.$GLOBALS['TYPO3_DB']->fullQuoteStr($domain.'/', 'sys_domain').') '.
613 $this->where_hid_del.$this->where_groupAccess,
614 '',
615 '',
616 1
617 );
618 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
619 $GLOBALS['TYPO3_DB']->sql_free_result($res);
620 if ($row) {
621 if ($row['redirectTo']) {
622 $redirectUrl = $row['redirectTo'];
623 if ($row['prepend_params']) {
624 $redirectUrl = rtrim($redirectUrl, '/');
625 $prependStr = ltrim(substr($request_uri,strlen($path)), '/');
626 $redirectUrl .= '/' . $prependStr;
627 }
628
629 $statusCode = intval($row['redirectHttpStatusCode']);
630 if ($statusCode && defined('t3lib_utility_Http::HTTP_STATUS_' . $statusCode)) {
631 t3lib_utility_Http::redirect($redirectUrl, constant('t3lib_utility_Http::HTTP_STATUS_' . $statusCode));
632 } else {
633 t3lib_utility_Http::redirect($redirectUrl, 't3lib_utility_Http::HTTP_STATUS_301');
634 }
635 exit;
636 } else {
637 return $row['uid'];
638 }
639 }
640 }
641
642 /**
643 * Returns array with fields of the pages from here ($uid) and back to the root
644 * NOTICE: This function only takes deleted pages into account! So hidden, starttime and endtime restricted pages are included no matter what.
645 * Further: If any "recycler" page is found (doktype=255) then it will also block for the rootline)
646 * If you want more fields in the rootline records than default such can be added by listing them in $GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields']
647 *
648 * @param integer The page uid for which to seek back to the page tree root.
649 * @param string Commalist of MountPoint parameters, eg. "1-2,3-4" etc. Normally this value comes from the GET var, MP
650 * @param boolean If set, some errors related to Mount Points in root line are ignored.
651 * @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.
652 * @see tslib_fe::getPageAndRootline()
653 */
654 function getRootLine($uid, $MP = '', $ignoreMPerrors = false) {
655 $cacheUid = $uid = intval($uid);
656 $cacheIgnoreMPerrors = ($ignoreMPerrors ? 1 : 0);
657
658 if (is_array($this->cache_getRootLine[$cacheUid][$this->sys_language_uid][$MP][$cacheIgnoreMPerrors])) {
659 return $this->cache_getRootLine[$cacheUid][$this->sys_language_uid][$MP][$cacheIgnoreMPerrors];
660 }
661
662 // Initialize:
663 $selFields = t3lib_div::uniqueList('pid,uid,t3ver_oid,t3ver_wsid,t3ver_state,t3ver_swapmode,title,alias,nav_title,media,layout,hidden,starttime,endtime,fe_group,extendToSubpages,doktype,TSconfig,storage_pid,is_siteroot,mount_pid,mount_pid_ol,fe_login_mode,'.$GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields']);
664 $this->error_getRootLine = '';
665 $this->error_getRootLine_failPid = 0;
666
667 // Splitting the $MP parameters if present
668 $MPA = array();
669 if ($MP) {
670 $MPA = explode(',',$MP);
671 foreach ($MPA as $MPAk => $v) {
672 $MPA[$MPAk] = explode('-', $MPA[$MPAk]);
673 }
674 }
675
676 $loopCheck = 0;
677 $theRowArray = Array();
678
679 while ($uid != 0 && $loopCheck < 99) { // Max 99 levels in the page tree.
680 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selFields, 'pages', 'uid='.intval($uid).' AND pages.deleted=0 AND pages.doktype!=255');
681 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
682 $GLOBALS['TYPO3_DB']->sql_free_result($res);
683 if ($row) {
684 $this->versionOL('pages',$row, FALSE, TRUE);
685 $this->fixVersioningPid('pages',$row);
686
687 if (is_array($row)) {
688 // Mount Point page types are allowed ONLY a) if they are the outermost record in rootline and b) if the overlay flag is not set:
689 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids'] && $row['doktype'] == t3lib_pageSelect::DOKTYPE_MOUNTPOINT && !$ignoreMPerrors) {
690 $mount_info = $this->getMountPointInfo($row['uid'], $row);
691 if ($loopCheck>0 || $mount_info['overlay']) {
692 $this->error_getRootLine = 'Illegal Mount Point found in rootline';
693 return array();
694 }
695 }
696
697 $uid = $row['pid']; // Next uid
698
699 if (count($MPA) && $GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids']) {
700 $curMP = end($MPA);
701 if (!strcmp($row['uid'],$curMP[0])) {
702
703 array_pop($MPA);
704 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selFields, 'pages', 'uid='.intval($curMP[1]).' AND pages.deleted=0 AND pages.doktype!=255');
705 $mp_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
706 $GLOBALS['TYPO3_DB']->sql_free_result($res);
707
708 $this->versionOL('pages',$mp_row, FALSE, TRUE);
709 $this->fixVersioningPid('pages',$mp_row);
710
711 if (is_array($mp_row)) {
712 $mount_info = $this->getMountPointInfo($mp_row['uid'], $mp_row);
713 if (is_array($mount_info) && $mount_info['mount_pid']==$curMP[0]) {
714 $uid = $mp_row['pid']; // Setting next uid
715
716 if ($mount_info['overlay']) { // Symlink style: Keep mount point (current row).
717 $row['_MOUNT_OL'] = TRUE; // Set overlay mode:
718 $row['_MOUNT_PAGE'] = array(
719 'uid' => $mp_row['uid'],
720 'pid' => $mp_row['pid'],
721 'title' => $mp_row['title'],
722 );
723 } else { // Normal operation: Insert the mount page row in rootline instead mount point.
724 if ($loopCheck>0) {
725 $row = $mp_row;
726 } else {
727 $this->error_getRootLine = 'Current Page Id is a mounted page of the overlay type and cannot be accessed directly!';
728 return array(); // Matching the page id (first run, $loopCheck = 0) with the MPvar is ONLY allowed if the mount point is the "overlay" type (otherwise it could be forged!)
729 }
730 }
731
732 $row['_MOUNTED_FROM'] = $curMP[0];
733 $row['_MP_PARAM'] = $mount_info['MPvar'];
734 } else {
735 $this->error_getRootLine = 'MP var was corrupted';
736 return array(); // The MP variables did NOT connect proper mount points:
737 }
738 } else {
739 $this->error_getRootLine = 'No moint point record found according to PID in MP var';
740 return array(); // The second PID in MP var was NOT a valid page.
741 }
742 }
743 }
744 }
745 // Add row to rootline with language overlaid:
746 $theRowArray[] = $this->getPageOverlay($row);
747 } else {
748 $this->error_getRootLine = 'Broken rootline';
749 $this->error_getRootLine_failPid = $uid;
750 return array(); // broken rootline.
751 }
752
753 $loopCheck++;
754 }
755
756 // If the MPA array is NOT empty, we have to return an error; All MP elements were not resolved!
757 if (count($MPA)) {
758 $this->error_getRootLine = 'MP value remain!';
759 return array();
760 }
761
762 // Create output array (with reversed order of numeric keys):
763 $output = Array();
764 $c = count($theRowArray);
765 foreach($theRowArray as $key => $val) {
766 $c--;
767 $output[$c] = $val;
768 }
769
770 // Note: rootline errors are not cached
771 $this->cache_getRootLine[$cacheUid][$this->sys_language_uid][$MP][$cacheIgnoreMPerrors] = $output;
772 return $output;
773 }
774
775 /**
776 * Creates a "path" string for the input root line array titles.
777 * Used for writing statistics.
778 *
779 * @param array A rootline array!
780 * @param integer The max length of each title from the rootline.
781 * @return string The path in the form "/page title/This is another pageti.../Another page"
782 * @see tslib_fe::getConfigArray()
783 */
784 function getPathFromRootline($rl,$len=20) {
785 if (is_array($rl)) {
786 $c=count($rl);
787 $path = '';
788 for ($a=0;$a<$c;$a++) {
789 if ($rl[$a]['uid']) {
790 $path.='/'.t3lib_div::fixed_lgd_cs(strip_tags($rl[$a]['title']),$len);
791 }
792 }
793 return $path;
794 }
795 }
796
797 /**
798 * Returns the URL type for the input page row IF the doktype is 3 and not disabled.
799 *
800 * @param array The page row to return URL type for
801 * @param boolean A flag to simply disable any output from here.
802 * @return string The URL type from $this->urltypes array. False if not found or disabled.
803 * @see tslib_fe::setExternalJumpUrl()
804 */
805 function getExtURL($pagerow,$disable=0) {
806 if ($pagerow['doktype'] == t3lib_pageSelect::DOKTYPE_LINK && !$disable) {
807 $redirectTo = $this->urltypes[$pagerow['urltype']].$pagerow['url'];
808
809 // If relative path, prefix Site URL:
810 $uI = parse_url($redirectTo);
811 if (!$uI['scheme'] && substr($redirectTo,0,1)!='/') { // relative path assumed now...
812 $redirectTo = t3lib_div::getIndpEnv('TYPO3_SITE_URL').$redirectTo;
813 }
814 return $redirectTo;
815 }
816 }
817
818 /**
819 * Returns MountPoint id for page
820 * Does a recursive search if the mounted page should be a mount page itself. It has a run-away break so it can't go into infinite loops.
821 *
822 * @param integer Page id for which to look for a mount pid. Will be returned only if mount pages are enabled, the correct doktype (7) is set for page and there IS a mount_pid (which has a valid record that is not deleted...)
823 * @param array Optional page record for the page id. If not supplied it will be looked up by the system. Must contain at least uid,pid,doktype,mount_pid,mount_pid_ol
824 * @param array Array accumulating formerly tested page ids for mount points. Used for recursivity brake.
825 * @param integer The first page id.
826 * @return mixed Returns FALSE if no mount point was found, "-1" if there should have been one, but no connection to it, otherwise an array with information about mount pid and modes.
827 * @see tslib_menu
828 */
829 function getMountPointInfo($pageId, $pageRec=FALSE, $prevMountPids=array(), $firstPageUid=0) {
830 $result = FALSE;
831
832 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids']) {
833
834 if (isset($this->cache_getMountPointInfo[$pageId])) {
835 return $this->cache_getMountPointInfo[$pageId];
836 }
837
838 // Get pageRec if not supplied:
839 if (!is_array($pageRec)) {
840 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,doktype,mount_pid,mount_pid_ol,t3ver_state', 'pages', 'uid='.intval($pageId).' AND pages.deleted=0 AND pages.doktype!=255');
841 $pageRec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
842 $GLOBALS['TYPO3_DB']->sql_free_result($res);
843 $this->versionOL('pages',$pageRec); // Only look for version overlay if page record is not supplied; This assumes that the input record is overlaid with preview version, if any!
844 }
845
846 // Set first Page uid:
847 if (!$firstPageUid) {
848 $firstPageUid = $pageRec['uid'];
849 }
850
851 // Look for mount pid value plus other required circumstances:
852 $mount_pid = intval($pageRec['mount_pid']);
853 if (is_array($pageRec) && $pageRec['doktype']== t3lib_pageSelect::DOKTYPE_MOUNTPOINT && $mount_pid > 0 && !in_array($mount_pid, $prevMountPids)) {
854
855 // Get the mount point record (to verify its general existence):
856 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,doktype,mount_pid,mount_pid_ol,t3ver_state', 'pages', 'uid='.$mount_pid.' AND pages.deleted=0 AND pages.doktype!=255');
857 $mountRec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
858 $GLOBALS['TYPO3_DB']->sql_free_result($res);
859 $this->versionOL('pages',$mountRec);
860
861 if (is_array($mountRec)) {
862 // Look for recursive mount point:
863 $prevMountPids[] = $mount_pid;
864 $recursiveMountPid = $this->getMountPointInfo($mount_pid, $mountRec, $prevMountPids, $firstPageUid);
865
866 // Return mount point information:
867 $result = $recursiveMountPid ?
868 $recursiveMountPid :
869 array(
870 'mount_pid' => $mount_pid,
871 'overlay' => $pageRec['mount_pid_ol'],
872 'MPvar' => $mount_pid.'-'.$firstPageUid,
873 'mount_point_rec' => $pageRec,
874 'mount_pid_rec' => $mountRec,
875 );
876 } else {
877 $result = -1; // Means, there SHOULD have been a mount point, but there was none!
878 }
879 }
880 }
881
882 $this->cache_getMountPointInfo[$pageId] = $result;
883 return $result;
884 }
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902 /*********************************
903 *
904 * Selecting records in general
905 *
906 **********************************/
907
908 /**
909 * Checks if a record exists and is accessible.
910 * The row is returned if everything's OK.
911 *
912 * @param string The table name to search
913 * @param integer The uid to look up in $table
914 * @param boolean If checkPage is set, it's also required that the page on which the record resides is accessible
915 * @return mixed Returns array (the record) if OK, otherwise blank/0 (zero)
916 */
917 function checkRecord($table,$uid,$checkPage=0) {
918 global $TCA;
919 $uid = intval($uid);
920 if (is_array($TCA[$table]) && $uid > 0) {
921 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, 'uid = ' . $uid . $this->enableFields($table));
922 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
923 $GLOBALS['TYPO3_DB']->sql_free_result($res);
924 if ($row) {
925 $this->versionOL($table,$row);
926 if (is_array($row)) {
927 if ($checkPage) {
928 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'uid='.intval($row['pid']).$this->enableFields('pages'));
929 $numRows = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
930 $GLOBALS['TYPO3_DB']->sql_free_result($res);
931 if ($numRows>0) {
932 return $row;
933 } else {
934 return 0;
935 }
936 } else {
937 return $row;
938 }
939 }
940 }
941 }
942 }
943
944 /**
945 * Returns record no matter what - except if record is deleted
946 *
947 * @param string The table name to search
948 * @param integer The uid to look up in $table
949 * @param string The fields to select, default is "*"
950 * @param boolean If set, no version overlay is applied
951 * @return mixed Returns array (the record) if found, otherwise blank/0 (zero)
952 * @see getPage_noCheck()
953 */
954 function getRawRecord($table,$uid,$fields='*',$noWSOL=FALSE) {
955 global $TCA;
956 $uid = intval($uid);
957 // Excluding pages here so we can ask the function BEFORE TCA gets initialized. Support for this is followed up in deleteClause()...
958 if ((is_array($TCA[$table]) || $table == 'pages') && $uid > 0) {
959 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $table, 'uid = ' . $uid . $this->deleteClause($table));
960 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
961 $GLOBALS['TYPO3_DB']->sql_free_result($res);
962 if ($row) {
963 if (!$noWSOL) {
964 $this->versionOL($table,$row);
965 }
966 if (is_array($row)) {
967 return $row;
968 }
969 }
970 }
971 }
972
973 /**
974 * Selects records based on matching a field (ei. other than UID) with a value
975 *
976 * @param string The table name to search, eg. "pages" or "tt_content"
977 * @param string The fieldname to match, eg. "uid" or "alias"
978 * @param string The value that fieldname must match, eg. "123" or "frontpage"
979 * @param string Optional additional WHERE clauses put in the end of the query. DO NOT PUT IN GROUP BY, ORDER BY or LIMIT!
980 * @param string Optional GROUP BY field(s), if none, supply blank string.
981 * @param string Optional ORDER BY field(s), if none, supply blank string.
982 * @param string Optional LIMIT value ([begin,]max), if none, supply blank string.
983 * @return mixed Returns array (the record) if found, otherwise nothing (void)
984 */
985 function getRecordsByField($theTable,$theField,$theValue,$whereClause='',$groupBy='',$orderBy='',$limit='') {
986 global $TCA;
987 if (is_array($TCA[$theTable])) {
988 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
989 '*',
990 $theTable,
991 $theField.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($theValue, $theTable).
992 $this->deleteClause($theTable).' '.
993 $whereClause, // whereClauseMightContainGroupOrderBy
994 $groupBy,
995 $orderBy,
996 $limit
997 );
998 $rows = array();
999 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
1000 #$this->versionOL($theTable,$row); // not used since records here are fetched based on other fields than uid!
1001 if (is_array($row)) $rows[] = $row;
1002 }
1003 $GLOBALS['TYPO3_DB']->sql_free_result($res);
1004 if (count($rows)) {
1005 return $rows;
1006 }
1007 }
1008 }
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023 /*********************************
1024 *
1025 * Caching and standard clauses
1026 *
1027 **********************************/
1028
1029 /**
1030 * Returns string value stored for the hash string in the cache "cache_hash"
1031 * Can be used to retrieved a cached value
1032 * Can be used from your frontend plugins if you like. It is also used to
1033 * store the parsed TypoScript template structures. You can call it directly
1034 * like t3lib_pageSelect::getHash()
1035 *
1036 * IDENTICAL to the function by same name found in t3lib_page
1037 *
1038 * @param string The hash-string which was used to store the data value
1039 * @return string The "content" field of the "cache_hash" cache entry.
1040 * @see tslib_TStemplate::start(), storeHash()
1041 */
1042 public static function getHash($hash, $expTime = 0) {
1043 $hashContent = null;
1044
1045 if (TYPO3_UseCachingFramework) {
1046 if (is_object($GLOBALS['typo3CacheManager'])) {
1047 $contentHashCache = $GLOBALS['typo3CacheManager']->getCache('cache_hash');
1048 $cacheEntry = $contentHashCache->get($hash);
1049
1050 if ($cacheEntry) {
1051 $hashContent = $cacheEntry;
1052 }
1053 }
1054 } else {
1055 $expTime = intval($expTime);
1056 if ($expTime) {
1057 $whereAdd = ' AND tstamp > '.($GLOBALS['ACCESS_TIME']-$expTime);
1058 }
1059 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('content', 'cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash').$whereAdd);
1060 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
1061 $GLOBALS['TYPO3_DB']->sql_free_result($res);
1062 if ($row) {
1063 $hashContent = $row['content'];
1064 }
1065 }
1066 return $hashContent;
1067 }
1068
1069 /**
1070 * Stores a string value in the cache_hash cache identified by $hash.
1071 * Can be used from your frontend plugins if you like. You can call it
1072 * directly like t3lib_pageSelect::storeHash()
1073 *
1074 * @param string 32 bit hash string (eg. a md5 hash of a serialized array identifying the data being stored)
1075 * @param string The data string. If you want to store an array, then just serialize it first.
1076 * @param string $ident is just a textual identification in order to inform about the content!
1077 * @param integer The lifetime for the cache entry in seconds
1078 * @return void
1079 * @see tslib_TStemplate::start(), getHash()
1080 */
1081 public static function storeHash($hash, $data, $ident, $lifetime = 0) {
1082 if (TYPO3_UseCachingFramework) {
1083 if (is_object($GLOBALS['typo3CacheManager'])) {
1084 $GLOBALS['typo3CacheManager']->getCache('cache_hash')->set(
1085 $hash,
1086 $data,
1087 array('ident_' . $ident),
1088 intval($lifetime)
1089 );
1090 }
1091 } else {
1092 $insertFields = array(
1093 'hash' => $hash,
1094 'content' => $data,
1095 'ident' => $ident,
1096 'tstamp' => $GLOBALS['EXEC_TIME']
1097 );
1098 $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash'));
1099 $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_hash', $insertFields);
1100 }
1101 }
1102
1103 /**
1104 * Returns the "AND NOT deleted" clause for the tablename given IF $TCA configuration points to such a field.
1105 *
1106 * @param string Tablename
1107 * @return string
1108 * @see enableFields()
1109 */
1110 function deleteClause($table) {
1111 global $TCA;
1112 if (!strcmp($table,'pages')) { // Hardcode for pages because TCA might not be loaded yet (early frontend initialization)
1113 return ' AND pages.deleted=0';
1114 } else {
1115 return $TCA[$table]['ctrl']['delete'] ? ' AND '.$table.'.'.$TCA[$table]['ctrl']['delete'].'=0' : '';
1116 }
1117 }
1118
1119 /**
1120 * Returns a part of a WHERE clause which will filter out records with start/end times or hidden/fe_groups fields set to values that should de-select them according to the current time, preview settings or user login. Definitely a frontend function.
1121 * Is using the $TCA arrays "ctrl" part where the key "enablefields" determines for each table which of these features applies to that table.
1122 *
1123 * @param string Table name found in the $TCA array
1124 * @param integer 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 tslib_cObj->enableFields where it's implemented correctly.
1125 * @param 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.
1126 * @param boolean If set, enableFields will be applied regardless of any versioning preview settings which might otherwise disable enableFields
1127 * @return string The clause starting like " AND ...=... AND ...=..."
1128 * @see tslib_cObj::enableFields(), deleteClause()
1129 */
1130 function enableFields($table,$show_hidden=-1,$ignore_array=array(),$noVersionPreview=FALSE) {
1131 global $TYPO3_CONF_VARS;
1132
1133 if ($show_hidden==-1 && is_object($GLOBALS['TSFE'])) { // If show_hidden was not set from outside and if TSFE is an object, set it based on showHiddenPage and showHiddenRecords from TSFE
1134 $show_hidden = $table=='pages' ? $GLOBALS['TSFE']->showHiddenPage : $GLOBALS['TSFE']->showHiddenRecords;
1135 }
1136 if ($show_hidden==-1) $show_hidden=0; // If show_hidden was not changed during the previous evaluation, do it here.
1137
1138 $ctrl = $GLOBALS['TCA'][$table]['ctrl'];
1139 $query='';
1140 if (is_array($ctrl)) {
1141
1142 // Delete field check:
1143 if ($ctrl['delete']) {
1144 $query.=' AND '.$table.'.'.$ctrl['delete'].'=0';
1145 }
1146
1147 // Filter out new place-holder records in case we are NOT in a versioning preview (that means we are online!)
1148 if ($ctrl['versioningWS'] && !$this->versioningPreview) {
1149 $query .= ' AND ' . $table . '.t3ver_state<=0 AND ' . $table . '.pid!=-1'; // Shadow state for new items MUST be ignored!
1150 }
1151
1152 // Enable fields:
1153 if (is_array($ctrl['enablecolumns'])) {
1154 if (!$this->versioningPreview || !$ctrl['versioningWS'] || $noVersionPreview) { // In case of versioning-preview, enableFields are ignored (checked in versionOL())
1155 if ($ctrl['enablecolumns']['disabled'] && !$show_hidden && !$ignore_array['disabled']) {
1156 $field = $table.'.'.$ctrl['enablecolumns']['disabled'];
1157 $query.=' AND '.$field.'=0';
1158 }
1159 if ($ctrl['enablecolumns']['starttime'] && !$ignore_array['starttime']) {
1160 $field = $table.'.'.$ctrl['enablecolumns']['starttime'];
1161 $query .= ' AND ' . $field . '<=' . $GLOBALS['SIM_ACCESS_TIME'];
1162 }
1163 if ($ctrl['enablecolumns']['endtime'] && !$ignore_array['endtime']) {
1164 $field = $table.'.'.$ctrl['enablecolumns']['endtime'];
1165 $query.=' AND ('.$field.'=0 OR '.$field.'>'.$GLOBALS['SIM_ACCESS_TIME'].')';
1166 }
1167 if ($ctrl['enablecolumns']['fe_group'] && !$ignore_array['fe_group']) {
1168 $field = $table.'.'.$ctrl['enablecolumns']['fe_group'];
1169 $query.= $this->getMultipleGroupsWhereClause($field, $table);
1170 }
1171
1172 // Call hook functions for additional enableColumns
1173 // It is used by the extension ingmar_accessctrl which enables assigning more than one usergroup to content and page records
1174 if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_page.php']['addEnableColumns'])) {
1175 $_params = array(
1176 'table' => $table,
1177 'show_hidden' => $show_hidden,
1178 'ignore_array' => $ignore_array,
1179 'ctrl' => $ctrl
1180 );
1181 foreach($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_page.php']['addEnableColumns'] as $_funcRef) {
1182 $query .= t3lib_div::callUserFunction($_funcRef,$_params,$this);
1183 }
1184 }
1185 }
1186 }
1187 } else {
1188 throw new InvalidArgumentException(
1189 'There is no entry in the $TCA array for the table "' . $table .
1190 '". This means that the function enableFields() is ' .
1191 'called with an invalid table name as argument.',
1192 1283790586
1193 );
1194 }
1195
1196 return $query;
1197 }
1198
1199 /**
1200 * Creating where-clause for checking group access to elements in enableFields function
1201 *
1202 * @param string Field with group list
1203 * @param string Table name
1204 * @return string AND sql-clause
1205 * @see enableFields()
1206 */
1207 function getMultipleGroupsWhereClause($field, $table) {
1208 $memberGroups = t3lib_div::intExplode(',',$GLOBALS['TSFE']->gr_list);
1209 $orChecks=array();
1210 $orChecks[]=$field.'=\'\''; // If the field is empty, then OK
1211 $orChecks[]=$field.' IS NULL'; // If the field is NULL, then OK
1212 $orChecks[]=$field.'=\'0\''; // If the field contsains zero, then OK
1213
1214 foreach($memberGroups as $value) {
1215 $orChecks[] = $GLOBALS['TYPO3_DB']->listQuery($field, $value, $table);
1216 }
1217
1218 return ' AND ('.implode(' OR ',$orChecks).')';
1219 }
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235 /*********************************
1236 *
1237 * Versioning Preview
1238 *
1239 **********************************/
1240
1241 /**
1242 * Finding online PID for offline version record
1243 * ONLY active when backend user is previewing records. MUST NEVER affect a site served which is not previewed by backend users!!!
1244 * Will look if the "pid" value of the input record is -1 (it is an offline version) and if the table supports versioning; if so, it will translate the -1 PID into the PID of the original record
1245 * Used whenever you are tracking something back, like making the root line.
1246 * Principle; Record offline! => Find online?
1247 *
1248 * @param string Table name
1249 * @param array 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.
1250 * @return void (Passed by ref).
1251 * @see t3lib_BEfunc::fixVersioningPid(), versionOL(), getRootLine()
1252 */
1253 function fixVersioningPid($table,&$rr) {
1254 global $TCA;
1255
1256 if ($this->versioningPreview && is_array($rr) && $rr['pid']==-1 && ($table=='pages' || $TCA[$table]['ctrl']['versioningWS'])) { // Have to hardcode it for "pages" table since TCA is not loaded at this moment!
1257
1258 // Check values for t3ver_oid and t3ver_wsid:
1259 if (isset($rr['t3ver_oid']) && isset($rr['t3ver_wsid'])) { // If "t3ver_oid" is already a field, just set this:
1260 $oid = $rr['t3ver_oid'];
1261 $wsid = $rr['t3ver_wsid'];
1262 } else { // Otherwise we have to expect "uid" to be in the record and look up based on this:
1263 $newPidRec = $this->getRawRecord($table,$rr['uid'],'t3ver_oid,t3ver_wsid',TRUE);
1264 if (is_array($newPidRec)) {
1265 $oid = $newPidRec['t3ver_oid'];
1266 $wsid = $newPidRec['t3ver_wsid'];
1267 }
1268 }
1269
1270 // If workspace ids matches and ID of current online version is found, look up the PID value of that:
1271 if ($oid && (($this->versioningWorkspaceId == 0 && $this->checkWorkspaceAccess($wsid)) || !strcmp((int)$wsid,$this->versioningWorkspaceId))) {
1272 $oidRec = $this->getRawRecord($table,$oid,'pid',TRUE);
1273
1274 if (is_array($oidRec)) {
1275 # SWAP uid as well? Well no, because when fixing a versioning PID happens it is assumed that this is a "branch" type page and therefore the uid should be kept (like in versionOL()). However if the page is NOT a branch version it should not happen - but then again, direct access to that uid should not happen!
1276 $rr['_ORIG_pid'] = $rr['pid'];
1277 $rr['pid'] = $oidRec['pid'];
1278 }
1279 }
1280 }
1281
1282 // changing PID in case of moving pointer:
1283 if ($movePlhRec = $this->getMovePlaceholder($table,$rr['uid'],'pid')) {
1284 $rr['pid'] = $movePlhRec['pid'];
1285 }
1286 }
1287
1288 /**
1289 * Versioning Preview Overlay
1290 * ONLY active when backend user is previewing records. MUST NEVER affect a site served which is not previewed by backend users!!!
1291 * Generally ALWAYS used when records are selected based on uid or pid. If records are selected on other fields than uid or pid (eg. "email = ....") then usage might produce undesired results and that should be evaluated on individual basis.
1292 * Principle; Record online! => Find offline?
1293 *
1294 * @param string Table name
1295 * @param array 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!
1296 * @param boolean 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)
1297 * @param boolean 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!
1298 * @return void (Passed by ref).
1299 * @see fixVersioningPid(), t3lib_BEfunc::workspaceOL()
1300 */
1301 function versionOL($table,&$row,$unsetMovePointers=FALSE,$bypassEnableFieldsCheck=FALSE) {
1302 global $TCA;
1303
1304 if ($this->versioningPreview && is_array($row)) {
1305 $movePldSwap = $this->movePlhOL($table,$row); // will overlay any movePlhOL found with the real record, which in turn will be overlaid with its workspace version if any.
1306 if ($wsAlt = $this->getWorkspaceVersionOfRecord($this->versioningWorkspaceId, $table, $row['uid'], implode(',',array_keys($row)), $bypassEnableFieldsCheck)) { // implode(',',array_keys($row)) = Using fields from original record to make sure no additional fields are selected. This is best for eg. getPageOverlay()
1307 if (is_array($wsAlt)) {
1308 // Always fix PID (like in fixVersioningPid() above). [This is usually not the important factor for versioning OL]
1309 $wsAlt['_ORIG_pid'] = $wsAlt['pid']; // Keep the old (-1) - indicates it was a version...
1310 $wsAlt['pid'] = $row['pid']; // Set in the online versions PID.
1311
1312 // "element" and "page" type versions:
1313 // For versions of single elements or page+content, preserve online UID and PID (this will produce true "overlay" of element _content_, not any references)
1314 // For page+content the "_ORIG_uid" should actually be used as PID for selection of tables with "versioning_followPages" enabled.
1315 if ($table!=='pages' || $wsAlt['t3ver_swapmode']<=0) {
1316 $wsAlt['_ORIG_uid'] = $wsAlt['uid'];
1317 $wsAlt['uid'] = $row['uid'];
1318
1319 // Translate page alias as well so links are pointing to the _online_ page:
1320 if ($table==='pages') {
1321 $wsAlt['alias'] = $row['alias'];
1322 }
1323 } else {
1324 // "branch" versions:
1325 // Keeping overlay uid and pid so references are changed. This is only for page-versions with BRANCH below!
1326 $wsAlt['_ONLINE_uid'] = $row['uid']; // The UID of the versionized record is kept and the uid of the online version is stored
1327 }
1328
1329 // Changing input record to the workspace version alternative:
1330 $row = $wsAlt;
1331
1332 // Check if it is deleted/new
1333 if ((int)$row['t3ver_state']===1 || (int)$row['t3ver_state']===2) {
1334 $row = FALSE; // Unset record if it turned out to be deleted in workspace
1335 }
1336
1337 // Check if move-pointer in workspace (unless if a move-placeholder is the reason why it appears!):
1338 // You have to specifically set $unsetMovePointers in order to clear these because it is normally a display issue if it should be shown or not.
1339 if ((int)$row['t3ver_state']===4 && !$movePldSwap && $unsetMovePointers) {
1340 $row = FALSE; // Unset record if it turned out to be deleted in workspace
1341 }
1342 } else {
1343 // No version found, then check if t3ver_state =1 (online version is dummy-representation)
1344 // Notice, that unless $bypassEnableFieldsCheck is TRUE, the $row is unset if enablefields for BOTH the version AND the online record deselects it. See note for $bypassEnableFieldsCheck
1345 if ($wsAlt<=-1 || (int)$row['t3ver_state']>0) {
1346 $row = FALSE; // Unset record if it turned out to be "hidden"
1347 }
1348 }
1349 }
1350 }
1351 }
1352
1353 /**
1354 * Checks if record is a move-placeholder (t3ver_state==3) and if so it will set $row to be the pointed-to live record (and return TRUE)
1355 * Used from versionOL
1356 *
1357 * @param string Table name
1358 * @param array Row (passed by reference) - only online records...
1359 * @return boolean True if overlay is made.
1360 * @see t3lib_BEfunc::movePlhOl()
1361 */
1362 function movePlhOL($table,&$row) {
1363 global $TCA;
1364
1365 if (($table=='pages' || (int)$TCA[$table]['ctrl']['versioningWS']>=2) && (int)$row['t3ver_state']===3) { // Only for WS ver 2... (moving)
1366
1367 // If t3ver_move_id is not found, then find it... (but we like best if it is here...)
1368 if (!isset($row['t3ver_move_id'])) {
1369 $moveIDRec = $this->getRawRecord($table,$row['uid'],'t3ver_move_id',TRUE);
1370 $moveID = $moveIDRec['t3ver_move_id'];
1371 } else {
1372 $moveID = $row['t3ver_move_id'];
1373 }
1374
1375 // Find pointed-to record.
1376 if ($moveID) {
1377 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',',array_keys($row)), $table, 'uid='.intval($moveID).$this->enableFields($table));
1378 $origRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
1379 $GLOBALS['TYPO3_DB']->sql_free_result($res);
1380 if ($origRow) {
1381 $row = $origRow;
1382 return TRUE;
1383 }
1384 }
1385 }
1386 return FALSE;
1387 }
1388
1389 /**
1390 * Returns move placeholder of online (live) version
1391 *
1392 * @param string Table name
1393 * @param integer Record UID of online version
1394 * @param string Field list, default is *
1395 * @return array If found, the record, otherwise nothing.
1396 * @see t3lib_BEfunc::getMovePlaceholder()
1397 */
1398 function getMovePlaceholder($table,$uid,$fields='*') {
1399 global $TCA;
1400
1401 if ($this->versioningPreview) {
1402 $workspace = (int)$this->versioningWorkspaceId;
1403 if (($table=='pages' || (int)$TCA[$table]['ctrl']['versioningWS']>=2) && $workspace!==0) {
1404
1405 // Select workspace version of record:
1406 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
1407 $fields,
1408 $table,
1409 'pid!=-1 AND
1410 t3ver_state=3 AND
1411 t3ver_move_id='.intval($uid).' AND
1412 t3ver_wsid='.intval($workspace).
1413 $this->deleteClause($table)
1414 );
1415
1416 if (is_array($rows[0])) {
1417 return $rows[0];
1418 }
1419 }
1420 }
1421 return FALSE;
1422 }
1423
1424 /**
1425 * Select the version of a record for a workspace
1426 *
1427 * @param integer Workspace ID
1428 * @param string Table name to select from
1429 * @param integer Record uid for which to find workspace version.
1430 * @param string Field list to select
1431 * @param boolean If true, enablefields are not checked for.
1432 * @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.
1433 * @see t3lib_befunc::getWorkspaceVersionOfRecord()
1434 */
1435 function getWorkspaceVersionOfRecord($workspace, $table, $uid, $fields='*',$bypassEnableFieldsCheck=FALSE) {
1436 global $TCA;
1437
1438 if ($workspace!==0 && ($table=='pages' || $TCA[$table]['ctrl']['versioningWS'])) { // Have to hardcode it for "pages" table since TCA is not loaded at this moment!
1439
1440 // Setting up enableFields for version record:
1441 if ($table=='pages') {
1442 $enFields = $this->versioningPreview_where_hid_del;
1443 } else {
1444 $enFields = $this->enableFields($table,-1,array(),TRUE);
1445 }
1446
1447 // Select workspace version of record, only testing for deleted.
1448 list($newrow) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
1449 $fields,
1450 $table,
1451 'pid=-1 AND
1452 t3ver_oid='.intval($uid).' AND
1453 t3ver_wsid='.intval($workspace).
1454 $this->deleteClause($table)
1455 );
1456
1457 // If version found, check if it could have been selected with enableFields on as well:
1458 if (is_array($newrow)) {
1459 if ($bypassEnableFieldsCheck || $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
1460 'uid',
1461 $table,
1462 'pid=-1 AND
1463 t3ver_oid='.intval($uid).' AND
1464 t3ver_wsid='.intval($workspace).
1465 $enFields
1466 )) {
1467 return $newrow; // Return offline version, tested for its enableFields.
1468 } else {
1469 return -1; // Return -1 because offline version was de-selected due to its enableFields.
1470 }
1471 } else {
1472 // OK, so no workspace version was found. Then check if online version can be selected with full enable fields and if so, return 1:
1473 if ($bypassEnableFieldsCheck || $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
1474 'uid',
1475 $table,
1476 'uid='.intval($uid).$enFields
1477 )) {
1478 return 1; // Means search was done, but no version found.
1479 } else {
1480 return -2; // Return -2 because the online record was de-selected due to its enableFields.
1481 }
1482 }
1483 }
1484
1485 return FALSE; // No look up in database because versioning not enabled / or workspace not offline
1486 }
1487
1488 /**
1489 * Checks if user has access to workspace.
1490 *
1491 * @param int $wsid Workspace ID
1492 * @return boolean <code>true</code> if has access
1493 */
1494 function checkWorkspaceAccess($wsid) {
1495 if (!$GLOBALS['BE_USER']) {
1496 return false;
1497 }
1498 if (isset($this->workspaceCache[$wsid])) {
1499 $ws = $this->workspaceCache[$wsid];
1500 }
1501 else {
1502 if ($wsid > 0) {
1503 $ws = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_workspace', 'uid='.intval($wsid).' AND deleted=0'); // No $TCA yet!
1504 if (count($ws)) {
1505 $ws = $ws[0];
1506 }
1507 else {
1508 return false;
1509 }
1510 }
1511 else {
1512 $ws = $wsid;
1513 }
1514 $ws = $GLOBALS['BE_USER']->checkWorkspace($ws);
1515 $this->workspaceCache[$wsid] = $ws;
1516 }
1517 return ($ws['_ACCESS'] != '');
1518 }
1519 }
1520
1521
1522
1523 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_page.php']) {
1524 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_page.php']);
1525 }
1526
1527 ?>