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