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