0

getSettings() seems to only read and output 1 settings.php file in the directory. How do I get it to read and output all the settings.php file contents?

<?php 
$config = array("os"=>"windows","directory"=>"../Core-Stack/Themes/","ignore"=>array('_vti_cnf','cgi-bin','.','..')); 

function getSettings($dir, $issubdir = false) { 
    global $config, $SETTINGS; 
    if ($config['os'] == "unix") 
        $delimiter = "/"; 
    else if ($config['os'] == "windows") 
        $delimiter = "\\"; 
    if (!file_exists($dir) || !is_dir($dir) || !is_readable($dir)) { 
        echo "Error: \"$dir\" is not a directory, or I cannot read it properly."; 
        return 0; 
    } else if ($od = opendir($dir)) { 
        while (($file = readdir($od)) !== false) { 
            if (!in_array($file, $config['ignore'])) { 
                $path = $dir . $delimiter . $file; 
                if (is_dir($path)) 
                    getSettings($path, true); 
                elseif (is_file($path) && $file == "settings.php") 
                    include ($path); 
            } 
        } 
        closedir($od); 
    } 
} 

getSettings($config['directory'], true); 
echo "Theme Name: "; 
echo $SETTINGS['theme_name']; 
echo "<br>"; 
echo "Theme Creator: "; 
echo $SETTINGS['theme_creator']; 
echo "<br>"; 
echo "Theme Version: "; 
echo $SETTINGS['theme_version']; 
echo "<br>"; 
echo "Theme Creation Date: "; 
echo $SETTINGS['theme_creation_date']; 
?>
GordonM
  • 31,179
  • 15
  • 87
  • 129
  • 3
    You don't need to do the operating system check to set the correct directory separator yourself, you know. You can just use the DIRECTORY_SEPARATOR constant, which always contains the correct seperator character sequence for the operating system PHP is running on – GordonM Apr 28 '11 at 13:30

2 Answers2

0

Most likely your "settings" files are defining settings somehow like $SETTINGS = array(...); and of course that way you will only see contents from the latest included file. What you could do here without remaking the whole thing would be either: without changing settings.php:

//...
elseif (is_file($path) && $file == "settings.php") {
    $OLD_SETTINGS = $SETTINGS;
    include ($path);
    $SETTINGS = array_merge($OLD_SETTINGS, $SETTINGS);
}
//...

or if you can change the settings.php files:

//...
elseif (is_file($path) && $file == "settings.php") {
    $SETTINGS = array_merge($SETTINGS, include ($path));
}
//...


//----in settings.php
return array(
    'option' => 'foobar',
    //...
);

That's of course if I got your intensions right. If not - then please edit your question and add more details.

UPDATE

also you could use scandir to fit the function in less lines and prevent potential problems with heap if the tree is VERY deep, like this:

function getSettings($dir, $issubdir = false) { 
    global $config, $SETTINGS; 
    if (!file_exists($dir) || !is_dir($dir) || !is_readable($dir)) { 
        echo "Error: \"$dir\" is not a directory, or I cannot read it properly."; 
        return 0; 
    } else if ($files = scandir($dir)) { 
        foreach ($files as $file) { 
            if (in_array($file, $config['ignore'])) continue;

            $path = $dir . DIRECTORY_SEPARATOR . $file; 
            if (is_dir($path)) 
                getSettings($path, true); 
            elseif (is_file($path) && $file == "settings.php") 
                $SETTINGS = array_merge($SETTINGS, include ($path));
        }
    } 
} 
Slava
  • 2,040
  • 15
  • 15
0

You should store directory contents before recursion, your else-if block should be like this:

else if ($od = opendir($dir)) { 
        $subdirs = array();
        while (($file = readdir($od)) !== false) { 
            if (!in_array($file, $config['ignore'])) { 
                $path = $dir . $delimiter . $file; 
                if (is_dir($path)) $subdirs[] = $path;
                elseif (is_file($path) && $file == "settings.php") include ($path); 
            }           
        }
        closedir($od);
        foreach($subdirs as $subdir)
            getSettings($subdir, true);
    }
Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
  • It doesn't really matter. This way you just eat some memory for storing subdirectories and introduce another cycle. But generally behaviour doesn't change. The problem must be somewhere else. – Slava Apr 28 '11 at 14:01