Fixed bug #11468: [Bug]: $altUrl parameter has no effect in t3lib_BEfunc::viewOnClick...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_page.php
old mode 100755 (executable)
new mode 100644 (file)
index 02ce152..8635ed9
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -63,7 +63,7 @@
  *  823:     function getRecordsByField($theTable,$theField,$theValue,$whereClause='',$groupBy='',$orderBy='',$limit='')
  *
  *              SECTION: Caching and standard clauses
- *  875:     function getHash($hash,$expTime=0)
+ *  875:     function getHash($hash)
  *  898:     function storeHash($hash,$data,$ident)
  *  916:     function deleteClause($table)
  *  936:     function enableFields($table,$show_hidden=-1,$ignore_array=array(),$noVersionPreview=FALSE)
@@ -113,15 +113,21 @@ class t3lib_pageSelect {
        var $sys_language_uid = 0;
 
                // Versioning preview related:
-       var $versioningPreview = FALSE;         // If true, 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!!!
+       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!!!
        var $versioningWorkspaceId = 0;         // Workspace ID for preview
-
+       var $workspaceCache = array();
 
 
                // Internal, dynamic:
        var $error_getRootLine = '';            // Error string set by getRootLine()
        var $error_getRootLine_failPid = 0;             // Error uid set by getRootLine()
 
+               // Internal caching
+       protected $cache_getRootLine = array();
+       protected $cache_getPage = array();
+       protected $cache_getPage_noCheck = array();
+       protected $cache_getPageIdFromAlias = array();
+       protected $cache_getMountPointInfo = array();
 
        /**
         * init() MUST be run directly after creating a new template-object
@@ -137,11 +143,11 @@ class t3lib_pageSelect {
                if (!$show_hidden)      {
                        $this->where_hid_del.= 'AND pages.hidden=0 ';
                }
-               $this->where_hid_del.= 'AND (pages.starttime<='.$GLOBALS['SIM_EXEC_TIME'].') AND (pages.endtime=0 OR pages.endtime>'.$GLOBALS['SIM_EXEC_TIME'].') ';
+               $this->where_hid_del.= 'AND (pages.starttime<='.$GLOBALS['SIM_ACCESS_TIME'].') AND (pages.endtime=0 OR pages.endtime>'.$GLOBALS['SIM_ACCESS_TIME'].') ';
 
-                       // Filter out new place-holder pages in case we are NOT in a versioning preview (that means we are online!)
+                       // Filter out new/deleted place-holder pages in case we are NOT in a versioning preview (that means we are online!)
                if (!$this->versioningPreview)  {
-                       $this->where_hid_del.= ' AND NOT(pages.t3ver_state=1)';
+                       $this->where_hid_del.= ' AND NOT (pages.t3ver_state>0)';
                } else {
                                // 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.
                        $this->versioningPreview_where_hid_del = $this->where_hid_del;  // Copy where_hid_del to other variable (used in relation to versionOL())
@@ -182,28 +188,65 @@ class t3lib_pageSelect {
         * @see getPage_noCheck()
         */
        function getPage($uid, $disableGroupAccessCheck=FALSE)  {
-               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid='.intval($uid).$this->where_hid_del.($disableGroupAccessCheck ? '' : $this->where_groupAccess));
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                       // Hook to manipulate the page uid for special overlay handling
+               if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'])) {
+                       foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'] as $classRef) {
+                               $hookObject = t3lib_div::getUserObj($classRef);
+
+                               if (!($hookObject instanceof t3lib_pageSelect_getPageHook)) {
+                                       throw new UnexpectedValueException('$hookObject must implement interface t3lib_pageSelect_getPageHook', 1251476766);
+                               }
+
+                               $hookObject->getPage_preProcess($uid, $disableGroupAccessCheck, $this);
+                       }
+               }
+
+               $accessCheck = $disableGroupAccessCheck ? '' : $this->where_groupAccess;
+               $cacheKey = md5($accessCheck . '-' . $this->where_hid_del . '-' . $this->sys_language_uid);
+
+               if (is_array($this->cache_getPage[$uid][$cacheKey])) {
+                       return $this->cache_getPage[$uid][$cacheKey];
+               }
+               $result = array();
+               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid='.intval($uid).$this->where_hid_del.$accessCheck);
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row) {
                        $this->versionOL('pages',$row);
-                       if (is_array($row))             return $this->getPageOverlay($row);
+                       if (is_array($row)) {
+                               $result = $this->getPageOverlay($row);
+                       }
                }
-               return Array();
+               $this->cache_getPage[$uid][$cacheKey] = $result;
+               return $result;
        }
 
        /**
         * Return the $row for the page with uid = $uid WITHOUT checking for ->where_hid_del (start- and endtime or hidden). Only "deleted" is checked!
         *
         * @param       integer         The page id to look up
-        * @return      array           The page row with overlayed localized fields. Empty it no page.
+        * @return      array           The page row with overlayed localized fields. Empty array if no page.
         * @see getPage()
         */
        function getPage_noCheck($uid)  {
+               if ($this->cache_getPage_noCheck[$uid]) {
+                       return $this->cache_getPage_noCheck[$uid];
+               }
+
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid='.intval($uid).$this->deleteClause('pages'));
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+
+               $result = array();
+               if ($row) {
                        $this->versionOL('pages',$row);
-                       if (is_array($row))             return $this->getPageOverlay($row);
+                       if (is_array($row)) {
+                               $result = $this->getPageOverlay($row);
+                       }
                }
-               return Array();
+               $this->cache_getPage_noCheck[$uid] = $result;
+
+               return $result;
        }
 
        /**
@@ -216,11 +259,14 @@ class t3lib_pageSelect {
        function getFirstWebPage($uid)  {
                $output = '';
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'pid='.intval($uid).$this->where_hid_del.$this->where_groupAccess, '', 'sorting', '1');
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
                        $this->versionOL('pages',$row);
-                       if (is_array($row))             $output = $this->getPageOverlay($row);
+                       if (is_array($row))     {
+                               $output = $this->getPageOverlay($row);
+                       }
                }
-               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                return $output;
        }
 
@@ -233,10 +279,17 @@ class t3lib_pageSelect {
         */
        function getPageIdFromAlias($alias)     {
                $alias = strtolower($alias);
+               if ($this->cache_getPageIdFromAlias[$alias]) {
+                       return $this->cache_getPageIdFromAlias[$alias];
+               }
                $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!)
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
+                       $this->cache_getPageIdFromAlias[$alias] = $row['uid'];
                        return $row['uid'];
                }
+               $this->cache_getPageIdFromAlias[$alias] = 0;
                return 0;
        }
 
@@ -282,6 +335,7 @@ class t3lib_pageSelect {
                                                        '1'
                                                );
                                $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                                $this->versionOL('pages_language_overlay',$row);
 
                                if (is_array($row))     {
@@ -337,9 +391,9 @@ class t3lib_pageSelect {
                                                                '1'
                                                        );
                                                        $olrow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                                                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
                                                        $this->versionOL($table,$olrow);
-       #debug($row);
-       #debug($olrow);
+
                                                                // Merge record content by traversing all fields:
                                                        if (is_array($olrow))   {
                                                                foreach($row as $fN => $fV)     {
@@ -362,7 +416,7 @@ class t3lib_pageSelect {
                                                        unset($row);
                                                }
                                        } else {
-                                                       // When default language is displayed, we never want to return a record carrying another language!:
+                                                       // When default language is displayed, we never want to return a record carrying another language!
                                                if ($row[$TCA[$table]['ctrl']['languageField']]>0)      {
                                                        unset($row);
                                                }
@@ -415,8 +469,7 @@ class t3lib_pageSelect {
                $output = Array();
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, 'pages', 'pid='.intval($uid).$this->where_hid_del.$this->where_groupAccess.' '.$addWhere, '', $sortField);
                while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))      {
-                       $this->versionOL('pages',$row);
-
+                       $this->versionOL('pages',$row,TRUE);
                        if (is_array($row))     {
                                        // Keep mount point:
                                $origUid = $row['uid'];
@@ -439,11 +492,17 @@ class t3lib_pageSelect {
                                                        // If a shortcut mode is set and no valid page is given to select subpags from use the actual page.
                                                $searchUid = intval($row['shortcut'])?intval($row['shortcut']):$row['uid'];
                                        }
-                                       $res2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', $searchField.'='.$searchUid.$this->where_hid_del.$this->where_groupAccess.' '.$addWhere, '', $sortField);
-                                       if (!$GLOBALS['TYPO3_DB']->sql_num_rows($res2)) {
+                                       $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
+                                               'uid',
+                                               'pages',
+                                               $searchField . '=' . $searchUid .
+                                                       $this->where_hid_del .
+                                                       $this->where_groupAccess .
+                                                       ' ' . $addWhere
+                                       );
+                                       if (!$count) {
                                                unset($row);
                                        }
-                                       $GLOBALS['TYPO3_DB']->sql_free_result($res2);
                                } elseif ($row['doktype'] == 4 && $checkShortcuts)      {
                                                // Neither shortcut target nor mode is set. Remove the page from the menu.
                                        unset($row);
@@ -455,6 +514,7 @@ class t3lib_pageSelect {
                                }
                        }
                }
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                return $output;
        }
 
@@ -470,15 +530,15 @@ class t3lib_pageSelect {
         */
        function getDomainStartPage($domain, $path='',$request_uri='')  {
                $domain = explode(':',$domain);
-               $domain = strtolower(ereg_replace('\.$','',$domain[0]));
+               $domain = strtolower(preg_replace('/\.$/','',$domain[0]));
                        // Removing extra trailing slashes
-               $path = trim(ereg_replace('\/[^\/]*$','',$path));
+               $path = trim(preg_replace('/\/[^\/]*$/','',$path));
                        // Appending to domain string
                $domain.= $path;
-               $domain = ereg_replace('\/*$','',$domain);
+               $domain = preg_replace('/\/*$/','',$domain);
 
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
-                                       'pages.uid,sys_domain.redirectTo,sys_domain.prepend_params',
+                                       'pages.uid,sys_domain.redirectTo,sys_domain.redirectHttpStatusCode,sys_domain.prepend_params',
                                        'pages,sys_domain',
                                        'pages.uid=sys_domain.pid
                                                AND sys_domain.hidden=0
@@ -488,15 +548,23 @@ class t3lib_pageSelect {
                                        '',
                                        1
                                );
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
                        if ($row['redirectTo']) {
-                               $rURL = $row['redirectTo'];
+                               $redirectUrl = $row['redirectTo'];
                                if ($row['prepend_params'])     {
-                                       $rURL = ereg_replace('\/$','',$rURL);
-                                       $prependStr = ereg_replace('^\/','',substr($request_uri,strlen($path)));
-                                       $rURL.= '/'.$prependStr;
+                                       $redirectUrl = rtrim($redirectUrl, '/');
+                                       $prependStr = ltrim(substr($request_uri,strlen($path)), '/');
+                                       $redirectUrl .= '/' . $prependStr;
+                               }
+
+                               $statusCode = intval($row['redirectHttpStatusCode']);
+                               if ($statusCode && defined('t3lib_utility_Http::HTTP_STATUS_' . $statusCode)) {
+                                       t3lib_utility_Http::redirect($redirectUrl, constant('t3lib_utility_Http::HTTP_STATUS_' . $statusCode));
+                               } else {
+                                       t3lib_utility_Http::redirect($redirectUrl, 't3lib_utility_Http::HTTP_STATUS_301');
                                }
-                               Header('Location: '.t3lib_div::locationHeaderUrl($rURL));
                                exit;
                        } else {
                                return $row['uid'];
@@ -516,7 +584,13 @@ class t3lib_pageSelect {
         * @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.
         * @see tslib_fe::getPageAndRootline()
         */
-       function getRootLine($uid, $MP='', $ignoreMPerrors=FALSE)       {
+       function getRootLine($uid, $MP = '', $ignoreMPerrors = false) {
+               $cacheUid = $uid = intval($uid);
+               $cacheIgnoreMPerrors = ($ignoreMPerrors ? 1 : 0);
+
+               if (is_array($this->cache_getRootLine[$cacheUid][$this->sys_language_uid][$MP][$cacheIgnoreMPerrors])) {
+                       return $this->cache_getRootLine[$cacheUid][$this->sys_language_uid][$MP][$cacheIgnoreMPerrors];
+               }
 
                        // Initialize:
                $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']);
@@ -527,20 +601,20 @@ class t3lib_pageSelect {
                $MPA = array();
                if ($MP)        {
                        $MPA = explode(',',$MP);
-                       reset($MPA);
-                       while(list($MPAk) = each($MPA)) {
+                       foreach ($MPA as $MPAk => $v) {
                                $MPA[$MPAk] = explode('-', $MPA[$MPAk]);
                        }
                }
 
                $loopCheck = 0;
                $theRowArray = Array();
-               $uid = intval($uid);
 
                while ($uid!=0 && $loopCheck<20)        {       // Max 20 levels in the page tree.
                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selFields, 'pages', 'uid='.intval($uid).' AND pages.deleted=0 AND pages.doktype!=255');
-                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
-                               $this->versionOL('pages',$row);
+                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                       if ($row)       {
+                               $this->versionOL('pages',$row, FALSE, TRUE);
                                $this->fixVersioningPid('pages',$row);
 
                                if (is_array($row))     {
@@ -562,8 +636,9 @@ class t3lib_pageSelect {
                                                        array_pop($MPA);
                                                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selFields, 'pages', 'uid='.intval($curMP[1]).' AND pages.deleted=0 AND pages.doktype!=255');
                                                        $mp_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                                                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
 
-                                                       $this->versionOL('pages',$mp_row);
+                                                       $this->versionOL('pages',$mp_row, FALSE, TRUE);
                                                        $this->fixVersioningPid('pages',$mp_row);
 
                                                        if (is_array($mp_row))  {
@@ -625,6 +700,8 @@ class t3lib_pageSelect {
                        $output[$c] = $val;
                }
 
+                       // Note: rootline errors are not cached
+               $this->cache_getRootLine[$cacheUid][$this->sys_language_uid][$MP][$cacheIgnoreMPerrors] = $output;
                return $output;
        }
 
@@ -683,17 +760,26 @@ class t3lib_pageSelect {
         * @see tslib_menu
         */
        function getMountPointInfo($pageId, $pageRec=FALSE, $prevMountPids=array(), $firstPageUid=0)    {
+               $result = FALSE;
+
                if ($GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids'])     {
 
+                       if (isset($this->cache_getMountPointInfo[$pageId]))     {
+                               return $this->cache_getMountPointInfo[$pageId];
+                       }
+
                                // Get pageRec if not supplied:
                        if (!is_array($pageRec))        {
                                $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');
                                $pageRec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                                $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!
                        }
 
                                // Set first Page uid:
-                       if (!$firstPageUid)     $firstPageUid = $pageRec['uid'];
+                       if (!$firstPageUid)     {
+                               $firstPageUid = $pageRec['uid'];
+                       }
 
                                // Look for mount pid value plus other required circumstances:
                        $mount_pid = intval($pageRec['mount_pid']);
@@ -701,31 +787,33 @@ class t3lib_pageSelect {
 
                                        // Get the mount point record (to verify its general existence):
                                $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');
-                               $mount_rec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
-                               $this->versionOL('pages',$mount_rec);
+                               $mountRec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                               $this->versionOL('pages',$mountRec);
 
-                               if (is_array($mount_rec))       {
+                               if (is_array($mountRec))        {
                                                // Look for recursive mount point:
                                        $prevMountPids[] = $mount_pid;
-                                       $recursiveMountPid = $this->getMountPointInfo($mount_pid, $mount_rec, $prevMountPids, $firstPageUid);
+                                       $recursiveMountPid = $this->getMountPointInfo($mount_pid, $mountRec, $prevMountPids, $firstPageUid);
 
                                                // Return mount point information:
-                                       return $recursiveMountPid ?
+                                       $result = $recursiveMountPid ?
                                                                $recursiveMountPid :
                                                                array(
                                                                        'mount_pid' => $mount_pid,
                                                                        'overlay' => $pageRec['mount_pid_ol'],
                                                                        'MPvar' => $mount_pid.'-'.$firstPageUid,
                                                                        'mount_point_rec' => $pageRec,
-                                                                       'mount_pid_rec' => $mount_rec,
+                                                                       'mount_pid_rec' => $mountRec,
                                                                );
                                } else {
-                                       return -1;      // Means, there SHOULD have been a mount point, but there was none!
+                                       $result = -1;   // Means, there SHOULD have been a mount point, but there was none!
                                }
                        }
                }
 
-               return FALSE;
+               $this->cache_getMountPointInfo[$pageId] = $result;
+               return $result;
        }
 
 
@@ -764,14 +852,16 @@ class t3lib_pageSelect {
                $uid = intval($uid);
                if (is_array($TCA[$table]))     {
                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, 'uid='.intval($uid).$this->enableFields($table));
-                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                       if ($row)       {
                                $this->versionOL($table,$row);
-                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
-
                                if (is_array($row))     {
                                        if ($checkPage) {
                                                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'uid='.intval($row['pid']).$this->enableFields('pages'));
-                                               if ($GLOBALS['TYPO3_DB']->sql_num_rows($res))   {
+                                               $numRows = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
+                                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                                               if ($numRows>0) {
                                                        return $row;
                                                } else {
                                                        return 0;
@@ -799,11 +889,15 @@ class t3lib_pageSelect {
                $uid = intval($uid);
                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()...
                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $table, 'uid='.intval($uid).$this->deleteClause($table));
-                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                       if ($row) {
                                if (!$noWSOL)   {
                                        $this->versionOL($table,$row);
                                }
-                               if (is_array($row))     return $row;
+                               if (is_array($row))     {
+                                       return $row;
+                               }
                        }
                }
        }
@@ -818,7 +912,7 @@ class t3lib_pageSelect {
         * @param       string          Optional GROUP BY field(s), if none, supply blank string.
         * @param       string          Optional ORDER BY field(s), if none, supply blank string.
         * @param       string          Optional LIMIT value ([begin,]max), if none, supply blank string.
-        * @return      mixed           Returns array (the record) if found, otherwise blank/0 (zero)
+        * @return      mixed           Returns array (the record) if found, otherwise nothing (void)
         */
        function getRecordsByField($theTable,$theField,$theValue,$whereClause='',$groupBy='',$orderBy='',$limit='')     {
                global $TCA;
@@ -839,7 +933,9 @@ class t3lib_pageSelect {
                                if (is_array($row)) $rows[] = $row;
                        }
                        $GLOBALS['TYPO3_DB']->sql_free_result($res);
-                       if (count($rows))       return $rows;
+                       if (count($rows))       {
+                               return $rows;
+                       }
                }
        }
 
@@ -863,47 +959,73 @@ class t3lib_pageSelect {
         **********************************/
 
        /**
-        * Returns string value stored for the hash string in the table "cache_hash"
+        * Returns string value stored for the hash string in the cache "cache_hash"
         * Can be used to retrieved a cached value
-        * Can be used from your frontend plugins if you like. Is also used to store the parsed TypoScript template structures. You can call it directly like t3lib_pageSelect::getHash()
+        * Can be used from your frontend plugins if you like. It is also used to
+        * store the parsed TypoScript template structures. You can call it directly
+        * like t3lib_pageSelect::getHash()
+        *
+        * IDENTICAL to the function by same name found in t3lib_page
         *
         * @param       string          The hash-string which was used to store the data value
-        * @param       integer         Allowed expiretime in seconds. Basically a record is selected only if it is not older than this value in seconds. If expTime is not set, the hashed value will never expire.
-        * @return      string          The "content" field of the "cache_hash" table row.
+        * @return      string          The "content" field of the "cache_hash" cache entry.
         * @see tslib_TStemplate::start(), storeHash()
         */
-       function getHash($hash,$expTime=0)      {
-                       //
-               $expTime = intval($expTime);
-               if ($expTime)   {
-                       $whereAdd = ' AND tstamp > '.(time()-$expTime);
-               }
-               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('content', 'cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash').$whereAdd);
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+       public static function getHash($hash, $expTime = 0)     {
+               $hashContent = null;
+
+               if (TYPO3_UseCachingFramework) {
+                       $contentHashCache = $GLOBALS['typo3CacheManager']->getCache('cache_hash');
+                       $cacheEntry = $contentHashCache->get($hash);
+
+                       if ($cacheEntry) {
+                               $hashContent = $cacheEntry;
+                       }
+               } else {
+                       $expTime = intval($expTime);
+                       if ($expTime) {
+                               $whereAdd = ' AND tstamp > '.($GLOBALS['ACCESS_TIME']-$expTime);
+                       }
+                       $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('content', 'cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash').$whereAdd);
+                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                        $GLOBALS['TYPO3_DB']->sql_free_result($res);
-                       return $row['content'];
+                       if ($row)       {
+                               $hashContent = $row['content'];
+                       }
                }
+               return $hashContent;
        }
 
        /**
-        * Stores a string value in the cache_hash table identified by $hash.
-        * Can be used from your frontend plugins if you like. You can call it directly like t3lib_pageSelect::storeHash()
+        * Stores a string value in the cache_hash cache identified by $hash.
+        * Can be used from your frontend plugins if you like. You can call it
+        * directly like t3lib_pageSelect::storeHash()
         *
         * @param       string          32 bit hash string (eg. a md5 hash of a serialized array identifying the data being stored)
         * @param       string          The data string. If you want to store an array, then just serialize it first.
-        * @param       string          $ident is just a textual identification in order to inform about the content! May be 20 characters long.
+        * @param       string          $ident is just a textual identification in order to inform about the content!
+        * @param       integer         The lifetime for the cache entry in seconds
         * @return      void
         * @see tslib_TStemplate::start(), getHash()
         */
-       function storeHash($hash,$data,$ident)  {
-               $insertFields = array(
-                       'hash' => $hash,
-                       'content' => $data,
-                       'ident' => $ident,
-                       'tstamp' => time()
-               );
-               $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash'));
-               $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_hash', $insertFields);
+       public static function storeHash($hash, $data, $ident, $lifetime = 0) {
+               if (TYPO3_UseCachingFramework) {
+                       $GLOBALS['typo3CacheManager']->getCache('cache_hash')->set(
+                               $hash,
+                               $data,
+                               array('ident_' . $ident),
+                               $lifetime
+                       );
+               } else {
+                       $insertFields = array(
+                               'hash' => $hash,
+                               'content' => $data,
+                               'ident' => $ident,
+                               'tstamp' => $GLOBALS['EXEC_TIME']
+                       );
+                       $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash'));
+                       $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_hash', $insertFields);
+               }
        }
 
        /**
@@ -952,7 +1074,7 @@ class t3lib_pageSelect {
 
                                // Filter out new place-holder records in case we are NOT in a versioning preview (that means we are online!)
                        if ($ctrl['versioningWS'] && !$this->versioningPreview) {
-                               $query.=' AND '.$table.'.t3ver_state!=1';       // Shadow state for new items MUST be ignored!
+                               $query.=' AND '.$table.'.t3ver_state<=0';       // Shadow state for new items MUST be ignored!
                        }
 
                                // Enable fields:
@@ -964,11 +1086,11 @@ class t3lib_pageSelect {
                                        }
                                        if ($ctrl['enablecolumns']['starttime'] && !$ignore_array['starttime']) {
                                                $field = $table.'.'.$ctrl['enablecolumns']['starttime'];
-                                               $query.=' AND ('.$field.'<='.$GLOBALS['SIM_EXEC_TIME'].')';
+                                               $query.=' AND ('.$field.'<='.$GLOBALS['SIM_ACCESS_TIME'].')';
                                        }
                                        if ($ctrl['enablecolumns']['endtime'] && !$ignore_array['endtime']) {
                                                $field = $table.'.'.$ctrl['enablecolumns']['endtime'];
-                                               $query.=' AND ('.$field.'=0 OR '.$field.'>'.$GLOBALS['SIM_EXEC_TIME'].')';
+                                               $query.=' AND ('.$field.'=0 OR '.$field.'>'.$GLOBALS['SIM_ACCESS_TIME'].')';
                                        }
                                        if ($ctrl['enablecolumns']['fe_group'] && !$ignore_array['fe_group']) {
                                                $field = $table.'.'.$ctrl['enablecolumns']['fe_group'];
@@ -1068,8 +1190,8 @@ class t3lib_pageSelect {
                                }
                        }
 
-                               // If workspace ids matches and ID of current online version is found, look up the PID value of that:
-                       if ($oid && !strcmp((int)$wsid,$this->versioningWorkspaceId))   {
+                       // If workspace ids matches and ID of current online version is found, look up the PID value of that:
+                       if ($oid && (($this->versioningWorkspaceId == 0 && $this->checkWorkspaceAccess($wsid)) || !strcmp((int)$wsid,$this->versioningWorkspaceId)))    {
                                $oidRec = $this->getRawRecord($table,$oid,'pid',TRUE);
 
                                if (is_array($oidRec))  {
@@ -1079,6 +1201,11 @@ class t3lib_pageSelect {
                                }
                        }
                }
+
+                       // changing PID in case of moving pointer:
+               if ($movePlhRec = $this->getMovePlaceholder($table,$rr['uid'],'pid'))   {
+                       $rr['pid'] = $movePlhRec['pid'];
+               }
        }
 
        /**
@@ -1089,16 +1216,18 @@ class t3lib_pageSelect {
         *
         * @param       string          Table name
         * @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!
+        * @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)
+        * @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!
         * @return      void            (Passed by ref).
         * @see fixVersioningPid(), t3lib_BEfunc::workspaceOL()
         */
-       function versionOL($table,&$row)        {
+       function versionOL($table,&$row,$unsetMovePointers=FALSE,$bypassEnableFieldsCheck=FALSE)        {
                global $TCA;
 
                if ($this->versioningPreview && is_array($row)) {
-                       if ($wsAlt = $this->getWorkspaceVersionOfRecord($this->versioningWorkspaceId, $table, $row['uid'], implode(',',array_keys($row))))      {       // implode(',',array_keys($row)) = Using fields from original record to make sure no additional fields are selected. This is best for eg. getPageOverlay()
+                       $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.
+                       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()
                                if (is_array($wsAlt))   {
-
                                                // Always fix PID (like in fixVersioningPid() above). [This is usually not the important factor for versioning OL]
                                        $wsAlt['_ORIG_pid'] = $wsAlt['pid'];    // Keep the old (-1) - indicates it was a version...
                                        $wsAlt['pid'] = $row['pid'];            // Set in the online versions PID.
@@ -1123,13 +1252,20 @@ class t3lib_pageSelect {
                                                // Changing input record to the workspace version alternative:
                                        $row = $wsAlt;
 
-                                               // Check if it is deleted in workspace:
-                                       if ((int)$row['t3ver_state']===2)       {
+                                               // Check if it is deleted/new
+                                       if ((int)$row['t3ver_state']===1 || (int)$row['t3ver_state']===2)       {
+                                               $row = FALSE;   // Unset record if it turned out to be deleted in workspace
+                                       }
+
+                                               // Check if move-pointer in workspace (unless if a move-placeholder is the reason why it appears!):
+                                               // 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.
+                                       if ((int)$row['t3ver_state']===4 && !$movePldSwap && $unsetMovePointers)        {
                                                $row = FALSE;   // Unset record if it turned out to be deleted in workspace
                                        }
                                } else {
                                                // No version found, then check if t3ver_state =1 (online version is dummy-representation)
-                                       if ($wsAlt==-1 || (int)$row['t3ver_state']===1) {
+                                               // 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
+                                       if ($wsAlt<=-1 || (int)$row['t3ver_state']>0)   {
                                                $row = FALSE;   // Unset record if it turned out to be "hidden"
                                        }
                                }
@@ -1138,16 +1274,88 @@ class t3lib_pageSelect {
        }
 
        /**
+        * 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)
+        * Used from versionOL
+        *
+        * @param       string          Table name
+        * @param       array           Row (passed by reference) - only online records...
+        * @return      boolean         True if overlay is made.
+        * @see t3lib_BEfunc::movePlhOl()
+        */
+       function movePlhOL($table,&$row)        {
+               global $TCA;
+
+               if (($table=='pages' || (int)$TCA[$table]['ctrl']['versioningWS']>=2) && (int)$row['t3ver_state']===3)  {       // Only for WS ver 2... (moving)
+
+                               // If t3ver_move_id is not found, then find it... (but we like best if it is here...)
+                       if (!isset($row['t3ver_move_id']))      {
+                               $moveIDRec = $this->getRawRecord($table,$row['uid'],'t3ver_move_id',TRUE);
+                               $moveID = $moveIDRec['t3ver_move_id'];
+                       } else {
+                               $moveID = $row['t3ver_move_id'];
+                       }
+
+                               // Find pointed-to record.
+                       if ($moveID)    {
+                               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',',array_keys($row)), $table, 'uid='.intval($moveID).$this->enableFields($table));
+                               $origRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                               if ($origRow)   {
+                                       $row = $origRow;
+                                       return TRUE;
+                               }
+                       }
+               }
+               return FALSE;
+       }
+
+       /**
+        * Returns move placeholder of online (live) version
+        *
+        * @param       string          Table name
+        * @param       integer         Record UID of online version
+        * @param       string          Field list, default is *
+        * @return      array           If found, the record, otherwise nothing.
+        * @see t3lib_BEfunc::getMovePlaceholder()
+        */
+       function getMovePlaceholder($table,$uid,$fields='*')    {
+               global $TCA;
+
+               if ($this->versioningPreview)   {
+                       $workspace = (int)$this->versioningWorkspaceId;
+                       if (($table=='pages' || (int)$TCA[$table]['ctrl']['versioningWS']>=2) && $workspace!==0)        {
+
+                                       // Select workspace version of record:
+                               $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
+                                       $fields,
+                                       $table,
+                                       'pid!=-1 AND
+                                        t3ver_state=3 AND
+                                        t3ver_move_id='.intval($uid).' AND
+                                        t3ver_wsid='.intval($workspace).
+                                       $this->deleteClause($table)
+                               );
+
+                               if (is_array($rows[0])) {
+                                       return $rows[0];
+                               }
+                       }
+               }
+               return FALSE;
+       }
+
+       /**
         * Select the version of a record for a workspace
         *
         * @param       integer         Workspace ID
         * @param       string          Table name to select from
         * @param       integer         Record uid for which to find workspace version.
         * @param       string          Field list to select
-        * @return      array           If found, return record, otherwise false. Returns 1 if version was sought for but not found, returns -1 if record existed but had enableFields that would disable it.
+        * @param       boolean         If true, enablefields are not checked for.
+        * @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.
         * @see t3lib_befunc::getWorkspaceVersionOfRecord()
         */
-       function getWorkspaceVersionOfRecord($workspace, $table, $uid, $fields='*')     {
+       function getWorkspaceVersionOfRecord($workspace, $table, $uid, $fields='*',$bypassEnableFieldsCheck=FALSE)      {
                global $TCA;
 
                if ($workspace!==0 && ($table=='pages' || $TCA[$table]['ctrl']['versioningWS']))        {       // Have to hardcode it for "pages" table since TCA is not loaded at this moment!
@@ -1171,7 +1379,7 @@ class t3lib_pageSelect {
 
                                // If version found, check if it could have been selected with enableFields on as well:
                        if (is_array($newrow))  {
-                               if ($GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
+                               if ($bypassEnableFieldsCheck || $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
                                        'uid',
                                        $table,
                                        'pid=-1 AND
@@ -1181,24 +1389,56 @@ class t3lib_pageSelect {
                                )) {
                                        return $newrow; // Return offline version, tested for its enableFields.
                                } else {
-                                       return -1;      // Return -1 because version was de-selected due to its enableFields.
+                                       return -1;      // Return -1 because offline version was de-selected due to its enableFields.
                                }
                        } else {
                                        // OK, so no workspace version was found. Then check if online version can be selected with full enable fields and if so, return 1:
-                               if ($GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
+                               if ($bypassEnableFieldsCheck || $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
                                        'uid',
                                        $table,
                                        'uid='.intval($uid).$enFields
                                ))      {
                                        return 1;       // Means search was done, but no version found.
                                } else {
-                                       return -1;      // Return -1 because the current record was de-selected due to its enableFields.
+                                       return -2;      // Return -2 because the online record was de-selected due to its enableFields.
                                }
                        }
                }
 
                return FALSE;   // No look up in database because versioning not enabled / or workspace not offline
        }
+
+       /**
+        * Checks if user has access to workspace.
+        *
+        * @param       int     $wsid   Workspace ID
+        * @return      boolean <code>true</code> if has access
+        */
+       function checkWorkspaceAccess($wsid) {
+               if (!$GLOBALS['BE_USER']) {
+                       return false;
+               }
+               if (isset($this->workspaceCache[$wsid])) {
+                       $ws = $this->workspaceCache[$wsid];
+               }
+               else {
+                       if ($wsid > 0) {
+                               $ws = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_workspace', 'uid='.intval($wsid).' AND deleted=0'); // No $TCA yet!
+                               if (count($ws)) {
+                                       $ws = $ws[0];
+                               }
+                               else {
+                                       return false;
+                               }
+                       }
+                       else {
+                               $ws = $wsid;
+                       }
+                       $ws = $GLOBALS['BE_USER']->checkWorkspace($ws);
+                       $this->workspaceCache[$wsid] = $ws;
+               }
+               return ($ws['_ACCESS'] != '');
+       }
 }
 
 
@@ -1206,4 +1446,5 @@ class t3lib_pageSelect {
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_page.php'])     {
        include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_page.php']);
 }
-?>
+
+?>
\ No newline at end of file