class.t3lib_befunc.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00183 class t3lib_BEfunc      {
00184 
00185 
00186 
00187         /*******************************************
00188          *
00189          * SQL-related, selecting records, searching
00190          *
00191          *******************************************/
00192 
00193 
00204         function deleteClause($table,$tableAlias='')    {
00205                 global $TCA;
00206                 if ($TCA[$table]['ctrl']['delete'])     {
00207                         return ' AND '.($tableAlias ? $tableAlias : $table).'.'.$TCA[$table]['ctrl']['delete'].'=0';
00208                 } else {
00209                         return '';
00210                 }
00211         }
00212 
00227         function getRecord($table,$uid,$fields='*',$where='')   {
00228                 if ($GLOBALS['TCA'][$table])    {
00229                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $table, 'uid='.intval($uid).t3lib_BEfunc::deleteClause($table).$where);
00230                         if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00231                                 return $row;
00232                         }
00233                 }
00234         }
00235 
00245         function getRecordWSOL($table,$uid,$fields='*',$where='') {
00246                 if ($fields !== '*') {
00247                         $internalFields = t3lib_div::uniqueList($fields.',uid,pid'.($table == 'pages' ? ',t3ver_swapmode' : ''));
00248                         $row = t3lib_BEfunc::getRecord($table,$uid,$internalFields,$where);
00249                         t3lib_BEfunc::workspaceOL($table,$row);
00250 
00251                         if (is_array ($row)) {
00252                                 foreach (array_keys($row) as $key) {
00253                                         if (!t3lib_div::inList($fields, $key) && $key{0} !== '_') {
00254                                                 unset ($row[$key]);
00255                                         }
00256                                 }
00257                         }
00258                 } else {
00259                         $row = t3lib_BEfunc::getRecord($table,$uid,$fields,$where);
00260                         t3lib_BEfunc::workspaceOL($table,$row);
00261                 }
00262                 return $row;
00263         }
00264 
00278         function getRecordRaw($table,$where='',$fields='*')     {
00279                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $table, $where);
00280                 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00281                         return $row;
00282                 }
00283         }
00284 
00300         function getRecordsByField($theTable,$theField,$theValue,$whereClause='',$groupBy='',$orderBy='',$limit='')     {
00301                 global $TCA;
00302                 if (is_array($TCA[$theTable])) {
00303                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00304                                                 '*',
00305                                                 $theTable,
00306                                                 $theField.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($theValue, $theTable).
00307                                                         t3lib_BEfunc::deleteClause($theTable).' '.
00308                                                         t3lib_BEfunc::versioningPlaceholderClause($theTable).' '.
00309                                                         $whereClause,   // whereClauseMightContainGroupOrderBy
00310                                                 $groupBy,
00311                                                 $orderBy,
00312                                                 $limit
00313                                         );
00314                         $rows = array();
00315                         while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00316                                 $rows[] = $row;
00317                         }
00318                         $GLOBALS['TYPO3_DB']->sql_free_result($res);
00319                         if (count($rows))       return $rows;
00320                 }
00321         }
00322 
00333         function searchQuery($searchWords,$fields,$table='')    {
00334                 return $GLOBALS['TYPO3_DB']->searchQuery($searchWords,$fields,$table);
00335         }
00336 
00348         function listQuery($field,$value)       {
00349                 return $GLOBALS['TYPO3_DB']->listQuery($field,$value,'');
00350         }
00351 
00360         function splitTable_Uid($str)   {
00361                 list($uid,$table) = explode('_',strrev($str),2);
00362                 return array(strrev($table),strrev($uid));
00363         }
00364 
00375         function getSQLselectableList($in_list,$tablename,$default_tablename)   {
00376                 $list = Array();
00377                 if ((string)trim($in_list)!='') {
00378                         $tempItemArray = explode(',',trim($in_list));
00379                         while(list($key,$val)=each($tempItemArray))     {
00380                                 $val = strrev($val);
00381                                 $parts = explode('_',$val,2);
00382                                 if ((string)trim($parts[0])!='')        {
00383                                         $theID = intval(strrev($parts[0]));
00384                                         $theTable = trim($parts[1]) ? strrev(trim($parts[1])) : $default_tablename;
00385                                         if ($theTable==$tablename)      {$list[]=$theID;}
00386                                 }
00387                         }
00388                 }
00389                 return implode(',',$list);
00390         }
00391 
00403         function BEenableFields($table,$inv=0)  {
00404                 $ctrl = $GLOBALS['TCA'][$table]['ctrl'];
00405                 $query=array();
00406                 $invQuery=array();
00407                 if (is_array($ctrl))    {
00408                         if (is_array($ctrl['enablecolumns']))   {
00409                                 if ($ctrl['enablecolumns']['disabled']) {
00410                                         $field = $table.'.'.$ctrl['enablecolumns']['disabled'];
00411                                         $query[]=$field.'=0';
00412                                         $invQuery[]=$field.'!=0';
00413                                 }
00414                                 if ($ctrl['enablecolumns']['starttime'])        {
00415                                         $field = $table.'.'.$ctrl['enablecolumns']['starttime'];
00416                                         $query[]='('.$field.'<='.$GLOBALS['SIM_EXEC_TIME'].')';
00417                                         $invQuery[]='('.$field.'!=0 AND '.$field.'>'.$GLOBALS['SIM_EXEC_TIME'].')';
00418                                 }
00419                                 if ($ctrl['enablecolumns']['endtime'])  {
00420                                         $field = $table.'.'.$ctrl['enablecolumns']['endtime'];
00421                                         $query[]='('.$field.'=0 OR '.$field.'>'.$GLOBALS['SIM_EXEC_TIME'].')';
00422                                         $invQuery[]='('.$field.'!=0 AND '.$field.'<='.$GLOBALS['SIM_EXEC_TIME'].')';
00423                                 }
00424                         }
00425                 }
00426                 $outQ = ' AND '.($inv ? '('.implode(' OR ',$invQuery).')' : implode(' AND ',$query));
00427 
00428                 return $outQ;
00429         }
00430 
00431 
00432 
00433 
00434 
00435 
00436 
00437 
00438 
00439 
00440         /*******************************************
00441          *
00442          * SQL-related, DEPRECATED functions
00443          * (use t3lib_DB functions instead)
00444          *
00445          *******************************************/
00446 
00447 
00467         function mm_query($select,$local_table,$mm_table,$foreign_table,$whereClause='',$groupBy='',$orderBy='',$limit='')      {
00468                 $query = $GLOBALS['TYPO3_DB']->SELECTquery(
00469                                         $select,
00470                                         $local_table.','.$mm_table.($foreign_table?','.$foreign_table:''),
00471                                         $local_table.'.uid='.$mm_table.'.uid_local'.($foreign_table?' AND '.$foreign_table.'.uid='.$mm_table.'.uid_foreign':'').' '.
00472                                                 $whereClause,   // whereClauseMightContainGroupOrderBy
00473                                         $groupBy,
00474                                         $orderBy,
00475                                         $limit
00476                                 );
00477                 return $query;
00478         }
00479 
00489         function DBcompileInsert($table,$fields_values) {
00490                 return $GLOBALS['TYPO3_DB']->INSERTquery($table, $fields_values);
00491         }
00492 
00503         function DBcompileUpdate($table,$where,$fields_values)  {
00504                 return $GLOBALS['TYPO3_DB']->UPDATEquery($table, $where, $fields_values);
00505         }
00506 
00507 
00508 
00509 
00510 
00511 
00512 
00513 
00514 
00515 
00516         /*******************************************
00517          *
00518          * Page tree, TCA related
00519          *
00520          *******************************************/
00521 
00533         function BEgetRootLine($uid,$clause='',$workspaceOL=FALSE)      {
00534                 $loopCheck = 100;
00535                 $theRowArray = Array();
00536                 $output = Array();
00537                 while ($uid!=0 && $loopCheck>0) {
00538                         $loopCheck--;
00539                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00540                                 'pid,uid,title,TSconfig,is_siteroot,storage_pid,t3ver_oid,t3ver_wsid,t3ver_state,t3ver_swapmode,t3ver_stage',
00541                                 'pages',
00542                                 'uid='.intval($uid).' '.
00543                                         t3lib_BEfunc::deleteClause('pages').' '.
00544                                         $clause         // whereClauseMightContainGroupOrderBy
00545                         );
00546                         if ($GLOBALS['TYPO3_DB']->sql_error())  {
00547                                 debug($GLOBALS['TYPO3_DB']->sql_error(),1);
00548                         }
00549                         if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00550                                 if($workspaceOL)        t3lib_BEfunc::workspaceOL('pages',$row);
00551                                 t3lib_BEfunc::fixVersioningPid('pages',$row);
00552                                 $uid = $row['pid'];
00553                                 $theRowArray[] = $row;
00554                         } else {
00555                                 break;
00556                         }
00557                 }
00558                 if ($uid==0) {$theRowArray[] = Array('uid'=>0,'title'=>'');}
00559                 if (is_array($theRowArray))     {
00560                         reset($theRowArray);
00561                         $c=count($theRowArray);
00562                         while(list($key,$val)=each($theRowArray))       {
00563                                 $c--;
00564                                 $output[$c]['uid'] = $val['uid'];
00565                                 $output[$c]['pid'] = $val['pid'];
00566                                 if (isset($val['_ORIG_pid'])) $output[$c]['_ORIG_pid'] = $val['_ORIG_pid'];
00567                                 $output[$c]['title'] = $val['title'];
00568                                 $output[$c]['TSconfig'] = $val['TSconfig'];
00569                                 $output[$c]['is_siteroot'] = $val['is_siteroot'];
00570                                 $output[$c]['storage_pid'] = $val['storage_pid'];
00571                                 $output[$c]['t3ver_oid'] = $val['t3ver_oid'];
00572                                 $output[$c]['t3ver_wsid'] = $val['t3ver_wsid'];
00573                                 $output[$c]['t3ver_state'] = $val['t3ver_state'];
00574                                 $output[$c]['t3ver_swapmode'] = $val['t3ver_swapmode'];
00575                                 $output[$c]['t3ver_stage'] = $val['t3ver_stage'];
00576                         }
00577                 }
00578 
00579                 return $output;
00580         }
00581 
00589         function openPageTree($pid,$clearExpansion)     {
00590                 global $BE_USER;
00591 
00592                         // Get current expansion data:
00593                 if ($clearExpansion)    {
00594                         $expandedPages = array();
00595                 } else {
00596                         $expandedPages = unserialize($BE_USER->uc['browseTrees']['browsePages']);
00597                 }
00598 
00599                         // Get rootline:
00600                 $rL = t3lib_BEfunc::BEgetRootLine($pid);
00601 
00602                         // First, find out what mount index to use (if more than one DB mount exists):
00603                 $mountIndex = 0;
00604                 $mountKeys = array_flip($BE_USER->returnWebmounts());
00605                 foreach($rL as $rLDat)  {
00606                         if (isset($mountKeys[$rLDat['uid']]))   {
00607                                 $mountIndex = $mountKeys[$rLDat['uid']];
00608                                 break;
00609                         }
00610                 }
00611 
00612                         // Traverse rootline and open paths:
00613                 foreach($rL as $rLDat)  {
00614                         $expandedPages[$mountIndex][$rLDat['uid']] = 1;
00615                 }
00616 
00617                         // Write back:
00618                 $BE_USER->uc['browseTrees']['browsePages'] = serialize($expandedPages);
00619                 $BE_USER->writeUC();
00620         }
00621 
00634         function getRecordPath($uid, $clause, $titleLimit, $fullTitleLimit=0)   {
00635                 if (!$titleLimit) { $titleLimit=1000; }
00636 
00637                 $loopCheck = 100;
00638                 $output = $fullOutput = '/';
00639                 while ($uid!=0 && $loopCheck>0) {
00640                         $loopCheck--;
00641                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00642                                                 'uid,pid,title,t3ver_oid,t3ver_wsid,t3ver_swapmode',
00643                                                 'pages',
00644                                                 'uid='.intval($uid).
00645                                                         t3lib_BEfunc::deleteClause('pages').
00646                                                         (strlen(trim($clause)) ? ' AND '.$clause : '')
00647                                         );
00648                         if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00649                                 t3lib_BEfunc::workspaceOL('pages',$row);
00650                                 t3lib_BEfunc::fixVersioningPid('pages',$row);
00651 
00652                                 if ($row['_ORIG_pid'] && $row['t3ver_swapmode']>0)      {       // Branch points
00653                                         $output = ' [#VEP#]'.$output;           // Adding visual token - Versioning Entry Point - that tells that THIS position was where the versionized branch got connected to the main tree. I will have to find a better name or something...
00654                                 }
00655                                 $uid = $row['pid'];
00656                                 $output = '/'.t3lib_div::fixed_lgd_cs(strip_tags($row['title']),$titleLimit).$output;
00657                                 if ($fullTitleLimit)    $fullOutput = '/'.t3lib_div::fixed_lgd_cs(strip_tags($row['title']),$fullTitleLimit).$fullOutput;
00658                         } else {
00659                                 break;
00660                         }
00661                 }
00662 
00663                 if ($fullTitleLimit)    {
00664                         return array($output, $fullOutput);
00665                 } else {
00666                         return $output;
00667                 }
00668         }
00669 
00677         function getExcludeFields()     {
00678                 global $TCA;
00679                         // All TCA keys:
00680                 $theExcludeArray = Array();
00681                 $tc_keys = array_keys($TCA);
00682                 foreach($tc_keys as $table)     {
00683                                 // Load table
00684                         t3lib_div::loadTCA($table);
00685                                 // All field names configured:
00686                         if (is_array($TCA[$table]['columns']))  {
00687                                 $f_keys = array_keys($TCA[$table]['columns']);
00688                                 foreach($f_keys as $field)      {
00689                                         if ($TCA[$table]['columns'][$field]['exclude']) {
00690                                                         // Get Human Readable names of fields and table:
00691                                                 $Fname=$GLOBALS['LANG']->sl($TCA[$table]['ctrl']['title']).': '.$GLOBALS['LANG']->sl($TCA[$table]['columns'][$field]['label']);
00692                                                         // add entry:
00693                                                 $theExcludeArray[] = Array($Fname , $table.':'.$field);
00694                                         }
00695                                 }
00696                         }
00697                 }
00698                 return $theExcludeArray;
00699         }
00700 
00707         function getExplicitAuthFieldValues()   {
00708                 global $TCA;
00709 
00710                         // Initialize:
00711                 $adLabel = array(
00712                         'ALLOW' => $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_core.xml:labels.allow'),
00713                         'DENY' => $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_core.xml:labels.deny'),
00714                 );
00715 
00716                         // All TCA keys:
00717                 $allowDenyOptions = Array();
00718                 $tc_keys = array_keys($TCA);
00719                 foreach($tc_keys as $table)     {
00720 
00721                                 // Load table
00722                         t3lib_div::loadTCA($table);
00723 
00724                                 // All field names configured:
00725                         if (is_array($TCA[$table]['columns']))  {
00726                                 $f_keys = array_keys($TCA[$table]['columns']);
00727                                 foreach($f_keys as $field)      {
00728                                         $fCfg = $TCA[$table]['columns'][$field]['config'];
00729                                         if ($fCfg['type']=='select' && $fCfg['authMode'])       {
00730 
00731                                                         // Check for items:
00732                                                 if (is_array($fCfg['items']))   {
00733                                                                 // Get Human Readable names of fields and table:
00734                                                         $allowDenyOptions[$table.':'.$field]['tableFieldLabel'] = $GLOBALS['LANG']->sl($TCA[$table]['ctrl']['title']).': '.$GLOBALS['LANG']->sl($TCA[$table]['columns'][$field]['label']);
00735 
00736                                                                 // Check for items:
00737                                                         foreach($fCfg['items'] as $iVal)        {
00738                                                                 if (strcmp($iVal[1],''))        {       // Values '' is not controlled by this setting.
00739 
00740                                                                                 // Find iMode:
00741                                                                         $iMode = '';
00742                                                                         switch((string)$fCfg['authMode'])       {
00743                                                                                 case 'explicitAllow':
00744                                                                                         $iMode = 'ALLOW';
00745                                                                                 break;
00746                                                                                 case 'explicitDeny':
00747                                                                                         $iMode = 'DENY';
00748                                                                                 break;
00749                                                                                 case 'individual':
00750                                                                                         if (!strcmp($iVal[4],'EXPL_ALLOW'))     {
00751                                                                                                 $iMode = 'ALLOW';
00752                                                                                         } elseif (!strcmp($iVal[4],'EXPL_DENY'))        {
00753                                                                                                 $iMode = 'DENY';
00754                                                                                         }
00755                                                                                 break;
00756                                                                         }
00757 
00758                                                                                 // Set iMode:
00759                                                                         if ($iMode)     {
00760                                                                                 $allowDenyOptions[$table.':'.$field]['items'][$iVal[1]] = array($iMode, $GLOBALS['LANG']->sl($iVal[0]), $adLabel[$iMode]);
00761                                                                         }
00762                                                                 }
00763                                                         }
00764                                                 }
00765                                         }
00766                                 }
00767                         }
00768                 }
00769 
00770                 return $allowDenyOptions;
00771         }
00772 
00778         function getSystemLanguages()   {
00779 
00780                         // Initialize, add default language:
00781                 $sysLanguages = array();
00782                 $sysLanguages[] = array('Default language', 0);
00783 
00784                         // Traverse languages
00785                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,title,flag','sys_language','pid=0'.t3lib_BEfunc::deleteClause('sys_language'));
00786                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00787                         $sysLanguages[] = array($row['title'].' ['.$row['uid'].']', $row['uid'], ($row['flag'] ? 'flags/'.$row['flag'] : ''));
00788                 }
00789 
00790                 return $sysLanguages;
00791         }
00792 
00803         function readPageAccess($id,$perms_clause)      {
00804                 if ((string)$id!='')    {
00805                         $id = intval($id);
00806                         if (!$id)       {
00807                                 if ($GLOBALS['BE_USER']->isAdmin())     {
00808                                         $path = '/';
00809                                         $pageinfo['_thePath'] = $path;
00810                                         return $pageinfo;
00811                                 }
00812                         } else {
00813                                 $pageinfo = t3lib_BEfunc::getRecord('pages',$id,'*',($perms_clause ? ' AND '.$perms_clause : ''));
00814                                 if ($pageinfo['uid'] && $GLOBALS['BE_USER']->isInWebMount($id,$perms_clause))   {
00815                                         t3lib_BEfunc::workspaceOL('pages', $pageinfo);
00816                                         t3lib_BEfunc::fixVersioningPid('pages', $pageinfo);
00817                                         list($pageinfo['_thePath'],$pageinfo['_thePathFull']) = t3lib_BEfunc::getRecordPath(intval($pageinfo['uid']), $perms_clause, 15, 1000);
00818                                         return $pageinfo;
00819                                 }
00820                         }
00821                 }
00822                 return false;
00823         }
00824 
00834         function getTCAtypes($table,$rec,$useFieldNameAsKey=0)  {
00835                 global $TCA;
00836 
00837                 t3lib_div::loadTCA($table);
00838                 if ($TCA[$table])       {
00839 
00840                                 // Get type value:
00841                         $fieldValue = t3lib_BEfunc::getTCAtypeValue($table,$rec);
00842 
00843                                 // Get typesConf
00844                         $typesConf = $TCA[$table]['types'][$fieldValue];
00845 
00846                                 // Get fields list and traverse it
00847                         $fieldList = explode(',', $typesConf['showitem']);
00848                         $altFieldList = array();
00849 
00850                                 // Traverse fields in types config and parse the configuration into a nice array:
00851                         foreach($fieldList as $k => $v) {
00852                                 list($pFieldName, $pAltTitle, $pPalette, $pSpec) = t3lib_div::trimExplode(';', $v);
00853                                 $defaultExtras = is_array($TCA[$table]['columns'][$pFieldName]) ? $TCA[$table]['columns'][$pFieldName]['defaultExtras'] : '';
00854                                 $specConfParts = t3lib_BEfunc::getSpecConfParts($pSpec, $defaultExtras);
00855 
00856                                 $fieldList[$k]=array(
00857                                         'field' => $pFieldName,
00858                                         'title' => $pAltTitle,
00859                                         'palette' => $pPalette,
00860                                         'spec' => $specConfParts,
00861                                         'origString' => $v
00862                                 );
00863                                 if ($useFieldNameAsKey) {
00864                                         $altFieldList[$fieldList[$k]['field']] = $fieldList[$k];
00865                                 }
00866                         }
00867                         if ($useFieldNameAsKey) {
00868                                 $fieldList = $altFieldList;
00869                         }
00870 
00871                                 // Return array:
00872                         return $fieldList;
00873                 }
00874         }
00875 
00887         function getTCAtypeValue($table,$rec)   {
00888                 global $TCA;
00889 
00890                         // If no field-value, set it to zero. If there is no type matching the field-value (which now may be zero...) test field-value '1' as default.
00891                 t3lib_div::loadTCA($table);
00892                 if ($TCA[$table])       {
00893                         $field = $TCA[$table]['ctrl']['type'];
00894                         $fieldValue = $field ? ($rec[$field] ? $rec[$field] : 0) : 0;
00895                         if (!is_array($TCA[$table]['types'][$fieldValue]))      $fieldValue = 1;
00896                         return $fieldValue;
00897                 }
00898         }
00899 
00910         function getSpecConfParts($str, $defaultExtras) {
00911 
00912                         // Add defaultExtras:
00913                 $specConfParts = t3lib_div::trimExplode(':', $defaultExtras.':'.$str, 1);
00914 
00915                 $reg = array();
00916                 if (count($specConfParts))      {
00917                         foreach($specConfParts as $k2 => $v2)   {
00918                                 unset($specConfParts[$k2]);
00919                                 if (ereg('(.*)\[(.*)\]',$v2,$reg))      {
00920                                         $specConfParts[trim($reg[1])] = array(
00921                                                 'parameters' => t3lib_div::trimExplode('|', $reg[2], 1)
00922                                         );
00923                                 } else {
00924                                         $specConfParts[trim($v2)] = 1;
00925                                 }
00926                         }
00927                 } else {
00928                         $specConfParts = array();
00929                 }
00930                 return $specConfParts;
00931         }
00932 
00941         function getSpecConfParametersFromArray($pArr)  {
00942                 $out=array();
00943                 if (is_array($pArr))    {
00944                         reset($pArr);
00945                         while(list($k,$v)=each($pArr))  {
00946                                 $parts=explode('=',$v,2);
00947                                 if (count($parts)==2)   {
00948                                         $out[trim($parts[0])]=trim($parts[1]);
00949                                 } else {
00950                                         $out[$k]=$v;
00951                                 }
00952                         }
00953                 }
00954                 return $out;
00955         }
00956 
00969         function getFlexFormDS($conf,$row,$table,$fieldName='',$WSOL=TRUE)      {
00970                 global $TYPO3_CONF_VARS;
00971 
00972                         // Get pointer field etc from TCA-config:
00973                 $ds_pointerField =      $conf['ds_pointerField'];
00974                 $ds_array =             $conf['ds'];
00975                 $ds_tableField =        $conf['ds_tableField'];
00976                 $ds_searchParentField =         $conf['ds_pointerField_searchParent'];
00977 
00978                         // Find source value:
00979                 $dataStructArray='';
00980                 if (is_array($ds_array))        {       // If there is a data source array, that takes precedence
00981                                 // If a pointer field is set, take the value from that field in the $row array and use as key.
00982                         if ($ds_pointerField)   {
00983                                 $srcPointer = $row[$ds_pointerField];
00984                                 $srcPointer = isset($ds_array[$srcPointer]) ? $srcPointer : 'default';
00985                         } else $srcPointer='default';
00986 
00987                                 // Get Data Source: Detect if it's a file reference and in that case read the file and parse as XML. Otherwise the value is expected to be XML.
00988                         if (substr($ds_array[$srcPointer],0,5)=='FILE:')        {
00989                                 $file = t3lib_div::getFileAbsFileName(substr($ds_array[$srcPointer],5));
00990                                 if ($file && @is_file($file))   {
00991                                         $dataStructArray = t3lib_div::xml2array(t3lib_div::getUrl($file));
00992                                 } else $dataStructArray = 'The file "'.substr($ds_array[$srcPointer],5).'" in ds-array key "'.$srcPointer.'" was not found ("'.$file.'")';      // Error message.
00993                         } else {
00994                                 $dataStructArray = t3lib_div::xml2array($ds_array[$srcPointer]);
00995                         }
00996 
00997                 } elseif ($ds_pointerField) {   // If pointer field AND possibly a table/field is set:
00998                                 // Value of field pointed to:
00999                         $srcPointer = $row[$ds_pointerField];
01000 
01001                                 // Searching recursively back if 'ds_pointerField_searchParent' is defined (typ. a page rootline, or maybe a tree-table):
01002                         if ($ds_searchParentField && !$srcPointer)      {
01003                                 $rr = t3lib_BEfunc::getRecord($table,$row['uid'],'uid,'.$ds_searchParentField); // Get the "pid" field - we cannot know that it is in the input record!
01004                                 if ($WSOL)      {
01005                                         t3lib_BEfunc::workspaceOL($table,$rr);
01006                                         t3lib_BEfunc::fixVersioningPid($table,$rr,TRUE);        // Added "TRUE" 23/03/06 before 4.0. (Also to similar call below!).  Reason: When t3lib_refindex is scanning the system in Live workspace all Pages with FlexForms will not find their inherited datastructure. Thus all references from workspaces are removed! Setting TRUE means that versioning PID doesn't check workspace of the record. I can't see that this should give problems anywhere. See more information inside t3lib_refindex!
01007                                 }
01008                                 $uidAcc=array();        // Used to avoid looping, if any should happen.
01009                                 $subFieldPointer = $conf['ds_pointerField_searchParent_subField'];
01010                                 while(!$srcPointer)             {
01011                                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
01012                                                                         'uid,'.$ds_pointerField.','.$ds_searchParentField.($subFieldPointer?','.$subFieldPointer:''),
01013                                                                         $table,
01014                                                                         'uid='.intval($rr[$ds_searchParentField]).t3lib_BEfunc::deleteClause($table)
01015                                                                 );
01016                                         $rr = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
01017 
01018                                                 // break if no result from SQL db or if looping...
01019                                         if (!is_array($rr) || isset($uidAcc[$rr['uid']]))       break;
01020                                         $uidAcc[$rr['uid']]=1;
01021 
01022                                         if ($WSOL)      {
01023                                                 t3lib_BEfunc::workspaceOL($table,$rr);
01024                                                 t3lib_BEfunc::fixVersioningPid($table,$rr,TRUE);
01025                                         }
01026                                         $srcPointer = ($subFieldPointer && $rr[$subFieldPointer]) ? $rr[$subFieldPointer] : $rr[$ds_pointerField];
01027                                 }
01028                         }
01029 
01030                                 // If there is a srcPointer value:
01031                         if ($srcPointer)        {
01032                                 if (t3lib_div::testInt($srcPointer))    {       // If integer, then its a record we will look up:
01033                                         list($tName,$fName) = explode(':',$ds_tableField,2);
01034                                         if ($tName && $fName && is_array($GLOBALS['TCA'][$tName]))      {
01035                                                 $dataStructRec = t3lib_BEfunc::getRecord($tName, $srcPointer);
01036                                                 if ($WSOL)      {
01037                                                         t3lib_BEfunc::workspaceOL($tName,$dataStructRec);
01038                                                 }
01039                                                 $dataStructArray = t3lib_div::xml2array($dataStructRec[$fName]);
01040                                         } else $dataStructArray = 'No tablename ('.$tName.') or fieldname ('.$fName.') was found an valid!';
01041                                 } else {        // Otherwise expect it to be a file:
01042                                         $file = t3lib_div::getFileAbsFileName($srcPointer);
01043                                         if ($file && @is_file($file))   {
01044                                                 $dataStructArray = t3lib_div::xml2array(t3lib_div::getUrl($file));
01045                                         } else $dataStructArray='The file "'.$srcPointer.'" was not found ("'.$file.'")';       // Error message.
01046                                 }
01047                         } else $dataStructArray='No source value in fieldname "'.$ds_pointerField.'"';  // Error message.
01048                 } else $dataStructArray='No proper configuration!';
01049 
01050                         // Hook for post-processing the Flexform DS. Introduces the possibility to configure Flexforms via TSConfig
01051                 if (is_array ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_befunc.php']['getFlexFormDSClass'])) {
01052                         foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_befunc.php']['getFlexFormDSClass'] as $classRef) {
01053                                 $hookObj = &t3lib_div::getUserObj($classRef);
01054                                 if (method_exists($hookObj, 'getFlexFormDS_postProcessDS')) {
01055                                         $hookObj->getFlexFormDS_postProcessDS($dataStructArray, $conf, $row, $table, $fieldName);
01056                                 }
01057                         }
01058                 }
01059 
01060                 return $dataStructArray;
01061         }
01062 
01063 
01064 
01065 
01066 
01067 
01068 
01069 
01070 
01071 
01072 
01073 
01074 
01075 
01076 
01077 
01078 
01079 
01080         /*******************************************
01081          *
01082          * Caching related
01083          *
01084          *******************************************/
01085 
01096         function storeHash($hash,$data,$ident)  {
01097                 $insertFields = array(
01098                         'hash' => $hash,
01099                         'content' => $data,
01100                         'ident' => $ident,
01101                         'tstamp' => time()
01102                 );
01103                 $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash'));
01104                 $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_hash', $insertFields);
01105         }
01106 
01116         function getHash($hash,$expTime=0)      {
01117                         // if expTime is not set, the hash will never expire
01118                 $expTime = intval($expTime);
01119                 if ($expTime)   {
01120                         $whereAdd = ' AND tstamp > '.(time()-$expTime);
01121                 }
01122                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('content', 'cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash').$whereAdd);
01123                 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
01124                         return $row['content'];
01125                 }
01126         }
01127 
01128 
01129 
01130 
01131 
01132 
01133 
01134 
01135         /*******************************************
01136          *
01137          * TypoScript related
01138          *
01139          *******************************************/
01140 
01152         function getPagesTSconfig($id,$rootLine='',$returnPartArray=0)  {
01153                 $id=intval($id);
01154                 if (!is_array($rootLine))       {
01155                         $rootLine = t3lib_BEfunc::BEgetRootLine($id,'',TRUE);
01156                 }
01157                 ksort($rootLine);       // Order correctly
01158                 $TSdataArray = array();
01159                 $TSdataArray['defaultPageTSconfig']=$GLOBALS['TYPO3_CONF_VARS']['BE']['defaultPageTSconfig'];   // Setting default configuration:
01160                 foreach($rootLine as $k => $v)  {
01161                         $TSdataArray['uid_'.$v['uid']]=$v['TSconfig'];
01162                 }
01163                 $TSdataArray = t3lib_TSparser::checkIncludeLines_array($TSdataArray);
01164                 if ($returnPartArray)   {
01165                         return $TSdataArray;
01166                 }
01167 
01168                         // Parsing the user TS (or getting from cache)
01169                 $userTS = implode($TSdataArray,chr(10).'[GLOBAL]'.chr(10));
01170                 $hash = md5('pageTS:'.$userTS);
01171                 $cachedContent = t3lib_BEfunc::getHash($hash,0);
01172                 $TSconfig = array();
01173                 if (isset($cachedContent))      {
01174                         $TSconfig = unserialize($cachedContent);
01175                 } else {
01176                         $parseObj = t3lib_div::makeInstance('t3lib_TSparser');
01177                         $parseObj->parse($userTS);
01178                         $TSconfig = $parseObj->setup;
01179                         t3lib_BEfunc::storeHash($hash,serialize($TSconfig),'PAGES_TSconfig');
01180                 }
01181 
01182                         // get User TSconfig overlay
01183                 $userTSconfig = $GLOBALS['BE_USER']->userTS['page.'];
01184                 if (is_array($userTSconfig))    {
01185                         $TSconfig = t3lib_div::array_merge_recursive_overrule($TSconfig, $userTSconfig);
01186                 }
01187                 return $TSconfig;
01188         }
01189 
01208         function updatePagesTSconfig($id,$pageTS,$TSconfPrefix,$impParams='')   {
01209                 $id=intval($id);
01210                 if (is_array($pageTS) && $id>0) {
01211                         if (!is_array($impParams))      {
01212                                 $impParams =t3lib_BEfunc::implodeTSParams(t3lib_BEfunc::getPagesTSconfig($id));
01213                         }
01214                         reset($pageTS);
01215                         $set=array();
01216                         while(list($f,$v)=each($pageTS))        {
01217                                 $f = $TSconfPrefix.$f;
01218                                 if ((!isset($impParams[$f])&&trim($v)) || strcmp(trim($impParams[$f]),trim($v)))        {
01219                                         $set[$f]=trim($v);
01220                                 }
01221                         }
01222                         if (count($set))        {
01223                                         // Get page record and TS config lines
01224                                 $pRec = t3lib_befunc::getRecord('pages',$id);
01225                                 $TSlines = explode(chr(10),$pRec['TSconfig']);
01226                                 $TSlines = array_reverse($TSlines);
01227                                         // Reset the set of changes.
01228                                 reset($set);
01229                                 while(list($f,$v)=each($set))   {
01230                                         reset($TSlines);
01231                                         $inserted=0;
01232                                         while(list($ki,$kv)=each($TSlines))     {
01233                                                 if (substr($kv,0,strlen($f)+1)==$f.'=') {
01234                                                         $TSlines[$ki]=$f.'='.$v;
01235                                                         $inserted=1;
01236                                                         break;
01237                                                 }
01238                                         }
01239                                         if (!$inserted) {
01240                                                 $TSlines = array_reverse($TSlines);
01241                                                 $TSlines[]=$f.'='.$v;
01242                                                 $TSlines = array_reverse($TSlines);
01243                                         }
01244                                 }
01245                                 $TSlines = array_reverse($TSlines);
01246 
01247                                         // store those changes
01248                                 $TSconf = implode(chr(10),$TSlines);
01249 
01250                                 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', 'uid='.intval($id), array('TSconfig' => $TSconf));
01251                         }
01252                 }
01253         }
01254 
01263         function implodeTSParams($p,$k='')      {
01264                 $implodeParams=array();
01265                 if (is_array($p))       {
01266                         reset($p);
01267                         while(list($kb,$val)=each($p))  {
01268                                 if (is_array($val))     {
01269                                         $implodeParams = array_merge($implodeParams,t3lib_BEfunc::implodeTSParams($val,$k.$kb));
01270                                 } else {
01271                                         $implodeParams[$k.$kb]=$val;
01272                                 }
01273                         }
01274                 }
01275                 return $implodeParams;
01276         }
01277 
01278 
01279 
01280 
01281 
01282 
01283 
01284 
01285         /*******************************************
01286          *
01287          * Users / Groups related
01288          *
01289          *******************************************/
01290 
01300         function getUserNames($fields='username,usergroup,usergroup_cached_list,uid',$where='') {
01301                 $be_user_Array=Array();
01302 
01303                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, 'be_users', 'pid=0 '.$where.t3lib_BEfunc::deleteClause('be_users'), '', 'username');
01304                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
01305                         $be_user_Array[$row['uid']]=$row;
01306                 }
01307                 return $be_user_Array;
01308         }
01309 
01318         function getGroupNames($fields='title,uid', $where='')  {
01319                 $be_group_Array = Array();
01320                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, 'be_groups', 'pid=0 '.$where.t3lib_BEfunc::deleteClause('be_groups'), '', 'title');
01321                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
01322                         $be_group_Array[$row['uid']] = $row;
01323                 }
01324                 return $be_group_Array;
01325         }
01326 
01335         function getListGroupNames($fields='title,uid') {
01336                 $exQ=' AND hide_in_lists=0';
01337                 if (!$GLOBALS['BE_USER']->isAdmin())    {
01338                         $exQ.=' AND uid IN ('.($GLOBALS['BE_USER']->user['usergroup_cached_list']?$GLOBALS['BE_USER']->user['usergroup_cached_list']:0).')';
01339                 }
01340                 return t3lib_BEfunc::getGroupNames($fields,$exQ);
01341         }
01342 
01354         function blindUserNames($usernames,$groupArray,$excludeBlindedFlag=0)   {
01355                 if (is_array($usernames) && is_array($groupArray))      {
01356                         while(list($uid,$row)=each($usernames)) {
01357                                 $userN=$uid;
01358                                 $set=0;
01359                                 if ($row['uid']!=$GLOBALS['BE_USER']->user['uid'])      {
01360                                         reset($groupArray);
01361                                         while(list(,$v)=each($groupArray))      {
01362                                                 if ($v && t3lib_div::inList($row['usergroup_cached_list'],$v))  {
01363                                                         $userN = $row['username'];
01364                                                         $set=1;
01365                                                 }
01366                                         }
01367                                 } else {
01368                                         $userN = $row['username'];
01369                                         $set=1;
01370                                 }
01371                                 $usernames[$uid]['username']=$userN;
01372                                 if ($excludeBlindedFlag && !$set) {unset($usernames[$uid]);}
01373                         }
01374                 }
01375                 return $usernames;
01376         }
01377 
01387         function blindGroupNames($groups,$groupArray,$excludeBlindedFlag=0)     {
01388                 if (is_array($groups) && is_array($groupArray)) {
01389                         while(list($uid,$row)=each($groups))    {
01390                                 $groupN=$uid;
01391                                 $set=0;
01392                                 if (t3lib_div::inArray($groupArray,$uid))       {
01393                                         $groupN=$row['title'];
01394                                         $set=1;
01395                                 }
01396                                 $groups[$uid]['title']=$groupN;
01397                                 if ($excludeBlindedFlag && !$set) {unset($groups[$uid]);}
01398                         }
01399                 }
01400                 return $groups;
01401         }
01402 
01403 
01404 
01405 
01406 
01407 
01408 
01409 
01410 
01411 
01412 
01413 
01414 
01415         /*******************************************
01416          *
01417          * Output related
01418          *
01419          *******************************************/
01420 
01428         function daysUntil($tstamp)     {
01429                 $delta_t = $tstamp-$GLOBALS['EXEC_TIME'];
01430                 return ceil($delta_t/(3600*24));
01431         }
01432 
01440         function date($tstamp)  {
01441                 return date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],$tstamp);
01442         }
01443 
01451         function datetime($value)       {
01452                 return date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'].' '.$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'], $value);
01453         }
01454 
01463         function time($value)   {
01464                 $hh = floor($value/3600);
01465                 $min = floor(($value-$hh*3600)/60);
01466                 $sec = $value-$hh*3600-$min*60;
01467                 $l = sprintf('%02d',$hh).':'.sprintf('%02d',$min).':'.sprintf('%02d',$sec);
01468                 return $l;
01469         }
01470 
01479         function calcAge($seconds,$labels = 'min|hrs|days|yrs') {
01480                 $labelArr = explode('|',$labels);
01481                 $prefix='';
01482                 if ($seconds<0) {$prefix='-'; $seconds=abs($seconds);}
01483                 if ($seconds<3600)      {
01484                         $seconds = round ($seconds/60).' '.trim($labelArr[0]);
01485                 } elseif ($seconds<24*3600)     {
01486                         $seconds = round ($seconds/3600).' '.trim($labelArr[1]);
01487                 } elseif ($seconds<365*24*3600) {
01488                         $seconds = round ($seconds/(24*3600)).' '.trim($labelArr[2]);
01489                 } else {
01490                         $seconds = round ($seconds/(365*24*3600)).' '.trim($labelArr[3]);
01491                 }
01492                 return $prefix.$seconds;
01493         }
01494 
01505         function dateTimeAge($tstamp,$prefix=1,$date='')        {
01506                 return $tstamp ?
01507                                 ($date=='date' ? t3lib_BEfunc::date($tstamp) : t3lib_BEfunc::datetime($tstamp)).
01508                                 ' ('.t3lib_BEfunc::calcAge($prefix*(time()-$tstamp),$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.minutesHoursDaysYears')).')' : '';
01509         }
01510 
01523         function titleAttrib($content='',$hsc=0)        {
01524                 global $CLIENT;
01525                 $attrib= ($CLIENT['BROWSER']=='net'&&$CLIENT['VERSION']<5)||$CLIENT['BROWSER']=='konqu' ? 'alt' : 'title';
01526                 return strcmp($content,'')?' '.$attrib.'="'.($hsc?htmlspecialchars($content):$content).'"' : $attrib;
01527         }
01528