Documentation TYPO3 par Ameos

class.t3lib_admin.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2005 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 ***************************************************************/
00090 class t3lib_admin {
00091         var $genTree_includeDeleted = 1;        // if set, genTree() includes deleted pages. This is default.
00092         var $perms_clause='';           // extra where-clauses for the tree-selection
00093         var $genTree_makeHTML = 0;                      // if set, genTree() generates HTML, that visualizes the tree.
00094                 // internal
00095         var $genTree_idlist = '';       // Will hold the id-list from genTree()
00096         var $getTree_HTML = '';         // Will hold the HTML-code visualising the tree. genTree()
00097         var $backPath='';
00098 
00099                 // internal
00100         var $checkFileRefs = Array();
00101         var $checkSelectDBRefs = Array();       // From the select-fields
00102         var $checkGroupDBRefs = Array();        // From the group-fields
00103 
00104         var $page_idArray=Array();
00105         var $recStat = Array();
00106         var $lRecords = Array();
00107         var $lostPagesList = '';
00108 
00116         function genTree($theID, $depthData)    {
00117                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00118                                         'uid,title,doktype,deleted'.(t3lib_extMgm::isLoaded('cms')?',hidden':''),
00119                                         'pages',
00120                                         'pid='.intval($theID).' '.((!$this->genTree_includeDeleted)?'AND deleted=0':'').$this->perms_clause,
00121                                         '',
00122                                         'sorting'
00123                                 );
00124                 $a=0;
00125                 $c = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
00126                 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))      {
00127                         $a++;
00128                         $newID =$row['uid'];
00129                         if ($this->genTree_makeHTML)    {
00130                                 $this->genTree_HTML.=chr(10).'<div><span class="nobr">';
00131                                 $PM = 'join';
00132                                 $LN = ($a==$c)?'blank':'line';
00133                                 $BTM = ($a==$c)?'bottom':'';
00134                                 $this->genTree_HTML.= $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" alt="" />'.t3lib_iconWorks::getIconImage('pages',$row,$this->backPath,'align="top"').htmlspecialchars($row['uid'].': '.t3lib_div::fixed_lgd_cs(strip_tags($row['title']),50)).'</span></div>';
00135                         }
00136 
00137                         if (isset($page_idlist[$newID]))        {
00138                                 $this->recStat['doublePageID'][]=$newID;
00139                         }
00140                         $this->page_idArray[$newID]=$newID;
00141                         if ($row['deleted']) {$this->recStat['deleted']++;}
00142                         if ($row['hidden']) {$this->recStat['hidden']++;}
00143                         $this->recStat['doktype'][$row['doktype']]++;
00144 
00145                         $this->genTree($newID,$this->genTree_HTML ? $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' align="top" alt="" />'  : '');
00146                 }
00147                 return $GLOBALS['TYPO3_DB']->sql_num_rows($res);
00148         }
00149 
00156         function lostRecords($pid_list) {
00157                 global $TCA;
00158                 reset($TCA);
00159                 $this->lostPagesList='';
00160                 if ($pid_list)  {
00161                         while (list($table)=each($TCA)) {
00162                                 t3lib_div::loadTCA($table);
00163                                 $garbage = $GLOBALS['TYPO3_DB']->exec_SELECTquery (
00164                                                                 'uid,pid,'.$TCA[$table]['ctrl']['label'],
00165                                                                 $table,
00166                                                                 'pid NOT IN ('.$pid_list.')'
00167                                                         );
00168                                 $lostIdList=Array();
00169                                 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($garbage))  {
00170                                         $this->lRecords[$table][$row['uid']]=Array('uid'=>$row['uid'], 'pid'=>$row['pid'], 'title'=> strip_tags($row[$TCA[$table]['ctrl']['label']]) );
00171                                         $lostIdList[]=$row['uid'];
00172                                 }
00173                                 if ($table=='pages')    {
00174                                         $this->lostPagesList=implode(',',$lostIdList);
00175                                 }
00176                         }
00177                 }
00178         }
00179 
00187         function fixLostRecord($table,$uid)     {
00188                 if ($table && $GLOBALS['TCA'][$table] && $uid && is_array($this->lRecords[$table][$uid]) && $GLOBALS['BE_USER']->user['admin']) {
00189 
00190                         $updateFields = array();
00191                         $updateFields['pid'] = 0;
00192                         if ($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'])       {       // If possible a lost record restored is hidden as default
00193                                 $updateFields[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled']] = 1;
00194                         }
00195 
00196                         $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
00197 
00198                         return TRUE;
00199                 } else return FALSE;
00200         }
00201 
00208         function countRecords($pid_list)        {
00209                 global $TCA;
00210                 reset($TCA);
00211                 $list=Array();
00212                 $list_n=Array();
00213                 if ($pid_list)  {
00214                         while (list($table)=each($TCA)) {
00215                                 t3lib_div::loadTCA($table);
00216                                 $count = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, 'pid IN ('.$pid_list.')');
00217                                 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($count)) {
00218                                         $list[$table]=$row[0];
00219                                 }
00220 
00221                                 $count = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, 'pid IN ('.$pid_list.')'.t3lib_BEfunc::deleteClause($table));
00222                                 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($count)) {
00223                                         $list_n[$table]=$row[0];
00224                                 }
00225                         }
00226                 }
00227                 return array('all' => $list, 'non_deleted' => $list_n);
00228         }
00229 
00236         function getGroupFields($mode)  {
00237                 global $TCA;
00238                 reset ($TCA);
00239                 $result = Array();
00240                 while (list($table)=each($TCA)) {
00241                         t3lib_div::loadTCA($table);
00242                         $cols = $TCA[$table]['columns'];
00243                         reset ($cols);
00244                         while (list($field,$config)=each($cols))        {
00245                                 if ($config['config']['type']=='group') {
00246                                         if (
00247                                                 ((!$mode||$mode=='file') && $config['config']['internal_type']=='file') ||
00248                                                 ((!$mode||$mode=='db') && $config['config']['internal_type']=='db')
00249                                                 )       {
00250                                                 $result[$table][]=$field;
00251                                         }
00252                                 }
00253                                 if ( (!$mode||$mode=='db') && $config['config']['type']=='select' && $config['config']['foreign_table'])        {
00254                                         $result[$table][]=$field;
00255                                 }
00256                         }
00257                         if ($result[$table])    {
00258                                 $result[$table] = implode(',',$result[$table]);
00259                         }
00260                 }
00261                 return $result;
00262         }
00263 
00270         function getFileFields($uploadfolder)   {
00271                 global $TCA;
00272                 reset ($TCA);
00273                 $result = Array();
00274                 while (list($table)=each($TCA)) {
00275                         t3lib_div::loadTCA($table);
00276                         $cols = $TCA[$table]['columns'];
00277                         reset ($cols);
00278                         while (list($field,$config)=each($cols))        {
00279                                 if ($config['config']['type']=='group' && $config['config']['internal_type']=='file' && $config['config']['uploadfolder']==$uploadfolder)       {
00280                                         $result[]=Array($table,$field);
00281                                 }
00282                         }
00283                 }
00284                 return $result;
00285         }
00286 
00293         function getDBFields($theSearchTable)   {
00294                 global $TCA;
00295                 $result = Array();
00296                 reset ($TCA);
00297                 while (list($table)=each($TCA)) {
00298                         t3lib_div::loadTCA($table);
00299                         $cols = $TCA[$table]['columns'];
00300                         reset ($cols);
00301                         while (list($field,$config)=each($cols))        {
00302                                 if ($config['config']['type']=='group' && $config['config']['internal_type']=='db')     {
00303                                         if (trim($config['config']['allowed'])=='*' || strstr($config['config']['allowed'],$theSearchTable))    {
00304                                                 $result[]=Array($table,$field);
00305                                         }
00306                                 } else if ($config['config']['type']=='select' && $config['config']['foreign_table']==$theSearchTable)  {
00307                                         $result[]=Array($table,$field);
00308                                 }
00309                         }
00310                 }
00311                 return $result;
00312         }
00313 
00321         function selectNonEmptyRecordsWithFkeys($fkey_arrays)   {
00322                 global $TCA;
00323                 if (is_array($fkey_arrays))     {
00324                         reset($fkey_arrays);
00325                         while (list($table,$field_list)=each($fkey_arrays))     {
00326                                 if ($TCA[$table] && trim($field_list))  {
00327                                         t3lib_div::loadTCA($table);
00328                                         $fieldArr = explode(',',$field_list);
00329 
00330                                         if(t3lib_extMgm::isLoaded('dbal')) {
00331                                                 $fields = $GLOBALS['TYPO3_DB']->admin_get_fields($table);
00332                                                 reset($fields);
00333                                                 list(,$field)=each($fieldArr);
00334                                                 $cl_fl = ($GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'],$table) == 'I' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'],$table) == 'N' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'],$table) == 'R') ?
00335                                                 $field.'!=0' : $field.'!=\'\'';
00336                                                 while (list(,$field)=each($fieldArr))   {
00337                                                         $cl_fl .= ($GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'],$table) == 'I' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'],$table) == 'N' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'],$table) == 'R') ?
00338                                                         ' OR '.$field.'!=0' : ' OR '.$field.'!=\'\'';
00339                                                 }
00340                                                 unset($fields);
00341                                         }
00342                                         else {
00343                                         $cl_fl = implode ('!="" OR ',$fieldArr). '!=""';
00344                                         }
00345 
00346                                         $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,'.$field_list, $table, $cl_fl);
00347                                         while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))     {
00348                                                 reset($fieldArr);
00349                                                 while (list(,$field)=each($fieldArr))   {
00350                                                         if (trim($row[$field]))         {
00351                                                                 $fieldConf = $TCA[$table]['columns'][$field]['config'];
00352                                                                 if ($fieldConf['type']=='group')        {
00353                                                                         if ($fieldConf['internal_type']=='file')        {
00354                                                                                 // files...
00355                                                                                 if ($fieldConf['MM'])   {
00356                                                                                         $tempArr=array();
00357                                                                                         $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00358                                                                                         $dbAnalysis->start('','files',$fieldConf['MM'],$row['uid']);
00359                                                                                         reset($dbAnalysis->itemArray);
00360                                                                                         while (list($somekey,$someval)=each($dbAnalysis->itemArray))    {
00361                                                                                                 if ($someval['id'])     {
00362                                                                                                         $tempArr[]=$someval['id'];
00363                                                                                                 }
00364                                                                                         }
00365                                                                                 } else {
00366                                                                                         $tempArr = explode(',',trim($row[$field]));
00367                                                                                 }
00368                                                                                 reset($tempArr);
00369                                                                                 while (list(,$file)=each($tempArr))     {
00370                                                                                         $file = trim($file);
00371                                                                                         if ($file)      {
00372                                                                                                 $this->checkFileRefs[$fieldConf['uploadfolder']][$file]+=1;
00373                                                                                         }
00374                                                                                 }
00375                                                                         }
00376                                                                         if ($fieldConf['internal_type']=='db')  {
00377                                                                                 // dbs - group
00378                                                                                 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00379                                                                                 $dbAnalysis->start($row[$field],$fieldConf['allowed'],$fieldConf['MM'],$row['uid']);
00380                                                                                 reset($dbAnalysis->itemArray);
00381                                                                                 while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
00382                                                                                         $this->checkGroupDBRefs[$tempArr['table']][$tempArr['id']]+=1;
00383                                                                                 }
00384                                                                         }
00385                                                                 }
00386                                                                 if ($fieldConf['type']=='select' && $fieldConf['foreign_table'])        {
00387                                                                         // dbs - select
00388                                                                         $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00389                                                                         $dbAnalysis->start($row[$field],$fieldConf['foreign_table'],$fieldConf['MM'],$row['uid']);
00390                                                                         reset($dbAnalysis->itemArray);
00391                                                                         while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
00392                                                                                 if ($tempArr['id']>0)   {
00393                                                                                         $this->checkGroupDBRefs[$fieldConf['foreign_table']][$tempArr['id']]+=1;
00394                                                                                 }
00395                                                                         }
00396                                                                 }
00397                                                         }
00398                                                 }
00399                                         }
00400                                 }
00401                         }
00402                 }
00403         }
00404 
00410         function testFileRefs ()        {
00411                 $output=Array();
00412                 reset($this->checkFileRefs);
00413                 while(list($folder,$fileArr)=each($this->checkFileRefs))        {
00414                         $path = PATH_site.$folder;
00415                         if (@is_dir($path))     {
00416                                 $d = dir($path);
00417                                 while($entry=$d->read()) {
00418                                         if (@is_file($path.'/'.$entry)) {
00419                                                 if (isset($fileArr[$entry]))    {
00420                                                         if ($fileArr[$entry] > 1)       {
00421                                                                 $temp = $this->whereIsFileReferenced($folder,$entry);
00422                                                                 $tempList = '';
00423                                                                 while(list(,$inf)=each($temp))  {
00424                                                                         $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
00425                                                                 }
00426                                                                 $output['moreReferences'][] = Array($path,$entry,$fileArr[$entry],$tempList);
00427                                                         }
00428                                                         unset($fileArr[$entry]);
00429                                                 } else {
00430                                                         if (!strstr($entry,'index.htm'))        {
00431                                                                 $output['noReferences'][] = Array($path,$entry);
00432                                                         }
00433                                                 }
00434                                         }
00435                                 }
00436                                 $d->close();
00437                                 reset($fileArr);
00438                                 $tempCounter=0;
00439                                 while(list($file,)=each($fileArr))      {
00440                                         $temp = $this->whereIsFileReferenced($folder,$file);
00441                                         $tempList = '';
00442                                         while(list(,$inf)=each($temp))  {
00443                                                 $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
00444                                         }
00445                                         $tempCounter++;
00446                                         $output['noFile'][substr($path,-3).'_'.substr($file,0,3).'_'.$tempCounter] = Array($path,$file,$tempList);
00447                                 }
00448                         } else {
00449                                 $output['error'][] = Array($path);
00450                         }
00451                 }
00452                 return $output;
00453         }
00454 
00461         function testDBRefs($theArray)  {
00462                 global $TCA;
00463                 reset($theArray);
00464                 while(list($table,$dbArr)=each($theArray))      {
00465                         if ($TCA[$table])       {
00466                                 $idlist = Array();
00467                                 while(list($id,)=each($dbArr))  {
00468                                         $idlist[]=$id;
00469                                 }
00470                                 $theList = implode(',',$idlist);
00471                                 if ($theList)   {
00472                                         $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, 'uid IN ('.$theList.')'.t3lib_BEfunc::deleteClause($table));
00473                                         while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))      {
00474                                                 if (isset($dbArr[$row['uid']])) {
00475                                                         unset ($dbArr[$row['uid']]);
00476                                                 } else {
00477                                                         $result.='Strange Error. ...<br />';
00478                                                 }
00479                                         }
00480                                         reset($dbArr);
00481                                         while (list($theId,$theC)=each($dbArr)) {
00482                                                 $result.='There are '.$theC.' records pointing to this missing or deleted record; ['.$table.']['.$theId.']<br />';
00483                                         }
00484                                 }
00485                         } else {
00486                                 $result.='Codeerror. Table is not a table...<br />';
00487                         }
00488                 }
00489                 return $result;
00490         }
00491 
00499         function whereIsRecordReferenced($searchTable,$id)      {
00500                 global $TCA;
00501                 $fileFields = $this->getDBFields($searchTable); // Gets tables / Fields that reference to files...
00502                 $theRecordList=Array();
00503                 while (list(,$info)=each($fileFields))  {
00504                         $table=$info[0];        $field=$info[1];
00505                         t3lib_div::loadTCA($table);
00506                         $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00507                                                         'uid,pid,'.$TCA[$table]['ctrl']['label'].','.$field,
00508                                                         $table,
00509                                                         $field.' LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($id, $table).'%\''
00510                                                 );
00511                         while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))     {
00512                                         // Now this is the field, where the reference COULD come from. But we're not garanteed, so we must carefully examine the data.
00513                                 $fieldConf = $TCA[$table]['columns'][$field]['config'];
00514                                 $allowedTables = ($fieldConf['type']=='group') ? $fieldConf['allowed'] : $fieldConf['foreign_table'];
00515 
00516                                 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00517                                 $dbAnalysis->start($row[$field],$allowedTables,$fieldConf['MM'],$row['uid']);
00518                                 reset($dbAnalysis->itemArray);
00519                                 while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
00520                                         if ($tempArr['table']==$searchTable && $tempArr['id']==$id)     {
00521                                                 $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);
00522                                         }
00523                                 }
00524                         }
00525                 }
00526                 return $theRecordList;
00527         }
00528 
00536         function whereIsFileReferenced($uploadfolder,$filename) {
00537                 global $TCA;
00538                 $fileFields = $this->getFileFields($uploadfolder);      // Gets tables / Fields that reference to files...
00539                 $theRecordList=Array();
00540                 while (list(,$info)=each($fileFields))  {
00541                         $table=$info[0];        $field=$info[1];
00542                         $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00543                                                         'uid,pid,'.$TCA[$table]['ctrl']['label'].','.$field,
00544                                                         $table,
00545                                                         $field.' LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($filename, $table).'%\''
00546                                                 );
00547                         while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))     {
00548                                 // Now this is the field, where the reference COULD come from. But we're not garanteed, so we must carefully examine the data.
00549                                 $tempArr = explode(',',trim($row[$field]));
00550                                 while (list(,$file)=each($tempArr))     {
00551                                         $file = trim($file);
00552                                         if ($file==$filename)   {
00553                                                 $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);
00554                                         }
00555                                 }
00556                         }
00557                 }
00558                 return $theRecordList;
00559         }
00560 }
00561 
00562 
00563 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_admin.php'])     {
00564         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_admin.php']);
00565 }
00566 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6