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