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