00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00090 class t3lib_admin {
00091 var $genTree_includeDeleted = 1;
00092 var $perms_clause='';
00093 var $genTree_makeHTML = 0;
00094
00095 var $genTree_idlist = '';
00096 var $getTree_HTML = '';
00097 var $backPath='';
00098
00099
00100 var $checkFileRefs = Array();
00101 var $checkSelectDBRefs = Array();
00102 var $checkGroupDBRefs = Array();
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']) {
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 $cl_fl = implode ('!="" OR ',$fieldArr). '!=""';
00330 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,'.$field_list, $table, $cl_fl);
00331 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
00332 reset($fieldArr);
00333 while (list(,$field)=each($fieldArr)) {
00334 if (trim($row[$field])) {
00335 $fieldConf = $TCA[$table]['columns'][$field]['config'];
00336 if ($fieldConf['type']=='group') {
00337 if ($fieldConf['internal_type']=='file') {
00338
00339 if ($fieldConf['MM']) {
00340 $tempArr=array();
00341 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00342 $dbAnalysis->start('','files',$fieldConf['MM'],$row['uid']);
00343 reset($dbAnalysis->itemArray);
00344 while (list($somekey,$someval)=each($dbAnalysis->itemArray)) {
00345 if ($someval['id']) {
00346 $tempArr[]=$someval['id'];
00347 }
00348 }
00349 } else {
00350 $tempArr = explode(',',trim($row[$field]));
00351 }
00352 reset($tempArr);
00353 while (list(,$file)=each($tempArr)) {
00354 $file = trim($file);
00355 if ($file) {
00356 $this->checkFileRefs[$fieldConf['uploadfolder']][$file]+=1;
00357 }
00358 }
00359 }
00360 if ($fieldConf['internal_type']=='db') {
00361
00362 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00363 $dbAnalysis->start($row[$field],$fieldConf['allowed'],$fieldConf['MM'],$row['uid']);
00364 reset($dbAnalysis->itemArray);
00365 while (list(,$tempArr)=each($dbAnalysis->itemArray)) {
00366 $this->checkGroupDBRefs[$tempArr['table']][$tempArr['id']]+=1;
00367 }
00368 }
00369 }
00370 if ($fieldConf['type']=='select' && $fieldConf['foreign_table']) {
00371
00372 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00373 $dbAnalysis->start($row[$field],$fieldConf['foreign_table'],$fieldConf['MM'],$row['uid']);
00374 reset($dbAnalysis->itemArray);
00375 while (list(,$tempArr)=each($dbAnalysis->itemArray)) {
00376 if ($tempArr['id']>0) {
00377 $this->checkGroupDBRefs[$fieldConf['foreign_table']][$tempArr['id']]+=1;
00378 }
00379 }
00380 }
00381 }
00382 }
00383 }
00384 }
00385 }
00386 }
00387 }
00388
00394 function testFileRefs () {
00395 $output=Array();
00396 reset($this->checkFileRefs);
00397 while(list($folder,$fileArr)=each($this->checkFileRefs)) {
00398 $path = PATH_site.$folder;
00399 if (@is_dir($path)) {
00400 $d = dir($path);
00401 while($entry=$d->read()) {
00402 if (@is_file($path.'/'.$entry)) {
00403 if (isset($fileArr[$entry])) {
00404 if ($fileArr[$entry] > 1) {
00405 $temp = $this->whereIsFileReferenced($folder,$entry);
00406 $tempList = '';
00407 while(list(,$inf)=each($temp)) {
00408 $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
00409 }
00410 $output['moreReferences'][] = Array($path,$entry,$fileArr[$entry],$tempList);
00411 }
00412 unset($fileArr[$entry]);
00413 } else {
00414 if (!strstr($entry,'index.htm')) {
00415 $output['noReferences'][] = Array($path,$entry);
00416 }
00417 }
00418 }
00419 }
00420 $d->close();
00421 reset($fileArr);
00422 $tempCounter=0;
00423 while(list($file,)=each($fileArr)) {
00424 $temp = $this->whereIsFileReferenced($folder,$file);
00425 $tempList = '';
00426 while(list(,$inf)=each($temp)) {
00427 $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
00428 }
00429 $tempCounter++;
00430 $output['noFile'][substr($path,-3).'_'.substr($file,0,3).'_'.$tempCounter] = Array($path,$file,$tempList);
00431 }
00432 } else {
00433 $output['error'][] = Array($path);
00434 }
00435 }
00436 return $output;
00437 }
00438
00445 function testDBRefs($theArray) {
00446 global $TCA;
00447 reset($theArray);
00448 while(list($table,$dbArr)=each($theArray)) {
00449 if ($TCA[$table]) {
00450 $idlist = Array();
00451 while(list($id,)=each($dbArr)) {
00452 $idlist[]=$id;
00453 }
00454 $theList = implode(',',$idlist);
00455 if ($theList) {
00456 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, 'uid IN ('.$theList.')'.t3lib_BEfunc::deleteClause($table));
00457 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
00458 if (isset($dbArr[$row['uid']])) {
00459 unset ($dbArr[$row['uid']]);
00460 } else {
00461 $result.='Strange Error. ...<br />';
00462 }
00463 }
00464 reset($dbArr);
00465 while (list($theId,$theC)=each($dbArr)) {
00466 $result.='There are '.$theC.' records pointing to this missing or deleted record; ['.$table.']['.$theId.']<br />';
00467 }
00468 }
00469 } else {
00470 $result.='Codeerror. Table is not a table...<br />';
00471 }
00472 }
00473 return $result;
00474 }
00475
00483 function whereIsRecordReferenced($searchTable,$id) {
00484 global $TCA;
00485 $fileFields = $this->getDBFields($searchTable);
00486 $theRecordList=Array();
00487 while (list(,$info)=each($fileFields)) {
00488 $table=$info[0]; $field=$info[1];
00489 t3lib_div::loadTCA($table);
00490 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00491 'uid,pid,'.$TCA[$table]['ctrl']['label'].','.$field,
00492 $table,
00493 $field.' LIKE "%'.$GLOBALS['TYPO3_DB']->quoteStr($id, $table).'%"'
00494 );
00495 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
00496
00497 $fieldConf = $TCA[$table]['columns'][$field]['config'];
00498 $allowedTables = ($fieldConf['type']=='group') ? $fieldConf['allowed'] : $fieldConf['foreign_table'];
00499
00500 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00501 $dbAnalysis->start($row[$field],$allowedTables,$fieldConf['MM'],$row['uid']);
00502 reset($dbAnalysis->itemArray);
00503 while (list(,$tempArr)=each($dbAnalysis->itemArray)) {
00504 if ($tempArr['table']==$searchTable && $tempArr['id']==$id) {
00505 $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);
00506 }
00507 }
00508 }
00509 }
00510 return $theRecordList;
00511 }
00512
00520 function whereIsFileReferenced($uploadfolder,$filename) {
00521 global $TCA;
00522 $fileFields = $this->getFileFields($uploadfolder);
00523 $theRecordList=Array();
00524 while (list(,$info)=each($fileFields)) {
00525 $table=$info[0]; $field=$info[1];
00526 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00527 'uid,pid,'.$TCA[$table]['ctrl']['label'].','.$field,
00528 $table,
00529 $field.' LIKE "%'.$GLOBALS['TYPO3_DB']->quoteStr($filename, $table).'%"'
00530 );
00531 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
00532
00533 $tempArr = explode(',',trim($row[$field]));
00534 while (list(,$file)=each($tempArr)) {
00535 $file = trim($file);
00536 if ($file==$filename) {
00537 $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);
00538 }
00539 }
00540 }
00541 }
00542 return $theRecordList;
00543 }
00544 }
00545
00546
00547 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_admin.php']) {
00548 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_admin.php']);
00549 }
00550 ?>