Fixed bug #15383: [Unit tests] Add tests for t3lib_div::validEmail
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_admin.php
index 8400393..a98855c 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -160,14 +160,14 @@ class t3lib_admin {
 
                                // Build HTML output:
                        if ($this->genTree_makeHTML)    {
-                               $this->genTree_HTML.=chr(10).'<div><span class="nobr">';
+                               $this->genTree_HTML.=LF.'<div><span class="nobr">';
                                $PM = 'join';
                                $LN = ($a==$c)?'blank':'line';
                                $BTM = ($a==$c)?'bottom':'';
                                $this->genTree_HTML.= $depthData.
                                        '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" alt="" />'.
                                        $versionLabel.
-                                       t3lib_iconWorks::getIconImage('pages',$row,$this->backPath,'align="top"').
+                                       t3lib_iconWorks::getSpriteIconForRecord('pages', $row) .
                                        htmlspecialchars($row['uid'].': '.t3lib_div::fixed_lgd_cs(strip_tags($row['title']),50)).'</span></div>';
                        }
 
@@ -250,14 +250,14 @@ class t3lib_admin {
 
                                // Build HTML output:
                        if ($this->genTree_makeHTML)    {
-                               $this->genTree_HTML.=chr(10).'<div><span class="nobr">';
+                               $this->genTree_HTML.=LF.'<div><span class="nobr">';
                                $PM = 'join';
                                $LN = ($a==$c)?'blank':'line';
                                $BTM = ($a==$c)?'bottom':'';
                                $this->genTree_HTML.= $depthData.
                                        '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" alt="" />'.
                                        $versionLabel.
-                                       t3lib_iconWorks::getIconImage($table,$row,$this->backPath,'align="top" title="'.$table.'"').htmlspecialchars($row['uid'].': '.t3lib_BEfunc::getRecordTitle($table,$row)).'</span></div>';
+                                       t3lib_iconWorks::getSpriteIconForRecord($table, $row, array('title'=> $table)) . htmlspecialchars($row['uid'] . ': ' . t3lib_BEfunc::getRecordTitle($table,$row)) . '</span></div>';
                        }
 
                                // Register various data for this item:
@@ -314,10 +314,9 @@ class t3lib_admin {
         */
        function lostRecords($pid_list) {
                global $TCA;
-               reset($TCA);
                $this->lostPagesList='';
                if ($pid_list)  {
-                       while (list($table)=each($TCA)) {
+                       foreach ($TCA as $table => $tableConf) {
                                t3lib_div::loadTCA($table);
 
                                $pid_list_tmp = $pid_list;
@@ -373,11 +372,10 @@ class t3lib_admin {
         */
        function countRecords($pid_list)        {
                global $TCA;
-               reset($TCA);
                $list=Array();
                $list_n=Array();
                if ($pid_list)  {
-                       while (list($table)=each($TCA)) {
+                       foreach ($TCA as $table => $tableConf) {
                                t3lib_div::loadTCA($table);
 
                                $pid_list_tmp = $pid_list;
@@ -386,14 +384,14 @@ class t3lib_admin {
                                        $pid_list_tmp = preg_replace('/^\-1,/','',$pid_list_tmp);
                                }
 
-                               $count = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, 'pid IN ('.$pid_list_tmp.')');
-                               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($count)) {
-                                       $list[$table]=$row[0];
+                               $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $table, 'pid IN ('.$pid_list_tmp.')');
+                               if ($count) {
+                                       $list[$table] = $count;
                                }
 
-                               $count = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, 'pid IN ('.$pid_list_tmp.')'.t3lib_BEfunc::deleteClause($table));
-                               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($count)) {
-                                       $list_n[$table]=$row[0];
+                               $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $table, 'pid IN ('.$pid_list_tmp.')' . t3lib_BEfunc::deleteClause($table));
+                               if ($count) {
+                                       $list_n[$table] = $count;
                                }
                        }
                }
@@ -408,13 +406,11 @@ class t3lib_admin {
         */
        function getGroupFields($mode)  {
                global $TCA;
-               reset ($TCA);
                $result = Array();
-               while (list($table)=each($TCA)) {
+               foreach ($TCA as $table => $tableConf) {
                        t3lib_div::loadTCA($table);
                        $cols = $TCA[$table]['columns'];
-                       reset ($cols);
-                       while (list($field,$config)=each($cols))        {
+                       foreach ($cols as $field => $config) {
                                if ($config['config']['type']=='group') {
                                        if (
                                                ((!$mode||$mode=='file') && $config['config']['internal_type']=='file') ||
@@ -442,13 +438,11 @@ class t3lib_admin {
         */
        function getFileFields($uploadfolder)   {
                global $TCA;
-               reset ($TCA);
                $result = Array();
-               while (list($table)=each($TCA)) {
+               foreach ($TCA as $table => $tableConf) {
                        t3lib_div::loadTCA($table);
                        $cols = $TCA[$table]['columns'];
-                       reset ($cols);
-                       while (list($field,$config)=each($cols))        {
+                       foreach ($cols as $field => $config) {
                                if ($config['config']['type']=='group' && $config['config']['internal_type']=='file' && $config['config']['uploadfolder']==$uploadfolder)       {
                                        $result[]=Array($table,$field);
                                }
@@ -467,11 +461,10 @@ class t3lib_admin {
                global $TCA;
                $result = Array();
                reset ($TCA);
-               while (list($table)=each($TCA)) {
+               foreach ($TCA as $table => $tableConf) {
                        t3lib_div::loadTCA($table);
                        $cols = $TCA[$table]['columns'];
-                       reset ($cols);
-                       while (list($field,$config)=each($cols))        {
+                       foreach ($cols as $field => $config) {
                                if ($config['config']['type']=='group' && $config['config']['internal_type']=='db')     {
                                        if (trim($config['config']['allowed'])=='*' || strstr($config['config']['allowed'],$theSearchTable))    {
                                                $result[]=Array($table,$field);
@@ -494,32 +487,29 @@ class t3lib_admin {
        function selectNonEmptyRecordsWithFkeys($fkey_arrays)   {
                global $TCA;
                if (is_array($fkey_arrays))     {
-                       reset($fkey_arrays);
-                       while (list($table,$field_list)=each($fkey_arrays))     {
+                       foreach ($fkey_arrays as $table => $field_list) {
                                if ($TCA[$table] && trim($field_list))  {
                                        t3lib_div::loadTCA($table);
                                        $fieldArr = explode(',',$field_list);
 
                                        if(t3lib_extMgm::isLoaded('dbal')) {
                                                $fields = $GLOBALS['TYPO3_DB']->admin_get_fields($table);
-                                               reset($fields);
-                                               list(,$field)=each($fieldArr);
+                                               $field = array_shift($fieldArr);
                                                $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') ?
                                                $field.'!=0' : $field.'!=\'\'';
-                                               while (list(,$field)=each($fieldArr))   {
+                                               foreach ($fieldArr as $field) {
                                                        $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') ?
                                                        ' OR '.$field.'!=0' : ' OR '.$field.'!=\'\'';
                                                }
                                                unset($fields);
                                        }
                                        else {
-                                       $cl_fl = implode ('!="" OR ',$fieldArr). '!=""';
+                                               $cl_fl = implode ('!=\'\' OR ',$fieldArr). '!=\'\'';
                                        }
 
                                        $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,'.$field_list, $table, $cl_fl);
                                        while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))     {
-                                               reset($fieldArr);
-                                               while (list(,$field)=each($fieldArr))   {
+                                               foreach ($fieldArr as $field) {
                                                        if (trim($row[$field]))         {
                                                                $fieldConf = $TCA[$table]['columns'][$field]['config'];
                                                                if ($fieldConf['type']=='group')        {
@@ -529,8 +519,7 @@ class t3lib_admin {
                                                                                        $tempArr=array();
                                                                                        $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
                                                                                        $dbAnalysis->start('','files',$fieldConf['MM'],$row['uid']);
-                                                                                       reset($dbAnalysis->itemArray);
-                                                                                       while (list($somekey,$someval)=each($dbAnalysis->itemArray))    {
+                                                                                       foreach ($dbAnalysis->itemArray as $somekey => $someval) {
                                                                                                if ($someval['id'])     {
                                                                                                        $tempArr[]=$someval['id'];
                                                                                                }
@@ -538,8 +527,7 @@ class t3lib_admin {
                                                                                } else {
                                                                                        $tempArr = explode(',',trim($row[$field]));
                                                                                }
-                                                                               reset($tempArr);
-                                                                               while (list(,$file)=each($tempArr))     {
+                                                                               foreach ($tempArr as $file) {
                                                                                        $file = trim($file);
                                                                                        if ($file)      {
                                                                                                $this->checkFileRefs[$fieldConf['uploadfolder']][$file]+=1;
@@ -550,8 +538,7 @@ class t3lib_admin {
                                                                                // dbs - group
                                                                                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
                                                                                $dbAnalysis->start($row[$field],$fieldConf['allowed'],$fieldConf['MM'],$row['uid'], $table, $fieldConf);
-                                                                               reset($dbAnalysis->itemArray);
-                                                                               while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
+                                                                               foreach ($dbAnalysis->itemArray as $tempArr) {
                                                                                        $this->checkGroupDBRefs[$tempArr['table']][$tempArr['id']]+=1;
                                                                                }
                                                                        }
@@ -560,8 +547,7 @@ class t3lib_admin {
                                                                        // dbs - select
                                                                        $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
                                                                        $dbAnalysis->start($row[$field],$fieldConf['foreign_table'],$fieldConf['MM'],$row['uid'], $table, $fieldConf);
-                                                                       reset($dbAnalysis->itemArray);
-                                                                       while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
+                                                                       foreach ($dbAnalysis->itemArray as $tempArr) {
                                                                                if ($tempArr['id']>0)   {
                                                                                        $this->checkGroupDBRefs[$fieldConf['foreign_table']][$tempArr['id']]+=1;
                                                                                }
@@ -570,6 +556,7 @@ class t3lib_admin {
                                                        }
                                                }
                                        }
+                                       $GLOBALS['TYPO3_DB']->sql_free_result($mres);
                                }
                        }
                }
@@ -582,8 +569,32 @@ class t3lib_admin {
         */
        function testFileRefs ()        {
                $output=Array();
-               reset($this->checkFileRefs);
-               while(list($folder,$fileArr)=each($this->checkFileRefs))        {
+                       // handle direct references with upload folder setting (workaround)
+               $newCheckFileRefs = array();
+               foreach ($this->checkFileRefs as $folder => $files) {
+                               // only direct references without a folder setting
+                       if ($folder !== '') {
+                               $newCheckFileRefs[$folder] = $files;
+                               continue;
+                       }
+
+                       foreach ($files as $file => $references) {
+
+                                       // direct file references have often many references (removes occurences in the moreReferences section of the result array)
+                               if ($references > 1) {
+                                       $references = 1;
+                               }
+
+                                       // the directory must be empty (prevents checking of the root directory)
+                               $directory = dirname($file);
+                               if ($directory !== '') {
+                                       $newCheckFileRefs[$directory][basename($file)] = $references;
+                               }
+                       }
+               }
+               $this->checkFileRefs = $newCheckFileRefs;
+
+               foreach ($this->checkFileRefs as $folder => $fileArr) {
                        $path = PATH_site.$folder;
                        if (@is_dir($path))     {
                                $d = dir($path);
@@ -593,26 +604,32 @@ class t3lib_admin {
                                                        if ($fileArr[$entry] > 1)       {
                                                                $temp = $this->whereIsFileReferenced($folder,$entry);
                                                                $tempList = '';
-                                                               while(list(,$inf)=each($temp))  {
+                                                               foreach ($temp as $inf) {
                                                                        $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
                                                                }
                                                                $output['moreReferences'][] = Array($path,$entry,$fileArr[$entry],$tempList);
                                                        }
                                                        unset($fileArr[$entry]);
                                                } else {
-                                                       if (!strstr($entry,'index.htm'))        {
+                                                               // contains workaround for direct references
+                                                       if (!strstr($entry, 'index.htm') && !preg_match('/^' . preg_quote($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/', $folder)) {
                                                                $output['noReferences'][] = Array($path,$entry);
                                                        }
                                                }
                                        }
                                }
                                $d->close();
-                               reset($fileArr);
                                $tempCounter=0;
-                               while(list($file,)=each($fileArr))      {
+                               foreach ($fileArr as $file => $value) {
+                                               // workaround for direct file references
+                                       if (preg_match('/^' . preg_quote($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/', $folder)) {
+                                               $file = $folder . '/' . $file;
+                                               $folder = '';
+                                               $path = substr(PATH_site, 0, - 1);
+                                       }
                                        $temp = $this->whereIsFileReferenced($folder,$file);
                                        $tempList = '';
-                                       while(list(,$inf)=each($temp))  {
+                                       foreach ($temp as $inf) {
                                                $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
                                        }
                                        $tempCounter++;
@@ -633,13 +650,10 @@ class t3lib_admin {
         */
        function testDBRefs($theArray)  {
                global $TCA;
-               reset($theArray);
-               while(list($table,$dbArr)=each($theArray))      {
+               foreach ($theArray as $table => $dbArr) {
                        if ($TCA[$table])       {
-                               $idlist = Array();
-                               while(list($id,)=each($dbArr))  {
-                                       $idlist[]=$id;
-                               }
+                               $idlist = array_keys($dbArr);
+
                                $theList = implode(',',$idlist);
                                if ($theList)   {
                                        $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, 'uid IN ('.$theList.')'.t3lib_BEfunc::deleteClause($table));
@@ -650,8 +664,7 @@ class t3lib_admin {
                                                        $result.='Strange Error. ...<br />';
                                                }
                                        }
-                                       reset($dbArr);
-                                       while (list($theId,$theC)=each($dbArr)) {
+                                       foreach ($dbArr as $theId => $theC) {
                                                $result.='There are '.$theC.' records pointing to this missing or deleted record; ['.$table.']['.$theId.']<br />';
                                        }
                                }
@@ -673,7 +686,7 @@ class t3lib_admin {
                global $TCA;
                $fileFields = $this->getDBFields($searchTable); // Gets tables / Fields that reference to files...
                $theRecordList=Array();
-               while (list(,$info)=each($fileFields))  {
+               foreach ($fileFields as $info) {
                        $table=$info[0];        $field=$info[1];
                        t3lib_div::loadTCA($table);
                        $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
@@ -688,13 +701,13 @@ class t3lib_admin {
 
                                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
                                $dbAnalysis->start($row[$field],$allowedTables,$fieldConf['MM'],$row['uid'], $table, $fieldConf);
-                               reset($dbAnalysis->itemArray);
-                               while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
+                               foreach ($dbAnalysis->itemArray as $tempArr) {
                                        if ($tempArr['table']==$searchTable && $tempArr['id']==$id)     {
                                                $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);
                                        }
                                }
                        }
+                       $GLOBALS['TYPO3_DB']->sql_free_result($mres);
                }
                return $theRecordList;
        }
@@ -710,7 +723,7 @@ class t3lib_admin {
                global $TCA;
                $fileFields = $this->getFileFields($uploadfolder);      // Gets tables / Fields that reference to files...
                $theRecordList=Array();
-               while (list(,$info)=each($fileFields))  {
+               foreach ($fileFields as $info) {
                        $table=$info[0];        $field=$info[1];
                        $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
                                                        'uid,pid,'.$TCA[$table]['ctrl']['label'].','.$field,
@@ -720,7 +733,7 @@ class t3lib_admin {
                        while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))     {
                                // Now this is the field, where the reference COULD come from. But we're not garanteed, so we must carefully examine the data.
                                $tempArr = explode(',',trim($row[$field]));
-                               while (list(,$file)=each($tempArr))     {
+                               foreach ($tempArr as $file) {
                                        $file = trim($file);
                                        if ($file==$filename)   {
                                                $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);