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